Subramanya Sastry has uploaded a new change for review.

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

Change subject: P-wrapper: Code cleanup + refactoring
......................................................................

P-wrapper: Code cleanup + refactoring

* Renamed some properties and methods to more accurately reflect
  functionality.

* Split up overloaded function into two distinct functions and
  extract common code into one of them.

* Moved buffer resets closer to where they are being processed.

* No change in parser test results (as it should be).

Change-Id: I51a0ae830bf4487e6ff68c869d1457734728fc47
---
M lib/ext.core.ParagraphWrapper.js
1 file changed, 42 insertions(+), 30 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/services/parsoid 
refs/changes/01/167301/1

diff --git a/lib/ext.core.ParagraphWrapper.js b/lib/ext.core.ParagraphWrapper.js
index 82cc3df..ca93c07 100644
--- a/lib/ext.core.ParagraphWrapper.js
+++ b/lib/ext.core.ParagraphWrapper.js
@@ -39,7 +39,7 @@
        this.tableTags = [];
        // This is the ordering of buffered tokens and how they should get 
emitted:
        //
-       //   non-nl-tokens        (from previous lines if newLineCount > 0)
+       //   token-buffer         (from previous lines if newLineCount > 0)
        //   newline-ws-tokens    (buffered nl+sol-transparent tokens since 
last non-nl-token)
        //   current-line-tokens  (all tokens after newline-ws-tokens)
        //
@@ -48,7 +48,7 @@
        // Periodically, when it is clear where an open/close p-tag is 
required, the buffers
        // are collapsed and emitted. Wherever tokens are buffered/emitted, 
verify that this
        // order is preserved.
-       this.nonNlTokens = [];
+       this.tokenBuffer = [];
        this.newLineCount = 0;
        this.nlWsTokens = [];
        this.hasOpenPTag = false;
@@ -120,16 +120,31 @@
        };
 };
 
