Вопрос

In general, can we consider an HTML document that is initially invalid but eventually becomes technically valid (through scripting) to be OK?

"OK" would be per good practices, or possibly with regard to the standard, if it answers this question.

For example, the document that results from parsing this serialized HTML markup validates initially using the W3 validator:

<!DOCTYPE html>
<title>foo</title>
bar

While this one doesn't:

<!DOCTYPE html>
<script>document.title = 'foo'</script>
bar

And that is even though the result is exactly the same for any browser that supports Javascript. Assuming this is a web application and JS is required, is this kind of thing "OK"?

I especially wonder about this situation when we don't have any correct (from an application perspective) way of satisfying the standard initially. For example, what if we don't know the title of the document initially, and must compute/retrieve it using a script?

In this particular case, using a placeholder feels wrong:

<!DOCTYPE html>
<title>placeholder</title>
<script>document.title = 'foo'</script>
bar

(Note that leaving the title element empty is still considered invalid.)

So, without debating too much about the title element in particular, is it generally accepted to distribute HTML resources that are only eventually valid?

Subquestion: I realize that validating the document (as represented by the DOM) and validating its serialized markup are two different things; are there any tools to do the former? (Either from a snapshot of the DOM or "continuously".) Example:

<!DOCTYPE html>
<title>foo</title>
<script>document.title = ''</script>
bar

This validates initially but technically results in an invalid document, without any obvious way to detect it.

Update: Obviously such a tool would have limited value in a static analysis context (halting problem, etc). A runtime tool should be useful however.

Update: W3C spec for DOM validation (Level 3).

Update: W3C spec for Service Workers, which seems like it could be used to ensure a DOM is valid before being rendered, even if the template is not (avoiding the use of placeholder elements and such). It's still early at the time of this writing (June 26th 2014, so don't quote me on that).

Это было полезно?

Решение

An invalid HTML document is not ok, even if you make it valid with scripts for various reasons.

HTML documents are validated and parsed as it's loading, before scripts are being run. So even if you "fix" your document with scripts, there's a high probability your document will be funky. Even if you generate all content in a script, a lot of browsers will not accept.

Besides, if you care about search engines, please remember that:

  • Search engines consider invalid HTML a bad thing and will affect your ranking
  • Scripts are not evaluated or parsed whatsoever, your HTML document will remain invalid

Last but not least, don't forget the small amount of clients that have scripts disabled.

Другие советы

i don't see how you want to decide whether a document 'eventually becomes technically valid'. scripting code may refer to arbitrary contextual state (existing dom, user client info, localstorage, user input, web request responses url). most of this info is inaccessible beforehand to the validator.

your scheme might work with a very restricted subset of scripting instructions but i can't see how that could be of any use in real life applications.

so, no, i don't think that the concept of eventual validation is sound. documents that do not validate intially shouldn't be considered ok.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top