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

Change subject: T112043: Handle anchors without hrefs
......................................................................


T112043: Handle anchors without hrefs

Change-Id: Ia9accda1054d4ae41ec5d289a9e1ea1a77375aee
---
M lib/html2wt/LinkHandler.js
M tests/parserTests-blacklist.js
M tests/parserTests.txt
3 files changed, 54 insertions(+), 23 deletions(-)

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



diff --git a/lib/html2wt/LinkHandler.js b/lib/html2wt/LinkHandler.js
index fabe864..fa1cb3e 100644
--- a/lib/html2wt/LinkHandler.js
+++ b/lib/html2wt/LinkHandler.js
@@ -676,29 +676,46 @@
                                return false;
                        };
 
-                       var hrefStr;
+                       var isFigure = false;
                        if (isComplexLink(node.attributes)) {
-                               env.log("error/html2wt/link", "Encountered",
-                                       node.outerHTML,
+                               env.log("error/html2wt/link", "Encountered", 
node.outerHTML,
                                        "-- serializing as extlink and dropping 
<a> attributes unsupported in wikitext.");
-                               hrefStr = escapeExtLinkURL(getHref(env, node));
-                               return state.serializeLinkChildrenToString(node,
-                                               
state.serializer.wteHandlers.aHandler).then(function(str) {
-                                       state.emitChunk(new ExtLinkText('[' + 
hrefStr + ' ' + str + ']',
-                                               node, wiki, 'mw:ExtLink'), 
node);
-                               });
-                       } else if (node.querySelector('IMG') &&
-                                       node.querySelector('IMG').parentElement 
=== node) {
+                       } else {
+                               var media = node.querySelector('img');
+                               isFigure = !!(media && media.parentElement === 
node);
+                       }
+
+                       var hrefStr;
+                       if (isFigure) {
                                // this is a basic html figure: <a><img></a>
                                return state.serializer.figureHandler(node);
                        } else {
                                // href is already percent-encoded, etc., but 
it might contain
                                // spaces or other wikitext nasties.  escape 
the nasties.
                                hrefStr = escapeExtLinkURL(getHref(env, node));
-                               return state.serializeLinkChildrenToString(node,
-                                               
state.serializer.wteHandlers.aHandler).then(function(str) {
-                                       state.emitChunk(new ExtLinkText('[' + 
hrefStr + ' ' + str + ']',
-                                               node, wiki, 'mw:ExtLink'), 
node);
+                               var handler = 
state.serializer.wteHandlers.aHandler;
+                               return 
state.serializeLinkChildrenToString(node, handler)
+                               .then(function(str) {
+                                       var chunk;
+                                       if (!hrefStr) {
+                                               // Without an href, we just 
emit the string as text.
+                                               // However, to preserve targets 
for anchor links,
+                                               // serialize as a span with a 
name.
+                                               var name = 
node.getAttribute('name');
+                                               if (name) {
+                                                       var doc = 
node.ownerDocument;
+                                                       var span = 
doc.createElement('span');
+                                                       
span.setAttribute('name', name);
+                                                       
span.appendChild(doc.createTextNode(str));
+                                                       chunk = span.outerHTML;
+                                               } else {
+                                                       chunk = str;
+                                               }
+                                       } else {
+                                               chunk = new ExtLinkText('[' + 
hrefStr + ' ' + str + ']',
+                                                               node, wiki, 
'mw:ExtLink');
+                                       }
+                                       state.emitChunk(chunk, node);
                                });
                        }
                }
diff --git a/tests/parserTests-blacklist.js b/tests/parserTests-blacklist.js
index 8f2d8c0..18597f9 100644
--- a/tests/parserTests-blacklist.js
+++ b/tests/parserTests-blacklist.js
@@ -396,7 +396,7 @@
 add("html2html", "Link containing double-single-quotes '' in text embedded in 
italics (T6598 sanity check)", "<p data-parsoid='{\"dsr\":[0,84,0,0]}'><i 
data-parsoid='{\"dsr\":[0,61,2,2]}'>Some 
[/index.php?title=Link&amp;action=edit&amp;redlink=1 pretty </i>italics<i 
data-parsoid='{\"dsr\":[68,84,2,2]}'> and stuff]!</i></p>\n");
 add("html2html", "Plain link to page with question mark in title", "<p 
data-parsoid='{\"dsr\":[0,16,0,0]}'><a rel=\"mw:WikiLink\" 
href=\"./Wiki/A%3Fb\" title=\"Wiki/A?b\" 
data-parsoid='{\"stx\":\"piped\",\"a\":{\"href\":\"./Wiki/A%3Fb\"},\"sa\":{\"href\":\"wiki/A?b\"},\"dsr\":[0,16,11,2]}'>A?b</a></p>\n\n<p
 data-parsoid='{\"dsr\":[18,34,0,0]}'><a rel=\"mw:WikiLink\" 
href=\"./Wiki/A%3Fb\" title=\"Wiki/A?b\" 
data-parsoid='{\"stx\":\"piped\",\"a\":{\"href\":\"./Wiki/A%3Fb\"},\"sa\":{\"href\":\"wiki/A?b\"},\"dsr\":[18,34,11,2]}'>Baz</a></p>\n");
 add("html2html", "Self-link to section should not be bold", "<p 
data-parsoid='{\"dsr\":[0,44,0,0]}'><a rel=\"mw:WikiLink\" 
href=\"./Wiki/Main_Page#section\" title=\"Wiki/Main Page\" 
data-parsoid='{\"stx\":\"piped\",\"a\":{\"href\":\"./Wiki/Main_Page#section\"},\"sa\":{\"href\":\"wiki/Main
 Page#section\"},\"dsr\":[0,44,25,2]}'>Main Page#section</a></p>\n");
-add("html2html", "Self-link to numeric title", "<p 
data-parsoid='{\"dsr\":[0,4,0,0]}'>[ 0]</p>\n");
+add("html2html", "Self-link to numeric title", "<p 
data-parsoid='{\"dsr\":[0,1,0,0]}'>0</p>\n");
 add("html2html", "Link to numeric-equivalent title", "<p 
data-parsoid='{\"dsr\":[0,14,0,0]}'><a rel=\"mw:WikiLink\" href=\"./Wiki/00\" 
title=\"Wiki/00\" 
data-parsoid='{\"stx\":\"piped\",\"a\":{\"href\":\"./Wiki/00\"},\"sa\":{\"href\":\"wiki/00\"},\"dsr\":[0,14,10,2]}'>00</a></p>\n");
 add("html2html", "<nowiki> inside a link", "<p 
data-parsoid='{\"dsr\":[0,84,0,0]}'><span typeof=\"mw:Nowiki\" 
data-parsoid='{\"dsr\":[0,30,8,9]}'>[[Main Page]]</span> <a rel=\"mw:WikiLink\" 
href=\"./Wiki/Main_Page\" title=\"Wiki/Main Page\" 
data-parsoid='{\"stx\":\"piped\",\"a\":{\"href\":\"./Wiki/Main_Page\"},\"sa\":{\"href\":\"wiki/Main
 Page\"},\"dsr\":[31,83,17,2]}'>the main page [it's not very good</a>]</p>\n");
 add("html2html", "Non-breaking spaces in title", "<p 
data-parsoid='{\"dsr\":[0,34,0,0]}'><a rel=\"mw:WikiLink\" 
href=\"./Wiki/Main_Page\" title=\"Wiki/Main Page\" 
data-parsoid='{\"stx\":\"piped\",\"a\":{\"href\":\"./Wiki/Main_Page\"},\"sa\":{\"href\":\"wiki/Main
 Page\"},\"dsr\":[0,34,17,2]}'>  Main   Page  </a></p>\n");
@@ -515,9 +515,9 @@
 add("html2html", "ISBN with space-delimited number", "<p 
data-parsoid='{\"dsr\":[0,58,0,0]}'><a rel=\"mw:WikiLink\" 
href=\"./Wiki/Special:BookSources/9290170328\" 
title=\"Wiki/Special:BookSources/9290170328\" 
data-parsoid='{\"stx\":\"piped\",\"a\":{\"href\":\"./Wiki/Special:BookSources/9290170328\"},\"sa\":{\"href\":\"wiki/Special:BookSources/9290170328\"},\"dsr\":[0,58,38,2]}'>ISBN
 92 9017 032 8</a></p>\n");
 add("html2html", "ISBN length", "<p data-parsoid='{\"dsr\":[0,14,0,0]}'>ISBN 
123456789</p>\n\n<p data-parsoid='{\"dsr\":[16,71,0,0]}'><a rel=\"mw:WikiLink\" 
href=\"./Wiki/Special:BookSources/1234567890\" 
title=\"Wiki/Special:BookSources/1234567890\" 
data-parsoid='{\"stx\":\"piped\",\"a\":{\"href\":\"./Wiki/Special:BookSources/1234567890\"},\"sa\":{\"href\":\"wiki/Special:BookSources/1234567890\"},\"dsr\":[16,71,38,2]}'>ISBN
 1234567890</a></p>\n\n<p data-parsoid='{\"dsr\":[73,89,0,0]}'>ISBN 
12345678901</p>\n");
 add("html2html", "ISBN with trailing year (T9110)", "<p 
data-parsoid='{\"dsr\":[0,65,0,0]}'><a rel=\"mw:WikiLink\" 
href=\"./Wiki/Special:BookSources/1234567890\" 
title=\"Wiki/Special:BookSources/1234567890\" 
data-parsoid='{\"stx\":\"piped\",\"a\":{\"href\":\"./Wiki/Special:BookSources/1234567890\"},\"sa\":{\"href\":\"wiki/Special:BookSources/1234567890\"},\"dsr\":[0,58,38,2]}'>ISBN
 1-234-56789-0</a> - 2006</p>\n\n<p data-parsoid='{\"dsr\":[67,132,0,0]}'><a 
rel=\"mw:WikiLink\" href=\"./Wiki/Special:BookSources/1234567890\" 
title=\"Wiki/Special:BookSources/1234567890\" 
data-parsoid='{\"stx\":\"piped\",\"a\":{\"href\":\"./Wiki/Special:BookSources/1234567890\"},\"sa\":{\"href\":\"wiki/Special:BookSources/1234567890\"},\"dsr\":[67,125,38,2]}'>ISBN
 1 234 56789 0</a> - 2006</p>\n");
-add("html2html", "Self-link in language variants", "<p 
data-parsoid='{\"dsr\":[0,52,0,0]}'>Both [ Dunav] and [ Дунав] are names for 
this river.</p>\n");
-add("html2html", "Link to another existing title shouldn't be parsed as 
self-link even if it's a variant of this title", "<p 
data-parsoid='{\"dsr\":[0,85,0,0]}'><a rel=\"mw:WikiLink\" href=\"./Wiki/Дуна\" 
title=\"Wiki/Дуна\" 
data-parsoid='{\"stx\":\"piped\",\"a\":{\"href\":\"./Wiki/Дуна\"},\"sa\":{\"href\":\"wiki/Дуна\"},\"dsr\":[0,18,12,2]}'>Дуна</a>
 is not a self-link while [ Duna] and [ Dуна] are still self-links.</p>\n");
-add("html2html", "Link to a section of a variant of this title shouldn't be 
parsed as self-link", "<p data-parsoid='{\"dsr\":[0,98,0,0]}'>[ Dуна] is a 
self-link while <a rel=\"mw:WikiLink\" href=\"./Wiki/Дуна\" title=\"Wiki/Дуна\" 
data-parsoid='{\"stx\":\"piped\",\"a\":{\"href\":\"./Wiki/Дуна\"},\"sa\":{\"href\":\"wiki/Дуна\"},\"dsr\":[29,51,12,2]}'>Dunа#Foo</a>
 and <a rel=\"mw:WikiLink\" href=\"./Wiki/Дуна\" title=\"Wiki/Дуна\" 
data-parsoid='{\"stx\":\"piped\",\"a\":{\"href\":\"./Wiki/Дуна\"},\"sa\":{\"href\":\"wiki/Дуна\"},\"dsr\":[56,78,12,2]}'>Dуна#Foo</a>
 are not self-links.</p>\n");
+add("html2html", "Self-link in language variants", "<p 
data-parsoid='{\"dsr\":[0,46,0,0]}'>Both Dunav and Дунав are names for this 
river.</p>\n");
+add("html2html", "Link to another existing title shouldn't be parsed as 
self-link even if it's a variant of this title", "<p 
data-parsoid='{\"dsr\":[0,79,0,0]}'><a rel=\"mw:WikiLink\" href=\"./Wiki/Дуна\" 
title=\"Wiki/Дуна\" 
data-parsoid='{\"stx\":\"piped\",\"a\":{\"href\":\"./Wiki/Дуна\"},\"sa\":{\"href\":\"wiki/Дуна\"},\"dsr\":[0,18,12,2]}'>Дуна</a>
 is not a self-link while Duna and Dуна are still self-links.</p>\n");
+add("html2html", "Link to a section of a variant of this title shouldn't be 
parsed as self-link", "<p data-parsoid='{\"dsr\":[0,95,0,0]}'>Dуна is a 
self-link while <a rel=\"mw:WikiLink\" href=\"./Wiki/Дуна\" title=\"Wiki/Дуна\" 
data-parsoid='{\"stx\":\"piped\",\"a\":{\"href\":\"./Wiki/Дуна\"},\"sa\":{\"href\":\"wiki/Дуна\"},\"dsr\":[26,48,12,2]}'>Dunа#Foo</a>
 and <a rel=\"mw:WikiLink\" href=\"./Wiki/Дуна\" title=\"Wiki/Дуна\" 
data-parsoid='{\"stx\":\"piped\",\"a\":{\"href\":\"./Wiki/Дуна\"},\"sa\":{\"href\":\"wiki/Дуна\"},\"dsr\":[53,75,12,2]}'>Dуна#Foo</a>
 are not self-links.</p>\n");
 add("html2html", "Link to pages in language variants", "<p 
data-parsoid='{\"dsr\":[0,56,0,0]}'>Main Page can be written as <a 
rel=\"mw:WikiLink\" href=\"./Wiki/Main_Page\" title=\"Wiki/Main Page\" 
data-parsoid='{\"stx\":\"piped\",\"a\":{\"href\":\"./Wiki/Main_Page\"},\"sa\":{\"href\":\"wiki/Main
 Page\"},\"dsr\":[28,56,17,2]}'>Маин Паге</a></p>\n");
 add("html2html", "Multiple links to pages in language variants", "<p 
data-parsoid='{\"dsr\":[0,113,0,0]}'><a rel=\"mw:WikiLink\" 
href=\"./Wiki/Main_Page\" title=\"Wiki/Main Page\" 
data-parsoid='{\"stx\":\"piped\",\"a\":{\"href\":\"./Wiki/Main_Page\"},\"sa\":{\"href\":\"wiki/Main
 Page\"},\"dsr\":[0,28,17,2]}'>Main Page</a> can be written as <a 
rel=\"mw:WikiLink\" href=\"./Wiki/Main_Page\" title=\"Wiki/Main Page\" 
data-parsoid='{\"stx\":\"piped\",\"a\":{\"href\":\"./Wiki/Main_Page\"},\"sa\":{\"href\":\"wiki/Main
 Page\"},\"dsr\":[47,75,17,2]}'>Маин Паге</a> same as <a rel=\"mw:WikiLink\" 
href=\"./Wiki/Main_Page\" title=\"Wiki/Main Page\" 
data-parsoid='{\"stx\":\"piped\",\"a\":{\"href\":\"./Wiki/Main_Page\"},\"sa\":{\"href\":\"wiki/Main
 Page\"},\"dsr\":[84,112,17,2]}'>Маин Паге</a>.</p>\n");
 add("html2html", "Prevent conversion of links with -{}- tags (language 
variants)", "<p data-parsoid='{\"dsr\":[0,28,0,0]}'><a rel=\"mw:WikiLink\" 
href=\"./Wiki/Main_Page\" title=\"Wiki/Main Page\" 
data-parsoid='{\"stx\":\"piped\",\"a\":{\"href\":\"./Wiki/Main_Page\"},\"sa\":{\"href\":\"wiki/Main
 Page\"},\"dsr\":[0,28,17,2]}'>Main Page</a></p>\n");
@@ -728,7 +728,7 @@
 add("html2wt", "Plain link to page with question mark in title", 
"[[wiki/A?b|A?b]]\n\n[[wiki/A?b|Baz]]\n");
 add("html2wt", "T2337: Escaped self-links should be bold", "[[Bug462]] 
[[Bug462]]\n");
 add("html2wt", "Self-link to section should not be bold", "[[wiki/Main 
Page#section|Main Page#section]]\n");
-add("html2wt", "Self-link to numeric title", "[ 0]\n");
+add("html2wt", "Self-link to numeric title", "0\n");
 add("html2wt", "Link to numeric-equivalent title", "[[wiki/00|00]]\n");
 add("html2wt", "<nowiki> inside a link", "<nowiki>[[Main Page]]</nowiki> 
[[wiki/Main Page|the main page [it's not very good]]]\n");
 add("html2wt", "Non-breaking spaces in title", "[[wiki/Main Page|  Main   Page 
 ]]\n");
@@ -1042,9 +1042,9 @@
 add("html2wt", "T8200: blockquotes and paragraph formatting", 
"<blockquote>\nfoo\n\n</blockquote>\nbar\n\n baz\n");
 add("html2wt", "T10293: Use of center tag ruins paragraph formatting", 
"<center>\nfoo\n\n</center>\nbar\n\n baz\n");
 add("html2wt", "Parsing of overlapping (improperly nested) inline html tags", 
"<span><s>x</s></span>\n");
-add("html2wt", "Self-link in language variants", "Both [ Dunav] and [ Дунав] 
are names for this river.\n");
-add("html2wt", "Link to another existing title shouldn't be parsed as 
self-link even if it's a variant of this title", "[[wiki/Дуна|Дуна]] is not a 
self-link while [ Duna] and [ Dуна] are still self-links.\n");
-add("html2wt", "Link to a section of a variant of this title shouldn't be 
parsed as self-link", "[ Dуна] is a self-link while [[wiki/Дуна|Dunа#Foo]] and 
[[wiki/Дуна|Dуна#Foo]] are not self-links.\n");
+add("html2wt", "Self-link in language variants", "Both Dunav and Дунав are 
names for this river.\n");
+add("html2wt", "Link to another existing title shouldn't be parsed as 
self-link even if it's a variant of this title", "[[wiki/Дуна|Дуна]] is not a 
self-link while Duna and Dуна are still self-links.\n");
+add("html2wt", "Link to a section of a variant of this title shouldn't be 
parsed as self-link", "Dуна is a self-link while [[wiki/Дуна|Dunа#Foo]] and 
[[wiki/Дуна|Dуна#Foo]] are not self-links.\n");
 add("html2wt", "Link to pages in language variants", "Main Page can be written 
as [[wiki/Main Page|Маин Паге]]\n");
 add("html2wt", "Multiple links to pages in language variants", "[[wiki/Main 
Page|Main Page]] can be written as [[wiki/Main Page|Маин Паге]] same as 
[[wiki/Main Page|Маин Паге]].\n");
 add("html2wt", "Simple template in language variants", "This is a test 
template\n");
diff --git a/tests/parserTests.txt b/tests/parserTests.txt
index 935c539..cb0001f 100644
--- a/tests/parserTests.txt
+++ b/tests/parserTests.txt
@@ -27449,6 +27449,20 @@
 |}
 !! end
 
+!! test
+Anchor without href scenarios
+!! options
+parsoid={ "modes": ["html2wt"], "suppressErrors": true }
+!! html/parsoid
+<a class="bc"></a>
+<a class="no">dice</a>
+<a name="foo"></a>
+!! wikitext
+
+dice
+<span name="foo"></span>
+!! end
+
 # -----------------------------------------------------------------
 # End of section for Parsoid-only html2wt tests for serialization
 # of new content

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

Gerrit-MessageType: merged
Gerrit-Change-Id: Ia9accda1054d4ae41ec5d289a9e1ea1a77375aee
Gerrit-PatchSet: 3
Gerrit-Project: mediawiki/services/parsoid
Gerrit-Branch: master
Gerrit-Owner: Arlolra <[email protected]>
Gerrit-Reviewer: Arlolra <[email protected]>
Gerrit-Reviewer: C. Scott Ananian <[email protected]>
Gerrit-Reviewer: Subramanya Sastry <[email protected]>
Gerrit-Reviewer: jenkins-bot <>

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

Reply via email to