Polyglot markup

In computing, a polyglot markup is a document or script written in a valid form of multiple markup languages, which performs the same output, independent of the markup's parser, layout engine, or interpreter. In general, the polyglot markup is a common subset of two or more languages, that can be used as a robust or simplified profile.

Polyglot HTML is HTML that has been written to conform to both the HTML and XHTML specifications.[1] A polyglot document can therefore be parsed as either HTML (which is SGML-compatible) or XML, and will produce the same DOM structure either way. For example, in order for an HTML5 document to meet these criteria, the two requirements are that it must have an HTML5 doctype, and be written in well-formed XHTML.[2] The same document can then be served as either HTML or XHTML, depending on browser support and MIME type.

Polyglot HTML requirements

As expressed by the html-polyglot recommendation,[1] to write a polyglot HTML5 document, the following key points should be observed:

  1. Processing instructions and the XML declaration are both forbidden in polyglot markup
  2. Specifying a document’s character encoding
  3. The DOCTYPE
  4. Namespaces
  5. Element syntax (i.e. End tags are not optional. Use self-closing tags for void elements.)
  6. Element content
  7. Text (i.e. pre and textarea should not start with newline character)
  8. Attributes (i.e. Values must be quoted)
  9. Named entity references (i.e. Only amp, lt, gt, apos, quot)
  10. Comments (i.e. Use <!-- syntax -->)
  11. Scripting and styling polyglot markup

The most basic possible polyglot markup document would therefore look like this:[1]

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" lang="" xml:lang="">
  <head>
    <title>The title element must not be empty.</title>
  </head>
  <body>
  </body>
</html>

In a polyglot markup document non-void elements (such as script, p, div) cannot be self-closing even if they are empty, as this is not valid HTML.[3] For example, to add an empty textarea to a page, one cannot use <textarea/>, but has to use <textarea></textarea> instead.

See also

References

This article is issued from Wikipedia. The text is licensed under Creative Commons - Attribution - Sharealike. Additional terms may apply for the media files.