From 5736b2ae566d8fb3513f49a0f657971b33f66489 Mon Sep 17 00:00:00 2001 From: Ken Sakurai Date: Sun, 19 Nov 2017 16:10:28 +0900 Subject: [PATCH] fix(plugin): Add url property to Link Node (#8) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Add url property to Link Node When applying `textlint-rule-no-dead-link` to HTML, an error occurred. `` `console ✖ Stack trace TypeError: Parameter "url" must be a string, not undefined at Url.parse (url.js: 102: 11) at Object.urlParse [as parse] (url.js: 96: 5) at isRelative (/usr/local/lib/node_modules/textlint-rule-no-dead-link/lib/no-dead-link .js:83:24) at /usr/local/lib/node_modules/textlint-rule-no-dead-link/lib/no-dead-link. js: 107: 11 at Generator.next () `` ` textlint-rule-no-dead-link expects the Link Node to hold the url property, and in the case of txt, markdown it holds the actual url property. I think that the Link Node generated from the html file should also keep the url property. [^ 1] [1]: The href attribute of `a` tag is not mandatory. If href does not exist, url is not set. If url does not exist, a fix to ignore the target Link node is required for `textlint-rule-no-dead-link`. * Fix undefined check of href, and add test for placeholder. Fix undefined check of href, and add test for placeholder. --- src/html-to-ast.js | 4 + .../element-attributes/index.html | 4 + .../element-attributes/index.json | 113 ++++++++++++++++-- 3 files changed, 114 insertions(+), 7 deletions(-) diff --git a/src/html-to-ast.js b/src/html-to-ast.js index 18617eb..0d1e900 100644 --- a/src/html-to-ast.js +++ b/src/html-to-ast.js @@ -78,6 +78,10 @@ export function parse(html) { node.range = range; node.raw = html.slice(range[0], range[1]); } + // map `url` to Link node + if (node.type === "Link" && typeof node.properties.href !== "undefined") { + node.url = node.properties.href; + } } removeUnusedProperties(node); }); diff --git a/test/ast-test-case/element-attributes/index.html b/test/ast-test-case/element-attributes/index.html index 7c695da..14f1b73 100644 --- a/test/ast-test-case/element-attributes/index.html +++ b/test/ast-test-case/element-attributes/index.html @@ -93,3 +93,7 @@ + + + +placeholder diff --git a/test/ast-test-case/element-attributes/index.json b/test/ast-test-case/element-attributes/index.json index 421f461..fda376c 100644 --- a/test/ast-test-case/element-attributes/index.json +++ b/test/ast-test-case/element-attributes/index.json @@ -42,6 +42,7 @@ { "type": "Link", "tagName": "a", + "url": "http://alpha.com", "properties": { "href": "http://alpha.com", "className": [ @@ -2136,22 +2137,120 @@ }, { "type": "Str", - "value": "\n", + "value": "\n\n", "loc": { "start": { "line": 95, "column": 63 }, "end": { - "line": 96, + "line": 97, "column": 0 } }, "range": [ 1405, - 1406 + 1407 ], - "raw": "\n" + "raw": "\n\n" + }, + { + "loc": { + "end": { + "column": 19, + "line": 97 + }, + "start": { + "column": 0, + "line": 97 + } + }, + "range": [ + 1407, + 1426 + ], + "raw": "", + "type": "Comment", + "value": "placeholder:" + }, + { + "loc": { + "end": { + "column": 0, + "line": 99 + }, + "start": { + "column": 19, + "line": 97 + } + }, + "range": [ + 1426, + 1428 + ], + "raw": "\n\n", + "type": "Str", + "value": "\n\n" + }, + { + "children": [ + { + "loc": { + "end": { + "column": 14, + "line": 99 + }, + "start": { + "column": 3, + "line": 99 + } + }, + "range": [ + 1431, + 1442 + ], + "raw": "placeholder", + "type": "Str", + "value": "placeholder" + } + ], + "loc": { + "end": { + "column": 18, + "line": 99 + }, + "start": { + "column": 0, + "line": 99 + } + }, + "properties": {}, + "range": [ + 1428, + 1446 + ], + "raw": "placeholder", + "tagName": "a", + "type": "Link" + }, + { + "loc": { + "end": { + "column": 0, + "line": 100 + }, + "start": { + "column": 18, + "line": 99 + } + }, + "range": [ + 1446, + 1447 + ], + "raw": "\n", + "type": "Str", + "value": "\n" } ], "loc": { @@ -2160,13 +2259,13 @@ "column": 0 }, "end": { - "line": 96, + "line": 100, "column": 0 } }, "range": [ 0, - 1406 + 1447 ], - "raw": "\n\n\n\n\n\n
\n\n\n\n\n\n\n\nng-whatevs.\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n
\n\n
\n\n
\n\n
\n\n
\n\n
\n\n
\n\n
\n\n
\n\n
\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n" + "raw": "\n\n\n\n\n\n
\n\n\n\n\n\n\n\nng-whatevs.\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n
\n\n
\n\n
\n\n
\n\n
\n\n
\n\n
\n\n
\n\n
\n\n
\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\nplaceholder\n" } \ No newline at end of file