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

Change subject: Also keep style attributes when flattening elements
......................................................................


Also keep style attributes when flattening elements

See comment https://phabricator.wikimedia.org/T177007#3899976.

Bug: T177007
Change-Id: I3703875bcf1cc924c0ec9bb5a4aa165efbb35d0d
---
M lib/transformations/flattenElements.js
M test/lib/transformations/flattenElements.test.js
2 files changed, 31 insertions(+), 8 deletions(-)

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



diff --git a/lib/transformations/flattenElements.js 
b/lib/transformations/flattenElements.js
index f3d917f..0ee39fc 100644
--- a/lib/transformations/flattenElements.js
+++ b/lib/transformations/flattenElements.js
@@ -1,24 +1,41 @@
 'use strict';
 
-const KEEP_ATTRIBUTE = 'class';
+const KEEP_ATTRIBUTES = [ 'class', 'style' ];
+
+/**
+ * @param {!Element} element the element to copy attributes from
+ * @param {!Array<String>} nameArray an array of attribute names
+ * @return {boolean} true if at least one of the attribute names exist
+ *         on the element false otherwise.
+ */
+function hasAttribute(element, nameArray) {
+    for (let i = 0; i < nameArray.length; i++) {
+        if (element.getAttribute(nameArray[i])) {
+            return true;
+        }
+    }
+    return false;
+}
 
 /**
  * Copies only select attributes from one DOM element to another.
  * @param {!Element} oldElement the element to copy attributes from
  * @param {!Element} newElement the element to copy attributes to
- * @param {!String} name the name of the attribute
+ * @param {!Array<String>} nameArray an array of attribute names
  */
-function copyAttribute(oldElement, newElement, name) {
-    if (oldElement.getAttribute(name)) {
-        newElement.setAttribute(name, oldElement.getAttribute(name));
-    }
+function copyAttributes(oldElement, newElement, nameArray) {
+    nameArray.forEach((name) => {
+        if (oldElement.getAttribute(name)) {
+            newElement.setAttribute(name, oldElement.getAttribute(name));
+        }
+    });
 }
 
 function createReplacementNode(oldElement, document) {
-    if (oldElement.getAttribute(KEEP_ATTRIBUTE)) {
+    if (hasAttribute(oldElement, KEEP_ATTRIBUTES)) {
         const spanElement = document.createElement('span');
         spanElement.innerHTML = oldElement.innerHTML;
-        copyAttribute(oldElement, spanElement, KEEP_ATTRIBUTE);
+        copyAttributes(oldElement, spanElement, KEEP_ATTRIBUTES);
         return spanElement;
     } else {
         return document.createTextNode(oldElement.innerHTML);
diff --git a/test/lib/transformations/flattenElements.test.js 
b/test/lib/transformations/flattenElements.test.js
index 4bd1726..3bc1c6c 100644
--- a/test/lib/transformations/flattenElements.test.js
+++ b/test/lib/transformations/flattenElements.test.js
@@ -11,6 +11,12 @@
         assert.deepEqual(document.body.innerHTML, '<span 
class="bar">foo</span>');
     });
 
+    it('replaces a with span, keeps style attribute', () => {
+        const document = domino.createDocument('<a style="bar" 
href="#">foo</a>');
+        flattenElements(document, 'a');
+        assert.deepEqual(document.body.innerHTML, '<span 
style="bar">foo</span>');
+    });
+
     it('replaces a tag with plain text if no attributes to keep', () => {
         const document = domino.createDocument('<a href="#">foo</a>');
         flattenElements(document, 'a');

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

Gerrit-MessageType: merged
Gerrit-Change-Id: I3703875bcf1cc924c0ec9bb5a4aa165efbb35d0d
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/services/mobileapps
Gerrit-Branch: master
Gerrit-Owner: BearND <[email protected]>
Gerrit-Reviewer: Fjalapeno <[email protected]>
Gerrit-Reviewer: Jdlrobson <[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