定制 totara/htmlpurifier-html5 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

totara/htmlpurifier-html5

Composer 安装命令:

composer require totara/htmlpurifier-html5

包简介

HTML5 support for HTML Purifier

README 文档

README

This is a Totara-specific fork of the HTML5 Purifier definition.

This library provides HTML5 element definitions for HTML Purifier, compliant with the WHATWG spec.

It is the most complete HTML5-compliant solution among all based on HTML Purifier. Apart from providing the most extensive set of element definitions, it provides tidy/sanitization rules for transforming the input into a valid HTML5 output.

Installation

Install with Composer by running the following command:

composer require xemlock/htmlpurifier-html5

Usage

The most basic usage is similar to the original HTML Purifier. Create a HTML5-compatible config using HTMLPurifier_HTML5Config::createDefault() factory method, and then pass it to an HTMLPurifier instance:

$config = HTMLPurifier_HTML5Config::createDefault();
$purifier = new HTMLPurifier($config);
$clean_html5 = $purifier->purify($dirty_html5);

To modify the config you can either instantiate the config with a configuration array passed to HTMLPurifier_HTML5Config::create(), or by calling set method on an already existing config instance.

For example, to allow IFRAMEs with Youtube videos you can do the following:

$config = HTMLPurifier_HTML5Config::create(array(
  'HTML.SafeIframe' => true,
  'URI.SafeIframeRegexp' => '%^//www\.youtube\.com/embed/%',
));

or equivalently:

$config = HTMLPurifier_HTML5Config::createDefault();
$config->set('HTML.SafeIframe', true);
$config->set('URI.SafeIframeRegexp', '%^//www\.youtube\.com/embed/%');

Configuration

Apart from HTML Purifier's built-in configuration directives, the following new directives are also supported:

  • Attr.AllowedInputTypes

    Version added: 0.1.12
    Type: Lookup (or null)
    Default: null

    List of allowed input types, chosen from the types defined in the spec. By default, the setting is null, meaning there is no restriction on allowed types. Empty array means that no explicit type attributes are allowed, effectively making all inputs a text inputs.

  • HTML.Forms

    Version added: 0.1.12
    Type: Boolean
    Default: false

    Whether or not to permit form elements in the user input, regardless of %HTML.Trusted value. Please be very careful when using this functionality, as enabling forms in untrusted documents may allow for phishing attacks.

  • HTML.IframeAllowFullscreen

    Version added: 0.1.11
    Type: Boolean
    Default: false

    Whether or not to permit allowfullscreen attribute on iframe tags. It requires either %HTML.SafeIframe or %HTML.Trusted to be true.

  • HTML.Link

    Version added: 0.1.12
    Type: Boolean
    Default: false

    Permit the link tags in the user input, regardless of %HTML.Trusted value. This effectively allows link tags without allowing other untrusted elements.

    If enabled, URIs in link tags will not be matched against a whitelist specified in %URI.SafeLinkRegexp (unless %HTML.SafeIframe is also enabled).

  • HTML.SafeLink

    Version added: 0.1.12
    Type: Boolean
    Default: false

    Whether to permit link tags in untrusted documents. This directive must be accompanied by a whitelist of permitted URIs via %URI.SafeLinkRegexp, otherwise no link tags will be allowed.

  • HTML.XHTML

    Version added: 0.1.12
    Type: Boolean
    Default: false

    While deprecated in HTML 4.01 / XHTML 1.0 context, in HTML5 it's used for enabling support for namespaced attributes and XML self-closing tags.

    When enabled it causes xml:lang attribute to take precedence over lang, when both attributes are present on the same element.

  • URI.SafeLinkRegexp

    Version added: 0.1.12
    Type: String
    Default: null

    A PCRE regular expression that will be matched against a <link> URI. This directive only has an effect if %HTML.SafeLink is enabled. Here are some example values: %^https?://localhost/% - Allow localhost URIs

    Use Attr.AllowedRel to control permitted link relationship types.

Supported HTML5 elements

Aside from HTML elements supported originally by HTML Purifier, this library adds support for the following HTML5 elements:

<article>, <aside>, <audio>, <bdi>, <data>, <details>, <dialog>, <figcaption>, <figure>, <footer>, <header>, <hgroup>, <main>, <mark>, <nav>, <picture>, <progress>, <section>, <source>, <summary>, <time>, <track>, <video>, <wbr>

as well as HTML5 attributes added to existing HTML elements, such as:

<a>, <del>, <fieldset>, <ins>, <script>

License

The MIT License (MIT). See the LICENSE file.

totara/htmlpurifier-html5 适用场景与选型建议

totara/htmlpurifier-html5 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 11.99k 次下载、GitHub Stars 达 0, 最近一次更新时间为 2025 年 09 月 25 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「security」 「validator」 「HTML5」 「html」 「htmlpurifier」 「xss」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 totara/htmlpurifier-html5 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 totara/htmlpurifier-html5 我们能提供哪些服务?
定制开发 / 二次开发

基于 totara/htmlpurifier-html5 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 11.99k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 37
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 14
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-09-25