Subramanya Sastry has uploaded a new change for review.

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

Change subject: WIP: T88495: Part 2 of 2: Handle more templated <td>-attr 
scenarios
......................................................................

WIP: T88495: Part 2 of 2: Handle more templated <td>-attr scenarios

* Code cleanup needed.
* More testing needed.
* More tests needed.
* But, this should now handle more complex multi-cell template
  scenarios.

Change-Id: I6244bdfe5b9a36f5b28357423d2ade1bc6c9b75f
---
M lib/dom.t.TableFixups.js
M tests/parserTests-blacklist.js
2 files changed, 54 insertions(+), 18 deletions(-)


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

diff --git a/lib/dom.t.TableFixups.js b/lib/dom.t.TableFixups.js
index 2978503..3258036 100644
--- a/lib/dom.t.TableFixups.js
+++ b/lib/dom.t.TableFixups.js
@@ -1,6 +1,7 @@
 "use strict";
 
-var DU = require('./mediawiki.DOMUtils.js').DOMUtils,
+var Util = require('./mediawiki.Util.js').Util,
+       DU = require('./mediawiki.DOMUtils.js').DOMUtils,
        PegTokenizer = require('./mediawiki.tokenizer.peg.js').PegTokenizer,
        Sanitizer = require('./ext.core.Sanitizer.js').Sanitizer,
        defines = require('./mediawiki.parser.defines.js');
@@ -110,10 +111,11 @@
  * nowiki content. Collection stops when encountering other nodes or a pipe
  * character.
  */
