Catrope has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/63251


Change subject: Send a full document back to Parsoid
......................................................................

Send a full document back to Parsoid

To do this, we take the document generated by the converter and
transplant things from the original Parsoid document into it.

Change-Id: I2f5058220669526130a360cec3389c3f42b41771
---
M modules/ve/init/mw/ve.init.mw.Target.js
1 file changed, 35 insertions(+), 8 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/VisualEditor 
refs/changes/51/63251/1

diff --git a/modules/ve/init/mw/ve.init.mw.Target.js 
b/modules/ve/init/mw/ve.init.mw.Target.js
index 1793dcb..5994326 100644
--- a/modules/ve/init/mw/ve.init.mw.Target.js
+++ b/modules/ve/init/mw/ve.init.mw.Target.js
@@ -182,8 +182,7 @@
 /**
  * Handle both DOM and modules being loaded and ready.
  *
- * This method is called within the context of a target instance. After the 
load event is emitted
- * this.doc is cleared, allowing it to be garbage collected.
+ * This method is called within the context of a target instance.
  *
  * @static
  * @method
@@ -192,8 +191,6 @@
 ve.init.mw.Target.onReady = function () {
        this.loading = false;
        this.emit( 'load', this.doc );
-       // Release DOM data
-       this.doc = null;
 };
 
 /**
@@ -359,6 +356,36 @@
 /* Methods */
 
 /**
+ * Get HTML to send to Parsoid. This takes a document generated by the 
converter and
+ * transplants the <head> tag from the old document into it, as well as the 
attributes on the
+ * <html> and <body> tags.
+ *
+ * @param {HTMLDocument} newDoc Document generated by ve.dm.Converter. Will be 
modified.
+ * @returns {string} Full HTML document
+ */
+ve.init.mw.Target.prototype.getHtml = function ( newDoc ) {
+       var i, len, nodes, oldDoc = this.doc;
+
+       function copyAttributes( from, to ) {
+               var i, len;
+               for ( i = 0, len = from.attributes.length; i < len; i++ ) {
+                       to.setAttribute( from.attributes[i].name, 
from.attributes[i].value );
+               }
+       }
+
+       // Copy the head from the old document
+       for ( i = 0, len = oldDoc.head.childNodes.length; i < len; i++ ) {
+               newDoc.head.appendChild( oldDoc.head.childNodes[i].cloneNode( 
true ) );
+       }
+       // Copy attributes from the old document for the html, head and body
+       copyAttributes( oldDoc.documentElement, newDoc.documentElement );
+       copyAttributes( oldDoc.head, newDoc.head );
+       copyAttributes( oldDoc.body, newDoc.body );
+       return '<!doctype html>' + ve.properOuterHtml( newDoc.documentElement );
+};
+
+
+/**
  * Get DOM data from the Parsoid API.
  *
  * This method performs an asynchronous action and uses a callback function to 
handle the result.
@@ -430,7 +457,7 @@
                        'oldid': this.oldid,
                        'basetimestamp': this.baseTimeStamp,
                        'starttimestamp': this.startTimeStamp,
-                       'html': ve.properInnerHTML( doc.body ), // TODO make 
this send the whole document in the future
+                       'html': this.getHtml( doc ),
                        'token': this.editToken,
                        'summary': options.summary,
                        'minor': Number( options.minor ),
@@ -461,7 +488,7 @@
                        'paction': 'diff',
                        'page': this.pageName,
                        'oldid': this.oldid,
-                       'html': ve.properInnerHTML( doc.body ), // TODO make 
this send the whole document in the future
+                       'html': this.getHtml( doc ),
                        // TODO: API required editToken, though not relevant 
for diff
                        'token': this.editToken
                },
@@ -549,7 +576,7 @@
                'data': {
                        'action': 'visualeditor',
                        'paction': 'serialize',
-                       'html': ve.properInnerHTML( doc.body ), // TODO make 
this send the whole document in the future
+                       'html': this.getHtml( doc ),
                        'page': this.pageName,
                        'oldid': this.oldid,
                        'token': this.editToken,
@@ -594,7 +621,7 @@
                                        store, internalList
                                ).getData(),
                        'editedData': editedData,
-                       'editedHtml': ve.properInnerHTML( 
ve.dm.converter.getDomFromData( editedData, store, internalList ).body ),
+                       'editedHtml': this.getHtml( 
ve.dm.converter.getDomFromData( editedData, store, internalList ) ),
                        'store': doc.data.getUsedStoreValues(),
                        'wiki': mw.config.get( 'wgDBname' )
                };

-- 
To view, visit https://gerrit.wikimedia.org/r/63251
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I2f5058220669526130a360cec3389c3f42b41771
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/VisualEditor
Gerrit-Branch: master
Gerrit-Owner: Catrope <[email protected]>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to