├── .npmrc ├── .gitattributes ├── .gitignore ├── .editorconfig ├── .github └── workflows │ └── main.yml ├── cli.js ├── license ├── readme.md ├── package.json ├── test.js └── index.js /.npmrc: -------------------------------------------------------------------------------- 1 | package-lock=false 2 | -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | * text=auto eol=lf 2 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | yarn.lock 3 | -------------------------------------------------------------------------------- /.editorconfig: -------------------------------------------------------------------------------- 1 | root = true 2 | 3 | [*] 4 | indent_style = tab 5 | end_of_line = lf 6 | charset = utf-8 7 | trim_trailing_whitespace = true 8 | insert_final_newline = true 9 | 10 | [*.yml] 11 | indent_style = space 12 | indent_size = 2 13 | -------------------------------------------------------------------------------- /.github/workflows/main.yml: -------------------------------------------------------------------------------- 1 | name: CI 2 | on: 3 | - push 4 | - pull_request 5 | jobs: 6 | test: 7 | name: Node.js ${{ matrix.node-version }} 8 | runs-on: ubuntu-latest 9 | strategy: 10 | fail-fast: false 11 | matrix: 12 | node-version: 13 | - 16 14 | - 14 15 | - 12 16 | steps: 17 | - uses: actions/checkout@v4 18 | - uses: actions/setup-node@v4 19 | with: 20 | node-version: ${{ matrix.node-version }} 21 | - run: npm install 22 | - run: npm test 23 | -------------------------------------------------------------------------------- /cli.js: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env node 2 | import logSymbols from 'log-symbols'; 3 | import meow from 'meow'; 4 | import validateElementName from './index.js'; 5 | 6 | const cli = meow(` 7 | Usage 8 | $ validate-element-name 9 | 10 | Example 11 | $ validate-element-name s-slider 12 | ${logSymbols.success} Valid element name 13 | `, { 14 | importMeta: import.meta 15 | }); 16 | 17 | if (cli.input.length === 0) { 18 | console.error('Specify an element name'); 19 | process.exit(1); 20 | } 21 | 22 | const result = validateElementName(cli.input[0]); 23 | 24 | if (result.isValid) { 25 | if (result.message) { 26 | console.log(`${logSymbols.success} Valid element name, but…`); 27 | console.log(`${logSymbols.warning} ${result.message}`); 28 | } else { 29 | console.log(`${logSymbols.success} Valid element name.`); 30 | } 31 | } else { 32 | console.error(`${logSymbols.error} ${result.message}`); 33 | } 34 | -------------------------------------------------------------------------------- /license: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) Sindre Sorhus (https://sindresorhus.com) 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 6 | 7 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 8 | 9 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 10 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | # validate-element-name 2 | 3 | > Validate the name of a [custom element](http://www.html5rocks.com/en/tutorials/webcomponents/customelements/) 4 | 5 | Custom element names should start with `a-z` and contain `a-z` and at least one `-` with optionally `0-9`. 6 | 7 | You should not use the [`x-`, `polymer-`](http://webcomponents.github.io/articles/how-should-i-name-my-element/), [`ng-`](http://docs.angularjs.org/guide/directive#creating-directives) prefixes. 8 | 9 | ## Install 10 | 11 | ```sh 12 | npm install validate-element-name 13 | ``` 14 | 15 | ## Usage 16 | 17 | ```js 18 | import validateElementName from 'validate-element-name'; 19 | 20 | validateElementName('unicorn'); 21 | //=> {isValid: false, message: 'Custom element names must contain a hyphen. Example: unicorn-cake'} 22 | ``` 23 | 24 | See [cli.js](cli.js) for real-world usage. 25 | 26 | ## CLI 27 | 28 | ```sh 29 | npm install --global validate-element-name 30 | ``` 31 | 32 | ``` 33 | $ validate-element-name --help 34 | 35 | Usage 36 | $ validate-element-name 37 | 38 | Example 39 | $ validate-element-name s-slider 40 | ✔︎ Valid element name. 41 | ``` 42 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "validate-element-name", 3 | "version": "3.0.0", 4 | "description": "Validate the name of a custom element", 5 | "license": "MIT", 6 | "repository": "sindresorhus/validate-element-name", 7 | "funding": "https://github.com/sponsors/sindresorhus", 8 | "author": { 9 | "name": "Sindre Sorhus", 10 | "email": "sindresorhus@gmail.com", 11 | "url": "https://sindresorhus.com/" 12 | }, 13 | "type": "module", 14 | "bin": "./cli.js", 15 | "exports": "./index.js", 16 | "sideEffects": false, 17 | "engines": { 18 | "node": ">=12.20" 19 | }, 20 | "scripts": { 21 | "test": "xo && ava" 22 | }, 23 | "files": [ 24 | "index.js", 25 | "cli.js" 26 | ], 27 | "keywords": [ 28 | "cli-app", 29 | "cli", 30 | "validate", 31 | "validator", 32 | "check", 33 | "custom", 34 | "element", 35 | "elements", 36 | "html", 37 | "tag", 38 | "name", 39 | "wc", 40 | "web", 41 | "components" 42 | ], 43 | "dependencies": { 44 | "is-potential-custom-element-name": "^1.0.1", 45 | "log-symbols": "^5.0.0", 46 | "meow": "^10.0.1" 47 | }, 48 | "devDependencies": { 49 | "ava": "^3.15.0", 50 | "xo": "^0.40.2" 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /test.js: -------------------------------------------------------------------------------- 1 | import test from 'ava'; 2 | import validateElementName from './index.js'; 3 | 4 | test('returns false for `isValid` and with a `message` for invalid names', t => { 5 | t.false(validateElementName('').isValid); 6 | t.false(validateElementName('foo').isValid); 7 | t.false(validateElementName('annotation-xml').isValid); 8 | t.false(validateElementName('0-foo').isValid); 9 | t.false(validateElementName('-foo').isValid); 10 | t.false(validateElementName('foo-$').isValid); 11 | t.false(validateElementName('foo-/').isValid); 12 | t.false(validateElementName('FOO-BAR').isValid); 13 | t.false(validateElementName('foo/').isValid); 14 | t.truthy(validateElementName('foo/').message); 15 | }); 16 | 17 | test('returns true for `isValid` and without `message` for valid names', t => { 18 | t.true(validateElementName('foo-bar').isValid); 19 | t.falsy(validateElementName('foo-bar').message); 20 | t.true(validateElementName('não-tém').isValid); 21 | t.true(validateElementName('foo-bÅr').isValid); 22 | }); 23 | 24 | test('returns true for `isValid` with warnings for not recommended names', t => { 25 | t.true(validateElementName('polymer-').isValid); 26 | t.truthy(validateElementName('polymer-').message); 27 | t.true(validateElementName('x-').isValid); 28 | t.true(validateElementName('ng-').isValid); 29 | t.true(validateElementName('unicorn-').isValid); 30 | t.truthy(validateElementName('unicorn-').message); 31 | t.truthy(validateElementName('unicorn-ø').message); 32 | t.truthy(validateElementName('uni--corn').message); 33 | t.truthy(validateElementName('uni-----corn').message); 34 | t.truthy(validateElementName('uni-co___rn').message); 35 | t.false(validateElementName('øl-unicorn').isValid); 36 | t.truthy(validateElementName('øl-unicorn').message); 37 | t.true(validateElementName('uni-co.rn').isValid); 38 | t.truthy(validateElementName('uni-co.rn').message); 39 | t.true(validateElementName('uni-corné').isValid); 40 | t.truthy(validateElementName('uni-corné').message); 41 | t.true(validateElementName('xml-unicorn').isValid); 42 | t.truthy(validateElementName('xml-unicorn').message); 43 | t.truthy(validateElementName('foo-💩').message); 44 | }); 45 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | import isPotentialCustomElementName from 'is-potential-custom-element-name'; 2 | 3 | // https://html.spec.whatwg.org/multipage/scripting.html#valid-custom-element-name 4 | const reservedNames = new Set([ 5 | 'annotation-xml', 6 | 'color-profile', 7 | 'font-face', 8 | 'font-face-src', 9 | 'font-face-uri', 10 | 'font-face-format', 11 | 'font-face-name', 12 | 'missing-glyph' 13 | ]); 14 | 15 | function hasError(name) { 16 | if (!name) { 17 | return 'Missing element name.'; 18 | } 19 | 20 | if (/[A-Z]/.test(name)) { 21 | return 'Custom element names must not contain uppercase ASCII characters.'; 22 | } 23 | 24 | if (!name.includes('-')) { 25 | return 'Custom element names must contain a hyphen. Example: unicorn-cake'; 26 | } 27 | 28 | if (/^\d/i.test(name)) { 29 | return 'Custom element names must not start with a digit.'; 30 | } 31 | 32 | if (/^-/i.test(name)) { 33 | return 'Custom element names must not start with a hyphen.'; 34 | } 35 | 36 | // https://html.spec.whatwg.org/multipage/scripting.html#prod-potentialcustomelementname 37 | if (!isPotentialCustomElementName(name)) { 38 | return 'Invalid element name.'; 39 | } 40 | 41 | if (reservedNames.has(name)) { 42 | return 'The supplied element name is reserved and can\'t be used.\nSee: https://html.spec.whatwg.org/multipage/scripting.html#valid-custom-element-name'; 43 | } 44 | } 45 | 46 | function hasWarning(name) { 47 | if (/^polymer-/i.test(name)) { 48 | return 'Custom element names should not start with `polymer-`.\nSee: http://webcomponents.github.io/articles/how-should-i-name-my-element'; 49 | } 50 | 51 | if (/^x-/i.test(name)) { 52 | return 'Custom element names should not start with `x-`.\nSee: http://webcomponents.github.io/articles/how-should-i-name-my-element/'; 53 | } 54 | 55 | if (/^ng-/i.test(name)) { 56 | return 'Custom element names should not start with `ng-`.\nSee: http://docs.angularjs.org/guide/directive#creating-directives'; 57 | } 58 | 59 | if (/^xml/i.test(name)) { 60 | return 'Custom element names should not start with `xml`.'; 61 | } 62 | 63 | if (/^[^a-z]/i.test(name)) { 64 | return 'This element name is only valid in XHTML, not in HTML. First character should be in the range a-z.'; 65 | } 66 | 67 | if (name.endsWith('-')) { 68 | return 'Custom element names should not end with a hyphen.'; 69 | } 70 | 71 | if (/\./.test(name)) { 72 | return 'Custom element names should not contain a dot character as it would need to be escaped in a CSS selector.'; 73 | } 74 | 75 | if (/[^\u0020-\u007E]/.test(name)) { 76 | return 'Custom element names should not contain non-ASCII characters.'; 77 | } 78 | 79 | if (/--/.test(name)) { 80 | return 'Custom element names should not contain consecutive hyphens.'; 81 | } 82 | 83 | if (/[^a-z\d]{2}/i.test(name)) { 84 | return 'Custom element names should not contain consecutive non-alpha characters.'; 85 | } 86 | } 87 | 88 | export default function validateElementName(name) { 89 | const errorMessage = hasError(name); 90 | 91 | return { 92 | isValid: !errorMessage, 93 | message: errorMessage || hasWarning(name) 94 | }; 95 | } 96 | --------------------------------------------------------------------------------