BearND has uploaded a new change for review. ( 
https://gerrit.wikimedia.org/r/349375 )

Change subject: Flatten links inside section titles anchors
......................................................................

Flatten links inside section titles anchors

Anchor names of section titles don't have any HTML in MediaWiki.

Change-Id: Ib774f286e47e6b0a05272fd9cb56dbb8ab00efce
---
M lib/parsoid-access.js
M 
test/diff/results/page_formatted-enwiki-User%3ABSitzmann_(WMF)_MCS_Test_Frankenstein.json
M 
test/diff/results/page_mobile-sections-enwiki-User%3ABSitzmann_(WMF)_MCS_Test_Frankenstein.json
M test/lib/parsoid/parsoid-access-test.js
4 files changed, 41 insertions(+), 7 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/services/mobileapps 
refs/changes/75/349375/1

diff --git a/lib/parsoid-access.js b/lib/parsoid-access.js
index 9ef3f61..b1c790a 100644
--- a/lib/parsoid-access.js
+++ b/lib/parsoid-access.js
@@ -80,6 +80,23 @@
 }
 
 /**
+ * Replaces links inside a string with their inner HTML
+ * @param {!string} input string that may contain links
+ * @return {!string} string without links
+ */
+function flattenLinks(input) {
+    if (/<a /.test(input)) {
+        // only incur the cost of creating a Document if there is a good 
chance of links in the
+        // forecast
+        const doc = domino.createDocument(input);
+        // TODO: check why domino.createDocumentFragment(input) does not work
+        // https://github.com/fgnass/domino/issues/73 seems to indicate it 
should
+        return doc.querySelector('body').textContent;
+    }
+    return input;
+}
+
+/**
  * @param {!document} doc the parsed DOM Document of the Parsoid output
  * @return {!sections[]} an array of section JSON elements
  */
@@ -98,7 +115,7 @@
             const className = currentSectionDiv.className;
             currentSection.toclevel = 
parseInt(className.substring('toclevel_'.length), 10);
             currentSection.line = currentSectionDiv.title;
-            currentSection.anchor = a.anchorencode(currentSection.line);
+            currentSection.anchor = 
a.anchorencode(flattenLinks(currentSection.line));
         }
 
         sections.push(currentSection);
@@ -233,6 +250,7 @@
     getRevisionFromEtag,
 
     // VisibleForTesting
+    _flattenLinks: flattenLinks,
     _addSectionDivs: addSectionDivs,
     _getSectionsText: getSectionsText,
     _getBaseUri: getBaseUri,
diff --git 
"a/test/diff/results/page_formatted-enwiki-User%3ABSitzmann_\050WMF\051_MCS_Test_Frankenstein.json"
 
"b/test/diff/results/page_formatted-enwiki-User%3ABSitzmann_\050WMF\051_MCS_Test_Frankenstein.json"
index 1883aa9..68e468c 100644
--- 
"a/test/diff/results/page_formatted-enwiki-User%3ABSitzmann_\050WMF\051_MCS_Test_Frankenstein.json"
+++ 
"b/test/diff/results/page_formatted-enwiki-User%3ABSitzmann_\050WMF\051_MCS_Test_Frankenstein.json"
@@ -88,14 +88,14 @@
         "text": "\n<div class=\"hatnote\">\"WP:TPA\" redirects here. For talk 
page archiving, see <a href=\"/wiki/Help:Archiving_a_talk_page\" 
title=\"Help:Archiving a talk page\">Help:Archiving a talk 
page</a>.</div>\n<p><big id=\"mwWg\"><b>A perfect Wikipedia 
article...</b></big></p>\n<ul><li id=\"mwXQ\"><b>Is on a <a 
href=\"/wiki/Wikipedia:Note\" title=\"Wikipedia:Note\">notable</a> 
topic.</b>\n<ul><li id=\"mwYQ\"><b>Fills a gap</b> not provided by existing or 
related articles.</li></ul></li>\n<li id=\"mwYw\"><b>Has an appropriate 
structure.</b></li></ul>\n\n",
         "toclevel": 1,
         "line": "From <a href=\"/wiki/Wikipedia:The_perfect_article\" 
