GWicke has uploaded a new change for review.

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


Change subject: Factor out compressHTML method in Util and use it from bugSplit
......................................................................

Factor out compressHTML method in Util and use it from bugSplit

VE output uses innerHTML directly without smart quoting for JSON attributes.
This produces a large diff, which makes it hard to detect actual content
changes.

This patch makes our smart quoting available from Util and uses it in bugSplit
for the editedHtml content.

Change-Id: Id5e628a8af3cacc515b37c7be01ce2f021635e20
---
M js/lib/mediawiki.Util.js
M js/tests/bugSplit.js
2 files changed, 55 insertions(+), 32 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/Parsoid 
refs/changes/65/59865/1

diff --git a/js/lib/mediawiki.Util.js b/js/lib/mediawiki.Util.js
index f401304..ecaf3b3 100644
--- a/js/lib/mediawiki.Util.js
+++ b/js/lib/mediawiki.Util.js
@@ -1041,6 +1041,45 @@
 },
 
 /**
+ * @method compressHTML
+ *
+ * Compress an HTML string by applying some smart quoting
+ *
+ * @param {string} html
+ * @returns {string}
+ */
+compressHTML = function(html) {
+       // now compress our output (and make it more readable) by using
+       // "smart quoting" of attribute values -- using single-quotes
+       // where the contents have a lot of double quotes.
+       // since the output of outerHTML is specified strictly, we know
+       // this regexp is safe. See:
+       // 
http://www.whatwg.org/specs/web-apps/current-work/multipage/the-end.html
+       // 
http://www.whatwg.org/specs/web-apps/current-work/multipage/syntax.html
+       var smart_quote = function(match, name, equals, value) {
+               if (!equals) { return match; }
+               var decoded = entities.decode(value, 2);
+               // try re-encoding with single-quotes escaped
+               var encoded = decoded.replace(/[&'\u00A0]/g, function(c) {
+                       switch(c) {
+                       case '&': return '&';
+                       case "'": return ''';
+                       case '\u00A0': return ' ';
+                       }
+               });
+               if (encoded.length >= value.length) { return match; /* no 
change */ }
+               return ' '+name+"='"+encoded+"'";
+       };
+       var process_attr_list = function(match, tag, attrs) {
+               attrs = attrs.replace(/ ([^\0-\cZ\s"'>\/=]+)(="([^"]*)")?/g,
+                                     smart_quote);
+               return tag + attrs + '>';
+       };
+       return html.replace(/(<\w+)((?: [^\0-\cZ\s"'>\/=]+(?:="[^"]*")?)+)>/g,
+                           process_attr_list);
+},
+
+/**
  * @method serializeNode
  *
  * Serialize a HTML document.
@@ -1072,35 +1111,7 @@
                fictional.appendChild(doc.cloneNode());
                html = fictional.innerHTML;
        }
-       // now compress our output (and make it more readable) by using
-       // "smart quoting" of attribute values -- using single-quotes
-       // where the contents have a lot of double quotes.
-       // since the output of outerHTML is specified strictly, we know
-       // this regexp is safe. See:
-       // 
http://www.whatwg.org/specs/web-apps/current-work/multipage/the-end.html
-       // 
http://www.whatwg.org/specs/web-apps/current-work/multipage/syntax.html
-       var smart_quote = function(match, name, equals, value) {
-               if (!equals) { return match; }
-               var decoded = entities.decode(value, 2);
-               // try re-encoding with single-quotes escaped
-               var encoded = decoded.replace(/[&'\u00A0]/g, function(c) {
-                       switch(c) {
-                       case '&': return '&amp;';
-                       case "'": return '&#39;';
-                       case '\u00A0': return '&nbsp;';
-                       }
-               });
-               if (encoded.length >= value.length) { return match; /* no 
change */ }
-               return ' '+name+"='"+encoded+"'";
-       };
-       var process_attr_list = function(match, tag, attrs) {
-               attrs = attrs.replace(/ ([^\0-\cZ\s"'>\/=]+)(="([^"]*)")?/g,
-                                     smart_quote);
-               return tag + attrs + '>';
-       };
-       html = html.replace(/(<\w+)((?: [^\0-\cZ\s"'>\/=]+(?:="[^"]*")?)+)>/g,
-                           process_attr_list);
-       return html;
+       return compressHTML(html);
 },
 
 /**
@@ -1115,8 +1126,10 @@
        return entities.encode(string, 0 /* xml entities */);
 };
 
+// FIXME gwicke: define this directly
 Util.encodeXml = encodeXml;
 Util.parseHTML = parseHTML;
+Util.compressHTML = compressHTML;
 Util.serializeNode = serializeNode;
 Util.normalizeHTML = normalizeHTML;
 Util.normalizeOut = normalizeOut;
diff --git a/js/tests/bugSplit.js b/js/tests/bugSplit.js
index f44ab5e..238bb40 100644
--- a/js/tests/bugSplit.js
+++ b/js/tests/bugSplit.js
@@ -2,15 +2,25 @@
  * Split up a bug report JSON file into a bunch of files
  */
 
-var fs = require('fs');
+var fs = require('fs'),
+       Util = require( '../lib/mediawiki.Util.js' ).Util;
 
 function writeFiles ( data ) {
-       var keys = Object.keys(data);
+       var keys = Object.keys(data),
+               val;
        for ( var i = 0; i < keys.length; i++ ) {
                var key = keys[i],
                        fileName = encodeURIComponent(key);
                console.log( 'Creating file ' + fileName );
-               fs.writeFileSync(fileName, data[key]);
+
+               val = data[key];
+
+               if (fileName === 'editedHtml') {
+                       // apply smart quoting to minimize diff
+                       val = Util.compressHTML(val);
+               }
+
+               fs.writeFileSync(fileName, val);
        }
 }
 

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: Id5e628a8af3cacc515b37c7be01ce2f021635e20
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/Parsoid
Gerrit-Branch: master
Gerrit-Owner: GWicke <[email protected]>

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

Reply via email to