MDL-55012 Atto: no style attrs on paste
authorDamyon Wiese <damyon@moodle.com>
Sun, 26 Jun 2016 00:20:54 +0000 (17:20 -0700)
committerDamyon Wiese <damyon@moodle.com>
Fri, 15 Jul 2016 05:34:11 +0000 (13:34 +0800)
Specifically on paste - we don't need all the random style attributes
from content on the clipboard. When you copy from a webpage - the browser
inlines all the styles from the page so that the pasted content will look
like the place it was copied from. But this is never what you want - you
want the content on paste, but with no special styling.

lib/editor/atto/yui/build/moodle-editor_atto-editor/moodle-editor_atto-editor-debug.js
lib/editor/atto/yui/build/moodle-editor_atto-editor/moodle-editor_atto-editor-min.js
lib/editor/atto/yui/build/moodle-editor_atto-editor/moodle-editor_atto-editor.js
lib/editor/atto/yui/src/editor/js/clean.js

index fbcfd8b..d9e359e 100644 (file)
Binary files a/lib/editor/atto/yui/build/moodle-editor_atto-editor/moodle-editor_atto-editor-debug.js and b/lib/editor/atto/yui/build/moodle-editor_atto-editor/moodle-editor_atto-editor-debug.js differ
index c8557d0..702a767 100644 (file)
Binary files a/lib/editor/atto/yui/build/moodle-editor_atto-editor/moodle-editor_atto-editor-min.js and b/lib/editor/atto/yui/build/moodle-editor_atto-editor/moodle-editor_atto-editor-min.js differ
index d2b7ea5..ed89772 100644 (file)
Binary files a/lib/editor/atto/yui/build/moodle-editor_atto-editor/moodle-editor_atto-editor.js and b/lib/editor/atto/yui/build/moodle-editor_atto-editor/moodle-editor_atto-editor.js differ
index 5b86018..7a7b1a8 100644 (file)
@@ -286,14 +286,6 @@ EditorClean.prototype = {
 
         // Run some more rules that care about quotes and whitespace.
         rules = [
-            // Get all style attributes so we can work on them.
-            {regex: /(<[^>]*?style\s*?=\s*?")([^>"]*)(")/gi, replace: function(match, group1, group2, group3) {
-                    // Remove MSO-blah, MSO:blah style attributes.
-                    group2 = group2.replace(/(?:^|;)[\s]*MSO[-:](?:&[\w]*;|[^;"])*/gi,"");
-                    // Remove backgroud color style.
-                    group2 = group2.replace(/background-color:.*?;/gi,"");
-                    return group1 + group2 + group3;
-                }},
             // Get all class attributes so we can work on them.
             {regex: /(<[^>]*?class\s*?=\s*?")([^>"]*)(")/gi, replace: function(match, group1, group2, group3) {
                     // Remove MSO classes.
@@ -306,6 +298,9 @@ EditorClean.prototype = {
             {regex: /<a [^>]*?name\s*?=\s*?"OLE_LINK\d*?"[^>]*?>\s*?<\/a>/gi, replace: ""}
         ];
 
+        // Clean all style attributes from the text.
+        content = this._cleanStyles(content);
+
         // Apply the rules.
         content = this._filterContentWithRules(content, rules);
 
@@ -318,6 +313,33 @@ EditorClean.prototype = {
         return content;
     },
 
+    /**
+     * Clean all inline styles from pasted text.
+     *
+     * This code intentionally doesn't use YUI Nodes. YUI was quite a bit slower at this, so using raw DOM objects instead.
+     *
+     * @method _cleanStyles
+     * @private
+     * @param {String} content The content to clean
+     * @return {String} The cleaned HTML
+     */
+    _cleanStyles: function(content) {
+        var holder = document.createElement('div');
+        holder.innerHTML = content;
+        var elementsWithStyle = holder.querySelectorAll('[style]');
+        var i = 0;
+
+        for (i = 0; i < elementsWithStyle.length; i++) {
+            elementsWithStyle[i].removeAttribute('style');
+        }
+
+        var elementsWithClass = holder.querySelectorAll('[class]');
+        for (i = 0; i < elementsWithClass.length; i++) {
+            elementsWithClass[i].removeAttribute('class');
+        }
+
+        return holder.innerHTML;
+    },
     /**
      * Clean empty or un-unused spans from passed HTML.
      *