Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there such a thing as a valid HTML5 fragment?

I obviously can't determine whether a fragment of HTML is valid without knowing what the rest of the document looks like (at a minimum, I would need a doctype in order to know which rules I'm validating against). But given the following HTML5 fragment:

<article><header></article>My header</header><p>My text</p></article>

I can certainly determine that it is invalid without seeing the rest of the document. So, is there such a thing as "provisionally valid" HTML, or "valid providing it fits into a certain place in a valid document"?

Is there more to it than the following pseudocode?

def is_valid_fragment(fragment):
 tmp = "<!doctype html><html><head><title></title></head><body>" + fragment + "</body></html>"
 return my_HTML5_validator.is_valid_html5_document(tmp)
like image 748
jl6 Avatar asked Nov 09 '10 12:11

jl6


People also ask

What are HTML fragments?

HTML fragments have a similar syntax as HTML views, but without the <template> tag. You can define a simple HTML fragment like this:   <div data-sap-ui-type="sap.m.Button" data-press="doSomething" data-text="Hello World"></div> The fragment is stored as …/my/useful/UiPartZ.

How do you use an HTML fragment?

To create a new HTML Fragment:Paste your HTML code into the large HTML Code box. Choose between the 'Manual', 'Head' and 'Body' types. Click the Add HTML Fragment button. You will be returned to the HTML Fragments screen where your new fragment will now be listed.


1 Answers

You can certainly talk about an XML document weing well-formed, and you can construct a document from any single element and its children. You could thus talk about singly-rooted XHTML5 fragments being well-formed. You could deal with a multiply-rooted fragment (like <img/><img/>) by dealing with it as a sequence of documents, or wrapping it in some synthetic container element - since we're only talking about well-formedness, that would be okay.

However, HTML5 still allows the SGML self-closing tags, like <hr> and so on, whose self-closingness can only be determined by appeal to the doctype. For instance, <div><hr></div> is okay, but <div><tr></div> is not. If you were dealing with DOM nodes rather than text as input, this would be a nonissue, but if you have text, you'd need a parser which knows enough about HTML to be able to deal with those elements. Beyond that, though, some very simple rules, lifted directly from XML, would be enough to handle well-formedness.

If you wanted to go beyond well-formedness and look at some aspects of validity, i think you can still do that at the singly-rooted fragment level with XML. As the spec says:

An XML document is valid if it has an associated document type declaration and if the document complies with the constraints expressed in it.

A DTD can name any element as the root, and the mechanics then take care of checking the relationship between that element and its children, and their children and so on, and the various other constraints that make up validity.

Again, you can transfer that idea directly to HTML. I don't know how you deal with multiply-rooted fragments, though. And bear in mind that certain whole-document constraints (like IDs being unique) might hold inside the fragment, but not in an otherwise valid document once the fragment has been inserted into it.

like image 120
Tom Anderson Avatar answered Sep 29 '22 12:09

Tom Anderson