htmlbeautifier
htmlbeautifier copied to clipboard
Allow custom elements
We use lookbook which relies on htmlbeautifier to output pretty HTML and we have some custom block elements (such as <turbo-frame>
) which are not rendered indented.
This PR replace the constants from HtmlBeautifier::HtmlParser
by methods which allows to specify additional elements:
HtmlBeautifier::HtmlParser.block_elements << "main"
HtmlBeautifier::HtmlParser.block_elements << "turbo-frame"
HtmlBeautifier::HtmlParser.block_elements << "whatever"
The downside is that mappings is compiled each time HtmlParser is initialized. If this is an issue, I guess I can add a method to compile and cache the mappings only once.
I've add tests in a second commit but:
- I'm not sure it's relevant to add tests because it's mostly a refactor which doesn't change current behaviors.
- It modify the block_elements globally
Sorry for neglecting this for so long! I think this is a good feature, but I'm not keen on the class attributes. I think I'd prefer to have these as attributes of the particular instance of HtmlParser.
Perhaps the best implementation would be to expose the list of elements, and let you inject a parser, so that you could then subclass the parser with whatever elements you like. I'll think about it.