Pass child DOM elements to annotations' toDomElements()

This is special-cased for annotations but should be supported for
all model types once the converter is rewritten to be bottom-up
entirely.

Add a toDomElements() stub to ve.dm.Annotation overriding the stub
in ve.dm.Model so there's a place for the new parameter to be documented.

Change-Id: Id81da87b8b83d556a3618cc6187b22443a1e37e6
This commit is contained in:
Roan Kattouw 2013-07-16 14:40:05 -07:00
parent 15a7822df1
commit 2942fa8fa0
2 changed files with 26 additions and 3 deletions

View file

@ -48,6 +48,26 @@ ve.dm.Annotation.static.enableAboutGrouping = false;
*/
ve.dm.Annotation.static.applyToAppendedContent = true;
/**
* Static function to convert a linear model data element for this annotation type back to
* a DOM element.
*
* As a special facility for annotations, the annotated content that the returned element will
* wrap around is passed in as childDomElements.
*
* @static
* @inheritable
* @method
* @param {Object|Array} dataElement Linear model element or array of linear model data
* @param {HTMLDocument} doc HTML document for creating elements
* @param {ve.dm.Converter} converter Converter object to optionally call .getDomSubtreeFromData() on
* @param {HTMLElement[]} childDomElements Children that will be appended to the returned element
* @returns {HTMLElement[]} DOM elements; only the first element is used
*/
ve.dm.Annotation.static.toDomElements = function ( /*dataElement, doc, converter, childDomElements*/ ) {
throw new Error( 've.dm.Annotation subclass must implement toDomElements' );
};
/* Methods */
/**

View file

@ -294,9 +294,10 @@ ve.dm.Converter.prototype.canCloseWrapper = function () {
* @method
* @param {Object|Array} dataElement Linear model element or data slice
* @param {HTMLDocument} doc Document to create DOM elements in
* @param {HTMLElement[]} [childDomElements] Array of child DOM elements to pass in (annotations only)
* @returns {HTMLElement|boolean} DOM element, or false if the element cannot be converted
*/
ve.dm.Converter.prototype.getDomElementsFromDataElement = function ( dataElements, doc ) {
ve.dm.Converter.prototype.getDomElementsFromDataElement = function ( dataElements, doc, childDomElements ) {
var domElements,
dataElement = ve.isArray( dataElements ) ? dataElements[0] : dataElements,
nodeClass = this.modelRegistry.lookup( dataElement.type );
@ -307,7 +308,7 @@ ve.dm.Converter.prototype.getDomElementsFromDataElement = function ( dataElement
if ( nodeClass.static.isInternal ) {
return false;
}
domElements = nodeClass.static.toDomElements( dataElements, doc, this );
domElements = nodeClass.static.toDomElements( dataElements, doc, this, childDomElements );
if ( !domElements || !domElements.length ) {
throw new Error( 'toDomElements() failed to return an array when converting element of type ' + dataElement.type );
}
@ -1005,7 +1006,9 @@ ve.dm.Converter.prototype.getDomSubtreeFromData = function ( data, container ) {
annotatedChildDomElements = annotatedDomElementStack.pop();
annotatedDomElements = annotatedDomElementStack[annotatedDomElementStack.length - 1];
annotationElement = conv.getDomElementsFromDataElement( annotation.getElement(), doc )[0];
annotationElement = conv.getDomElementsFromDataElement(
annotation.getElement(), doc, annotatedChildDomElements
)[0];
for ( i = 0, len = annotatedChildDomElements.length; i < len; i++ ) {
annotationElement.appendChild( annotatedChildDomElements[i] );
}