How do user agents handle unrecognized HTML elements?

I tried to find the answer to this in the W3C HTML specs, but still no luck.

For example, if I have the following HTML code:

<body> <p> <foo>bar</foo> </p> </body> 

Does the W3C indicate how the user agent should be handled? For example, if the element "foo" is completely ignored? Should the element "foo" be ignored, but the string "bar" is parsed?

Also, is it even β€œlegal” for this?

Edit: Great answers from all of you! I fully agree that it would be bad practice to implement common XML if, if possible, if you have full control over which browser your users will use. I was mostly curious what really would or should happen if such markup was to be made :-)

+6
source share
3 answers

Great tips from @Andy E. These are just some of the additions to this.

An HTML5 project defines how to parse unknown elements, however it is clearly non-trivial. To see the rules, see http://dev.w3.org/html5/spec/tree-construction.html

Note that the first version of Firefox to use these rules is FireFox 4, and the first version of IE to use the rules is IE 10. In older versions, there are several different and often very strange behaviors.

HTML has no concept of "legitimacy", only validity or compliance with the standard. You can decide whether you want your pages to conform to a specific standard or not. There is no W3C HTML standard where the use of arbitrarily named elements is consistent.

In general, it is recommended that you create your HTML code to avoid unpredictable errors in browsers and other HTML consumers that you have not tested with.

+2
source

The HTML specification does not say this except :

The HTMLUnknownElement interface should be used for HTML elements that are not defined by this specification (or other applicable specifications).

This can be verified according to browsers using the following JavaScript code in the console:

 Object.prototype.toString.call(document.createElement("foo")); //-> "[object HTMLUnknownElement]" 

However, some browsers either do not follow the specification given here. For example, Chrome 13 provides [object HTMLElement] , IE 8 gives [object HTMLGenericElement] (IE 9 is true).

As far as I know, all browsers will parse <foo> as an element, but the default style and behavior are not guaranteed to be the same. Where HTMLUnknownElement is implemented and the specification is respected, it must be inherited directly from HTMLElement and, therefore, has many default properties found for other elements.

Please note that your HTML will not check when you have non-standard elements in your markup. It is also worth mentioning that search robots, screen readers and other software will not be able to extract semantic meaning from these elements.

Further reading:

+5
source

"bar" must be provided. For example, in an HTML5 video element, the content of the element contains fallback content that will be displayed in older browsers for this reason. This is also why people traditionally put comments around style declarations:

 <style><!-- (styling goes here) --></style> 

to hide style information from pre HTML4 browsers. (I think comments are no longer considered good practice.)

+2
source

Source: https://habr.com/ru/post/897607/


All Articles