Skip to content

Commit cd7c0b6

Browse files
Purifier Html fix
1 parent 043d179 commit cd7c0b6

File tree

1 file changed

+42
-25
lines changed

1 file changed

+42
-25
lines changed

src/Purify.php

Lines changed: 42 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -26,41 +26,31 @@ private static function initHtml()
2626
$config->set('AutoFormat.AutoParagraph', true);
2727
$config->set('Core.EscapeInvalidTags', false);
2828

29-
// Set unique ID for extended definition (required for maybeGetRawHTMLDefinition)
30-
$config->set('HTML.DefinitionID', 'cms-html5-purifier'); // unique name
31-
$config->set('HTML.DefinitionRev', 1); // revision number
29+
// Unique definition ID/revision
30+
$config->set('HTML.DefinitionID', 'cms-html5-purifier');
31+
$config->set('HTML.DefinitionRev', 2);
3232

33-
// Allowed tags and attributes for CMS/blog content
33+
// Base allowed tags/attributes (only those HTMLPurifier natively supports)
3434
$config->set('HTML.Allowed', implode(',', [
35-
// Links & references
36-
'a[href|title|target]', // links with optional title and target
37-
'abbr[title]', // abbreviations
38-
'acronym[title]', // acronyms
39-
'b', 'strong', // bold text
40-
'i', 'em', // italic text
41-
'u', // underline
42-
'strike', // strikethrough
43-
'sub', 'sup', // subscripts/superscripts
44-
'mark', // highlighted text
35+
'a[href|title|target]',
36+
'abbr[title]', 'acronym[title]',
37+
'b', 'strong', 'i', 'em', 'u', 'strike',
38+
'sub', 'sup',
4539
'p', 'br', 'hr',
4640
'h1','h2','h3','h4','h5','h6',
4741
'blockquote[cite]',
4842
'code', 'pre',
4943
'ul','ol','li','dl','dt','dd',
5044
'table','thead','tbody','tfoot','tr','th','td',
5145
'img[src|alt|title|width|height]',
52-
'audio[src|controls|width|height|preload]',
53-
'video[src|controls|width|height|preload|poster]',
54-
'header','footer','main','section','article','aside','figure','figcaption','nav',
5546
'div[style|class|id]',
5647
'span[style|class|id]',
57-
'iframe[src|width|height|frameborder|allow|allowfullscreen]',
5848
]));
5949

60-
// Extend HTML5 support for semantic tags and media
61-
$def = $config->maybeGetRawHTMLDefinition();
62-
if ($def) {
63-
// Semantic blocks
50+
// Extend HTML5 support
51+
if ($def = $config->maybeGetRawHTMLDefinition()) {
52+
// Semantic HTML5
53+
$def->addElement('mark', 'Inline', 'Inline', 'Common');
6454
$def->addElement('header', 'Block', 'Flow', 'Common');
6555
$def->addElement('footer', 'Block', 'Flow', 'Common');
6656
$def->addElement('main', 'Block', 'Flow', 'Common');
@@ -72,11 +62,38 @@ private static function initHtml()
7262
$def->addElement('nav', 'Block', 'Flow', 'Common');
7363

7464
// Multimedia
75-
$def->addElement('audio', 'Block', 'Optional: Flow', 'Common', ['src' => 'URI', 'controls' => 'Bool', 'width' => 'Length', 'height' => 'Length', 'preload' => 'Enum#auto,metadata,none']);
76-
$def->addElement('video', 'Block', 'Optional: Flow', 'Common', ['src' => 'URI', 'controls' => 'Bool', 'width' => 'Length', 'height' => 'Length', 'poster' => 'URI', 'preload' => 'Enum#auto,metadata,none']);
65+
$def->addElement('audio', 'Block', 'Optional: Flow', 'Common', [
66+
'src' => 'URI',
67+
'controls'=> 'Bool',
68+
'width' => 'Length',
69+
'height' => 'Length',
70+
'preload' => 'Enum#auto,metadata,none'
71+
]);
72+
$def->addElement('video', 'Block', 'Optional: Flow', 'Common', [
73+
'src' => 'URI',
74+
'controls'=> 'Bool',
75+
'width' => 'Length',
76+
'height' => 'Length',
77+
'poster' => 'URI',
78+
'preload' => 'Enum#auto,metadata,none'
79+
]);
80+
$def->addElement('source', 'Block', 'Flow', 'Common', [
81+
'src' => 'URI',
82+
'type' => 'Text'
83+
]);
84+
85+
// iframe with extended attributes
86+
$def->addElement('iframe', 'Block', 'Flow', 'Common', [
87+
'src' => 'URI',
88+
'width' => 'Length',
89+
'height' => 'Length',
90+
'frameborder' => 'Text',
91+
'allow' => 'Text',
92+
'allowfullscreen' => 'Bool'
93+
]);
7794
}
7895

79-
self::$purifierHtml = new HTMLPurifier($config);
96+
self::$purifierHtml = new \HTMLPurifier($config);
8097
}
8198
}
8299

0 commit comments

Comments
 (0)