Subramanya Sastry has uploaded a new change for review.

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


Change subject: Bug fix in trivial special case in token-and-attr-collector.
......................................................................

Bug fix in trivial special case in token-and-attr-collector.

* In wikitext like this: {{echo|<math>a=b</math>}}, the tokenizer
  parses this as:

  {"type":"SelfclosingTagTk",
   "name":"template",
   "attribs":[{"k":"echo","v":""},
              {"k":[{"type":"TagTk","name":"math","attribs":[]},"a"],
               "v":["b",{"type":"EndTagTk","name":"math","attribs":[]}]}
             ]
  }

  The <math> and </math> tags end up in the attributes of the template.
  TokenAndAttrCollector attempts to merge this into a single array of
  tokens.  But, it didn't correctly handle the special/trivial case where
  the opening and closing tags ended up in the same attribute (in this
  case, the second attribute).

  Note that this bug does not do anything bad for this code snippet,
  or for other examples probably, but it does lead to duplicate content
  in the merged token during tracing, and could possibly cause problems
  on other non-html tags (I couldn't easily come up with an example
  where parsed/RT output is fixed).

  This patch fixes this.

Change-Id: I6789439cd3629aca6002ab964fad5582ae48a928
---
M js/lib/ext.util.TokenAndAttrCollector.js
1 file changed, 43 insertions(+), 32 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/Parsoid 
refs/changes/44/51044/1

diff --git a/js/lib/ext.util.TokenAndAttrCollector.js 
b/js/lib/ext.util.TokenAndAttrCollector.js
index 24410e8..6ab3ccc 100644
--- a/js/lib/ext.util.TokenAndAttrCollector.js
+++ b/js/lib/ext.util.TokenAndAttrCollector.js
@@ -234,50 +234,61 @@
 
                // Merge all attributes between openD.attrIndex and 
closeD.attrIndex
                // Tricky bits:
-               // - every attribute is a (k,v) pair, and we need to merge
+               //  every attribute is a (k,v) pair, and we need to merge
                //   both the k and v into one set of tokens and insert a "=" 
token
                //   in between.
                // - we need to handle the first/last attribute specially since 
the
                //   openD/closeD may show up in either k/v of those attrs.  
That
                //   will determine what the merged k/v value will be.
                if (openD && closeD && i < j) {
-                       var attrs = token.attribs, toks;
+                       var attrs = token.attribs,
+                               toks, mergedK, mergedV;
 
                        // console.warn("openD: " + JSON.stringify(openD));
                        // console.warn("closeD: " + JSON.stringify(closeD));
 
-                       if (openD.k === -1) {
-                               // openD didn't show up in k. Start with v
-                               toks = mergeToks([], attrs[openD.attrIndex].v);
+                       if (openD.attrIndex === closeD.attrIndex) {
+                               // Special Case: openD and closeD showed up in 
k and v
+                               // of the same attr. In this case, openD would 
have showed up
+                               // in k and closeD in v.
+                               //
+                               // assert(openD.k !== -1);
+                               // assert(closeD.k === -1);
+                               mergedK = mergeAttr([], attrs[openD.attrIndex]);
+                               mergedV = [];
                        } else {
-                               // openD showed up in k.  Merge k & v
-                               toks = mergeAttr([], attrs[openD.attrIndex]);
-                       }
-
-                       var x = openD.attrIndex + 1;
-                       while (x < closeD.attrIndex) {
-                               // Compute toks + a.k + "=" + a.v
-                               toks = mergeAttr(toks, attrs[x]);
-                               x++;
-                       }
-
-                       // Compute merged (k,v)
-                       var mergedK, mergedV;
-                       if (openD.k === -1) {
-                               // openD didn't show up in k.
-                               // Use orig-k for the merged KV
-                               // Merge closeD's attr into toks and use it for 
v
-                               mergedK = attrs[openD.attrIndex].k;
-                               mergedV = mergeAttr(toks, 
attrs[closeD.attrIndex]);
-                       } else {
-                               // openD showed up in k.
-                               // check where closedD showed up.
-                               if (closeD.k !== -1) {
-                                       mergedK = mergeToks(toks, 
attrs[closeD.attrIndex].k);
-                                       mergedV = attrs[closeD.attrIndex].v;
+                               if (openD.k === -1) {
+                                       // openD didn't show up in k. Start 
with v
+                                       toks = mergeToks([], 
attrs[openD.attrIndex].v);
                                } else {
-                                       mergedK = mergeAttr(toks, 
attrs[closeD.attrIndex]);
-                                       mergedV = [];
+                                       // openD showed up in k.  Merge k & v
+                                       toks = mergeAttr([], 
attrs[openD.attrIndex]);
+                               }
+
+                               var x = openD.attrIndex + 1;
+                               while (x < closeD.attrIndex) {
+                                       // Compute toks + a.k + "=" + a.v
+                                       toks = mergeAttr(toks, attrs[x]);
+                                       x++;
+                               }
+
+                               // Compute merged (k,v)
+                               if (openD.k === -1) {
+                                       // openD didn't show up in k.
+                                       // Use orig-k for the merged KV
+                                       // Merge closeD's attr into toks and 
use it for v
+                                       mergedK = attrs[openD.attrIndex].k;
+                                       mergedV = mergeAttr(toks, 
attrs[closeD.attrIndex]);
+                               } else {
+                                       // openD showed up in k.
+                                       // check where closedD showed up.
+                                       if (closeD.k !== -1) {
+                                               mergedK = mergeToks(toks, 
attrs[closeD.attrIndex].k);
+                                               mergedV = 
attrs[closeD.attrIndex].v;
+                                       } else {
+                                               mergedK = mergeAttr(toks, 
attrs[closeD.attrIndex]);
+                                               mergedV = [];
+                                       }
                                }
                        }
 

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: I6789439cd3629aca6002ab964fad5582ae48a928
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/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