Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 766807
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T17:06:27+00:00 2026-05-14T17:06:27+00:00

Premise I’d like to use HTML Purifier to transform <body> tags to <div> tags,

  • 0

Premise

I’d like to use HTML Purifier to transform <body> tags to <div> tags, to preserve inline styling on the <body> element, e.g. <body style="background:color#000000;">Hi there.</body> would turn to <div style="background:color#000000;">Hi there.</div>. I’m looking at a combination of a custom tag and a TagTransform class.

Current setup

In my configuration section, I’m currently doing this:

$htmlDef  = $this->configuration->getHTMLDefinition(true);
// defining the element to avoid triggering 'Element 'body' is not supported'
$bodyElem = $htmlDef->addElement('body', 'Block', 'Flow', 'Core');
$bodyElem->excludes = array('body' => true);
// add the transformation rule
$htmlDef->info_tag_transform['body'] = new HTMLPurifier_TagTransform_Simple('div');

…as well as allowing <body> and its style (and class, and id) attribute via the configuration directives (they’re part of a working, large list that’s parsed into HTML.AllowedElements and HTML.AllowedAttributes).

I’ve turned definition caching off.

$config->set('Cache.DefinitionImpl', null);

Unfortunately, in this setup, it seems like HTMLPurifier_TagTransform_Simple never has its transform() method called.

HTML.Parent?

I presume the culprit is my HTML.Parent, which is set to 'div' since, quite naturally, <div> does not allow a child <body> element. However, setting HTML.Parent to 'html' nets me:

ErrorException: Cannot use unrecognized element as parent

Adding…

$htmlElem = $htmlDef->addElement('html', 'Block', 'Flow', 'Core');
$htmlElem->excludes = array('html' => true);

…gets rid of that error message but still doesn’t transform the tag – it’s removed instead.

Adding…

$htmlElem = $htmlDef->addElement('html', 'Block', 'Custom: head?, body', 'Core');
$htmlElem->excludes = array('html' => true);

…also does nothing, because it nets me an error message:

ErrorException: Trying to get property of non-object       

[...]/library/HTMLPurifier/Strategy/FixNesting.php:237
[...]/library/HTMLPurifier/Strategy/Composite.php:18
[...]/library/HTMLPurifier.php:181
[...]

I’m still tweaking around with the last option now, trying to figure out the exact syntax I need to provide, but if someone knows how to help me based on their own past experience, I’d appreciate any pointers in the right direction.

HTML.TidyLevel?

As the only other culprit I can imagine it being, my HTML.TidyLevel is set to 'heavy'. I’ve yet to try all possible constellations on this, but so far, this is making no difference.

(Since I’ve only been touching this secondarily, I struggle to recall which constellations I’ve already tried, lest I would list them here, but as it is I lack confidence I wouldn’t miss something I’ve done or misreport something. I might edit this section later when I’ve done some dedicated testing, though!)

Full Configuration

My configuration data is stored in JSON and then parsed into HTML Purifier. Here’s the file:

{
    "CSS" : {
        "MaxImgLength" : "800px"
    },
    "Core" : {
        "CollectErrors" : true,
        "HiddenElements" : {
            "script"   : true,
            "style"    : true,
            "iframe"   : true,
            "noframes" : true
        },
        "RemoveInvalidImg" : false
    },
    "Filter" : {
        "ExtractStyleBlocks" : true
    },
    "HTML" : {
        "MaxImgLength" : 800,
        "TidyLevel"    : "heavy",
        "Doctype"      : "XHTML 1.0 Transitional",
        "Parent"       : "html"
    },
    "Output" : {
        "TidyFormat"   : true
    },
    "Test" : {
        "ForceNoIconv" : true
    },
    "URI" : {
        "AllowedSchemes" : {
            "http"     : true,
            "https"    : true,
            "mailto"   : true,
            "ftp"      : true
        },
        "DisableExternalResources" : true
    }
}

(URI.Base, URI.Munge and Cache.SerializerPath are also set, but I’ve removed them in this paste. Also, HTML.Parent caveat: As mentioned, usually, this is set to 'div'.)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T17:06:28+00:00Added an answer on May 14, 2026 at 5:06 pm

    This code is the reason why what you’re doing doesn’t work:

    /**
     * Takes a string of HTML (fragment or document) and returns the content
     * @todo Consider making protected
     */
    public function extractBody($html) {
        $matches = array();
        $result = preg_match('!<body[^>]*>(.*)</body>!is', $html, $matches);
        if ($result) {
            return $matches[1];
        } else {
            return $html;
        }
    }
    

    You can turn it off using %Core.ConvertDocumentToFragment as false; if the rest of your code is bugfree, it should work straight from there. I don’t believe your bodyElem definition is necessary.j

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Premise: I want to take a chunk of html off of a document, and
with the premise that I have NO EXPERIENCE of C language, I would like
EDIT: The first question is my premise itself. Is rails/html SUPPOSED to generate a
Premise: I've started to study javascript and the DOM and I have this HTML
Is it possible to use live id authentication for in premise SharePoint 2010 site
Premise This problem has a known solution (shown below actually), I'm just wondering if
Premise: This Wikipedia page suggests that the computational complexity of Schoolbook long division is
Premise Hi, I received multiple reports from a Redis user that experienced server crashes,
Premise: I'm not trying to reinvent the wheel, I'm just trying to understand. Output
The basic premise is I want to find a character following another and then

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.