Created
April 11, 2013 00:49
-
-
Save catrope/5359763 to your computer and use it in GitHub Desktop.
Test class for handling own children
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*! | |
* VisualEditor DataModel MWEntityNode class. | |
* | |
* @copyright 2011-2013 VisualEditor Team and others; see AUTHORS.txt | |
* @license The MIT License (MIT); see LICENSE.txt | |
*/ | |
/** | |
* DataModel MediaWiki image node. | |
* | |
* @class | |
* @extends ve.dm.LeafNode | |
* @constructor | |
* @param {number} [length] Length of content data in document | |
* @param {Object} [element] Reference to element in linear model | |
*/ | |
ve.dm.MWThumbImageNode = function VeDmMWThumbImageNode( length, element ) { | |
ve.dm.LeafNode.call( this, 0, element ); | |
}; | |
/* Inheritance */ | |
ve.inheritClass( ve.dm.MWThumbImageNode, ve.dm.LeafNode ); | |
/* Static Properties */ | |
ve.dm.MWThumbImageNode.static.name = 'MWthumbimage'; | |
ve.dm.MWThumbImageNode.static.handlesOwnChildren = true; | |
ve.dm.MWThumbImageNode.static.matchRdfaTypes = [ 'mw:Image/Thumb' ]; | |
ve.dm.MWThumbImageNode.static.toDataElement = function ( domElements, converter ) { | |
var $figure = $( domElements[0] ), | |
$a = $figure.children( 'a' ).eq( 0 ), | |
$img = $a.children( 'img' ).eq( 0 ), | |
$caption = $figure.children( 'figcaption' ).eq( 0 ), | |
href = $a.attr( 'href' ), | |
src = $img.attr( 'src' ), | |
width = $img.attr( 'width' ), | |
height = $img.attr( 'height' ), | |
resource = $img.attr( 'resource' ), | |
captionData = converter.getDataFromDomRecursion( $caption[0], { 'type': 'MWimagecaption' } ); | |
return [ | |
{ | |
'type': 'MWthumbimage', | |
'attributes': { | |
'href': href, | |
'src': src, | |
'width': width, | |
'height': height, | |
'resource': resource | |
} | |
} | |
].concat( captionData ).concat( [ { 'type': '/MWthumbimage' } ] ); | |
}; | |
ve.dm.MWThumbImageNode.static.toDomElements = function ( data, doc, converter ) { | |
var dataElement = data[0], | |
figure = doc.createElement( 'figure' ), | |
a = doc.createElement( 'a' ), | |
img = doc.createElement( 'img' ), | |
wrapper = doc.createElement( 'div' ); | |
figure.setAttribute( 'typeof', 'mw:Image/Thumb' ); | |
a.setAttribute( 'rel', 'mw:thumb' ); | |
a.setAttribute( 'href', dataElement.attributes.href ); | |
img.setAttribute( 'src', dataElement.attributes.src ); | |
img.setAttribute( 'width', dataElement.attributes.width ); | |
img.setAttribute( 'height', dataElement.attributes.height ); | |
img.setAttribute( 'resource', dataElement.attributes.resource ); | |
figure.appendChild( a ); | |
a.appendChild( img ); | |
converter.getDomSubtreeFromData( converter.getStore(), data.slice( 1, -1 ), wrapper ); | |
while ( wrapper.firstChild ) { | |
figure.appendChild( wrapper.firstChild ); | |
} | |
return [ figure ]; | |
}; | |
/* Registration */ | |
ve.dm.modelRegistry.register( ve.dm.MWThumbImageNode ); | |
ve.dm.MWImageCaptionNode = function VeDmMWImageCaptionNode( length, element ) { | |
ve.dm.LeafNode.call( this, 0, element ); | |
}; | |
ve.inheritClass( ve.dm.MWImageCaptionNode, ve.dm.LeafNode ); | |
ve.dm.MWImageCaptionNode.static.name = 'MWimagecaption'; | |
ve.dm.MWImageCaptionNode.static.matchTagNames = []; | |
ve.dm.MWImageCaptionNode.static.toDataElement = function () {}; | |
ve.dm.MWImageCaptionNode.static.toDomElements = function ( dataElement, doc ) { | |
return [ doc.createElement( 'figcaption' ) ]; | |
}; | |
ve.dm.modelRegistry.register( ve.dm.MWImageCaptionNode ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment