NAME HTML::Acid - Reformat HTML fragment to strict criteria VERSION This document describes HTML::Acid version 0.0.3 SYNOPSIS use HTML::Acid; my $acid = HTML::Acid->new; return $acid->burn($html) DESCRIPTION Fragments of HTML returned by a rich text editor tend to be not entirely standards compliant. `img' tags tend not to be closed. Paragraphs breaks might be represented by double `br' tags rather than `p' tags. Of course we also need to do all the XSS avoidance an HTML clean up routine would, such as controlling `href' tags, removing javascript and inline styling. Furthermore what one often wants is not simply a standards compliant cleaned up version of the input HTML. Sometimes one wants to know that the HTML conforms to a much tighter standard, as then it will be easier to style. So this module, given a fragment of HTML, will rewrite it into a very restricted subset of XHTML. The default dialect has the following properties. * Documents consist entirely of `p' elements and `h3' elements. * Every header will have `id' attribute automatically generated from the header contents. * Every paragraph may consist of text, `a' elements, `img' elements, `strong' and `em' elements. * Anchors must have an `href' attribute referring to an internal URL. They may also have a `title' attribute. * Images must have `src', `title', `alt', `height' and `width' attributes. The `src' attribute must match the same regular expression as `href'. If any of these tags are missing the image is replaced by the contents of the `alt' attribute, so long as it consists only of alphanumeric characters, spaces, full stops and commas. Otherwise the image is removed. * All other tags must have no attributes and may only contain text. * Double `br' elements in the source will be interpreted as paragraph breaks. INTERFACE new This constructor takes a number of optional named parameters. *url_regex* This is a regular expression that controls what `href' and `src' tags are permitted. It defaults to an expression that restricts access to internal absolute paths with an optional sub-reference. *tag_hierarchy* This is a hash reference that for each supported tag specifies what the containing tag must be. Standards based HTML is not as strict as this. This defaults to the value returned by the `default_tag_hierarchy' method. *img_height_default* If set this creates a default height value for all images. If not set images without height attributes will be rejected. *img_width_default* If set this creates a default width value for all images. If not set images without width attributes will be rejected. *text_manip* If set this must be subroutine reference. It takes text (and the `alt' attribute from invalid images) and what is returned will be used instead. *text_container* If set this must be subroutine reference. It takes the `alt' (modified by *text_manip* if present) and returns what would be used in the event of an invalid image. burn This method takes the input HTML as an input and returns the cleaned up HTML. default_tag_hierarchy This is a class method that returns the default tag hierarchy. So if you want to add support for a tag you can use a modified copy of the output when setting up the HTML::Acid instance. The default mapping is as follows: { h3 => '', p => '', a => 'p', img => 'p', em => 'p', strong => 'p', } Mapping an element onto the empty string implies that the element appears at the top-level of an HTML fragment. So for example h3 => '', p => '', implies that
can be at the top of the document fragment.
Mapping onto another element implies that the element must always be
contained within that element. So
a => 'p',
img => 'p',
em => 'p',
strong => 'p',
implies that , , and must be within a element.
It is also possible to specify alternatives:
img => ['p','a'],
which implies that can be within a