Skip to content

Commit ab1f9ce

Browse files
committed
fix(rules): improve 'valid-by-tagname' rule
1 parent 77536c8 commit ab1f9ce

File tree

3 files changed

+23
-139
lines changed

3 files changed

+23
-139
lines changed

docs/rules/valid-by-tagname.md

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,23 @@
1-
# Prohibit use of invalid Html Tag Name value when using `by.tagName()` locator
1+
# Prohibit use of invalid Tag Name value when using `by.tagName()` locator
22

3-
Ensure a valid Tag Name is being used with `by.tagName()` locator. We only accept the listed
4-
[HTML tags here](http://www.w3schools.com/tags/) to determine the validness of the Tag Name.
3+
Ensure a valid Tag Name is being used with `by.tagName()` locator. [A valid Tag Name](https://www.w3.org/TR/html/syntax.html#tag-name) should only
4+
contain alphanumeric and cannot start with a number.
55

6-
This rule is very useful for notifying when an invalid html Tag Name is being used.
6+
This rule is very useful for notifying when an invalid Tag Name is being used.
77
It will also prevent unintentionally putting different types of locators instead of the actual Tag name.
88

99
## Rule details
1010

1111
Any use of the following patterns are considered errors when using `by.tagName`:
1212

1313
```js
14-
element(by.tagName("customTagName"));
14+
element(by.tagName("_customTagName"));
1515
element(by.tagName("div.classname"));
16-
element(by.tagName("_blockquote"));
17-
element(by.tagName("divs"));
16+
element(by.tagName("blockquote:"));
17+
element(by.tagName("multiple tagnames"));
1818
element(by.tagName('option[value="Test"]'));
1919
element(by.tagName(" div "));
20+
element(by.tagName("12345"));
2021
```
2122

2223
The following patterns are not errors:

lib/rules/valid-by-tagname.js

Lines changed: 3 additions & 126 deletions
Original file line numberDiff line numberDiff line change
@@ -1,134 +1,11 @@
11
'use strict'
22

33
/**
4-
* @fileoverview Ensure tag name is html valid when using `by.tagName()` locator.
4+
* @fileoverview Ensure tag name is valid when using `by.tagName()` locator.
55
* @author Raul Gallegos
66
*/
77

8-
var validHTMLTagNames = [
9-
'a',
10-
'abbr',
11-
'acronym',
12-
'address',
13-
'applet',
14-
'area',
15-
'article',
16-
'aside',
17-
'audio',
18-
'b',
19-
'base',
20-
'basefont',
21-
'bdi',
22-
'bdo',
23-
'big',
24-
'blockquote',
25-
'body',
26-
'br',
27-
'button',
28-
'canvas',
29-
'caption',
30-
'center',
31-
'cite',
32-
'code',
33-
'col',
34-
'colgroup',
35-
'datalist',
36-
'dd',
37-
'del',
38-
'details',
39-
'dfn',
40-
'dialog',
41-
'dir',
42-
'div',
43-
'dl',
44-
'dt',
45-
'em',
46-
'embed',
47-
'fieldset',
48-
'figcaption',
49-
'figure',
50-
'font',
51-
'footer',
52-
'form',
53-
'frame',
54-
'frameset',
55-
'h1',
56-
'h2',
57-
'h3',
58-
'h4',
59-
'h5',
60-
'h6',
61-
'head',
62-
'header',
63-
'hr',
64-
'html',
65-
'i',
66-
'iframe',
67-
'img',
68-
'input',
69-
'ins',
70-
'kbd',
71-
'keygen',
72-
'label',
73-
'legend',
74-
'li',
75-
'link',
76-
'main',
77-
'map',
78-
'mark',
79-
'menu',
80-
'menuitem',
81-
'meta',
82-
'meter',
83-
'nav',
84-
'noframes',
85-
'noscript',
86-
'object',
87-
'ol',
88-
'optgroup',
89-
'option',
90-
'output',
91-
'p',
92-
'param',
93-
'picture',
94-
'pre',
95-
'progress',
96-
'q',
97-
'rp',
98-
'rt',
99-
'ruby',
100-
's',
101-
'samp',
102-
'script',
103-
'section',
104-
'select',
105-
'small',
106-
'source',
107-
'span',
108-
'strike',
109-
'strong',
110-
'style',
111-
'sub',
112-
'summary',
113-
'sup',
114-
'table',
115-
'tbody',
116-
'td',
117-
'textarea',
118-
'tfoot',
119-
'th',
120-
'thead',
121-
'time',
122-
'title',
123-
'tr',
124-
'track',
125-
'tt',
126-
'u',
127-
'ul',
128-
'var',
129-
'video',
130-
'wbr'
131-
]
8+
var isTagName = /^[A-Za-z][A-Za-z0-9]*$/
1329

13310
module.exports = {
13411
meta: {
@@ -146,7 +23,7 @@ module.exports = {
14623

14724
if (insideByTagName && argumentExists) {
14825
var tagName = node.arguments[0].value
149-
if (validHTMLTagNames.indexOf(tagName.toLowerCase()) === -1) {
26+
if (!tagName.match(isTagName)) {
15027
context.report({
15128
node: node,
15229
message: 'Invalid TagName value: "' + tagName + '"'

test/rules/valid-by-tagname.js

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@ eslintTester.run('valid-by-tagname', rule, {
1919

2020
invalid: [
2121
{
22-
code: 'element(by.tagName("customTagName"));',
22+
code: 'element(by.tagName("_customTagName"));',
2323
errors: [{
24-
message: 'Invalid TagName value: "customTagName"'
24+
message: 'Invalid TagName value: "_customTagName"'
2525
}]
2626
},
2727
{
@@ -31,15 +31,15 @@ eslintTester.run('valid-by-tagname', rule, {
3131
}]
3232
},
3333
{
34-
code: 'element(by.tagName("_blockquote"));',
34+
code: 'element(by.tagName("blockquote:"));',
3535
errors: [{
36-
message: 'Invalid TagName value: "_blockquote"'
36+
message: 'Invalid TagName value: "blockquote:"'
3737
}]
3838
},
3939
{
40-
code: 'element(by.tagName("divs"));',
40+
code: 'element(by.tagName("multiple tagnames"));',
4141
errors: [{
42-
message: 'Invalid TagName value: "divs"'
42+
message: 'Invalid TagName value: "multiple tagnames"'
4343
}]
4444
},
4545
{
@@ -53,6 +53,12 @@ eslintTester.run('valid-by-tagname', rule, {
5353
errors: [{
5454
message: 'Invalid TagName value: " div "'
5555
}]
56+
},
57+
{
58+
code: 'element(by.tagName("12345"));',
59+
errors: [{
60+
message: 'Invalid TagName value: "12345"'
61+
}]
5662
}
5763
]
5864
})

0 commit comments

Comments
 (0)