-TableFixups.prototype.collectAttributishContent = function (node) {
+TableFixups.prototype.collectAttributishContent = function(node, 
templateWrapper) {
        var buf = [],
                nodes = [],
-               transclusionNode = DU.hasTypeOf(node, 'mw:Transclusion') ? node 
: null;
+               transclusionNode = templateWrapper ||
+                       (DU.hasTypeOf(node, 'mw:Transclusion') ? node : null);
 
        // Build the result.
        var buildRes = function () {
@@ -193,19 +195,7 @@
  *   limitation can be lifted with more advanced data-mw construction.
  */
 
-// SSS FIXME: It is silly to examine every fricking <td> for possible fixup.
-// We only need to examine <td>s that either have mw:Transclusion typeof or
-// have a child (not descendent) with a mw:Transclusion typeof.
-//
-// This info is not readily available right now, but perhaps could be provided
-// based on annotating nodes via a tmp attribute during tpl wrapping.
-//
-// Or, perhaps the tokenizer can mark <td>s that have a transclusion node
-// on the same wikitext line.
-//
-// TO BE DONE.
-
-TableFixups.prototype.reparseTemplatedAttributes = function (env, node) {
+TableFixups.prototype.fixupTD = function(env, node, templateWrapper) {
        var dp = DU.getDataParsoid( node );
 
        // Cheap checks first:
@@ -222,7 +212,7 @@
        }
 
        // Collect attribute content and examine it
-       var attributishContent = this.collectAttributishContent(node),
+       var attributishContent = this.collectAttributishContent(node, 
templateWrapper),
                transclusionNode = attributishContent.transclusionNode;
 
        // First of all make sure we have a transclusion
@@ -335,6 +325,53 @@
        return true;
 };
 
+// SSS FIXME: It is silly to examine every fricking <td> for possible fixup.
+// We only need to examine <td>s that either have mw:Transclusion typeof or
+// have a child (not descendent) with a mw:Transclusion typeof.
+//
+// This info is not readily available right now, but perhaps could be provided
+// based on annotating nodes via a tmp attribute during tpl wrapping.
+//
+// Or, perhaps the tokenizer can mark <td>s that have a transclusion node
+// on the same wikitext line.
+//
+// TO BE DONE.
+
+TableFixups.prototype.reparseTemplatedAttributes = function (env, node) {
+
+       // First, fixup the <td> for templated attrs
+       var about = node.getAttribute("about");
+       var templateWrapper = about && Util.isParsoidObjectId(about) ? node : 
null;
+       var fixupVal = this.fixupTD(env, node, templateWrapper);
+
+       // Now, examine the <td> to see if it hides additional <td>s
+       // and split it up if required.
+       //
+       // Recursion takes care of fixing up style attrs and additional splits.
+       var newTD, ownerDoc = node.ownerDocument;
+       var child = node.firstChild;
+       while (child) {
+               var next = child.nextSibling;
+
+               if (newTD) {
+                       newTD.appendChild(child);
+               } else if (DU.isText(child)) {
+                       var match = 
child.textContent.match(/^(.*?[^|])?\|\|([^|].*)?$/);
+                       if (match) {
+                               newTD = ownerDoc.createElement('td');
+                               newTD.setAttribute("about", 
node.getAttribute("about"));
+                               child.textContent = match[1];
+                               
newTD.appendChild(ownerDoc.createTextNode(match[2] || ''));
+                               node.parentNode.insertBefore(newTD, 
node.nextSibling);
+                       }
+               }
+
+               child = next;
+       }
+
+       return fixupVal;
+};
+
 if (typeof module === "object") {
        module.exports.TableFixups = TableFixups;
 }
diff --git a/tests/parserTests-blacklist.js b/tests/parserTests-blacklist.js
index fb057a3..6027478 100644
--- a/tests/parserTests-blacklist.js
+++ b/tests/parserTests-blacklist.js
@@ -54,7 +54,6 @@
 add("wt2html", "Definition Lists: colons and tables 1", "<dl 
data-parsoid='{\"dsr\":[0,21,0,0]}'><dd 
data-parsoid='{\"dsr\":[0,10,1,0]}'><table 
data-parsoid='{\"dsr\":[1,10,2,2]}'>\n<tbody 
data-parsoid='{\"dsr\":[4,8,0,0]}'><tr 
data-parsoid='{\"autoInsertedEnd\":true,\"autoInsertedStart\":true,\"dsr\":[4,7,0,0]}'><td
 data-parsoid='{\"autoInsertedEnd\":true,\"dsr\":[4,7,1,0]}'> 
x</td></tr>\n</tbody></table></dd>\n<dd 
data-parsoid='{\"dsr\":[11,21,1,0]}'><table 
data-parsoid='{\"dsr\":[12,21,2,2]}'>\n<tbody 
data-parsoid='{\"dsr\":[15,19,0,0]}'><tr 
data-parsoid='{\"autoInsertedEnd\":true,\"autoInsertedStart\":true,\"dsr\":[15,18,0,0]}'><td
 data-parsoid='{\"autoInsertedEnd\":true,\"dsr\":[15,18,1,0]}'> 
y</td></tr>\n</tbody></table></dd></dl>");
 add("wt2html", "Bug 2702: Mismatched <i>, <b> and <a> tags are invalid", "<p 
data-parsoid='{\"dsr\":[0,204,0,0]}'><i 
data-parsoid='{\"autoInsertedEnd\":true,\"dsr\":[0,29,2,0]}'><a 
rel=\"mw:ExtLink\" href=\"http://example.com\"; 
data-parsoid='{\"targetOff\":22,\"contentOffsets\":[22,28],\"dsr\":[2,29,20,1]}'>text<i
 data-parsoid='{\"autoInsertedEnd\":true,\"dsr\":[26,28,2,0]}'></i></a></i>\n<a 
rel=\"mw:ExtLink\" href=\"http://example.com\"; 
data-parsoid='{\"targetOff\":50,\"contentOffsets\":[50,57],\"dsr\":[30,58,20,1]}'><b
 data-parsoid='{\"autoInsertedEnd\":true,\"dsr\":[50,57,3,0]}'>text</b></a><b 
data-parsoid='{\"autoInsertedEnd\":true,\"dsr\":[58,61,3,0]}'></b>\n<i 
data-parsoid='{\"autoInsertedEnd\":true,\"dsr\":[62,106,2,0]}'>Something <a 
rel=\"mw:ExtLink\" href=\"http://example.com\"; 
data-parsoid='{\"targetOff\":94,\"contentOffsets\":[94,105],\"dsr\":[74,106,20,1]}'>in
 italic<i 
data-parsoid='{\"autoInsertedEnd\":true,\"dsr\":[103,105,2,0]}'></i></a></i>\n<i
 data-parsoid='{\"dsr\":[107,164,2,2]}'>Something <a rel=\"mw:ExtLink\" 
href=\"http://example.com\"; 
data-parsoid='{\"targetOff\":139,\"contentOffsets\":[139,160],\"dsr\":[119,161,20,1]}'>mixed<b
 data-parsoid='{\"autoInsertedEnd\":true,\"dsr\":[144,160,3,0]}'><i 
data-parsoid='{\"autoInsertedEnd\":true,\"dsr\":[147,160,2,0]}'>, even 
bold</i></b></a>'</i>\n<b 
data-parsoid='{\"autoInsertedEnd\":true,\"dsr\":[165,204,3,0]}'><i 
data-parsoid='{\"autoInsertedEnd\":true,\"dsr\":[168,204,2,0]}'>Now <a 
rel=\"mw:ExtLink\" href=\"http://example.com\"; 
data-parsoid='{\"targetOff\":194,\"contentOffsets\":[194,203],\"dsr\":[174,204,20,1]}'>both<b
 data-parsoid='{\"autoInsertedEnd\":true,\"dsr\":[198,203,3,0]}'><i 
data-parsoid='{\"autoInsertedEnd\":true,\"dsr\":[201,203,2,0]}'></i></b></a></i></b></p>");
 add("wt2html", "External link containing double-single-quotes in text embedded 
in italics (bug 4598 sanity check)", "<p data-parsoid='{\"dsr\":[0,60,0,0]}'><i 
data-parsoid='{\"dsr\":[0,60,2,2]}'>Some <a rel=\"mw:ExtLink\" 
href=\"http://example.com/\"; 
data-parsoid='{\"targetOff\":28,\"contentOffsets\":[28,56],\"dsr\":[7,57,21,1]}'>pretty
 <i data-parsoid='{\"dsr\":[35,46,2,2]}'>italics</i> and stuff</a>!</i></p>");
-add("wt2html", "3. Template-generated table cell attributes and cell content", 
"<table data-parsoid='{\"dsr\":[0,35,2,2]}'>\n<tbody 
data-parsoid='{\"dsr\":[3,33,0,0]}'><tr 
data-parsoid='{\"autoInsertedEnd\":true,\"autoInsertedStart\":true,\"dsr\":[3,32,0,0]}'><td
 align=\"center\" style=\"color: red\" typeof=\"mw:Transclusion\" 
about=\"#mwt1\" 
data-parsoid='{\"autoInsertedEnd\":true,\"dsr\":[3,32,1,0],\"pi\":[[]]}' 
data-mw='{\"parts\":[\"|align=center 
\",{\"template\":{\"target\":{\"wt\":\"table_cells\",\"href\":\"./Template:Table_cells\"},\"params\":{},\"i\":0}}]}'>
 Foo || Bar || Baz</td></tr>\n</tbody></table>");
 add("wt2html", "Self-link to numeric title", "<p 
data-parsoid='{\"dsr\":[0,5,0,0]}'><a rel=\"mw:WikiLink\" href=\"./0\" 
title=\"0\" 
data-parsoid='{\"stx\":\"simple\",\"a\":{\"href\":\"./0\"},\"sa\":{\"href\":\"0\"},\"dsr\":[0,5,2,2]}'>0</a></p>");
 add("wt2html", "<nowiki> inside a link", "<p 
data-parsoid='{\"dsr\":[0,96,0,0]}'><a rel=\"mw:WikiLink\" href=\"./Main_Page\" 
title=\"Main Page\" 
data-parsoid='{\"stx\":\"simple\",\"a\":{\"href\":\"./Main_Page\"},\"sa\":{\"href\":\"Main&lt;nowiki>
 Page&lt;/nowiki>\"},\"dsr\":[0,30,2,2]}'>Main Page</a> <a rel=\"mw:WikiLink\" 
href=\"./Main_Page\" title=\"Main Page\" 
data-parsoid='{\"stx\":\"piped\",\"a\":{\"href\":\"./Main_Page\"},\"sa\":{\"href\":\"Main
 Page\"},\"dsr\":[31,96,12,2]}'>the main page <span typeof=\"mw:Nowiki\" 
data-parsoid='{\"dsr\":[57,94,8,9]}'>[it's not very good]</span></a></p>");
 add("wt2html", "2. Lists with start-of-line-transparent tokens before bullets: 
Template close", "<ul about=\"#mwt1\" typeof=\"mw:Transclusion\" 
data-parsoid='{\"dsr\":[0,18,0,0],\"pi\":[[{\"k\":\"1\",\"spc\":[\"\",\"\",\"\",\"\"]}]]}'
 data-mw='{\"parts\":[\"*foo 
\",{\"template\":{\"target\":{\"wt\":\"echo\",\"href\":\"./Template:Echo\"},\"params\":{\"1\":{\"wt\":\"bar\\n\"}},\"i\":0}}]}'><li>foo
 bar</li></ul><span about=\"#mwt1\">\n</span><p 
data-parsoid='{\"dsr\":[18,22,0,0]}'>*baz</p>");

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: I6244bdfe5b9a36f5b28357423d2ade1bc6c9b75f
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