jenkins-bot has submitted this change and it was merged.

Change subject: Cleanup: Refactor separator regexps and assign meaningful names
......................................................................


Cleanup: Refactor separator regexps and assign meaningful names

* Extracted from the now reverted patch b319c3d7

Change-Id: Id5b5911d18e63cd0697bf6063cc2be29603e8c64
---
M lib/mediawiki.WikitextSerializer.js
M lib/wts.separators.js
2 files changed, 18 insertions(+), 9 deletions(-)

Approvals:
  Arlolra: Looks good to me, approved
  jenkins-bot: Verified



diff --git a/lib/mediawiki.WikitextSerializer.js 
b/lib/mediawiki.WikitextSerializer.js
index 81fccf9..23c12cf 100644
--- a/lib/mediawiki.WikitextSerializer.js
+++ b/lib/mediawiki.WikitextSerializer.js
@@ -26,6 +26,7 @@
 require('./core-upgrade.js');
 
 var util = require('util');
+var JSUtils = require('./jsutils.js').JSUtils;
 var wtConsts = require('./mediawiki.wikitext.constants.js');
 var Util = require('./mediawiki.Util.js').Util;
 var DU = require('./mediawiki.DOMUtils.js').DOMUtils;
@@ -814,18 +815,25 @@
        }
 };
 
+WSP.separatorREs = {
+       pureSepRE: /^\s*$/,
+       sepPrefixWithNlsRE: /^[ \t]*\n+\s*/,
+       sepSuffixWithNlsRE: /\n\s*$/,
+       doubleNewlineRE_G: /\n([ \t]*\n)+/g,
+};
+
 /**
  * Serialize the content of a text node
  */
 WSP._serializeTextNode = function(node, state, cb) {
        // write out a potential separator?
        var res = node.nodeValue;
-       var doubleNewlineMatch = res.match(/\n([ \t]*\n)+/g);
+       var doubleNewlineMatch = res.match(this.separatorREs.doubleNewlineRE_G);
        var doubleNewlineCount = doubleNewlineMatch && 
doubleNewlineMatch.length || 0;
 
        // Deal with trailing separator-like text (at least 1 newline and other 
whitespace)
-       var newSepMatch = res.match(/\n\s*$/);
-       res = res.replace(/\n\s*$/, '');
+       var newSepMatch = res.match(this.separatorREs.sepSuffixWithNlsRE);
+       res = res.replace(this.separatorREs.sepSuffixWithNlsRE, '');
 
        if (!state.inIndentPre) {
                // Don't strip two newlines for wikitext like this:
@@ -837,7 +845,7 @@
                if (!state.inHTMLPre && 
(!DU.allChildrenAreText(node.parentNode) ||
                        doubleNewlineCount !== 1)) {
                        // Strip more than one consecutive newline
-                       res = res.replace(/\n([ \t]*\n)+/g, '\n');
+                       res = res.replace(this.separatorREs.doubleNewlineRE_G, 
'\n');
                }
                // Strip trailing newlines from text content
                // if (node.nextSibling && DU.isElt(node.nextSibling)) {
@@ -848,7 +856,7 @@
 
                // Strip leading newlines and other whitespace
                // They are already added to the separator source in 
handleSeparatorText.
-               res = res.replace(/^[ \t]*\n+\s*/, '');
+               res = res.replace(this.separatorREs.sepPrefixWithNlsRE, '');
        }
 
        // Always escape entities
@@ -895,8 +903,8 @@
        // in handleSeparatorText.
        var res = text.replace(/^\n/, '');
        // Deal with trailing newlines
-       var newSepMatch = res.match(/\n\s*$/);
-       res = res.replace(/\n\s*$/, '');
+       var newSepMatch = res.match(this.separatorREs.sepSuffixWithNlsRE);
+       res = res.replace(this.separatorREs.sepSuffixWithNlsRE, '');
        cb(res, node);
        state.sep.lastSourceNode = node;
        // Move trailing newlines into the next separator
diff --git a/lib/wts.separators.js b/lib/wts.separators.js
index 31353cf..3cbeaca 100644
--- a/lib/wts.separators.js
+++ b/lib/wts.separators.js
@@ -147,8 +147,9 @@
  * XXX: Support separator-transparent elements!
  */
 var handleSeparatorText = function(node, state) {
+       var separatorREs = state.serializer.separatorREs;
        if (!state.inIndentPre && DU.isText(node)) {
-               if (node.nodeValue.match(/^\s*$/)) {
+               if (node.nodeValue.match(separatorREs.pureSepRE)) {
                        state.sep.src = (state.sep.src || '') + node.nodeValue;
 
                        // Same caveat about onSOL and <li> nodes
@@ -157,7 +158,7 @@
 
                        return true;
                } else {
-                       var match = node.nodeValue.match(/^[ \t]*\n+\s*/);
+                       var match = 
node.nodeValue.match(separatorREs.sepPrefixWithNlsRE);
                        if (match) {
                                state.sep.src = (state.sep.src || '') + 
match[0];
 

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

Gerrit-MessageType: merged
Gerrit-Change-Id: Id5b5911d18e63cd0697bf6063cc2be29603e8c64
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/services/parsoid
Gerrit-Branch: master
Gerrit-Owner: Subramanya Sastry <[email protected]>
Gerrit-Reviewer: Arlolra <[email protected]>
Gerrit-Reviewer: jenkins-bot <>

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

Reply via email to