Esanders has uploaded a new change for review.

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

Change subject: Internal paste rules and type conversions
......................................................................

Internal paste rules and type conversions

Separate out paste rules into external only rules, and 'all'
rules which apply to both external and internal pastes.

Also add support for model type conversions, e.g. headings to paragraphs.

Change-Id: Id995f41d5f5bca4b8f31306e3b3ce25745e30ecc
---
M modules/ve/ce/ve.ce.Surface.js
M modules/ve/dm/lineardata/ve.dm.ElementLinearData.js
M modules/ve/init/ve.init.Target.js
3 files changed, 41 insertions(+), 18 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/VisualEditor/VisualEditor 
refs/changes/35/111435/1

diff --git a/modules/ve/ce/ve.ce.Surface.js b/modules/ve/ce/ve.ce.Surface.js
index b1cb910..ff393f7 100644
--- a/modules/ve/ce/ve.ce.Surface.js
+++ b/modules/ve/ce/ve.ce.Surface.js
@@ -939,6 +939,8 @@
                $elements, parts, pasteData, slice, tx, internalListRange,
                data, doc, htmlDoc,
                context, left, right, contextRange,
+               allSourcesPasteRules,
+               pasteRules = this.getSurface().getPasteRules(),
                beforePasteData = this.beforePasteData || {},
                $window = this.$( OO.ui.Element.getWindow( this.$.context ) ),
                selection = this.model.getSelection();
@@ -1011,8 +1013,8 @@
                                ve.copy( slice.getOriginalData() )
                        );
 
-                       if ( this.pasteSpecial ) {
-                               pasteData.sanitize( 
this.getSurface().getPasteRules(), true );
+                       if ( pasteRules.all || this.pasteSpecial ) {
+                               pasteData.sanitize( pasteRules.all || {}, 
this.pasteSpecial );
                        }
 
                        // Annotate
@@ -1032,8 +1034,8 @@
                                ve.copy( slice.getBalancedData() )
                        );
 
-                       if ( this.pasteSpecial ) {
-                               pasteData.sanitize( 
this.getSurface().getPasteRules(), true );
+                       if ( pasteRules.all || this.pasteSpecial ) {
+                               pasteData.sanitize( pasteRules.all || {}, 
this.pasteSpecial );
                        }
 
                        // Annotate
@@ -1079,14 +1081,19 @@
                data = doc.data;
                // Clear metadata
                doc.metadata = new ve.dm.MetaLinearData( doc.getStore(), new 
Array( 1 + data.getLength() ) );
-               // If the clipboardKey is set (paste from other VE instance), 
and it's a non-special paste, skip sanitization
-               if ( !clipboardKey || this.pasteSpecial ) {
-                       data.sanitize( this.getSurface().getPasteRules(), 
this.pasteSpecial );
+               // If the clipboardKey isn't set (paste from non-VE instance) 
use external paste rules
+               if ( !clipboardKey ) {
+                       data.sanitize( pasteRules.external, this.pasteSpecial );
+                       if ( pasteRules.all ) {
+                               data.sanitize( pasteRules.all );
+                       }
                } else {
-                       // ...except not quite - contentEditable can't be 
trusted not
-                       // to add styles, so for now remove them
+                       // contentEditable can't be trusted not to add styles, 
so for now remove them as well
                        // TODO: store original styles in data
-                       data.sanitize( { 'removeStyles': true } );
+                       allSourcesPasteRules = ve.extendObject( pasteRules.all, 
{
+                               'removeStyles': true
+                       } );
+                       data.sanitize( allSourcesPasteRules, this.pasteSpecial 
);
                }
                data.remapInternalListKeys( 
this.model.getDocument().getInternalList() );
 
@@ -1102,7 +1109,7 @@
                        );
                        if ( this.pasteSpecial ) {
                                // The context may have been sanitized, so 
sanitize here as well for comparison
-                               context.sanitize( 
this.getSurface().getPasteRules(), this.pasteSpecial, true );
+                               context.sanitize( pasteRules, 
this.pasteSpecial, true );
                        }
 
                        // Remove matching context from the left
diff --git a/modules/ve/dm/lineardata/ve.dm.ElementLinearData.js 
b/modules/ve/dm/lineardata/ve.dm.ElementLinearData.js
index 0486326..481d654 100644
--- a/modules/ve/dm/lineardata/ve.dm.ElementLinearData.js
+++ b/modules/ve/dm/lineardata/ve.dm.ElementLinearData.js
@@ -797,6 +797,7 @@
  *
  * @param {Object} rules Sanitization rules
  * @param {string[]} [rules.blacklist] Blacklist of model types which aren't 
allowed
+ * @param {Object} [rules.conversions] Model type conversions to apply, e.g. { 
'heading': 'paragraph' }
  * @param {boolean} [rules.removeHtmlAttributes] Remove all left over HTML 
attributes
  * @param {boolean} [rules.removeStyles] Remove HTML style attributes
  * @param {boolean} [plainText=false] Remove all formatting for plain text 
paste
@@ -835,6 +836,10 @@
        for ( i = 0, len = this.getLength(); i < len; i++ ) {
                if ( this.isElementData( i ) ) {
                        type = this.getType( i );
+                       // Apply type conversions
+                       if ( rules.conversions && rules.conversions[type] ) {
+                               this.getData( i ).type = ( 
this.isCloseElementData( i ) ? '/' : '' ) + rules.conversions[type];
+                       }
                        // Remove blacklisted nodes
                        if (
                                ve.indexOf( type, rules.blacklist ) !== -1 ||
diff --git a/modules/ve/init/ve.init.Target.js 
b/modules/ve/init/ve.init.Target.js
index 2a1cd7d..520569e 100644
--- a/modules/ve/init/ve.init.Target.js
+++ b/modules/ve/init/ve.init.Target.js
@@ -139,12 +139,23 @@
        'pasteSpecial'
 ];
 
+/**
+ * Surface paste rules
+ *
+ * One set for external (non-VE) paste sources and one for all paste sources.
+ *
+ * @see ve.dm.ElementLinearData#sanitize
+ * @type {Object}
+ */
 ve.init.Target.static.pasteRules = {
-       'blacklist': [
-               // Annotations
-               // TODO: allow spans
-               'textStyle/span',
-               // Nodes
-               'alienInline', 'alienBlock'
-       ]
+       'external': {
+               'blacklist': [
+                       // Annotations
+                       // TODO: allow spans
+                       'textStyle/span',
+                       // Nodes
+                       'alienInline', 'alienBlock'
+               ]
+       },
+       'all': null
 };

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: Id995f41d5f5bca4b8f31306e3b3ce25745e30ecc
Gerrit-PatchSet: 1
Gerrit-Project: VisualEditor/VisualEditor
Gerrit-Branch: master
Gerrit-Owner: Esanders <esand...@wikimedia.org>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to