htacg / tidy-html5

The granddaddy of HTML tools, with support for modern standards
http://www.html-tidy.org
2.72k stars 419 forks source link

No way to get past complaints about <style> outside of <head> #730

Open petdance opened 6 years ago

petdance commented 6 years ago

Here's an HTML file with <style> tag outside of the <head>.

$ cat -n foo.html
     1  <!DOCTYPE html>
     2  <html>
     3      <head>
     4          <title> style outside of the head </title>
     5      </head>
     6      <body>
     7          <style>
     8              foo { text-align: center }
     9          </style>
    10          <p class="foo">
    11              This should be centered
    12          </p>
    13      </body>
    14  </html>

Tidy complains about it.

$ tidy -q -e foo.html
line 7 column 9 - Warning: moved <style> tag to <head>! fix-style-tags: no to avoid.

So I do what tidy says and it complains a different complaint.

$ tidy -q -e --fix-style-tags no foo.html
line 7 column 9 - Warning: found <style> tag in <body>! fix-style-tags: yes to move.

What can I do to make tidy happy about this situation?

geoffmcl commented 6 years ago

@petdance yes, I think this is a bug, and needs to be fixed

This seems yet another case where tidy needs to support the current HTML5 spec...

I can read in style docs <style> can be used In the body, where flow content is expected.... although there are some warning type notes about it...

One quick patch I experimented with was -

diff --git a/src/clean.c b/src/clean.c
index e96dd3f..8023616 100644
--- a/src/clean.c
+++ b/src/clean.c
@@ -2747,7 +2747,7 @@ static void StyleToHead(TidyDocImpl* doc, Node *head, Node *node, Bool fix, int
                TY_(InsertNodeAtEnd)(head, node);   /* add to end of head */
                TY_(Report)(doc, node, head, MOVED_STYLE_TO_HEAD); /* report move */
            }
-           else
+           else if (!TY_(IsHTML5Mode)(doc)) /* Is. #730 - style allowed in body */
            {
                TY_(Report)(doc, node, head, FOUND_STYLE_IN_BODY);
            }

But maybe this is not the complete story... because this would allow it to be the child of anything... maybe there needs to be more checking... although, with --fix-style-tags no it does fix the sample given...

Note such a fix may also mean adjusting, changing the fix-style-tags documentation, or its yes default, and/or the MOVED_STYLE_TO_HEAD and the FOUND_STYLE_IN_BODY messages...

Look forward to feedback, patches, or PR... thanks...

petdance commented 6 years ago

I don't have any feedback one way or the other. I would just expect that if you turn on one flag, it wouldn't suggest you turn on a different one.