Esanders has uploaded a new change for review.
https://gerrit.wikimedia.org/r/277550
Change subject: Trigger plain transfer handler if HTML paste was plain
......................................................................
Trigger plain transfer handler if HTML paste was plain
Bug: T129882
Change-Id: I084337cb4e4712178b35c952d448249551ff6887
---
M src/ce/ve.ce.Surface.js
M src/dm/lineardata/ve.dm.ElementLinearData.js
M src/ui/ve.ui.DataTransferItem.js
M tests/dm/lineardata/ve.dm.ElementLinearData.test.js
4 files changed, 55 insertions(+), 9 deletions(-)
git pull ssh://gerrit.wikimedia.org:29418/VisualEditor/VisualEditor
refs/changes/50/277550/1
diff --git a/src/ce/ve.ce.Surface.js b/src/ce/ve.ce.Surface.js
index 887aed3..42862db 100644
--- a/src/ce/ve.ce.Surface.js
+++ b/src/ce/ve.ce.Surface.js
@@ -1800,7 +1800,7 @@
var clipboardKey, clipboardId, clipboardIndex, clipboardHash, range,
$elements, parts, pasteData, slice, tx, internalListRange,
data, pastedDocumentModel, htmlDoc, $images, i,
- context, left, right, contextRange,
+ context, left, right, contextRange, handled,
tableAction,
items = [],
importantElement = '[id],[typeof],[rel]',
@@ -2121,12 +2121,24 @@
contextRange = new ve.Range( left, right );
}
- tx = ve.dm.Transaction.newFromDocumentInsertion(
- documentModel,
- range.start,
- pastedDocumentModel,
- contextRange
- );
+ // If the external HTML turned out to be plain text after
+ // sanitization then run it as a plain text transfer item
+ if ( pastedDocumentModel.data.isPlainText( true, contextRange )
) {
+ handled = this.handleDataTransferItems(
+ [ ve.ui.DataTransferItem.static.newFromString(
pastedDocumentModel.data.getText( true, contextRange ) ) ],
+ true,
+ surfaceModel.getLinearFragment( range )
+ );
+ }
+
+ if ( !handled ) {
+ tx = ve.dm.Transaction.newFromDocumentInsertion(
+ documentModel,
+ range.start,
+ pastedDocumentModel,
+ contextRange
+ );
+ }
}
if ( this.getSelection().isNativeCursor() ) {
diff --git a/src/dm/lineardata/ve.dm.ElementLinearData.js
b/src/dm/lineardata/ve.dm.ElementLinearData.js
index 8ec0bc0..3f49a4a 100644
--- a/src/dm/lineardata/ve.dm.ElementLinearData.js
+++ b/src/dm/lineardata/ve.dm.ElementLinearData.js
@@ -615,9 +615,34 @@
};
/**
+ * Check if the data is just plain (un-annotated) text
+ *
+ * @param {boolean} [allowContentBranchNodes] Include content branch nodes in
the definition of plain text, e.g. <p>, <h1>
+ * @param {ve.Range} [range] Range to get the data for. The whole data set if
not specified.
+ * @return {boolean} The data is plain text
+ */
+ve.dm.ElementLinearData.prototype.isPlainText = function (
allowContentBranchNodes, range ) {
+ var i;
+ range = range || new ve.Range( 0, this.getLength() );
+
+ for ( i = range.start; i < range.end; i++ ) {
+ if (
+ typeof this.data[ i ] === 'string' ||
+ allowContentBranchNodes && this.isElementData( i ) &&
+ ve.dm.nodeFactory.canNodeContainContent( this.getType(
i ) )
+ ) {
+ continue;
+ } else {
+ return false;
+ }
+ }
+ return true;
+};
+
+/**
* Get the data as plain text
*
- * @param {boolean} maintainIndices Maintain data offset to string index
alignment by replacing elements with line breaks
+ * @param {boolean} [maintainIndices] Maintain data offset to string index
alignment by replacing elements with line breaks
* @param {ve.Range} [range] Range to get the data for. The whole data set if
not specified.
* @return {string} Data as plain text
*/
diff --git a/src/ui/ve.ui.DataTransferItem.js b/src/ui/ve.ui.DataTransferItem.js
index 0608231..ed88411 100644
--- a/src/ui/ve.ui.DataTransferItem.js
+++ b/src/ui/ve.ui.DataTransferItem.js
@@ -55,7 +55,7 @@
* Create a data transfer item from string data.
*
* @param {string} stringData Native string data
- * @param {string} type Native MIME type
+ * @param {string} [type] Native MIME type
* @param {string} [htmlStringData] HTML string representation of data transfer
* @return {ve.ui.DataTransferItem} New data transfer item
*/
diff --git a/tests/dm/lineardata/ve.dm.ElementLinearData.test.js
b/tests/dm/lineardata/ve.dm.ElementLinearData.test.js
index 6c0664f..da403de 100644
--- a/tests/dm/lineardata/ve.dm.ElementLinearData.test.js
+++ b/tests/dm/lineardata/ve.dm.ElementLinearData.test.js
@@ -799,6 +799,15 @@
}
} );
+QUnit.test( 'isPlainText', 4, function ( assert ) {
+ var doc = ve.dm.example.createExampleDocument();
+
+ assert.strictEqual( doc.data.isPlainText( false, new ve.Range( 1, 2 )
), true, 'Plain text' );
+ assert.strictEqual( doc.data.isPlainText( true, new ve.Range( 1, 3 ) ),
false, 'Annotated text' );
+ assert.strictEqual( doc.data.isPlainText( false, new ve.Range( 9, 11 )
), false, 'Paragraph and text (no content nodes)' );
+ assert.strictEqual( doc.data.isPlainText( true, new ve.Range( 9, 11 )
), true, 'Paragraph and text (content nodes allowed)' );
+} );
+
QUnit.test( 'getText', 4, function ( assert ) {
var doc = ve.dm.example.createExampleDocument();
--
To view, visit https://gerrit.wikimedia.org/r/277550
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: newchange
Gerrit-Change-Id: I084337cb4e4712178b35c952d448249551ff6887
Gerrit-PatchSet: 1
Gerrit-Project: VisualEditor/VisualEditor
Gerrit-Branch: master
Gerrit-Owner: Esanders <[email protected]>
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits