Skip to content

Commit 0a79c27

Browse files
committed
fix(rules): improving 'valid-by-tagname' rule
1 parent c1f4f77 commit 0a79c27

File tree

3 files changed

+27
-5
lines changed

3 files changed

+27
-5
lines changed

docs/rules/valid-by-tagname.md

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
11
# 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. [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.
3+
Ensure a valid Tag Name is being used with `by.tagName()` locator.
4+
The rule supports [HTML specifications]((https://www.w3.org/TR/html/syntax.html#tag-name)) and
5+
Angular's ability to create custom elements via directives. A Tag Name is considered valid when:
6+
7+
- It can contain alphanumeric characters.
8+
- It can contain the dash`(-)` symbol.
9+
- It cannot start with dash or a number.
10+
- It cannot end with dash.
511

612
This rule is very useful for notifying when an invalid Tag Name is being used.
713
It will also prevent unintentionally putting different types of locators instead of the actual Tag name.
@@ -18,6 +24,8 @@ element(by.tagName("multiple tagnames"));
1824
element(by.tagName('option[value="Test"]'));
1925
element(by.tagName(" div "));
2026
element(by.tagName("12345"));
27+
element(by.tagName("-"));
28+
element(by.tagName("customtag-"));
2129
```
2230

2331
The following patterns are not errors:
@@ -31,4 +39,5 @@ element(by.tagName("Area"));
3139
element(by.tagName("BlockQuote"));
3240
element(by.tagName("h1"));
3341
element(by.tagName("H1"));
42+
element(by.tagName("my-custom-tag"));
3443
```

lib/rules/valid-by-tagname.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* @author Raul Gallegos
66
*/
77

8-
var isTagName = /^[A-Za-z][A-Za-z0-9]*$/
8+
var isTagName = /^[A-Za-z][A-Za-z0-9-]*$/
99

1010
module.exports = {
1111
meta: {
@@ -23,7 +23,7 @@ module.exports = {
2323

2424
if (insideByTagName && argumentExists) {
2525
var tagName = node.arguments[0].value
26-
if (!tagName.match(isTagName)) {
26+
if (!tagName.match(isTagName) || tagName.endsWith('-')) {
2727
context.report({
2828
node: node,
2929
message: 'Invalid TagName value: "' + tagName + '"'

test/rules/valid-by-tagname.js

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ eslintTester.run('valid-by-tagname', rule, {
1414
'element(by.tagName("Area"));',
1515
'element(by.tagName("BlockQuote"));',
1616
'element(by.tagName("h1"));',
17-
'element(by.tagName("H1"));'
17+
'element(by.tagName("H1"));',
18+
'element(by.tagName("my-custom-tag"));'
1819
],
1920

2021
invalid: [
@@ -59,6 +60,18 @@ eslintTester.run('valid-by-tagname', rule, {
5960
errors: [{
6061
message: 'Invalid TagName value: "12345"'
6162
}]
63+
},
64+
{
65+
code: 'element(by.tagName("-"));',
66+
errors: [{
67+
message: 'Invalid TagName value: "-"'
68+
}]
69+
},
70+
{
71+
code: 'element(by.tagName("customtag-"));',
72+
errors: [{
73+
message: 'Invalid TagName value: "customtag-"'
74+
}]
6275
}
6376
]
6477
})

0 commit comments

Comments
 (0)