Esanders has uploaded a new change for review.

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


Change subject: Protect RDFa attributes from obliteration
......................................................................

Protect RDFa attributes from obliteration

Firefox decides to not keep some attributes when copying, so JSON
encode them in a data attribute.

Bug: 59145
Bug: 59154
Bug: 59170
Bug: 59171
Bug: 59173
Change-Id: Iae87a6e145d92eb98f46367318230c4c6fc2f4ed
---
M modules/ve/ce/ve.ce.Surface.js
1 file changed, 43 insertions(+), 1 deletion(-)


  git pull ssh://gerrit.wikimedia.org:29418/VisualEditor/VisualEditor 
refs/changes/80/108680/1

diff --git a/modules/ve/ce/ve.ce.Surface.js b/modules/ve/ce/ve.ce.Surface.js
index c45849e..8b40940 100644
--- a/modules/ve/ce/ve.ce.Surface.js
+++ b/modules/ve/ce/ve.ce.Surface.js
@@ -161,6 +161,28 @@
  * a pair of blur-focus events is emitted anyway.
  */
 
+/* Static properties */
+
+/**
+ * Attributes considered 'unsafe' for copy/paste
+ *
+ * These attributes may be dropped by the browser during copy/paste, so
+ * any element containing these attributes will have them JSON encoded into
+ * data-ve-attributes on copy.
+ *
+ * @type {string[]}
+ */
+ve.ce.Surface.static.unsafeAttributes = [
+       'about',
+       'content',
+       'datatype',
+       'property',
+       'rel',
+       'resource',
+       'rev',
+       'typeof'
+];
+
 /* Static methods */
 
 /**
@@ -688,7 +710,7 @@
 ve.ce.Surface.prototype.onCopy = function ( e ) {
        var rangyRange, sel, originalRange,
                clipboardIndex, clipboardItem, pasteData,
-               scrollTop,
+               scrollTop, unsafeSelector,
                view = this,
                slice = this.model.documentModel.cloneSliceFromRange( 
this.model.getSelection() ),
                clipboardData = e.originalEvent.clipboardData,
@@ -707,6 +729,19 @@
        // paste target (e.g. plain spans) so we must protect against this
        // by adding a dummy class, which we can remove after paste.
        this.$pasteTarget.find( 'span' ).addClass( 've-pasteProtect' );
+
+       // Some attributes (e.g RDFa attributes in Firefox) aren't preserved by 
copy
+       unsafeSelector = '[' + ve.ce.Surface.static.unsafeAttributes.join( 
'],[') + ']';
+       this.$pasteTarget.find( unsafeSelector ).attr( 'data-ve-attributes', 
function () {
+               var i, val, attrs = {}, ua = 
ve.ce.Surface.static.unsafeAttributes;
+               for ( i = ua.length; i >= 0; i-- ) {
+                       val = this.getAttribute( ua[i] );
+                       if ( val !== null ) {
+                               attrs[ua[i]] = val;
+                       }
+               }
+               return JSON.stringify( attrs );
+       } );
 
        clipboardItem = { 'slice': slice, 'hash': null };
        clipboardIndex = this.clipboard.push( clipboardItem ) - 1;
@@ -908,6 +943,13 @@
        // Remove the pasteProtect class. See #onCopy.
        this.$pasteTarget.find( 'span' ).removeClass( 've-pasteProtect' );
 
+       // Restore attributes. See #onCopy.
+       this.$pasteTarget.find( '[data-ve-attributes]' ).each( function () {
+               var attrs = JSON.parse( this.getAttribute( 'data-ve-attributes' 
) );
+               $( this ).attr( attrs );
+               this.removeAttribute( 'data-ve-attributes' );
+       } );
+
        // Find the clipboard key
        if ( beforePasteData.custom ) {
                clipboardKey = beforePasteData.custom;

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: Iae87a6e145d92eb98f46367318230c4c6fc2f4ed
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

Reply via email to