HTML markup: space between element attributes

I want to remove the extra bytes from my output, and it seems acceptable (in practice) to strip something that can add quite a lot of whitespace from HTML markup by omitting / clearing the gaps between DOM element attributes.

Although I've tested and researched (a bit in both cases), I'm wondering how safe is it?

I've tested in Chrome (43.0.2357.65 m)

, IE (11.0.9600.17801)

, FF (38.0.1)

and Safari (5.1.7 (blah-di-blah))

and they didn't seem to mind and couldn't find anything special in the Specs about whitespace between attributes.

The w3.org Validator complains that it is strong evidence that this is unsafe and should not be expected to work, but (there is always a "but"), perhaps the space requirement is strict if there are no quotes (for obvious reasons).
Also (snippy but edgy): their SSL is "out of date", which doesn't inspire confidence in their opinion.

I also noted that someone HTML compressor could (when enabled) use quotes around attribute values ​​where those values ​​did not contain spaces inside them (for example id

), which implies that at least most if not all HTML parsing, focus on the text on both sides of the equal signs (except, of course, booleans), and where quotes are used, they will be considered a priority delimiter.

So it would be:

<!DOCTYPE html><html><body>
<a href="http://example.org"target="_blank"title="This is a test">Yabba Dabba Doo!</a>
</body></html>
      

Run codeHide result


β–² What ever goes wrong, and if so, under what conditions?

What other reasons could there be for keeping this gap in a production release ("code readability" is not an issue in this case)?

Update (since looking for an answer):

While I have basically answered my own question, since there is a specification that defines whether there should be space between attributes, I still wonder if it is practically safe to exclude them when using quoted values, and would appreciate some feedback on this.

Given how often whitespace can be omitted accidentally in HTML production, and that the browsers I've tested don't seem to mind when they are, I guess it would be very rare if ever a browser was unable to process documents with those whitespace omitted.

While it is prudent to follow specs in almost all situations, maybe it could be a one-time scam that might be acceptable?

After all, if we can magically save a few hundred bytes without affecting the output quality, why not?

+3


source to share


2 answers


There is a specification (after all)

It turns out I should have looked more complicated. My bad one.

According to these specifications :

If an attribute using empty attribute syntax is to be followed by another attribute, then there must be a white space separating the two .

and

If an attribute using unquoted syntax is to be followed by another attribute or the optional U + 002F SOLIDUS (/) character permitted in step 6 of the start tag syntax above, then there must be a space separating the two .



and

If an attribute using single-quoted syntax is to be followed by another attribute, then there must be a white space separating the two .

and

If an attribute using double-quoted syntax is to be followed by another attribute, then there must be a space separating the two .

Which, if I'm not mistaken (again) means there should always be between attributes .

+1


source


You can try online HTML minifiers like http://www.whak.ca/minify/HTML.htm or http://www.scriptcompress.com/minify-HTML.htm (google search for more) and find little things, which they change for tooltips that can be retrieved but still display HTML.

In the first link, your code:

<!DOCTYPE html><html><body> <a href="http://example.org"target="_blank"title="This is a test">Yabba Dabba Doo!</a> </body></html>



Included in:

<!DOCTYPE html><html><body><a href=http://example.org target=_blank title="This is a test">Yabba Dabba Doo!</a>

save you 18 bytes ...

0


source







All Articles