Skip to content

Commit 5736b2a

Browse files
kemsakuraiazu
authored andcommitted
fix(plugin): Add url property to Link Node (#8)
* 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 (<anonymous>) `` ` 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.
1 parent 61db8c6 commit 5736b2a

File tree

3 files changed

+114
-7
lines changed

3 files changed

+114
-7
lines changed

src/html-to-ast.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,10 @@ export function parse(html) {
7878
node.range = range;
7979
node.raw = html.slice(range[0], range[1]);
8080
}
81+
// map `url` to Link node
82+
if (node.type === "Link" && typeof node.properties.href !== "undefined") {
83+
node.url = node.properties.href;
84+
}
8185
}
8286
removeUnusedProperties(node);
8387
});

test/ast-test-case/element-attributes/index.html

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,3 +93,7 @@
9393
<!--numeric:-->
9494

9595
<meter min="0" low="40" high="90" max="100" value="95"></meter>
96+
97+
<!--placeholder:-->
98+
99+
<a>placeholder</a>

test/ast-test-case/element-attributes/index.json

Lines changed: 106 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
{
4343
"type": "Link",
4444
"tagName": "a",
45+
"url": "http://alpha.com",
4546
"properties": {
4647
"href": "http://alpha.com",
4748
"className": [
@@ -2136,22 +2137,120 @@
21362137
},
21372138
{
21382139
"type": "Str",
2139-
"value": "\n",
2140+
"value": "\n\n",
21402141
"loc": {
21412142
"start": {
21422143
"line": 95,
21432144
"column": 63
21442145
},
21452146
"end": {
2146-
"line": 96,
2147+
"line": 97,
21472148
"column": 0
21482149
}
21492150
},
21502151
"range": [
21512152
1405,
2152-
1406
2153+
1407
21532154
],
2154-
"raw": "\n"
2155+
"raw": "\n\n"
2156+
},
2157+
{
2158+
"loc": {
2159+
"end": {
2160+
"column": 19,
2161+
"line": 97
2162+
},
2163+
"start": {
2164+
"column": 0,
2165+
"line": 97
2166+
}
2167+
},
2168+
"range": [
2169+
1407,
2170+
1426
2171+
],
2172+
"raw": "<!--placeholder:-->",
2173+
"type": "Comment",
2174+
"value": "placeholder:"
2175+
},
2176+
{
2177+
"loc": {
2178+
"end": {
2179+
"column": 0,
2180+
"line": 99
2181+
},
2182+
"start": {
2183+
"column": 19,
2184+
"line": 97
2185+
}
2186+
},
2187+
"range": [
2188+
1426,
2189+
1428
2190+
],
2191+
"raw": "\n\n",
2192+
"type": "Str",
2193+
"value": "\n\n"
2194+
},
2195+
{
2196+
"children": [
2197+
{
2198+
"loc": {
2199+
"end": {
2200+
"column": 14,
2201+
"line": 99
2202+
},
2203+
"start": {
2204+
"column": 3,
2205+
"line": 99
2206+
}
2207+
},
2208+
"range": [
2209+
1431,
2210+
1442
2211+
],
2212+
"raw": "placeholder",
2213+
"type": "Str",
2214+
"value": "placeholder"
2215+
}
2216+
],
2217+
"loc": {
2218+
"end": {
2219+
"column": 18,
2220+
"line": 99
2221+
},
2222+
"start": {
2223+
"column": 0,
2224+
"line": 99
2225+
}
2226+
},
2227+
"properties": {},
2228+
"range": [
2229+
1428,
2230+
1446
2231+
],
2232+
"raw": "<a>placeholder</a>",
2233+
"tagName": "a",
2234+
"type": "Link"
2235+
},
2236+
{
2237+
"loc": {
2238+
"end": {
2239+
"column": 0,
2240+
"line": 100
2241+
},
2242+
"start": {
2243+
"column": 18,
2244+
"line": 99
2245+
}
2246+
},
2247+
"range": [
2248+
1446,
2249+
1447
2250+
],
2251+
"raw": "\n",
2252+
"type": "Str",
2253+
"value": "\n"
21552254
}
21562255
],
21572256
"loc": {
@@ -2160,13 +2259,13 @@
21602259
"column": 0
21612260
},
21622261
"end": {
2163-
"line": 96,
2262+
"line": 100,
21642263
"column": 0
21652264
}
21662265
},
21672266
"range": [
21682267
0,
2169-
1406
2268+
1447
21702269
],
2171-
"raw": "<!--core:-->\n\n<a href=\"http://alpha.com\" class=\"bravo\" download></a>\n\n<!--double:-->\n\n<div class class></div>\n\n<!--unknown:-->\n\n<a ng-init></a>\n\n<a ng-repeat=\"\"></a>\n\n<a ng-click=\"ctrl.onintentclick($scope)\">ng-whatevs</a>.\n\n<!--empty:-->\n\n<abbr title></abbr>\n\n<!--space separated:-->\n\n<code class=\"language-foo bar\"></code>\n\n<code class=\"\"></code>\n\n<code class></code>\n\n<code class=\" \"></code>\n\n<!--comma separated:-->\n\n<img srcset=\"medium.jpg 1000w, large.jpg 2000w\">\n\n<img srcset=\"medium.jpg 1000w\">\n\n<img srcset=\"\">\n\n<img srcset=\" \">\n\n<img srcset=\",\">\n\n<img srcset=\" , \">\n\n<img srcset=\",foo\">\n\n<img srcset=\"bar,\">\n\n<img srcset=\",baz,\">\n\n<!--style:-->\n\n<div></div>\n\n<div style=\"\"></div>\n\n<div style=\"color\"></div>\n\n<div style=\"color:\"></div>\n\n<div style=\"color:red\"></div>\n\n<div style=\"color:;\"></div>\n\n<div style=\"color:red;\"></div>\n\n<div style=\"color:red;background-color\"></div>\n\n<div style=\"color:red;background-color: blue\"></div>\n\n<div style=\"color:red;background-color: blue;\"></div>\n\n<!--boolean:-->\n\n<script async></script>\n<script async=\"\"></script>\n<script async=\"async\"></script>\n\n<!--overloaded boolean (as boolean):-->\n\n<a download></a>\n<a download=\"\"></a>\n<a download=\"download\"></a>\n\n<!--overloaded boolean (as overloaded):-->\n\n<a download=\"example.mp3\"></a>\n\n<!--positive numeric:-->\n\n<img height=\"100\">\n\n<!--numeric:-->\n\n<meter min=\"0\" low=\"40\" high=\"90\" max=\"100\" value=\"95\"></meter>\n"
2270+
"raw": "<!--core:-->\n\n<a href=\"http://alpha.com\" class=\"bravo\" download></a>\n\n<!--double:-->\n\n<div class class></div>\n\n<!--unknown:-->\n\n<a ng-init></a>\n\n<a ng-repeat=\"\"></a>\n\n<a ng-click=\"ctrl.onintentclick($scope)\">ng-whatevs</a>.\n\n<!--empty:-->\n\n<abbr title></abbr>\n\n<!--space separated:-->\n\n<code class=\"language-foo bar\"></code>\n\n<code class=\"\"></code>\n\n<code class></code>\n\n<code class=\" \"></code>\n\n<!--comma separated:-->\n\n<img srcset=\"medium.jpg 1000w, large.jpg 2000w\">\n\n<img srcset=\"medium.jpg 1000w\">\n\n<img srcset=\"\">\n\n<img srcset=\" \">\n\n<img srcset=\",\">\n\n<img srcset=\" , \">\n\n<img srcset=\",foo\">\n\n<img srcset=\"bar,\">\n\n<img srcset=\",baz,\">\n\n<!--style:-->\n\n<div></div>\n\n<div style=\"\"></div>\n\n<div style=\"color\"></div>\n\n<div style=\"color:\"></div>\n\n<div style=\"color:red\"></div>\n\n<div style=\"color:;\"></div>\n\n<div style=\"color:red;\"></div>\n\n<div style=\"color:red;background-color\"></div>\n\n<div style=\"color:red;background-color: blue\"></div>\n\n<div style=\"color:red;background-color: blue;\"></div>\n\n<!--boolean:-->\n\n<script async></script>\n<script async=\"\"></script>\n<script async=\"async\"></script>\n\n<!--overloaded boolean (as boolean):-->\n\n<a download></a>\n<a download=\"\"></a>\n<a download=\"download\"></a>\n\n<!--overloaded boolean (as overloaded):-->\n\n<a download=\"example.mp3\"></a>\n\n<!--positive numeric:-->\n\n<img height=\"100\">\n\n<!--numeric:-->\n\n<meter min=\"0\" low=\"40\" high=\"90\" max=\"100\" value=\"95\"></meter>\n\n<!--placeholder:-->\n\n<a>placeholder</a>\n"
21722271
}

0 commit comments

Comments
 (0)