HTMLPurifier_HTMLModule::addElement PHP Method

addElement() public method

Convenience function that sets up a new element
public addElement ( string $element, string | boolean $type, string $contents, array $attr_includes = [], array $attr = [] ) : HTMLPurifier_ElementDef
$element string Name of element to add
$type string | boolean What content set should element be registered to? Set as false to skip this step.
$contents string Allowed children in form of: "$content_model_type: $content_model"
$attr_includes array What attribute collections to register to element?
$attr array What unique attributes does the element define?
return HTMLPurifier_ElementDef Created element definition object, so you can set advanced parameters
    public function addElement($element, $type, $contents, $attr_includes = array(), $attr = array())
    {
        $this->elements[] = $element;
        // parse content_model
        list($content_model_type, $content_model) = $this->parseContents($contents);
        // merge in attribute inclusions
        $this->mergeInAttrIncludes($attr, $attr_includes);
        // add element to content sets
        if ($type) {
            $this->addElementToContentSet($element, $type);
        }
        // create element
        $this->info[$element] = HTMLPurifier_ElementDef::create($content_model, $content_model_type, $attr);
        // literal object $contents means direct child manipulation
        if (!is_string($contents)) {
            $this->info[$element]->child = $contents;
        }
        return $this->info[$element];
    }

Usage Example

 public function test_addModule()
 {
     $manager = $this->createManager();
     // ...but we add user modules
     $common_module = new HTMLPurifier_HTMLModule();
     $common_module->name = 'Common';
     $common_module->attr_collections['Common'] = array('class' => 'NMTOKENS');
     $common_module->content_sets['Flow'] = 'Block | Inline';
     $manager->addModule($common_module);
     $structural_module = new HTMLPurifier_HTMLModule();
     $structural_module->name = 'Structural';
     $structural_module->addElement('p', 'Block', 'Inline', 'Common');
     $manager->addModule($structural_module);
     $formatting_module = new HTMLPurifier_HTMLModule();
     $formatting_module->name = 'Formatting';
     $formatting_module->addElement('em', 'Inline', 'Inline', 'Common');
     $manager->addModule($formatting_module);
     $unsafe_module = new HTMLPurifier_HTMLModule();
     $unsafe_module->name = 'Unsafe';
     $unsafe_module->safe = false;
     $unsafe_module->addElement('div', 'Block', 'Flow');
     $manager->addModule($unsafe_module);
     $config = HTMLPurifier_Config::createDefault();
     $config->set('HTML.Trusted', false);
     $config->set('HTML.CustomDoctype', 'Blank');
     $manager->setup($config);
     $attrdef_nmtokens = new HTMLPurifier_AttrDef_HTML_Nmtokens();
     $p = new HTMLPurifier_ElementDef();
     $p->attr['class'] = $attrdef_nmtokens;
     $p->child = new HTMLPurifier_ChildDef_Optional(array('em', '#PCDATA'));
     $p->content_model = 'em | #PCDATA';
     $p->content_model_type = 'optional';
     $p->descendants_are_inline = true;
     $em = new HTMLPurifier_ElementDef();
     $em->attr['class'] = $attrdef_nmtokens;
     $em->child = new HTMLPurifier_ChildDef_Optional(array('em', '#PCDATA'));
     $em->content_model = 'em | #PCDATA';
     $em->content_model_type = 'optional';
     $em->descendants_are_inline = true;
     $this->assertEqual(array('p' => $p, 'em' => $em), $manager->getElements());
     // test trusted parameter override
     $div = new HTMLPurifier_ElementDef();
     $div->child = new HTMLPurifier_ChildDef_Optional(array('p', 'div', 'em', '#PCDATA'));
     $div->content_model = 'p | div | em | #PCDATA';
     $div->content_model_type = 'optional';
     $div->descendants_are_inline = false;
     $this->assertEqual($div, $manager->getElement('div', true));
 }
All Usage Examples Of HTMLPurifier_HTMLModule::addElement