Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Re: Dynamically cleaning up HTML fragments

by halfcountplus (Hermit)
on Sep 24, 2010 at 00:13 UTC ( [id://861697]=note: print w/replies, xml ) Need Help??


in reply to Dynamically cleaning up HTML fragments

>>We should reject tags that do not have certain mandatory attibutes. No "a" without an href for example.

Local anchors do not have "href" as an attribute, they have a "name", eg <a name="local page anchor">here</a> ;)

Are you familiar with the event driven HTML::Parser? I have not used it to clean invalid source, but here's an idea (eg): when your start tag handler hits an img tag, put the entire tag text into an otherwise null global. In both the end and start tag handler, you check this global for content; if the img is not closed add / to it.

Alternately, I can tell you for a fact that HTML::Parser treats / used XHTMLishly (ie, not the first character in the tag) as an attribute. Therefore, with tags like image you can check for the / attribute and if not present, do the edit.

The rest of your requirements -- stripping certain tags, working with attributes, checking for text inside p tags, etc. -- can also easily be accomplished via HTML::Parser, but you will have to write some code to do it.

  • Comment on Re: Dynamically cleaning up HTML fragments

Replies are listed 'Best First'.
Re^2: Dynamically cleaning up HTML fragments
by Anonymous Monk on Sep 24, 2010 at 01:04 UTC
    Fragment identifiers should be id attributes, which can go on any element and are unique within the document, instead of name attributesm, which need not be unique and only belong on certain elements.
      Of course I have. However this is completely irrelevant to the question. The HTML fragments that I have in mind have no need of either "name" or "id" attributes. Of course someone else with the same general question but different specifics may require them which is why configurability is one of the criteria.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://861697]
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: (3)
As of 2024-04-20 04:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found