threedaymonk / htmlbeautifier

A normaliser/beautifier for HTML that also understands embedded Ruby. Ideal for tidying up Rails templates.
MIT License
325 stars 59 forks source link

Conditional Comments - Indentation incorrect #3

Closed paulmsmith closed 11 years ago

paulmsmith commented 11 years ago

Hi,

Love this gem. Really simple to use. I'm bolting on to Middleman, at the end of its build I run this on all the HTML Files. Works a treat apart from when it hits IE-Conditional comments.

This:

<!DOCTYPE html>
<!--[if lt IE 7]><html lang="en-us" class="ie6"><![endif]-->
<!--[if IE 7]><html lang="en-us" class="ie7"><![endif]-->
<!--[if IE 8]><html lang="en-us" class="ie8"><![endif]-->
<!--[if gt IE 8]><!--><html lang="en-us"><!--<![endif]-->

becomes:

<!DOCTYPE html>
<!--[if lt IE 7]>
    <html lang="en-us" class="ie6">
    <![endif]-->
    <!--[if IE 7]>
        <html lang="en-us" class="ie7">
        <![endif]-->
        <!--[if IE 8]>
            <html lang="en-us" class="ie8">
            <![endif]-->
            <!--[if gt IE 8]>
                <!-->
                <html lang="en-us">
                <!--<![endif]-->

Anyway to have it ignore/skip conditional comments? I'm not a ruby dev otherwise I'd have tried patching myself.

threedaymonk commented 11 years ago

I've committed a possible fix to a branch. Please try it out and let me know how you get on! You'll need to put this in your Gemfile:

gem "htmlbeautifier", git: "https://github.com/threedaymonk/htmlbeautifier.git", branch: "ie-cc"
paulmsmith commented 11 years ago

Brilliant and thank you for the swift response. That is vast improvement! Ace!

I'm planning on releasing a framework for front-end development based on middleman with your gem as a requirement.

If you have anymore spare time bud. I wonder if you can fix this? Maybe not, I'm not sure as I have a broken understanding of how you clever chaps write gems and require in other libs, etc. So this might be out of your hands.

Now that its ignoring the conditional comments the indentation is much better however because the conditional comments I'm using (as per HTML5 Boiler plate) are essentially wrapping the HTML element in a comment. I think the tidy/html-parser is tripping up and doesn't know how to handle the remaining indentation....it does a decent enough job but I'm assuming once it's gone ary with that HTML tag it doesn't know where things should stop being indented.

See this mark-up - before and after.

Original: https://gist.github.com/paulmsmith/85d9758341ed849e2512 Beautified: https://gist.github.com/paulmsmith/541a297ef294a3ed6ef1

Notice how the beautified mark-up is nice and tidy but not quite indented correctly. It would be amazing if that was fixed/resolved.

What its doing now is a brilliant improvement but thought I'd ask. Thanks again for the super fast turn around on this!

threedaymonk commented 11 years ago

From a brief look at that, it appears to me that the incorrect nesting is happening due to standalone tags like <meta ...>. Do you concur? There is a solution to that, I think, but I don't think it's quick. I wrote the code with the assumption that it would be used with XHTML. To handle <meta ...> without a final />, the parser would need to know which tags can be standalone and to handle them separately.

paulmsmith commented 11 years ago

Ah you might be right sir. Maybe one for your to-do list if you ever get the time or motivation. I can change my mark-up so its XHTML style for now.

Thanks again for the response and the gem.

paulmsmith commented 11 years ago

Just confirm, updating the mark-up to XHTML style self closing tags has corrected things. I actually prefer that style of mark-up anyway, just when using code completion in certain editors it doesn't always do it.

threedaymonk commented 11 years ago

That's good to hear. I think I'll close this issue, then; please do open an issue for non-XHTML self-closing tag support if you think it would be useful.