jenkins-bot has submitted this change and it was merged.

Change subject: Refine wikitext escaping for template parameters
......................................................................


Refine wikitext escaping for template parameters

Take into account the position of the string token within the parameter token
array, and escape generic [{}] less aggressively in the middle of a parameter.

Added a parser test to ensure we don't escape too agressively.

Fixes
node roundtrip-test.js --prefix zh 'HUNTER×HUNTER角色列表'
echo '{{echo|{{echo| }}[bar]}}' | node parse --wt2wt

Change-Id: If1d1775225647e17db93c460144af2650d801648
---
M js/lib/mediawiki.WikitextSerializer.js
M js/tests/parserTests.txt
2 files changed, 30 insertions(+), 18 deletions(-)

Approvals:
  Subramanya Sastry: Looks good to me, approved
  jenkins-bot: Verified



diff --git a/js/lib/mediawiki.WikitextSerializer.js 
b/js/lib/mediawiki.WikitextSerializer.js
index 4511da9..410b361 100644
--- a/js/lib/mediawiki.WikitextSerializer.js
+++ b/js/lib/mediawiki.WikitextSerializer.js
@@ -767,24 +767,21 @@
  *    the entire DOM range they span in the end.
  */
 WSP.escapeTplArgWT = function(state, arg, isPositional) {
-       function escapeStr(str, buf) {
-               if (isPositional) {
-                       // Can't allow '=' in positional parameters
-                       if (str.match(/[\{\[\]\}\|\=]/)) {
-                               buf.push("<nowiki>");
-                               buf.push(str);
-                               buf.push("</nowiki>");
-                       } else {
-                               buf.push(str);
-                       }
+       function escapeStr(str, buf, pos) {
+               var bracketPairStrippedStr = str.replace(/\[([^\[\]]*)\]/g, 
'_$1_'),
+                       genericMatch =
+                       pos.start && /^{/.test(str) ||
+                       pos.end && /}$/.test(str) ||
+                       /{{|}}|[\[\]\|]/.test(bracketPairStrippedStr);
+               if (genericMatch ||
+                               // Can't allow '=' in positional parameters
+                               (isPositional && /[=]/.test(str)))
+               {
+                       buf.push("<nowiki>");
+                       buf.push(str);
+                       buf.push("</nowiki>");
                } else {
-                       if (str.match(/[\{\[\]\}\|]/)) {
-                               buf.push("<nowiki>");
-                               buf.push(str);
-                               buf.push("</nowiki>");
-                       } else {
-                               buf.push(str);
-                       }
+                       buf.push(str);
                }
        }
 
@@ -820,7 +817,11 @@
                                break;
 
                        case String:
-                               escapeStr(t, buf);
+                               var pos = {
+                                       atStart: i === 0,
+                                       atEnd: i === tokens.length - 1
+                               };
+                               escapeStr(t, buf, pos);
                                break;
 
                        case pd.EOFTk:
diff --git a/js/tests/parserTests.txt b/js/tests/parserTests.txt
index 2f0c471..2492e74 100644
--- a/js/tests/parserTests.txt
+++ b/js/tests/parserTests.txt
@@ -1515,6 +1515,17 @@
 
 !! end
 
+!! test
+Templates: Parsoid parameter escaping test 1
+!! options
+parsoid
+!! input
+{{echo|[foo]|{{echo|[bar]}}}}
+!! result
+<p about="#mwt1" typeof="mw:Transclusion"
+data-mw="{&quot;target&quot;:{&quot;wt&quot;:&quot;echo&quot;,&quot;href&quot;:&quot;./Template:Echo&quot;},&quot;params&quot;:{&quot;1&quot;:{&quot;wt&quot;:&quot;[foo]&quot;},&quot;2&quot;:{&quot;wt&quot;:&quot;{{echo|[bar]}}&quot;}},&quot;i&quot;:0}">[foo]</p>
+!! end
+
 ###
 ### Parsoid-centric tests for testing RT edge cases for pre
 ###

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

Gerrit-MessageType: merged
Gerrit-Change-Id: If1d1775225647e17db93c460144af2650d801648
Gerrit-PatchSet: 3
Gerrit-Project: mediawiki/extensions/Parsoid
Gerrit-Branch: master
Gerrit-Owner: GWicke <[email protected]>
Gerrit-Reviewer: GWicke <[email protected]>
Gerrit-Reviewer: Subramanya Sastry <[email protected]>
Gerrit-Reviewer: jenkins-bot

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

Reply via email to