Arlolra has uploaded a new change for review.
https://gerrit.wikimedia.org/r/99719
Change subject: WIP: Avoid indent-pre when line contains block tags
......................................................................
WIP: Avoid indent-pre when line contains block tags
Change-Id: I130bcad0437d2b1aaa7a38d3cfbcd54cda33756d
---
M js/lib/ext.core.PreHandler.js
M js/tests/parserTests.txt
2 files changed, 55 insertions(+), 18 deletions(-)
git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/Parsoid
refs/changes/19/99719/1
diff --git a/js/lib/ext.core.PreHandler.js b/js/lib/ext.core.PreHandler.js
index 498455d..7b07a5e 100644
--- a/js/lib/ext.core.PreHandler.js
+++ b/js/lib/ext.core.PreHandler.js
@@ -41,7 +41,7 @@
+ --------------+-----------------+---------------+--------------------------+
| PRE_COLLECT | --- nl --> | MULTILINE_PRE | save nl token |
| PRE_COLLECT | --- eof --> | SOL | gen-pre |
- | PRE_COLLECT | --- blk tag --> | IGNORE | gen-pre |
+ | PRE_COLLECT | --- blk tag --> | IGNORE | gen-pre/purge (#) |
| PRE_COLLECT | --- any --> | PRE_COLLECT | TOKS << tok |
+ --------------+-----------------+---------------+--------------------------+
| MULTILINE_PRE | --- nl --> | SOL | gen-pre |
@@ -54,6 +54,9 @@
| IGNORE | --- nl --> | SOL | purge |
| IGNORE | --- eof --> | SOL | purge |
+ --------------+-----------------+---------------+--------------------------+
+
+ # If we've collected any tokens from previous lines, generate a pre. This
+ line gets purged.
## In these states, check if the whitespace token is a single space or has
additional chars (white-space or non-whitespace) -- if yes, slice it off
@@ -123,6 +126,7 @@
// preceding newline to initialize this.
handler.preTSR = 0;
handler.tokens = [];
+ handler.preCollectCurrentLine = [];
handler.preWSToken = null;
handler.multiLinePreWSToken = null;
handler.solTransparentTokens = [];
@@ -142,6 +146,29 @@
ret.push(this.lastNlTk);
this.lastNlTk = null;
}
+};
+
+PreHandler.prototype.resetPreCollectCurrentLine = function () {
+ this.tokens = this.tokens.concat( this.preCollectCurrentLine );
+ this.preCollectCurrentLine = [];
+};
+
+PreHandler.prototype.encounteredBlockWhileCollecting = function ( token ) {
+ var ret = [];
+
+ if ( this.tokens.length > 0 ) {
+ ret = this.processPre( null ).slice(0, -1); // drop the null
token
+ }
+
+ if ( this.preWSToken ) {
+ ret.push( this.preWSToken );
+ this.preWSToken = null;
+ }
+
+ this.resetPreCollectCurrentLine();
+ ret = ret.concat( this.getResultAndReset( token ) );
+ ret.rank = this.skipRank;
+ return ret;
};
PreHandler.prototype.getResultAndReset = function(token) {
@@ -173,7 +200,7 @@
if (this.preTSR !== -1) {
da = { tsr: [this.preTSR, this.preTSR+1] };
}
- ret = [ new TagTk('pre', [], da)
].concat(ret).concat(this.tokens);
+ ret = [ new TagTk('pre', [], da) ].concat( this.tokens );
ret.push(new EndTagTk('pre'));
}
@@ -229,6 +256,9 @@
break;
case PreHandler.STATE_PRE_COLLECT:
+ this.preWSToken = null;
+ this.multiLinePreWSToken = null;
+ this.resetPreCollectCurrentLine();
this.lastNlTk = token;
this.state = PreHandler.STATE_MULTILINE_PRE;
break;
@@ -303,6 +333,9 @@
case PreHandler.STATE_PRE_COLLECT:
case PreHandler.STATE_MULTILINE_PRE:
+ this.preWSToken = null;
+ this.multiLinePreWSToken = null;
+ this.resetPreCollectCurrentLine();
ret = this.processPre(token);
break;
}
@@ -342,26 +375,19 @@
ret = this.getResultAndReset(token);
this.moveToIgnoreState();
} else {
- this.tokens =
this.tokens.concat(this.solTransparentTokens);
- this.tokens.push(token);
+ this.preCollectCurrentLine =
this.solTransparentTokens.concat( token );
this.solTransparentTokens = [];
- // discard pre/multiline-pre ws tokens
that got us here
- this.preWSToken = null;
- this.multiLinePreWSToken = null;
this.state =
PreHandler.STATE_PRE_COLLECT;
}
break;
case PreHandler.STATE_PRE_COLLECT:
if (token.isHTMLTag && token.isHTMLTag() &&
Util.isBlockTag(token.name)) {
- ret = this.processPre(token);
+ ret =
this.encounteredBlockWhileCollecting( token );
this.moveToIgnoreState();
} else {
- // discard pre/multiline-pre ws tokens
that got us here
- this.preWSToken = null;
- this.multiLinePreWSToken = null;
// nothing to do .. keep collecting!
- this.tokens.push(token);
+ this.preCollectCurrentLine.push( token
);
}
break;
diff --git a/js/tests/parserTests.txt b/js/tests/parserTests.txt
index 6a96970..5b1e387 100644
--- a/js/tests/parserTests.txt
+++ b/js/tests/parserTests.txt
@@ -2118,19 +2118,30 @@
!!test
3a. Indent-Pre and block tags (single-line html)
!!input
- <p> foo </p>
- <div> foo </div>
- <blockquote> foo </blockquote>
+ a <p> foo </p>
+ b <div> foo </div>
+ c <blockquote> foo </blockquote>
<span> foo </span>
!!result
- <p> foo </p>
- <div> foo </div>
- <blockquote> foo </blockquote>
+ a <p> foo </p>
+ b <div> foo </div>
+ c <blockquote> foo </blockquote>
<pre><span> foo </span>
</pre>
!!end
!!test
+3c. Indent-Pre and block tags (multi-line html)
+!!input
+ a <span>foo</span>
+ b <div> foo </div>
+!!result
+<pre>a <span>foo</span>
+</pre>
+ b <div> foo </div>
+!!end
+
+!!test
3b. Indent-Pre and block tags (pre-content on separate line)
!!input
<p>
--
To view, visit https://gerrit.wikimedia.org/r/99719
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: newchange
Gerrit-Change-Id: I130bcad0437d2b1aaa7a38d3cfbcd54cda33756d
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/Parsoid
Gerrit-Branch: master
Gerrit-Owner: Arlolra <[email protected]>
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits