jenkins-bot has submitted this change and it was merged.

Change subject: ve.ce.Surface: Move insert HTML path to DM
......................................................................


ve.ce.Surface: Move insert HTML path to DM

Provided through new ve.dm.SurfaceFragment#insertHtml and
ve.dm.SurfaceFragment#insertDocument methods via
ve.dm.Document#newFromHtml, so it can be used by other pathways.

Co-Authored by: Ed Sanders <[email protected]>
Change-Id: I17191f6ffc18cc1f0d780ae48a53d39ceec35643
---
M src/ce/ve.ce.Surface.js
M src/dm/ve.dm.Document.js
M src/dm/ve.dm.SurfaceFragment.js
3 files changed, 83 insertions(+), 24 deletions(-)

Approvals:
  Catrope: Looks good to me, approved
  jenkins-bot: Verified



diff --git a/src/ce/ve.ce.Surface.js b/src/ce/ve.ce.Surface.js
index f3aff83..250dd95 100644
--- a/src/ce/ve.ce.Surface.js
+++ b/src/ce/ve.ce.Surface.js
@@ -894,7 +894,6 @@
        // Properties may be nullified by other events, so cache before 
setTimeout
        var selectionJSON, dragSelection, dragRange, originFragment, originData,
                targetRange, targetOffset, targetFragment, dragHtml, dragText,
-               htmlDoc, doc, data, importRules,
                i, l, name, insert,
                fileHandlers = [],
                dataTransfer = e.originalEvent.dataTransfer,
@@ -978,31 +977,18 @@
                        // Re-insert data at new location
                        targetFragment.insertContent( originData );
                } else if ( fileHandlers.length ) {
-                       insert = function ( data ) {
-                               targetFragment.collapseToEnd().insertContent( 
data );
+                       insert = function ( docOrData ) {
+                               if ( docOrData instanceof ve.dm.Document ) {
+                                       
targetFragment.collapseToEnd().insertDocument( docOrData );
+                               } else {
+                                       
targetFragment.collapseToEnd().insertContent( docOrData );
+                               }
                        };
                        for ( i = 0, l = fileHandlers.length; i < l; i++ ) {
                                fileHandlers[i].getInsertableData().done( 
insert );
                        }
                } else if ( dragHtml ) {
-                       importRules = this.getSurface().getImportRules();
-                       htmlDoc = ve.createDocumentFromHtml( dragHtml );
-                       doc = ve.dm.converter.getModelFromDom( htmlDoc, 
this.getModel().getDocument().getHtmlDocument() );
-                       data = doc.data;
-                       // Clear metadata
-                       doc.metadata = new ve.dm.MetaLinearData( 
doc.getStore(), new Array( 1 + data.getLength() ) );
-                       data.sanitize( importRules.external, this.pasteSpecial 
);
-                       if ( importRules.all ) {
-                               data.sanitize( importRules.all );
-                       }
-                       data.remapInternalListKeys( 
this.model.getDocument().getInternalList() );
-                       // Initialize node tree
-                       doc.buildNodeTree();
-                       this.getModel().change( new 
ve.dm.Transaction.newFromDocumentInsertion(
-                               this.getModel().getDocument(),
-                               targetOffset,
-                               doc
-                       ) );
+                       targetFragment.insertHtml( dragHtml, 
this.getSurface().getImportRules() );
                } else if ( dragText ) {
                        targetFragment.insertContent( dragText );
                }
diff --git a/src/dm/ve.dm.Document.js b/src/dm/ve.dm.Document.js
index 56085fa..e8dae5e 100644
--- a/src/dm/ve.dm.Document.js
+++ b/src/dm/ve.dm.Document.js
@@ -104,6 +104,7 @@
 /**
  * Split data into element data and meta data.
  *
+ * @static
  * @param {ve.dm.FlatLinearData} fullData Full data from converter
  * @returns {Object} Object containing element linear data and meta linear 
data (if processed)
  */
@@ -161,7 +162,7 @@
  *
  * This method modifies data in place.
  *
- * @method
+ * @static
  * @param {Array} data Data to apply annotations to
  * @param {ve.dm.AnnotationSet} annotationSet Annotations to apply
  */
@@ -1208,6 +1209,39 @@
 };
 
 /**
+ * Create a document given an HTML string.
+ *
+ * @method
+ * @param {string} html HTML to insert
+ * @param {Object} importRules The import rules with which to sanitize the HTML
+ * @return {ve.dm.Document} New document
+ */
+ve.dm.Document.prototype.newFromHtml = function ( html, importRules ) {
+       var htmlDoc = ve.createDocumentFromHtml( html ),
+               doc = ve.dm.converter.getModelFromDom( htmlDoc, 
this.getHtmlDocument() ),
+               data = doc.data;
+
+       // FIXME: This is a paste-specific thing and possibly should not be in 
the generic newFromHtml()
+       // function. Maybe make this be triggered by a pasteRules property?
+       // Clear metadata
+       doc.metadata = new ve.dm.MetaLinearData( doc.getStore(), new Array( 1 + 
data.getLength() ) );
+
+       if ( importRules ) {
+               data.sanitize( importRules.external );
+               if ( importRules.all ) {
+                       data.sanitize( importRules.all );
+               }
+       }
+
+       data.remapInternalListKeys( this.getInternalList() );
+       // Initialize node tree
+       // BUG T75569: This shouldn't be needed
+       doc.buildNodeTree();
+
+       return doc;
+};
+
+/**
  * Get the length of the complete history stack. This is also the current 
pointer.
  * @returns {number} Length of the complete history stack
  */
