HTML Tidy - add opening tags instead of removing closing tags?
I basically agree with Sky Sanders' answer. Besides:
You would expect a simple neat parser to do the pre-intent.
You can write a parser that provides the described functionality simply without invoking any intent, working only deterministic. It is easy (yes, more or less easy :)) to write an algorithm that does the job. The idea was as follows:
Adding closing tags
After all, this can be done with HTML Tidy already, and every browser / parser does it implicitly already (I'm not talking about valid XHTML here):
<div>some <span><em>text</span> here</div>
gets
<div>some <span><em>text</em></span> here</div>
Adding opening tags
Now we can go and do some algorithm that parses the following, starting at the end of the string and searching backwards:
<div>some <span>text</em></span> here</div>
to create the next one as it sees the tag em
is embedded within the tag span
.
<div>some <span><em>text</em></span> here</div>
Combining these two
Now we need to write an algorithm that does how to add the missing closing and opening tags. Now, let's grab this html snippet:
<div>some <span>text</em> here</div>
First, apply the "add all missing end tags" method:
<div>some <span>text</em> here</span></div>
This algorithm assumes that every close and open tag that appears after <span>
is embedded in span
. It stops only if it sees an end tag for some start tag that came before <span>
. In this case, it is the </div>
one that had a valid start tag <div>
before. Then apply the same semantics in reverse search as described above:
<div>some <span><em>text</em> here</span></div>
et voila.
Does it all make sense?
In my opinion: No. It's technically possible, but not worth the effort. You would have to implement your own parser along with these pseudo-smart methods described above. Also, this will apply semantics to the html, which doesn't exist anyway: each browser / parser just ignores the individual closing tags, so why would you want to pay attention to them?
If I hadn't been able to convince you yet, consider the html semantics:
some <b>text</b> here
reads: "type" some. start rendering in bold. type "text". stop bold. print 'here'. "
While:
some text</b> here
reads like "type" some text. stop bold. " "What? I haven't even started visualizing anything bold!" I'll just ignore it ... ":)
a source to share