符合HTML5的HTML过滤器

如何解决符合HTML5的HTML过滤器

| 有没有简单的方法可以为HTMLPurifier添加HTML5规则集? 可以将HP配置为通过以下方式识别新标签:
// 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标签,新的全局属性也应该可以组合。 (很难从文档中判断如何扩充核心规则)。因此,是否有更有用的配置格式/数组结构可将新的和更新的标记+属性+上下文配置(内联/块/空/流/ ..)馈送到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(HP已经过滤掉了),它充其量是没有用的。但是其他标签和属性可能是不可取的。因此,灵活的配置结构对于启用/禁用标签及其相关属性至关重要。 (猜猜我应该更新一些研究...)。但是,仍然没有适合HP配置的实用的纲要/规范(不,XML DTD不是)。 http://simon.html5.org/html-elements http://www.w3.org/TR/html5-diff/#new-elements http://www.w3.org/TR/html5-diff/#new-attributes (嗯,HTML5不再是草稿。)     

解决方法

php tidy扩展名可以配置为识别html5标签。 http://tidy.sourceforge.net/docs/quickref.html#new-blocklevel-tags     ,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\',\'Common\');
    $def->addElement(\'article\',\'Common\');
    $def->addElement(\'aside\',\'Common\');
    $def->addElement(\'header\',\'Common\');
    $def->addElement(\'footer\',\'Common\');

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

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

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

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

    // http://developers.whatwg.org/edits.html
    $def->addElement(\'ins\',array(\'cite\' => \'URI\',\'datetime\' => \'CDATA\'));
    $def->addElement(\'del\',\'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\',\'Text\');
    $def->addAttribute(\'tr\',\'width\',\'Text\');
  }

  return new HTMLPurifier($config);
}
    ,即时通讯使用的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解析器   与主要的桌面网络浏览器兼容。     ,我知道这个话题确实很老,但是由于它仍然很重要,因此我决定做出回应。自最初提出问题以来,尤其是当景观发生变化时。 您可以使用https://github.com/xemlock/htmlpurifier-html5,它使用符合规范的HTML5元素和属性定义扩展了HTML Purifier。 用法与原始HTML Purifier几乎相同,只需将
HTMLPurifier_Config
替换为
HTMLPurifier_HTML5Config
$config = HTMLPurifier_HTML5Config::createDefault();
$purifier = new HTMLPurifier($config);

$clean_html5 = $purifier->purify($dirty_html5);
免责声明:我是扩展程序的作者。     ,Gallery Role具有基于HTMLPurifier的实验性HTML5解析器: https://github.com/gallery/gallery3-vendor/blob/master/htmlpurifier/modified/HTMLPurifier/Lexer/PH5P.php     

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


依赖报错 idea导入项目后依赖报错,解决方案:https://blog.csdn.net/weixin_42420249/article/details/81191861 依赖版本报错:更换其他版本 无法下载依赖可参考:https://blog.csdn.net/weixin_42628809/a
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下 2021-12-03 13:33:33.927 ERROR 7228 [ main] o.s.b.d.LoggingFailureAnalysisReporter : *************************** APPL
错误1:gradle项目控制台输出为乱码 # 解决方案:https://blog.csdn.net/weixin_43501566/article/details/112482302 # 在gradle-wrapper.properties 添加以下内容 org.gradle.jvmargs=-Df
错误还原:在查询的过程中,传入的workType为0时,该条件不起作用 &lt;select id=&quot;xxx&quot;&gt; SELECT di.id, di.name, di.work_type, di.updated... &lt;where&gt; &lt;if test=&qu
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct redisServer’没有名为‘server_cpulist’的成员 redisSetCpuAffinity(server.server_cpulist); ^ server.c: 在函数‘hasActiveC
解决方案1 1、改项目中.idea/workspace.xml配置文件,增加dynamic.classpath参数 2、搜索PropertiesComponent,添加如下 &lt;property name=&quot;dynamic.classpath&quot; value=&quot;tru
删除根组件app.vue中的默认代码后报错:Module Error (from ./node_modules/eslint-loader/index.js): 解决方案:关闭ESlint代码检测,在项目根目录创建vue.config.js,在文件中添加 module.exports = { lin
查看spark默认的python版本 [root@master day27]# pyspark /home/software/spark-2.3.4-bin-hadoop2.7/conf/spark-env.sh: line 2: /usr/local/hadoop/bin/hadoop: No s
使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams[&#39;font.sans-serif&#39;] = [&#39;SimHei&#39;] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -&gt; systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping(&quot;/hires&quot;) public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate&lt;String
使用vite构建项目报错 C:\Users\ychen\work&gt;npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-