Reputation: 185
I'm using htmlPurifier to filter user generated html. I want to filter all image URLs using a specific rule set while allowing more general hyperlinks.
How can I set up an URL filter that is only applied to img.src attributes but not to a.href attributes?
I guess, I need to set up a URI filter as described in http://htmlpurifier.org/docs/enduser-uri-filter.html. But I need to somehow restrict it to img.src attributes.
If I write a filter like this, is there a way to determine whether I'm currently operating on an img.src attribute or not?
class HTMLPurifier_URIFilter_NameOfFilter extends HTMLPurifier_URIFilter
{
public $name = 'NameOfFilter';
public function prepare($config) {}
public function filter(&$uri, $config, $context) {}
}
Upvotes: 2
Views: 235
Reputation: 185
I just found the solution: The $context object contains information about the tag that is currently being processed. Thus, one can use:
class HTMLPurifier_URIFilter_MyImages extends HTMLPurifier_URIFilter {
public $name = 'MyImages';
public $post = true;
public function prepare($config) {}
public function filter(&$uri, $config, $context) {
if ($context->get('CurrentToken')->name == 'img') {
// do something with the image url here
}
return true;
}
}
Upvotes: 1