Subramanya Sastry has uploaded a new change for review.

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


Change subject: WTS: Cleanup/fix emitSeparator code.
......................................................................

WTS: Cleanup/fix emitSeparator code.

* Cleaned up code, improved documentation, and fixed some
  missing cases in that code.
* Greens 1 more wt2wt test.
* Also seems to fix the following snippets which are both
  variations of the same scenario (TODO: Add to parser tests)
  - <span> <s>x</span> </s>
  - ''' ''x''' ''

Change-Id: I24c5bce2dc9505ee9ff6f9e38836d63ca228b195
---
M js/lib/mediawiki.WikitextSerializer.js
1 file changed, 66 insertions(+), 57 deletions(-)


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

diff --git a/js/lib/mediawiki.WikitextSerializer.js 
b/js/lib/mediawiki.WikitextSerializer.js
index 19a46df..21e4c83 100644
--- a/js/lib/mediawiki.WikitextSerializer.js
+++ b/js/lib/mediawiki.WikitextSerializer.js
@@ -2574,81 +2574,89 @@
                src = state.env.page.src,
                serializeInfo = state.selser.serializeInfo,
                constraints = state.sep.constraints,
-               prevNode = state.sep.lastSourceNode;
+               prevNode = state.sep.lastSourceNode,
+               dsrA, dsrB;
 
        if (src && !state.selser.serializeInfo && node && prevNode) {
-               // FIXME: Maybe we shouldn't set dsr in the dsr pass if both 
aren't valid?
-               //
-               // When we are in the lastChild sep scenario and the parent 
doesn't have
-               // useable dsr, if possible, walk up the ancestor nodes till we 
find a dsr-bearing node
-               if (prevNode.parentNode === node) {
-                       while (!node.nextSibling && node.nodeName !== 'BODY' &&
-                               (!node.data ||
-                               !node.data.parsoid.dsr ||
-                               node.data.parsoid.dsr[0] === null ||
-                               node.data.parsoid.dsr[1] === null))
-                       {
-                               node = node.parentNode;
-                       }
-               }
-
-               if (node && node.nodeType === node.TEXT_NODE) {
-                       // Check if this is the first child of a zero-width 
element, and use
-                       // that for dsr purposes instead. Typical case: text in 
p.
-                       if (!node.previousSibling &&
-                                       node.parentNode &&
-                                       node.parentNode !== prevNode &&
-                                       node.parentNode.data.parsoid.dsr &&
-                                       node.parentNode.data.parsoid.dsr[2] === 
0)
-                       {
-                               node = node.parentNode;
-                       }
-               }
-
-               if (prevNode && prevNode.nodeType === prevNode.TEXT_NODE) {
+               if (prevNode && !DU.isElt(prevNode)) {
                        // Check if this is the last child of a zero-width 
element, and use
                        // that for dsr purposes instead. Typical case: text in 
p.
                        if (!prevNode.nextSibling &&
-                                       prevNode.parentNode &&
-                                       prevNode.parentNode !== node &&
-                                       prevNode.parentNode.data.parsoid.dsr &&
-                                       prevNode.parentNode.data.parsoid.dsr[3] 
=== 0)
+                               prevNode.parentNode &&
+                               prevNode.parentNode !== node &&
+                               prevNode.parentNode.data.parsoid.dsr &&
+                               prevNode.parentNode.data.parsoid.dsr[3] === 0)
                        {
-                               prevNode = prevNode.parentNode;
+                               dsrA = prevNode.parentNode.data.parsoid.dsr;
                        } else if (prevNode.previousSibling &&
                                        prevNode.previousSibling.nodeType === 
prevNode.ELEMENT_NODE &&
                                        
prevNode.previousSibling.data.parsoid.dsr)
                        {
-                               //console.log('faking prevNode');
-                               var prevSiblingEndDSR = 
prevNode.previousSibling.data.parsoid.dsr[1];
-                               // create fake node
-                               prevNode = {
-                                       nodeName: '#fakeelement',
-                                       nodeType: prevNode.ELEMENT_NODE,
-                                       data: {
-                                               parsoid: {
-                                                       dsr: [
-                                                               
prevSiblingEndDSR,
-                                                               
prevSiblingEndDSR + prevNode.nodeValue.length + 
DU.indentPreDSRCorrection(prevNode),
-                                                               0, 0
-                                                       ]
-                                               }
-                                       }
-                               };
+                               var endDsr = 
prevNode.previousSibling.data.parsoid.dsr[1],
+                                       correction;
+                               if (prevNode.nodeType === 
prevNode.COMMENT_NODE) {
+                                       correction = prevNode.nodeValue.length 
+ 7;
+                               } else {
+                                       correction = prevNode.nodeValue.length;
+                               }
+                               dsrA = [endDsr, endDsr + correction + 
DU.indentPreDSRCorrection(prevNode), 0, 0];
                        } else {
                                //console.log( prevNode.nodeValue, 
prevNode.parentNode.outerHTML);
                        }
+               } else if (prevNode.data && prevNode.data.parsoid) {
+                       dsrA = prevNode.data.parsoid.dsr;
                }
 
