jenkins-bot has submitted this change and it was merged.
Change subject: Merge adjacent <a> tags with identical attrs
......................................................................
Merge adjacent <a> tags with identical attrs
* Merged only if:
- at least one of them is new, and
- merging explicitly requested via the scrubWikitext=true API param
* Tests added to spec behavior.
Change-Id: I45e6c198b3578426fec7731a03825be040deb7e3
---
M lib/wts.normalizeDOM.js
M tests/parserTests.txt
2 files changed, 90 insertions(+), 31 deletions(-)
Approvals:
Cscott: Looks good to me, approved
jenkins-bot: Verified
diff --git a/lib/wts.normalizeDOM.js b/lib/wts.normalizeDOM.js
index 770ae60..dda9d9f 100644
--- a/lib/wts.normalizeDOM.js
+++ b/lib/wts.normalizeDOM.js
@@ -12,14 +12,23 @@
var DU = require('./mediawiki.DOMUtils.js').DOMUtils;
var Consts = require('./mediawiki.wikitext.constants.js').WikitextConstants;
-var ignoreableAttribs = new Set(['data-parsoid', 'data-parsoid-diff']);
+var wtIgnorableAttrs = new Set(['data-parsoid', 'id', 'title']);
+var htmlIgnorableAttrs = new Set(['data-parsoid']);
function similar(a, b) {
- var aIsHtml = DU.isLiteralHTMLNode(a);
- var bIsHtml = DU.isLiteralHTMLNode(b);
+ if (a.nodeName === 'A') {
+ return DU.attribsEquals(a, b, wtIgnorableAttrs);
+ } else {
+ var aIsHtml = DU.isLiteralHTMLNode(a);
+ var bIsHtml = DU.isLiteralHTMLNode(b);
+ var ignorableAttrs = aIsHtml ? htmlIgnorableAttrs :
wtIgnorableAttrs;
- return (!aIsHtml && !bIsHtml) ||
- (aIsHtml && bIsHtml && DU.attribsEquals(a, b,
ignoreableAttribs));
+ // FIXME: For non-HTML I/B tags, we seem to be dropping all
attributes
+ // in our tag handlers (which seems like a bug). Till that is
fixed,
+ // we'll preserve existing functionality here.
+ return (!aIsHtml && !bIsHtml) ||
+ (aIsHtml && bIsHtml && DU.attribsEquals(a, b,
ignorableAttrs));
+ }
}
/** Can a and b be merged into a single node? */
@@ -91,40 +100,47 @@
* This function merges adjacent nodes of the same type
* and swaps nodes where possible to enable further merging.
*
- * See examples below for a (B, I) tag-pair:
+ * See examples below:
*
* 1. <b>X</b><b>Y</b>
* ==> <b>XY</b>
*
* 2. <i>A</i><b><i>X</i></b><b><i>Y</i></b><i>Z</i>
* ==> <i>A<b>XY</b>Z</i>
+ *
+ * 3. <a href="Football">Foot</a><a href="Football">ball</a>
+ * ==> <a href="Football">Football</a>
*/
-function rewriteablePair(a, b) {
- // Currently supported: 'a' and 'b' are both B/I tags
- //
- // For <i>/<b> pair, we need not check whether the node being
transformed
- // are new / edited, etc. since these minimization scenarios can
- // never show up in HTML that came from parsed wikitext.
- //
- // <i>..</i><i>..</i> can never show up without a <nowiki/> in between.
- // Similarly for <b>..</b><b>..</b> and <b><i>..</i></b><i>..</i>.
- //
- // This is because a sequence of 4 quotes is not parsed as ..</i><i>..
- // Neither is a sequence of 7 quotes parsed as ..</i></b><i>..
- //
- // So, if we see a minimizable pair of nodes, it is because the HTML
- // didn't originate from wikitext OR the HTML has been subsequently
edited.
- // In both cases, we want to transform the DOM.
+function rewriteablePair(env, a, b) {
+ if (Consts.WTQuoteTags.has(a.nodeName)) {
+ // For <i>/<b> pair, we need not check whether the node being
transformed
+ // are new / edited, etc. since these minimization scenarios can
+ // never show up in HTML that came from parsed wikitext.
+ //
+ // <i>..</i><i>..</i> can never show up without a <nowiki/> in
between.
+ // Similarly for <b>..</b><b>..</b> and
<b><i>..</i></b><i>..</i>.
+ //
+ // This is because a sequence of 4 quotes is not parsed as
..</i><i>..
+ // Neither is a sequence of 7 quotes parsed as ..</i></b><i>..
+ //
+ // So, if we see a minimizable pair of nodes, it is because the
HTML
+ // didn't originate from wikitext OR the HTML has been
subsequently edited.
+ // In both cases, we want to transform the DOM.
- return Consts.WTQuoteTags.has(a.nodeName) &&
- Consts.WTQuoteTags.has(b.nodeName);
+ return Consts.WTQuoteTags.has(b.nodeName);
+ } else if (env.scrubWikitext && a.nodeName === 'A') {
+ // Link merging is only supported in scrubWikitext mode.
+ // For <a> tags, we require at least one of the two tags
+ // to be a newly created element.
+ return b.nodeName === 'A' && (DU.isNewElt(a) || DU.isNewElt(b));
+ }
}
function normalizeSiblingPair(env, a, b) {
// If 'a' and 'b' make a rewriteable tag-pair,
// we are good to go.
- if (rewriteablePair(a, b)) {
+ if (rewriteablePair(env, a, b)) {
if (mergable(a, b)) {
a = merge(env, a, b);
// The new a's children have new siblings. So let's look
diff --git a/tests/parserTests.txt b/tests/parserTests.txt
index 079566c..164eec6 100644
--- a/tests/parserTests.txt
+++ b/tests/parserTests.txt
@@ -23660,9 +23660,9 @@
* asd sdf
!! end
-#-----------------------------
-# I/B quote minimization tests
-#-----------------------------
+#-----------------------
+# Tag minimization tests
+#-----------------------
!! test
1. I/B quote minimization: wikitext-only tags should be combined
@@ -23767,9 +23767,52 @@
''ac''
!! end
-#------------------------------------
-# End of I/B quote minimization tests
-#------------------------------------
+!! test
+1. Merge adjacent link nodes as long as at least one element is new
+!! options
+parsoid={
+ "modes": ["html2wt"],
+ "scrubWikitext": true
+}
+!! html
+<a rel="mw:WikiLink" href="./Football">Foot</a><a rel="mw:WikiLink"
href="./Football">ball</a>
+<a data-parsoid="{}" rel="mw:WikiLink" href="./Football">Foot</a><a
rel="mw:WikiLink" href="./Football">ball</a>
+<a data-parsoid="{}" rel="mw:WikiLink" href="./Football">Foot</a><a
data-parsoid="{}" rel="mw:WikiLink" href="./Football">ball</a>
+!! wikitext
+[[Football]]
+[[Football]]
+[[Football|Foot]][[Football|ball]]
+!! end
+
+!! test
+2. Merge adjacent link nodes and enable additional normalizations
+!! options
+parsoid={
+ "modes": ["html2wt"],
+ "scrubWikitext": true
+}
+!! html
+<a rel="mw:WikiLink" href="./Football"><i>Foot</i></a><a rel="mw:WikiLink"
href="./Football"><i>ball</i></a>
+!! wikitext
+[[Football|''Football'']]
+!! end
+
+!! test
+3. Don't merge adjacent link nodes if scrubWikitext is false
+!! options
+parsoid={
+ "modes": ["html2wt"],
+ "scrubWikitext": false
+}
+!! html
+<a rel="mw:WikiLink" href="./Football">Foot</a><a rel="mw:WikiLink"
href="./Football">ball</a>
+!! wikitext
+[[Football|Foot]][[Football|ball]]
+!! end
+
+#------------------------------
+# End of tag minimization tests
+#------------------------------
!!test
Bug 54262: New entities
--
To view, visit https://gerrit.wikimedia.org/r/201926
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: merged
Gerrit-Change-Id: I45e6c198b3578426fec7731a03825be040deb7e3
Gerrit-PatchSet: 8
Gerrit-Project: mediawiki/services/parsoid
Gerrit-Branch: master
Gerrit-Owner: Subramanya Sastry <[email protected]>
Gerrit-Reviewer: Arlolra <[email protected]>
Gerrit-Reviewer: Catrope <[email protected]>
Gerrit-Reviewer: Cscott <[email protected]>
Gerrit-Reviewer: Esanders <[email protected]>
Gerrit-Reviewer: Marcoil <[email protected]>
Gerrit-Reviewer: Subramanya Sastry <[email protected]>
Gerrit-Reviewer: Tim Starling <[email protected]>
Gerrit-Reviewer: jenkins-bot <>
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits