In the HTML specification there is a concept called custom elements. There is a definite expression to which the names of these elements should follow. But, however, after opening the editor in the browser, we can safely write elements that do not follow these rules, or simply create a simple page with elements that do not follow this rule. For example, <redcar> </redcar>
. Why is this allowed and does not cause any errors? After all, if we write something like this: <~hello> </~hello>
then the opening tag will be treated as text, and the closing tag will be commented out. In any case, you need specific links that will explain this behavior.
A valid custom element name is a sequence of characters name that meets all of the following requirements:
- name must match the PotentialCustomElementName production:
PotentialCustomElementName ::=
[a-z] (PCENChar)* '-' (PCENChar)*
PCENChar ::=
"-" | "." | [0-9] | "_" | [a-z] | #xB7 | [#xC0-#xD6] | [#xD8-#xF6] | [#xF8-#x37D] | [#x37F-#x1FFF] | [#x200C-#x200D] | [#x203F-#x2040] | [#x2070-#x218F] | [#x2C00-#x2FEF] | [#x3001-#xD7FF] | [#xF900-#xFDCF] | [#xFDF0-#xFFFD] | [#x10000-#xEFFFF]
This uses the EBNF notation from the XML specification. [XML]
- name must not be any of the following:
- annotation-xml
- color-profile
- font-face
- font-face-src
- font-face-uri
- font-face-format
- font-face-name
- missing-glyph
2 Answers
Answers 1
It's unclear what you'd consider an error.
HTML parsing is mainly oriented toward a never throw principle, and will try to convert everything to something valid.
In your specific case, what you created is an HTMLUnknownElement, and this follows the specs:
The element interface for an element with name name in the HTML namespace is determined as follows:
If name is
applet
,bgsound
,blink
,isindex
,keygen
,multicol
,nextid
, orspacer
, then return HTMLUnknownElement.If name is
acronym
,basefont
,big
,center
,nobr
,noembed
,noframes
,plaintext
,rb
,rtc
,strike
, ortt
, then return HTMLElement.If name is
listing
orxmp
, then return HTMLPreElement.Otherwise, if this specification defines an interface appropriate for the element type corresponding to the local name name, then return that interface.
If other applicable specifications define an appropriate interface for name, then return the interface they define.
If name is a valid custom element name, then return HTMLElement.
Return HTMLUnknownElement.
With <redcar></redcar>
you gone the whole way until bullet #7.
Answers 2
I agree to @Kaiido. I would just explain why <~hello> </~hello>
throws no error: a parser works eating character by character or token by token (it depends from how the programmer wants to do), but anyway, after a <
the browser expects a set of valid characters followed by a >
to declare a opening tag. If between <
and >
there is an invalid set of characters, simply it isn't a tag, so the browser parses it as a text node. Regarding the closing tag, I think probably that behavior can change among different browsers. Anyway, the one you're using simply after a <
and a /
expect necessarily a valid set of characters followed by a >
, otherwise it wouldn't consider it as a text node because of the invalid token </
joined to invalid characters, so the browser comments it since it doesn't close any tag.
0 comments:
Post a Comment