40

If you have a snippet in a StackOverflow post and you hit the Tidy button, the result is not always equivalent to the original!

Take this snippet for instance

<input name = textbox>

Then if you hit Tidy, the result is as follows.

<input name=t extbox>

That is, whenever an attribute value is unquoted and is preceded by one or more spaces, tidy inserts a space after the first letter of the value.

(Note that while unquoted attribute values are not recommended, they're perfectly valid HTML, as is using spaces around the = sign.)

Can this be corrected?

9
  • 25
    Of course, this argues for always quoting your attributes in HTML... :). Still a bug though. Commented Sep 28, 2015 at 15:11
  • Only happens when there is a space after the = and the attribute value isn't quoted. e.g. <input name= textbox>
    – DavidG
    Commented Sep 28, 2015 at 16:41
  • 2
    Actually, the expected result is wrong. It should tidy the code to <input name="" textbox>. Any value with a space, after the =, is an attribute (when unquoted). The tidy function is trying too hard to remove textbox from being an attribute to being a value, when it should give it just a pair of quotes (to indicate an empty attribute). Consider this: <input type=checkbox checked= disabled>. What do you expect? I expect checked to have an empty string, as it should be parsed. That example should produce <input type="checkbox" checked="" disabled>. Commented Sep 29, 2015 at 10:35
  • 3
    @IsmaelMiguel Your assumption about spaces is wrong. spaces around = signs are valid in HTML! attr=value means exactly the same as attr= value. See the official parsing rules.
    – Mr Lister
    Commented Sep 29, 2015 at 10:44
  • @MrLister Spaces are attribute separators. Commented Sep 29, 2015 at 10:46
  • @MrLister I can't make sense out of that link Commented Sep 29, 2015 at 10:50
  • 5
    The link says 1) skip preceding spaces (and tabs etc), 2..5) read the attribute name, 6) skip spaces 7..8) read the =, 9) skip spaces, 10..12) read the value.
    – Mr Lister
    Commented Sep 29, 2015 at 10:53
  • 2
    @MrLister: The rules on the page you linked to are specifically about the pre-parsing step of determining the character encoding (which can require a preliminary parsing step to sniff for <meta> tags, hence the inclusion of those pseudo-parsing rules there). That said, the actual HTML attribute syntax does also clearly permit whitespace around the = sign. Commented Nov 7, 2015 at 14:27
  • 4
    <input name = textbox> may be valid but I am consumed by physical revulsion when I look at it. Maybe the Tidy engine feels the same? Commented Nov 8, 2015 at 16:36

1 Answer 1

3

Sorry, declined.

While it may be strictly "legal", using unquoted attributes is widely recognized as a bad practice, and can lead to nothing but problems. Code snippets (and all code blocks, for that matter) in Stack Overflow are used, amongst other things, as a learning resource by beginner developers; therefore it would be a good idea to try and post proper code whenever possible.

So considering that, while strictly speaking this is indeed a bug, we are not going to invest time in fixing it. Consider that bug kind of a "punishment" for not following best practices. :-)

1

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .