ezyang / ezyang/htmlpurifier

Using addElement to add support for unsupported elements does not appear to be consistent

Open
#435 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
PHP
Stars
3.4k
Forks
361
Avg merge
3d 41m
Merged PRs (30d)
2

Description

Hello, I am trying to add support for some additional elements (many of them HTML5) that are not supported by default. To do this, I followed the customize documentation here: http://htmlpurifier.org/docs/enduser-customize.html
However, I am seeing inconsistent results when trying to add elements in this way.

Below is an example that demonstrates my point.
I am using the latest version 4.18.0
My basic setup looks like this:

$config = HTMLPurifier_Config::createDefault();
$config->set('HTML.DefinitionID', 'html5-definitions');
$config->set('HTML.DefinitionRev', 1);
if ($def = $config->maybeGetRawHTMLDefinition()) {
    $def->addElement('figure', 'Block', 'Flow', 'Common');
    $def->addElement('figcaption', 'Inline', 'Flow', 'Common');
    $def->addElement('article', 'Block', 'Flow', 'Common');
}

$purifier = HTMLPurifier::instance($config);
$purifiedData = $purifier->purify($html);

The raw HTML I am testing with looks like:

<figure>test</figure>
<figcaption>test</figcaption>
<article>
    <h1>Test article heading</h1>
</article>

After running this HTML through the purifier, this is what I see being returned:

<figure>test</figure>
<figcaption>test</figcaption>
    <h1>Test article heading</h1>

As you can see, the figure and figcaption elements are not stripped as expected. However, article is still being removed.
How am I meant to add support for this? Is something in my config incorrect?
I have also tested with other elements like input, label, header, footer, section, etc and got the same results.

Is my only option trying to use another library with more HTML5 support like https://github.com/xemlock/htmlpurifier-html5 ?

EDIT:
After further testing, I have found that this config works and does not remove the article element:

$def = $config->getDefinition('HTML', true);
$def->addElement('figure', 'Block', 'Flow', 'Common');
$def->addElement('figcaption', 'Inline', 'Flow', 'Common');
$def->addElement('article', 'Block', 'Flow', 'Common');

However, I'm still confused on why it didn't work with my prior config as that is what is stated in the docs.

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the documented customization flow and the provided PHP example, comparing maybeGetRawHTMLDefinition() with getDefinition('HTML', true). Run the example using HTMLPurifier 4.18.0 and check why figure, figcaption, and article are handled differently. Done means the behavior is consistent or the customization documentation clearly explains the distinction.

Written by the indexing model from the issue text.

Assessment

Tech stack
php
Domain
security
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.