2011-04-14 89 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仍然會刪除