|
1 | | -import { validTagName, reservedTags, PCENChar, builtInTagMap } from "eslint-plugin-custom-elements/lib/tag-names.js" |
| 1 | +import { builtInTagMap, PCENChar, reservedTags, validTagName } from "eslint-plugin-custom-elements/lib/tag-names.js" |
2 | 2 |
|
3 | 3 | const validChar = new RegExp(`^${PCENChar}$`) |
4 | 4 |
|
5 | | -customElements.define( |
6 | | - "tag-name-input", |
7 | | - class extends HTMLInputElement { |
8 | | - connectedCallback() { |
9 | | - this.addEventListener("input", this) |
10 | | - this.handleEvent() |
11 | | - } |
| 5 | +export default class extends HTMLInputElement { |
| 6 | + static define(tag = "tag-name-input") { |
| 7 | + customElements.define(tag, this, { extends: "input" }) |
| 8 | + } |
| 9 | + connectedCallback() { |
| 10 | + this.addEventListener("input", this) |
| 11 | + this.handleEvent() |
| 12 | + } |
12 | 13 |
|
13 | | - handleEvent() { |
14 | | - const { value } = this |
15 | | - this.setCustomValidity("") |
16 | | - let hint = "" |
17 | | - if (value) { |
18 | | - if (/[A-Z]/.test(value)) { |
19 | | - this.setCustomValidity(`${value} is not valid, it cannot contain capital letters`) |
20 | | - } else if (reservedTags.has(value)) { |
21 | | - this.setCustomValidity(`${value} is not valid, it's a reserved tag`) |
22 | | - } |
23 | | - if (!value.includes("-")) { |
24 | | - this.setCustomValidity(`${value} is not valid, it must include a dash (-)`) |
25 | | - } else if (value.startsWith("-")) { |
26 | | - this.setCustomValidity(`${value} is not valid, it must not start with a dash (-)`) |
27 | | - } else if (!/^[a-z]/.test(value)) { |
28 | | - this.setCustomValidity(`${value} is not valid, it must start with a letter (a-z)`) |
29 | | - } else if (!validTagName(value)) { |
30 | | - const chars = new Set() |
31 | | - for (const char of value) { |
32 | | - if (!validChar.test(char)) chars.add(`'${char}'`) |
33 | | - } |
34 | | - this.setCustomValidity(`${value} is not a valid tag name, cannot contain ${[...chars].join(", ")}`) |
| 14 | + handleEvent() { |
| 15 | + const { value } = this |
| 16 | + this.setCustomValidity("") |
| 17 | + let hint = "" |
| 18 | + if (value) { |
| 19 | + if (/[A-Z]/.test(value)) { |
| 20 | + this.setCustomValidity(`${value} is not valid, it cannot contain capital letters`) |
| 21 | + } else if (reservedTags.has(value)) { |
| 22 | + this.setCustomValidity(`${value} is not valid, it's a reserved tag`) |
| 23 | + } |
| 24 | + if (!value.includes("-")) { |
| 25 | + this.setCustomValidity(`${value} is not valid, it must include a dash (-)`) |
| 26 | + } else if (value.startsWith("-")) { |
| 27 | + this.setCustomValidity(`${value} is not valid, it must not start with a dash (-)`) |
| 28 | + } else if (!/^[a-z]/.test(value)) { |
| 29 | + this.setCustomValidity(`${value} is not valid, it must start with a letter (a-z)`) |
| 30 | + } else if (!validTagName(value)) { |
| 31 | + const chars = new Set() |
| 32 | + for (const char of value) { |
| 33 | + if (!validChar.test(char)) chars.add(`'${char}'`) |
35 | 34 | } |
| 35 | + this.setCustomValidity(`${value} is not a valid tag name, cannot contain ${[...chars].join(", ")}`) |
| 36 | + } |
36 | 37 |
|
37 | | - const parts = value.split(/-/g) |
38 | | - for (const part in parts) { |
39 | | - if (part in builtInTagMap) { |
40 | | - hint = `${value} is similar to the built-in ${builtInTagMap[part]}` |
41 | | - } |
| 38 | + const parts = value.split(/-/g) |
| 39 | + for (const part in parts) { |
| 40 | + if (part in builtInTagMap) { |
| 41 | + hint = `${value} is similar to the built-in ${builtInTagMap[part]}` |
42 | 42 | } |
43 | 43 | } |
44 | | - this.reportValidity() |
45 | | - const errorEl = this.parentElement.querySelector(".error span") |
46 | | - errorEl.textContent = this.validationMessage |
47 | | - const hintEl = this.parentElement.querySelector(".hint span") |
48 | | - hintEl.textContent = hint |
49 | 44 | } |
50 | | - }, |
51 | | - { extends: "input" } |
52 | | -) |
| 45 | + this.reportValidity() |
| 46 | + const errorEl = this.parentElement.querySelector(".error span") |
| 47 | + errorEl.textContent = this.validationMessage |
| 48 | + const hintEl = this.parentElement.querySelector(".hint span") |
| 49 | + hintEl.textContent = hint |
| 50 | + } |
| 51 | +} |
0 commit comments