Skip to content

Instantly share code, notes, and snippets.

@rotty3000
Last active March 11, 2022 19:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rotty3000/537e9218ad4792258ad0711fab8b5f56 to your computer and use it in GitHub Desktop.
Save rotty3000/537e9218ad4792258ad0711fab8b5f56 to your computer and use it in GitHub Desktop.
Validate Web Component Custom Element name with bash and grep
#!/bin/bash
# Derived from https://html.spec.whatwg.org/multipage/custom-elements.html#valid-custom-element-name
PCENCHAR=[-.0-9_a-z\\xB7\\xC0-\\xD6\\xD8-\\xF6\\xF8-\\x{037D}\\x{037F}-\\x{1FFF}\\x{200C}-\\x{200D}\\x{203F}-\\x{2040}\\x{2070}-\\x{218F}\\x{2C00}-\\x{2F2F}\\x{3001}-\\x{D7FF}\\x{F900}-\\x{FDCF}\\x{FDF0}-\\x{FFFD}\\x{10000}-\\x{EFFFF}]
CUSTOM_ELEMENT_NAME_REGEX=^[a-z]${PCENCHAR}*-${PCENCHAR}*$
if ! (echo "$1" | grep -q -P $CUSTOM_ELEMENT_NAME_REGEX); then
echo "invalid custom element name '$1'"
fi
@rotty3000
Copy link
Author

I find it strange that a-, a--, etc, are supported.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment