2011-04-14 87 views
54

有没有一种简单的方法为HTMLPurifier添加HTML5规则集?与HTML5兼容的HTML过滤器

惠普可以configured to识别新标签有:

// setup configurable HP instance 
$config = HTMLPurifier_Config::createDefault(); 
$config->set('HTML.DefinitionID', 'html5 draft'); 
$config->set('HTML.DefinitionRev', 1); 
$config->set('Cache.DefinitionImpl', null); // no caching 
$def = $config->getHTMLDefinition(true); 

// add a new tag 
$form = $def->addElement(
    'article', // name 
    'Block',  // content set 
    'Flow',  // allowed children 
    'Common', // attribute collection 
    array(  // attributes 
) 
); 

// add a new attribute 
$def->addAttribute('a', 'contextmenu', "ID"); 

但是这显然是一个工作位。由于有很多新的HTML5标签和属性需要注册。即使使用现有的HTML 4标签,新的全局属性也应该可以组合。 (很难从文档中判断如何增加核心规则)。那么,是否有一个更有用的配置格式/数组结构,可将新的更新的标记+属性+上下文配置(inline/block/empty/flow/..)提供给HTMLPurifier?

# mostly confused about how to extend existing tags: 
$def->addAttribute('input', 'type', "...|...|..."); 

# or how to allow data-* attributes (if I actually wanted that): 
$def->addAttribute("data-*", ... 

当然,并非所有新的HTML5标签都适合不受限制的补贴。 HTMLPurifier是关于内容过滤的。定义价值约束就在它的位置。 - 例如,<canvas>在用户内容中可能不是什么大不了的。因为没有Javascript(惠普已经过滤掉),它最好没用。但其他标签和属性可能不合需要;所以灵活的配置结构对于启用/禁用标签及其相关属性是必不可少的。

猜我应该更新一些研究...)。但是仍然没有适合惠普配置的实际汇编/规范(不,XML DTD不适用)。

(嗯,和HTML5不再是草案。)

+0

你问过谁可以吗? http://stackoverflow.com/questions/4566301/htmlpurifier-with-an-html5-doctype/4567261#4567261 – thirtydot 2011-04-18 01:43:44

+0

@thirtydot:这可能是他添加PH5P解析器thingy之前。无论如何,这是不相关的,因为您可以添加新标签,并且HTML5解析技术在HTML5上应该可以正常工作。 – mario 2011-04-18 01:53:12

+2

你应该知道,HTML 5仍然是一个草稿(即使它已经在“最后一次调用”已经超过2年了)......并因此可以改变......因此,今天支持的HTML 5并不一定就是明天。 – Eugene 2011-04-23 00:54:34

回答

11

PHP的整齐扩展可以被配置成识别HTML5标记。 http://tidy.sourceforge.net/docs/quickref.html#new-blocklevel-tags

+0

你可能会在你自己的类中包装整洁的cleanAndRepair并将其与strip_tags和htmlspecialchars混合。 Tidy也有一个裸露的选项,剥夺专有属性,也许这将有所帮助。 – chris 2011-04-18 14:48:35

+6

