Welcome! » Log In » Create A New Profile

Altering the Strategy

Posted by bago 
Altering the Strategy
December 15, 2008 07:53AM

I see that HTMLPurifier uses the strategy pattern for the main strategy sequence and the base strategy is a composite strategy "Core" that hardcode the following strategies: RemoveForeignElements, MakeWellFormed, FixNesting, ValidateAttributes.

I have use cases where I only need to run MakeWellFormed AND ValidateAttributes and I already checked that changing the "Core" strategy fix my issue.

I looking into the code but it seems there is no way to alter the strategy because strategy is set during initialization and it is a private property.

Am I missing anything?

Is it possible to add some way to alter the strategy object without altering the core htmlpurifier code?

Re: Altering the Strategy
December 15, 2008 04:02PM

Extend HTMLPurifier and modify the constructor to pass your own strategy.

If you find any bugs running MakeWellFormed w/o RemoveForeignElements please report them; HTML Purifier's supposed to work without it, but it may not be perfect. Also this modified strategy will not be XSS safe or standards compliant, and, to boot, validate attributes will nuke some attributes that you might want to keep.

Your Email:


HTML input is enabled. Make sure you escape all HTML and angled brackets with < and >.

Auto-paragraphing is enabled. Double newlines will be converted to paragraphs; for single newlines, use the pre tag.

Allowed tags: a, abbr, acronym, b, blockquote, caption, cite, code, dd, del, dfn, div, dl, dt, em, i, ins, kbd, li, ol, p, pre, s, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, var.

For inputting literal code such as HTML and PHP for display, use CDATA tags to auto-escape your angled brackets, and pre to preserve newlines:

Place code here

Power users, you can hide this notice with:

.htmlpurifier-help {display:none;}