Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re: Ensuring HTML is "balanced"

by DrHyde (Prior)
on Mar 08, 2006 at 09:41 UTC ( [id://535128]=note: print w/replies, xml ) Need Help??


in reply to Ensuring HTML is "balanced"

Actually, fixing that is pretty easy. Yes, you keep a stack of opening tags that you're within, and then whenever you find a closing tag which doesn't match the top of the stack, you need to add the right closing tag and then try again - and again, and again, and again, until everything's back in sync. You also need to handle the end of the document correctly so that you automagically close anything left on the stack.

With a little more trickery (but only a little) you can take account of stuff like <HR> and <BR> not needing to be closed, that only certain tags are legal immediately inside others (eg <TR> is legal inside <TABLE> but <HR> isn't) and so on.

Even if you ignore all those special cases, you'll have a good solution.

Replies are listed 'Best First'.
Re^2: Ensuring HTML is "balanced"
by apotheon (Deacon) on Mar 09, 2006 at 14:34 UTC

    . . . except that you also need to account for closing tags that aren't nested properly. For instance, in the following example, using that simple stack approach would give you extra closing tags:

    Try <i><b>this</i></b> on for size.

    Instead of fixing improper nesting, a straight-up stack matching approach would give you this:

    Try <i><b>this</b></i></b> on for size.

    It's also probably best these days to stick to valid XHTML, which means that all tags get closed (for instance, use <hr /> instead of <hr>).

    print substr("Just another Perl hacker", 0, -2);
    - apotheon
    CopyWrite Chad Perrin

      Detecting that error is so trivial that I thought it not worthy to mention.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://535128]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (4)
As of 2024-04-20 00:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found