'tidy_clean_repair()'可能最适合重新格式化您自己的代码。 ['drop-proprietary-attributes'](http://tidy.sourceforge.net/docs/quickref.html#drop-proprietary-attributes)标志有趣地删除了任何新的HTML5属性,但保持onClick =等。所以它真的没有HTMLPurifier替代。 – mario 2011-04-18 23:43:13

+0

但是,您可以使用'new-block-tags'等来定义它们。 – 2011-11-11 20:13:34

4

即时通讯使用WordPress的,但也许这也可以帮助你(至少在阵列部分)

http://nicolasgallagher.com/using-html5-elements-in-wordpress-post-content/修复

http://hybridgarden.com/blog/misc/adding-html5-capability-to-wordpress/

也:

http://code.google.com/p/html5lib/一Python和PHP实现 基于WHATWG HTML5规范的HTML解析器最大 兼容主要桌面网页浏览器。

+0

不知道WP片段,但html5lib听起来很有前途。它可能没有惠普功能集,但至少可能会成为一个出色的过渡解决方案。 – mario 2011-08-01 23:26:38

0
+1

PH5P是HTMLPurifier的一部分。据我所知,它只是根据HTML5 SGML式的序列化来实现解析规则。它不会增强我相信的HP输出滤波器(或者只是没有让它工作)。 @EZY可能还没有时间来整合或完成。 – mario 2011-08-01 23:24:32

+1

如果需要,您可以使用PH5P和HTML Purifier。但是,这不会将您需要的属性集添加到HTML Purifier中。 – 2011-08-02 23:13:27

6

有此配置HTMLpurify允许新的HTML5标签。

来源:https://github.com/kennberg/php-htmlpurfier-html5

<?php 
/** 
* Load HTMLPurifier with HTML5, TinyMCE, YouTube, Video support. 
* 
* Copyright 2014 Alex Kennberg (https://github.com/kennberg/php-htmlpurifier-html5) 
* 
* Licensed under the Apache License, Version 2.0 (the "License"); 
* you may not use this file except in compliance with the License. 
* You may obtain a copy of the License at 
* 
*  http://www.apache.org/licenses/LICENSE-2.0 
* 
* Unless required by applicable law or agreed to in writing, software 
* distributed under the License is distributed on an "AS IS" BASIS, 
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
* See the License for the specific language governing permissions and 
* limitations under the License. 
*/ 

require_once(LIB_DIR . 'third-party/htmlpurifier/HTMLPurifier.safe-includes.php'); 


function load_htmlpurifier($allowed) { 
    $config = HTMLPurifier_Config::createDefault(); 
    $config->set('HTML.Doctype', 'HTML 4.01 Transitional'); 
    $config->set('CSS.AllowTricky', true); 
    $config->set('Cache.SerializerPath', '/tmp'); 

    // Allow iframes from: 
    // o YouTube.com 
    // o Vimeo.com 
    $config->set('HTML.SafeIframe', true); 
    $config->set('URI.SafeIframeRegexp', '%^(http:|https:)?//(www.youtube(?:-nocookie)?.com/embed/|player.vimeo.com/video/)%'); 

    $config->set('HTML.Allowed', implode(',', $allowed)); 

    // Set some HTML5 properties 
    $config->set('HTML.DefinitionID', 'html5-definitions'); // unqiue id 
    $config->set('HTML.DefinitionRev', 1); 

    if ($def = $config->maybeGetRawHTMLDefinition()) { 
    // http://developers.whatwg.org/sections.html 
    $def->addElement('section', 'Block', 'Flow', 'Common'); 
    $def->addElement('nav',  'Block', 'Flow', 'Common'); 
    $def->addElement('article', 'Block', 'Flow', 'Common'); 
    $def->addElement('aside', 'Block', 'Flow', 'Common'); 
    $def->addElement('header', 'Block', 'Flow', 'Common'); 
    $def->addElement('footer', 'Block', 'Flow', 'Common'); 

    // Content model actually excludes several tags, not modelled here 
    $def->addElement('address', 'Block', 'Flow', 'Common'); 
    $def->addElement('hgroup', 'Block', 'Required: h1 | h2 | h3 | h4 | h5 | h6', 'Common'); 

    // http://developers.whatwg.org/grouping-content.html 
    $def->addElement('figure', 'Block', 'Optional: (figcaption, Flow) | (Flow, figcaption) | Flow', 'Common'); 
    $def->addElement('figcaption', 'Inline', 'Flow', 'Common'); 

    // http://developers.whatwg.org/the-video-element.html#the-video-element 
    $def->addElement('video', 'Block', 'Optional: (source, Flow) | (Flow, source) | Flow', 'Common', array(
     'src' => 'URI', 
     'type' => 'Text', 
     'width' => 'Length', 
     'height' => 'Length', 
     'poster' => 'URI', 
     'preload' => 'Enum#auto,metadata,none', 
     'controls' => 'Bool', 
    )); 
    $def->addElement('source', 'Block', 'Flow', 'Common', array(
     'src' => 'URI', 
     'type' => 'Text', 
    )); 

    // http://developers.whatwg.org/text-level-semantics.html 
    $def->addElement('s', 'Inline', 'Inline', 'Common'); 
    $def->addElement('var', 'Inline', 'Inline', 'Common'); 
    $def->addElement('sub', 'Inline', 'Inline', 'Common'); 
    $def->addElement('sup', 'Inline', 'Inline', 'Common'); 
    $def->addElement('mark', 'Inline', 'Inline', 'Common'); 
    $def->addElement('wbr', 'Inline', 'Empty', 'Core'); 

    // http://developers.whatwg.org/edits.html 
    $def->addElement('ins', 'Block', 'Flow', 'Common', array('cite' => 'URI', 'datetime' => 'CDATA')); 
    $def->addElement('del', 'Block', 'Flow', 'Common', array('cite' => 'URI', 'datetime' => 'CDATA')); 

    // TinyMCE 
    $def->addAttribute('img', 'data-mce-src', 'Text'); 
    $def->addAttribute('img', 'data-mce-json', 'Text'); 

    // Others 
    $def->addAttribute('iframe', 'allowfullscreen', 'Bool'); 
    $def->addAttribute('table', 'height', 'Text'); 
    $def->addAttribute('td', 'border', 'Text'); 
    $def->addAttribute('th', 'border', 'Text'); 
    $def->addAttribute('tr', 'width', 'Text'); 
    $def->addAttribute('tr', 'height', 'Text'); 
    $def->addAttribute('tr', 'border', 'Text'); 
    } 

    return new HTMLPurifier($config); 
} 
+0

很好找。不过,您可能想要摘录相关章节,并附上基本的归属内容。链接经常变得无法访问(特别是对于github;很少有互联网存档缓存)。 - 至少注册基本标签;然而缺乏许多新的属性,例如只列出了一些选定的数据 - 道具。 – mario 2015-05-05 23:34:21

+0

已记录,并且完整列表现已包含在帖子中。 – 2015-05-06 09:53:16

+0

酷脚本。 HTML Purifier仍然会删除