diff --git a/src/dm/ve.dm.SurfaceFragment.js b/src/dm/ve.dm.SurfaceFragment.js
index 267b31b..37f4c19 100644
--- a/src/dm/ve.dm.SurfaceFragment.js
+++ b/src/dm/ve.dm.SurfaceFragment.js
@@ -707,11 +707,12 @@
                return this;
        }
 
-       var annotations, tx, offset, newRange;
-
        if ( !this.getSelection( true ).isCollapsed() ) {
                this.removeContent();
        }
+
+       var annotations, tx, offset, newRange;
+
        offset = this.getSelection( true ).getRange().start;
        // Auto-convert content to array of plain text characters
        if ( typeof content === 'string' ) {
@@ -743,6 +744,44 @@
 };
 
 /**
+ * Insert HTML in the fragment.
+ *
+ * @method
+ * @param {string} html HTML to insert
+ * @param {Object} importRules The import rules for the target surface
+ * @chainable
+ */
+ve.dm.SurfaceFragment.prototype.insertHtml = function ( html, importRules ) {
+       this.insertDocument( this.getDocument().newFromHtml( html, importRules 
) );
+       return this;
+};
+
+/**
+ * Insert a ve.dm.Document in the fragment.
+ *
+ * @method
+ * @param {ve.dm.Document} doc Document to insert
+ * @chainable
+ */
+ve.dm.SurfaceFragment.prototype.insertDocument = function ( doc ) {
+       if ( !( this.selection instanceof ve.dm.LinearSelection ) ) {
+               return this;
+       }
+
+       if ( !this.getSelection( true ).isCollapsed() ) {
+               this.removeContent();
+       }
+
+       this.change( new ve.dm.Transaction.newFromDocumentInsertion(
+               this.getDocument(),
+               this.getSelection().getRange().start,
+               doc
+       ) );
+
+       return this;
+};
+
+/**
  * Remove content in the fragment.
  *
  * @method

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

Gerrit-MessageType: merged
Gerrit-Change-Id: I17191f6ffc18cc1f0d780ae48a53d39ceec35643
Gerrit-PatchSet: 9
Gerrit-Project: VisualEditor/VisualEditor
Gerrit-Branch: master
Gerrit-Owner: Jforrester <[email protected]>
Gerrit-Reviewer: Catrope <[email protected]>
Gerrit-Reviewer: Esanders <[email protected]>
Gerrit-Reviewer: Jforrester <[email protected]>
Gerrit-Reviewer: jenkins-bot <>

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

Reply via email to