title=\"Wikipedia:The perfect article\">Wikipedia:The perfect article</a>",
-        "anchor": 
"From_.3Ca_href.3D.22.2Fwiki.2FWikipedia:The_perfect_article.22_title.3D.22Wikipedia:The_perfect_article.22.3EWikipedia:The_perfect_article.3C.2Fa.3E"
+        "anchor": "From_Wikipedia:The_perfect_article"
       },
       {
         "id": 11,
         "text": "\n\n",
         "toclevel": 1,
         "line": "From <a href=\"/wiki/Wikipedia:Manual_of_Style/Layout\" 
title=\"Wikipedia:Manual of 
Style/Layout\">Wikipedia:Manual_of_Style/Layout</a>",
-        "anchor": 
"From_.3Ca_href.3D.22.2Fwiki.2FWikipedia:Manual_of_Style.2FLayout.22_title.3D.22Wikipedia:Manual_of_Style.2FLayout.22.3EWikipedia:Manual_of_Style.2FLayout.3C.2Fa.3E"
+        "anchor": "From_Wikipedia:Manual_of_Style.2FLayout"
       },
       {
         "id": 12,
diff --git 
"a/test/diff/results/page_mobile-sections-enwiki-User%3ABSitzmann_\050WMF\051_MCS_Test_Frankenstein.json"
 
"b/test/diff/results/page_mobile-sections-enwiki-User%3ABSitzmann_\050WMF\051_MCS_Test_Frankenstein.json"
index ad0f691..4155ed7 100644
--- 
"a/test/diff/results/page_mobile-sections-enwiki-User%3ABSitzmann_\050WMF\051_MCS_Test_Frankenstein.json"
+++ 
"b/test/diff/results/page_mobile-sections-enwiki-User%3ABSitzmann_\050WMF\051_MCS_Test_Frankenstein.json"
@@ -77,13 +77,13 @@
       {
         "id": 10,
         "toclevel": 1,
-        "anchor": 
"From_.3Ca_href.3D.22.2Fwiki.2FWikipedia:The_perfect_article.22_title.3D.22Wikipedia:The_perfect_article.22.3EWikipedia:The_perfect_article.3C.2Fa.3E",
+        "anchor": "From_Wikipedia:The_perfect_article",
         "line": "From <a href=\"/wiki/Wikipedia:The_perfect_article\" 
title=\"Wikipedia:The perfect article\">Wikipedia:The perfect article</a>"
       },
       {
         "id": 11,
         "toclevel": 1,
-        "anchor": 
"From_.3Ca_href.3D.22.2Fwiki.2FWikipedia:Manual_of_Style.2FLayout.22_title.3D.22Wikipedia:Manual_of_Style.2FLayout.22.3EWikipedia:Manual_of_Style.2FLayout.3C.2Fa.3E",
+        "anchor": "From_Wikipedia:Manual_of_Style.2FLayout",
         "line": "From <a href=\"/wiki/Wikipedia:Manual_of_Style/Layout\" 
title=\"Wikipedia:Manual of Style/Layout\">Wikipedia:Manual_of_Style/Layout</a>"
       },
       {
@@ -230,14 +230,14 @@
         "text": "\n<div class=\"hatnote\">\"WP:TPA\" redirects here. For talk 
page archiving, see <a href=\"/wiki/Help:Archiving_a_talk_page\" 
title=\"Help:Archiving a talk page\">Help:Archiving a talk 
page</a>.</div>\n<p><big id=\"mwWg\"><b>A perfect Wikipedia 
article...</b></big></p>\n<ul><li id=\"mwXQ\"><b>Is on a <a 
href=\"/wiki/Wikipedia:Note\" title=\"Wikipedia:Note\">notable</a> 
topic.</b>\n<ul><li id=\"mwYQ\"><b>Fills a gap</b> not provided by existing or 
related articles.</li></ul></li>\n<li id=\"mwYw\"><b>Has an appropriate 
structure.</b></li></ul>\n\n",
         "toclevel": 1,
         "line": "From <a href=\"/wiki/Wikipedia:The_perfect_article\" 
title=\"Wikipedia:The perfect article\">Wikipedia:The perfect article</a>",
-        "anchor": 
"From_.3Ca_href.3D.22.2Fwiki.2FWikipedia:The_perfect_article.22_title.3D.22Wikipedia:The_perfect_article.22.3EWikipedia:The_perfect_article.3C.2Fa.3E"
+        "anchor": "From_Wikipedia:The_perfect_article"
       },
       {
         "id": 11,
         "text": "\n\n",
         "toclevel": 1,
         "line": "From <a href=\"/wiki/Wikipedia:Manual_of_Style/Layout\" 
title=\"Wikipedia:Manual of 
Style/Layout\">Wikipedia:Manual_of_Style/Layout</a>",
-        "anchor": 
"From_.3Ca_href.3D.22.2Fwiki.2FWikipedia:Manual_of_Style.2FLayout.22_title.3D.22Wikipedia:Manual_of_Style.2FLayout.22.3EWikipedia:Manual_of_Style.2FLayout.3C.2Fa.3E"
+        "anchor": "From_Wikipedia:Manual_of_Style.2FLayout"
       },
       {
         "id": 12,
diff --git a/test/lib/parsoid/parsoid-access-test.js 
b/test/lib/parsoid/parsoid-access-test.js
index d20a833..da9a7ef 100644
--- a/test/lib/parsoid/parsoid-access-test.js
+++ b/test/lib/parsoid/parsoid-access-test.js
@@ -100,4 +100,20 @@
         assert.equal(parsoid._getParsoidLinkTitle(doc),
             'User:BSitzmann_(WMF)/MCS/Test/Title_with_:');
     });
+
+    it('_flattenLinks should keep input text intact if there is no link', () 
=> {
+        assert.equal(parsoid._flattenLinks(''), '');
+    });
+
+    it('_flattenLinks should remove link from input text with one link', () => 
{
+        assert.equal(parsoid._flattenLinks(
+            'From <a href="/wiki/Foo">Foo</a>'),
+            'From Foo');
+    });
+
+    it('_flattenLinks should remove links from input text with two links', () 
=> {
+        assert.equal(parsoid._flattenLinks(
+            'From <a href="/wiki/Foo">Foo</a> and <a 
href="/wiki/Bar">Bar</a>'),
+            'From Foo and Bar');
+    });
 });

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: Ib774f286e47e6b0a05272fd9cb56dbb8ab00efce
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/services/mobileapps
Gerrit-Branch: master
Gerrit-Owner: BearND <[email protected]>

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

Reply via email to