-ParagraphWrapper.prototype._getTokensAndReset = function (res) {
+ParagraphWrapper.prototype._processBuffers = function(token, isBlockToken, 
flushCurrentLine) {
+       var res = this.processPendingNLs(isBlockToken);
+
+       this.currLine.tokens.push(token);
+       if (flushCurrentLine) {
+               res = res.concat(this.currLine.tokens);
+               this.resetCurrLine();
+       }
+
+       res = removeUselessPWrappers(res);
+       this.env.log("trace/p-wrap", this.manager.pipelineId, "---->  ", 
function() { return JSON.stringify(res); });
+       res.rank = this.SKIP_RANK; // ensure this propagates further in the 
pipeline, and doesn't hit the AnyHandler
+       return res;
+};
+
+ParagraphWrapper.prototype._flushBuffers = function() {
        // Assertion to catch bugs in p-wrapping; both cannot be true.
-       if (!res && this.newLineCount > 0) {
+       if (this.newLineCount > 0) {
                this.env.log("error/p-wrap",
-                       "Failed assertion in _getTokensAndReset: 
newline-count:", this.newLineCount,
+                       "Failed assertion in _flushBuffers: newline-count:", 
this.newLineCount,
                        "; buffered tokens: ", JSON.stringify(this.nlWsTokens));
        }
 
-       var resToks = res ? res : this.nonNlTokens.concat(this.nlWsTokens);
-       this.nonNlTokens = [];
+       var resToks = this.tokenBuffer.concat(this.nlWsTokens);
+       this.tokenBuffer = [];
        this.nlWsTokens = [];
        this.newLineCount = 0;
        var newRes = removeUselessPWrappers(resToks);
@@ -251,13 +266,13 @@
                        "; current line tokens: ", JSON.stringify(l.tokens));
        }
 
-       this.nonNlTokens = this.nonNlTokens.concat(l.tokens);
+       this.tokenBuffer = this.tokenBuffer.concat(l.tokens);
 
        this.resetCurrLine();
 
        if (token.constructor === EOFTk) {
                this.nlWsTokens.push(token);
-               this.closeOpenPTag(this.nonNlTokens);
+               this.closeOpenPTag(this.tokenBuffer);
                var res = this.processPendingNLs(false);
                this.inPre = false;
                this.hasOpenPTag = false;
@@ -275,7 +290,7 @@
 };
 
 ParagraphWrapper.prototype.processPendingNLs = function(isBlockToken) {
-       var resToks = this.nonNlTokens,
+       var resToks = this.tokenBuffer,
                newLineCount = this.newLineCount,
                nlTk;
 
@@ -330,6 +345,12 @@
 
        // Gather remaining ws and nl tokens
        resToks = resToks.concat(this.nlWsTokens);
+
+       // reset buffers
+       this.tokenBuffer = [];
+       this.nlWsTokens = [];
+       this.newLineCount = 0;
+
        return resToks;
 };
 
@@ -389,15 +410,9 @@
                        this.currLine.tokens.push(' ');
                        return {};
                } else {
-                       res = this.processPendingNLs(true);
-                       this.currLine.tokens.push(token);
-                       res = res.concat(this.currLine.tokens);
-
                        this.manager.removeTransform(this.NEWLINE_RANK, 
'newline');
                        this.inPre = true;
-                       this.resetCurrLine();
-
-                       return { tokens: this._getTokensAndReset(res) };
+                       return { tokens: this._processBuffers(token, true, 
true) };
                }
        } else if (tc === EndTagTk && token.name === 'pre') {
                if (this.hasOpenHTMLPTag) {
@@ -432,8 +447,8 @@
                if (this.newLineCount === 0) {
                        this.currLine.tokens.push(token);
                        // Since we have no pending newlines to trip us up,
-                       // no need to buffer -- just emit everything
-                       return { tokens: this._getTokensAndReset() };
+                       // no need to buffer -- just flush everything
+                       return { tokens: this._flushBuffers() };
                } else {
                        // We are in buffering mode waiting till we are ready to
                        // process pending newlines.
@@ -443,11 +458,12 @@
        } else if (tc !== String && Util.isSolTransparent(token)) {
                if (this.newLineCount === 0) {
                        this.currLine.tokens.push(token);
-                       // Safe to push these out since we have no pending 
newlines to trip us up.
-                       return { tokens: this._getTokensAndReset() };
+                       // Since we have no pending newlines to trip us up,
+                       // no need to buffer -- just flush everything
+                       return { tokens: this._flushBuffers() };
                } else if (this.newLineCount === 1) {
                        // Swallow newline, whitespace, comments, and the 
current line
-                       this.nonNlTokens = 
this.nonNlTokens.concat(this.nlWsTokens, this.currLine.tokens);
+                       this.tokenBuffer = 
this.tokenBuffer.concat(this.nlWsTokens, this.currLine.tokens);
                        this.newLineCount = 0;
                        this.nlWsTokens = [];
                        this.resetCurrLine();
@@ -456,9 +472,7 @@
                        this.currLine.tokens.push(token);
                        return {};
                } else {
-                       res = this.processPendingNLs(false);
-                       this.currLine.tokens.push(token);
-                       return { tokens: this._getTokensAndReset(res) };
+                       return { tokens: this._processBuffers(token, false, 
false) };
                }
        } else {
                var isBlockToken = Util.isBlockToken(token);
@@ -486,16 +500,14 @@
                        this.hasOpenHTMLPTag = false;
                }
 
-               res = this.processPendingNLs(isBlockToken);
+               this.currLine.hasWrappableTokens = true;
+               var res = this._processBuffers(token, isBlockToken, false);
 
                // Partial DOM-building! What a headache!
                // This is necessary to avoid introducing fosterable tags 
inside the table.
                updateTableContext(this.tableTags, token);
 
-               this.currLine.tokens.push(token);
-               this.currLine.hasWrappableTokens = true;
-
-               return { tokens: this._getTokensAndReset(res) };
+               return { tokens: res };
        }
 };
 

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: I51a0ae830bf4487e6ff68c869d1457734728fc47
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/services/parsoid
Gerrit-Branch: master
Gerrit-Owner: Subramanya Sastry <[email protected]>

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

Reply via email to