jenkins-bot has submitted this change and it was merged. ( 
https://gerrit.wikimedia.org/r/404609 )

Change subject: Do not completely flatten links with child nodes
......................................................................


Do not completely flatten links with child nodes

Only flatten an anchor link to text content.

Bug: T185050
Change-Id: I05edcf02163289e2e735aff54743cd51cb7da42b
---
M lib/transformations/flattenElements.js
M test/lib/transformations/flattenElements.test.js
2 files changed, 14 insertions(+), 2 deletions(-)

Approvals:
  BearND: Looks good to me, but someone else must approve
  jenkins-bot: Verified
  Mholloway: Looks good to me, approved



diff --git a/lib/transformations/flattenElements.js 
b/lib/transformations/flattenElements.js
index 0ee39fc..8153bad 100644
--- a/lib/transformations/flattenElements.js
+++ b/lib/transformations/flattenElements.js
@@ -32,13 +32,13 @@
 }
 
 function createReplacementNode(oldElement, document) {
-    if (hasAttribute(oldElement, KEEP_ATTRIBUTES)) {
+    if (hasAttribute(oldElement, KEEP_ATTRIBUTES) || 
oldElement.querySelectorAll('*').length) {
         const spanElement = document.createElement('span');
         spanElement.innerHTML = oldElement.innerHTML;
         copyAttributes(oldElement, spanElement, KEEP_ATTRIBUTES);
         return spanElement;
     } else {
-        return document.createTextNode(oldElement.innerHTML);
+        return document.createTextNode(oldElement.textContent);
     }
 }
 
diff --git a/test/lib/transformations/flattenElements.test.js 
b/test/lib/transformations/flattenElements.test.js
index 3bc1c6c..afbd6ab 100644
--- a/test/lib/transformations/flattenElements.test.js
+++ b/test/lib/transformations/flattenElements.test.js
@@ -22,4 +22,16 @@
         flattenElements(document, 'a');
         assert.deepEqual(document.body.innerHTML, 'foo');
     });
+
+    it('retains HTML inside elements', () => {
+        const document = domino.createDocument('<a><i>The Mummy</i> 
franchise</a>');
+        flattenElements(document, 'a');
+        assert.deepEqual(document.body.innerHTML, '<span><i>The Mummy</i> 
franchise</span>');
+    });
+
+    it('does not change the text content of the node', () => {
+        const document = domino.createDocument('<a>&lt;uh oh&gt;</a>');
+        flattenElements(document, 'a');
+        assert.deepEqual(document.body.innerHTML, '&lt;uh oh&gt;');
+    });
 });

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

Gerrit-MessageType: merged
Gerrit-Change-Id: I05edcf02163289e2e735aff54743cd51cb7da42b
Gerrit-PatchSet: 2
Gerrit-Project: mediawiki/services/mobileapps
Gerrit-Branch: master
Gerrit-Owner: Jdlrobson <[email protected]>
Gerrit-Reviewer: BearND <[email protected]>
Gerrit-Reviewer: Fjalapeno <[email protected]>
Gerrit-Reviewer: Mholloway <[email protected]>
Gerrit-Reviewer: Mhurd <[email protected]>
Gerrit-Reviewer: Ppchelko <[email protected]>
Gerrit-Reviewer: jenkins-bot <>

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

Reply via email to