Subramanya Sastry has uploaded a new change for review.

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

Change subject: Cleanup attribute expansion helpers + some style cleanup
......................................................................

Cleanup attribute expansion helpers + some style cleanup

* Cleaned up expandValuesToDOM helper to accept a wrapTemplates
  flag to handle scenarios where the expansion might be happening
  in the context of a nested template (See T93368. This can also
  happen when we are using native Parsoid template expansion code.)

* Remove direct use of processTokensToDOM code in LinkHandler where
  an attribute value was being expanded as well.

* Some minimal style cleanup.

Change-Id: If7e9b3e2ca7b84a5bf6e18433ec2011f4bc29a94
---
M lib/ext.core.AttributeExpander.js
M lib/ext.core.LinkHandler.js
M lib/mediawiki.Util.js
3 files changed, 47 insertions(+), 39 deletions(-)


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

diff --git a/lib/ext.core.AttributeExpander.js 
b/lib/ext.core.AttributeExpander.js
index 2693197..f8fb63e 100644
--- a/lib/ext.core.AttributeExpander.js
+++ b/lib/ext.core.AttributeExpander.js
@@ -206,22 +206,23 @@
  */
 AttributeExpander.prototype._returnAttributes = function( token, cb, 
expandedAttrs ) {
        var env = this.manager.env;
+       var wrapTemplates = this.options.wrapTemplates;
 
        env.dp( 'AttributeExpander._returnAttributes: ', expandedAttrs );
 
-       var modified = false,
-               metaTokens = [],
-               postNLToks = [],
-               skipRank = this.skipRank,
-               tmpDataMW,
-               oldAttrs = token.attribs,
+       var modified = false;
+       var metaTokens = [];
+       var postNLToks = [];
+       var skipRank = this.skipRank;
+       var tmpDataMW;
+       var oldAttrs = token.attribs;
                // Build newAttrs lazily (on-demand) to avoid creating
                // objects in the common case where nothing of significance
                // happens in this code.
-               newAttrs = null,
-               nlTkPos = -1,
-               dp, i, l,
-               nlTkOkay = !token.isHTMLTag() && !Util.isTableTag(token);
+       var newAttrs = null;
+       var nlTkPos = -1;
+       var dp, i, l;
+       var nlTkOkay = !token.isHTMLTag() && !Util.isTableTag(token);
 
        // Identify attributes that were generated in full or in part using 
templates
        for (i = 0, l = oldAttrs.length; i < l; i++) {
@@ -296,16 +297,16 @@
                        // and probably use appropriately-named flags to convey 
type information.
                        if (Array.isArray(oldA.k)) {
                                if (!(expandedK.constructor === String && 
/(^|\s)mw:maybeContent(\s|$)/.test(expandedK))) {
-                                       nlTkPos = nlTkIndex(nlTkOkay, 
expandedK, this.options.wrapTemplates);
+                                       nlTkPos = nlTkIndex(nlTkOkay, 
expandedK, wrapTemplates);
                                        if (nlTkPos !== -1) {
                                                // Scenario 1 from the 
documentation comment above.
-                                               updatedK = splitTokens(env, 
token, nlTkPos, expandedK, this.options.wrapTemplates);
+                                               updatedK = splitTokens(env, 
token, nlTkPos, expandedK, wrapTemplates);
                                                expandedK = updatedK.preNLBuf;
                                                postNLToks = updatedK.postNLBuf;
                                                metaTokens = 
updatedK.metaTokens;
                                        } else {
                                                // Scenario 2 from the 
documentation comment above.
-                                               updatedK = 
stripMetaTags(expandedK, this.options.wrapTemplates);
+                                               updatedK = 
stripMetaTags(expandedK, wrapTemplates);
                                                expandedK = updatedK.value;
                                        }
 
@@ -349,16 +350,16 @@
                        var attrValTokens = origV;
                        if ( expandedK.constructor === String && 
Array.isArray(oldA.v) ) {
                                if (!expandedK.match(/^mw:/)) {
-                                       nlTkPos = nlTkIndex(nlTkOkay, 
attrValTokens, this.options.wrapTemplates);
+                                       nlTkPos = nlTkIndex(nlTkOkay, 
attrValTokens, wrapTemplates);
                                        if (nlTkPos !== -1) {
                                                // Scenario 1 from the 
documentation comment above.
-                                               updatedV = splitTokens(env, 
token, nlTkPos, attrValTokens, this.options.wrapTemplates);
+                                               updatedV = splitTokens(env, 
token, nlTkPos, attrValTokens, wrapTemplates);
                                                attrValTokens = 
updatedV.preNLBuf;
                                                postNLToks = updatedV.postNLBuf;
                                                metaTokens = 
updatedV.metaTokens;
                                        } else {
                                                // Scenario 2 from the 
documentation comment above.
-                                               updatedV = 
stripMetaTags(attrValTokens, this.options.wrapTemplates);
+                                               updatedV = 
stripMetaTags(attrValTokens, wrapTemplates);
                                                attrValTokens = updatedV.value;
                                        }
                                        expandedA.v = attrValTokens;
@@ -423,7 +424,7 @@
                        var manager = this.manager;
 
                        // Async-expand all token arrays to DOM.
-                       Util.expandValuesToDOM(manager.env, manager.frame, 
vals, function(err, eVals) {
+                       Util.expandValuesToDOM(manager.env, manager.frame, 
vals, wrapTemplates, function(err, eVals) {
                                // Rebuild flattened k-v pairs.
                                var expandedAttrs = [];
                                for (var i = 0; i < eVals.length; i += 2) {
diff --git a/lib/ext.core.LinkHandler.js b/lib/ext.core.LinkHandler.js
index b1d82ae..f2bb595 100644
--- a/lib/ext.core.LinkHandler.js
+++ b/lib/ext.core.LinkHandler.js
@@ -27,6 +27,7 @@
 
 function WikiLinkHandler( manager, options ) {
        this.manager = manager;
+       this.options = options;
        // Handle redirects first as they can emit additonal link tokens
        this.manager.addTransform( this.onRedirect.bind( this ), 
"WikiLinkHandler:onRedirect", this.rank, 'tag', 'mw:redirect' );
        // Now handle regular wikilinks.
@@ -473,14 +474,15 @@
        } else {
                // Deal with sort keys that come from generated content 
(transclusions, etc.)
                cb( { async: true } );
-               Util.processTokensToDOM(
+               var inVals = [ { "txt": "mw:sortKey" }, { "html": content } ];
+               Util.expandValuesToDOM(
                        this.manager.env,
                        this.manager.frame,
-                       content,
-                       null /* default options */,
-                       function(_, dom) {
-                               var sortKeyInfo = [ {"txt": "mw:sortKey"}, 
{"html": dom.body.innerHTML} ],
-                                       dataMW = newTk.getAttribute("data-mw");
+                       inVals,
+                       this.options.wrapTemplates,
+                       function(_, outVals) {
+                               var sortKeyInfo = outVals;
+                               var dataMW = newTk.getAttribute("data-mw");
                                if (dataMW) {
                                        dataMW = JSON.parse(dataMW);
                                        dataMW.attribs.push(sortKeyInfo);
@@ -493,7 +495,7 @@
                                newTk.addSpaceSeparatedAttribute("typeof", 
"mw:ExpandedAttrs");
                                newTk.addAttribute("data-mw", 
JSON.stringify(dataMW));
 
-                               cb( { tokens: tokens } );
+                               cb({ tokens: tokens });
                        }
                );
        }
@@ -1135,7 +1137,8 @@
        } else if (optSources) {
                cb({ async: true });
                var manager = this.manager;
-               Util.expandValuesToDOM(manager.env, manager.frame, 
optSources.map(function(e) { return e[1]; }), function(err, vals) {
+               var inVals = optSources.map(function(e) { return e[1]; });
+               Util.expandValuesToDOM(manager.env, manager.frame, inVals, 
this.options.wrapTemplates, function(err, outVals) {
                        if (!dataMWObj) {
                                dataMWObj = dataMW ? JSON.parse(dataMW) : {};
                        }
@@ -1144,8 +1147,8 @@
                                dataMWObj.attribs = [];
                        }
 
-                       for (var i = 0; i < vals.length; i++) {
-                               
dataMWObj.attribs.push([optSources[i][0].optKey, vals[i]]);
+                       for (var i = 0; i < outVals.length; i++) {
+                               
dataMWObj.attribs.push([optSources[i][0].optKey, outVals[i]]);
                        }
                        container.addAttribute("about", 
manager.env.newAboutId());
                        container.addSpaceSeparatedAttribute("typeof", 
"mw:ExpandedAttrs");
diff --git a/lib/mediawiki.Util.js b/lib/mediawiki.Util.js
index fa865ef..f7f1dd7 100644
--- a/lib/mediawiki.Util.js
+++ b/lib/mediawiki.Util.js
@@ -1129,21 +1129,12 @@
         */
        processTokensToDOM: function(env, frame, tokens, opts, cb) {
                cb = JSUtils.mkPromised(cb);
-               var pipelineOpts = opts && opts.pipelineOpts;
-               if (!pipelineOpts) {
-                       pipelineOpts = {
-                               attrExpansion: true,
-                               noPWrapping: true,
-                               noPre: true,
-                               wrapTemplates: true
-                       };
-               }
                this.processContentInPipeline(
                        env,
                        frame,
                        tokens.concat([new pd.EOFTk()]), {
                                pipelineType: "tokens/x-mediawiki/expanded",
-                               pipelineOpts: pipelineOpts,
+                               pipelineOpts: opts.pipelineOpts,
                                srcOffsets: opts ? opts.srcOffsets : undefined,
                                // processContentInPipeline has no error 
callback :(
                                documentCB: function(dom) { cb(null, dom); }
@@ -1169,22 +1160,35 @@
         *    The html property is expanded to DOM only if it is an array (of 
tokens).
         *    Non-arrays are passed back unexpanded.
         *
+        * @param {boolean} wrapTemplates
+        *    Should any templates encountered here be marked up?
+        *    (usually false for nested templates since they are never directly 
editable).
+        *
         * @param {Function} finalCB
         *    The callback to pass the expanded values into.
         *
         * FIXME: This could be generified a bit more.
         */
-       expandValuesToDOM: function(env, frame, vals, finalCB) {
+       expandValuesToDOM: function(env, frame, vals, wrapTemplates, finalCB) {
                var self = this;
                async.map(
                        vals,
                        function(v, cb) {
                                if (Array.isArray(v.html)) {
+                                       // Set up pipeline options
+                                       var opts = {
+                                               pipelineOpts: {
+                                                       attrExpansion: true,
+                                                       noPWrapping: true,
+                                                       noPre: true,
+                                                       wrapTemplates: 
wrapTemplates
+                                               }
+                                       };
                                        self.processTokensToDOM(
                                                env,
                                                frame,
                                                v.html,
-                                               null /* default options */,
+                                               opts,
                                                function(err, dom) {
                                                        if (!err) { v.html = 
dom.body.innerHTML; }
                                                        cb(err, v);

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

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