普贤教程 › 包参考
htmlsanitizer 包
HTML 清洗 / XSS 防护
| 项 | 值 |
|---|---|
| 包名 | htmlsanitizer |
| 版本 | 0.2.0 |
| 可用版本 | 0.1.0 / 0.2.0 |
| 入口 | htmlsanitizer.px |
| 分类 | 认证与安全 |
| 文件数 / 行数 | 1 / 440 |
安装与引入
bash
pxpkg init # 首次 pxpkg add htmlsanitizer@0.2.0 pxpkg install
px
import htmlsanitizer所属分类教程:认证与安全
公开接口
| 函数 | 参数 |
|---|---|
hs_escape | text |
hs_encode_attr | text |
hs_unescape | text |
hs_remove_comments | text |
hs_remove_scripts | text |
hs_remove_styles | text |
hs_strip_tags | text, allowed = [] |
hs_url_probe | url |
hs_is_safe_url | url |
hs_clean_url | url |
hs_strip_attrs | text, banned = [] |
hs_sanitize | text, options = {} |
其余 12 个 def(内部与辅助,可直接调用但不属稳定接口)
| 函数 | 参数 |
|---|---|
hs_replace_all | text, old, new |
hs_to_lower | s |
hs_is_space | c |
hs_find_char | s, ch, start |
hs_find_str | s, sub, start |
hs_remove_tag_content | text, tag |
hs_extract_tag_name | content |
hs_is_attr_banned | name, banned |
hs_entity_char | ent |
hs_url_scheme | url |
hs_value_hazard | value, banned |
hs_rebuild_tag | inner, banned |
包内说明(源码头注释)
text
============================================================ 普贤生态库:registry/htmlsanitizer — HTML 清洗 / XSS 防护 ------------------------------------------------------------ 纯函数库,双模式一致。 实现要点: - 标签按结构解析:取标签名、逐个解析属性(引号值 / 无引号值 / 布尔属性), 命中黑名单的属性整条丢掉,其余原样重建 —— 不再有 "=" 残渣。 - URL 判定先归一化(去空白与控制字符、解 &#NN; / &#xNN; / : 实体)再看协议。 - 未闭合的 <script> 只丢开标签,不再吞掉剩余文档。 - hs_sanitize 支持更多选项(见函数注释)。 公开函数: hs_escape / hs_unescape / hs_encode_attr hs_strip_tags(text, allowed) hs_strip_attrs(text, banned) ← 结构化解析 hs_remove_scripts / hs_remove_styles / hs_remove_comments hs_is_safe_url / hs_clean_url / hs_url_probe hs_sanitize(text, options) ============================================================