From 2942fa8fa002ffba681e708184166b921da32785 Mon Sep 17 00:00:00 2001 From: Roan Kattouw Date: Tue, 16 Jul 2013 14:40:05 -0700 Subject: [PATCH] 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 --- modules/ve/dm/ve.dm.Annotation.js | 20 ++++++++++++++++++++ modules/ve/dm/ve.dm.Converter.js | 9 ++++++--- 2 files changed, 26 insertions(+), 3 deletions(-) diff --git a/modules/ve/dm/ve.dm.Annotation.js b/modules/ve/dm/ve.dm.Annotation.js index 0f8ccc4d38..d0e562386e 100644 --- a/modules/ve/dm/ve.dm.Annotation.js +++ b/modules/ve/dm/ve.dm.Annotation.js @@ -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 */ /** diff --git a/modules/ve/dm/ve.dm.Converter.js b/modules/ve/dm/ve.dm.Converter.js index 455682385f..7631889136 100644 --- a/modules/ve/dm/ve.dm.Converter.js +++ b/modules/ve/dm/ve.dm.Converter.js @@ -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] ); }