Copy DOM elements properly, into the correct document

* Provide a utility for copying an array of DOM elements into a
  different document
* Copy the DOM elements returned in toDomElements(), otherwise weird
  issues arise when the same data is converted to DOM twice

Change-Id: Ie927420624f0d4af0692e18d1bc6f952c8013d61
This commit is contained in:
Roan Kattouw 2013-06-18 11:51:39 -07:00
parent 2d4101168b
commit dc798befcf
5 changed files with 20 additions and 9 deletions

View file

@ -41,9 +41,7 @@ ve.ce.GeneratedContentNode.prototype.onUpdate = function () {
this.emit( 'teardown' ); this.emit( 'teardown' );
} }
this.$.empty().append( this.$.empty().append(
this.$$( store.value( index ) ).map( function ( i, domElement ) { ve.copyDomElements( store.value( index ), doc )
return doc.importNode( domElement, true );
} )
); );
if ( this.live ) { if ( this.live ) {
this.emit( 'setup' ); this.emit( 'setup' );

View file

@ -39,8 +39,8 @@ ve.dm.AlienMetaItem.static.toDataElement = function ( domElements ) {
}; };
}; };
ve.dm.AlienMetaItem.static.toDomElements = function ( dataElement ) { ve.dm.AlienMetaItem.static.toDomElements = function ( dataElement, doc ) {
return dataElement.attributes.domElements; return ve.copyDomElements( dataElement.attributes.domElements, doc );
}; };
/* Registration */ /* Registration */

View file

@ -52,8 +52,8 @@ ve.dm.AlienNode.static.toDataElement = function ( domElements, converter ) {
}; };
}; };
ve.dm.AlienNode.static.toDomElements = function ( dataElement ) { ve.dm.AlienNode.static.toDomElements = function ( dataElement, doc ) {
return dataElement.attributes.domElements; return ve.copyDomElements( dataElement.attributes.domElements, doc );
}; };
/* Concrete subclasses */ /* Concrete subclasses */

View file

@ -47,8 +47,8 @@ ve.dm.MWReferenceListNode.static.toDataElement = function ( domElements ) {
}; };
}; };
ve.dm.MWReferenceListNode.static.toDomElements = function ( dataElement ) { ve.dm.MWReferenceListNode.static.toDomElements = function ( dataElement, doc ) {
return dataElement.attributes.domElements; return ve.copyDomElements( dataElement.attributes.domElements, doc );
}; };
/* Registration */ /* Registration */

View file

@ -105,6 +105,19 @@
*/ */
ve.copyObject = oo.copy; ve.copyObject = oo.copy;
/**
* Copy an array of DOM elements, optionally into a different document.
*
* @param {HTMLElement[]} domElements DOM elements to copy
* @param {HTMLDocument} [doc] Document to create the copies in; if unset, simply clone each element
* @return {HTMLElement[]} Copy of domElements with copies of each element
*/
ve.copyDomElements = function ( domElements, doc ) {
return domElements.map( function ( domElement ) {
return doc ? doc.importNode( domElement, true ) : domElement.cloneNode( true );
} );
};
/** /**
* Check to see if an object is a plain object (created using "{}" or "new Object"). * Check to see if an object is a plain object (created using "{}" or "new Object").
* *