-               if (DU.isElt(node) && DU.isElt(prevNode) &&
-                       !DU.isListOrListElt(node) &&
-                       node.data && node.data.parsoid.dsr &&
-                       prevNode.data && prevNode.data.parsoid.dsr)
+               if (node && !DU.isElt(node)) {
+                       // Check if this is the first child of a zero-width 
element,
+                       // and use that for dsr purposes instead.
+                       // Examples:
+                       // 1. text in p.
+                       // 2. ws-only child of a node with auto-inserted start 
tag
+                       //    Ex: <span> <s>x</span> </s> --> <span> 
<s>x</s*></span><s*> </s>
+                       if (!node.previousSibling &&
+                               node.parentNode &&
+                               node.parentNode !== prevNode &&
+                               node.parentNode.data.parsoid.dsr &&
+                               node.parentNode.data.parsoid.dsr[2] === 0)
+                       {
+                               dsrB = node.parentNode.data.parsoid.dsr;
+                               if (node.nodeType === node.TEXT_NODE && 
node.nodeValue.match(/^\s*$/)) {
+                                       dsrB = Util.clone(dsrB);
+                                       dsrB[0] += node.nodeValue.length;
+                               }
+                       }
+               } else {
+                       if (prevNode.parentNode === node) {
+                               // FIXME: Maybe we shouldn't set dsr in the dsr 
pass if both aren't valid?
+                               //
+                               // When we are in the lastChild sep scenario 
and the parent doesn't have
+                               // useable dsr, if possible, walk up the 
ancestor nodes till we find
+                               // a dsr-bearing node
+                               //
+                               // This fix is needed to handle trailing 
newlines in this wikitext:
+                               // 
[[File:foo.jpg|thumb|300px|foo\n{{echo|A}}\n{{echo|B}}\n{{echo|C}}\n\n]]
+                               while (!node.nextSibling && node.nodeName !== 
'BODY' &&
+                                       (!node.data ||
+                                       !node.data.parsoid.dsr ||
+                                       node.data.parsoid.dsr[0] === null ||
+                                       node.data.parsoid.dsr[1] === null))
+                               {
+                                       node = node.parentNode;
+                               }
+                       }
+
+                       if (node.data && node.data.parsoid) {
+                               dsrB = node.data.parsoid.dsr;
+                       }
+               }
+
+               // FIXME: Maybe we shouldn't set dsr in the dsr pass if both 
aren't valid?
+               if (dsrA && dsrA[0] !== null && dsrA[1] !== null &&
+                       dsrB && dsrB[0] !== null && dsrB[1] !== null)
                {
                        //console.log(prevNode.data.parsoid.dsr, 
node.data.parsoid.dsr);
                        // Figure out containment relationship
-                       var dsrA = prevNode.data.parsoid.dsr,
-                               dsrB = node.data.parsoid.dsr;
                        if (dsrA[0] <= dsrB[0]) {
                                if (dsrB[1] <= dsrA[1]) {
                                        if (dsrA[0] === dsrB[0] && dsrA[1] === 
dsrB[1]) {
@@ -2674,6 +2682,7 @@
                        } else {
                                console.error('dsr backwards: should not 
happen!');
                        }
+
                        if (state.sep.lastSourceSep) {
                                //console.log('lastSourceSep', 
state.sep.lastSourceSep);
                                sep = state.sep.lastSourceSep + sep;

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: I24c5bce2dc9505ee9ff6f9e38836d63ca228b195
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/Parsoid
Gerrit-Branch: master
Gerrit-Owner: Subramanya Sastry <ssas...@wikimedia.org>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to