HTMLEntities

Build Status - Master macOS Linux Apache 2 codecov Carthage compatible

概要

用于 Swift 的纯 Swift HTML 编码/解码实用工具。

包含对 HTML5 命名字符引用的支持。 您可以在这里找到所有 2231 个 HTML5 命名字符引用的列表。

HTMLEntities 可以转义所有非 ASCII 字符以及字符 <>&",因为这五个字符是 HTML 标签和 HTML 属性语法的一部分。

此外,HTMLEntities 可以取消转义包含十进制、十六进制或 HTML5 命名字符引用的已编码 HTML 文本。

API 文档

HTMLEntities 的 API 文档位于这里

特性

版本信息

最新版本的 HTMLEntities 需要 Swift 4.0 及更高版本。

安装

通过 Swift Package Manager

HTMLEntities 添加到您的 Package.swift

import PackageDescription

let package = Package(
  name: "<package-name>",
  ...
  dependencies: [
    .package(url: "https://github.com/Kitura/swift-html-entities.git", from: "3.0.0")
  ]
  // Also, make sure to add HTMLEntities to your package target's dependencies
)

通过 CocoaPods

HTMLEntities 添加到您的 Podfile

target '<project-name>' do
  pod 'HTMLEntities', :git => 'https://github.com/Kitura/swift-html-entities.git'
end

通过 Carthage

HTMLEntities 添加到您的 Cartfile

github "Kitura/swift-html-entities"

用法

import HTMLEntities

// encode example
let html = "<script>alert(\"abc\")</script>"

print(html.htmlEscape())
// Prints "&#x3C;script&#x3E;alert(&#x22;abc&#x22;)&#x3C;/script&#x3E;"

// decode example
let htmlencoded = "&lt;script&gt;alert(&quot;abc&quot;)&lt;/script&gt;"

print(htmlencoded.htmlUnescape())
// Prints "<script>alert(\"abc\")</script>"

高级选项

HTMLEntities 在转义和取消转义 HTML 字符时支持各种选项。

转义选项

allowUnsafeSymbols

默认为 false。 指定是否应跳过不安全的 ASCII 字符。

import HTMLEntities

let html = "<p>\"café\"</p>"

print(html.htmlEscape())
// Prints "&#x3C;p&#x3E;&#x22;caf&#xE9;&#x22;&#x3C;/p&#x3E;"

print(html.htmlEscape(allowUnsafeSymbols: true))
// Prints "<p>\"caf&#xE9;\"</p>"

decimal

默认为 false。 指定在使用数字字符转义时,应使用十进制字符转义而不是十六进制字符转义(即,不影响命名字符引用转义)。 建议使用十六进制字符转义。

import HTMLEntities

let text = "한, 한, ế, ế, 🇺🇸"

print(text.htmlEscape())
// Prints "&#x1112;&#x1161;&#x11AB;, &#xD55C;, &#x1EBF;, e&#x302;&#x301;, &#x1F1FA;&#x1F1F8;"

print(text.htmlEscape(decimal: true))
// Prints "&#4370;&#4449;&#4523;, &#54620;, &#7871;, e&#770;&#769;, &#127482;&#127480;"

encodeEverything

默认为 false。 指定是否应转义所有字符,即使某些字符是安全的。 如果为 true,则覆盖 allowUnsafeSymbols 的设置。

import HTMLEntities

let text = "A quick brown fox jumps over the lazy dog"

print(text.htmlEscape())
// Prints "A quick brown fox jumps over the lazy dog"

print(text.htmlEscape(encodeEverything: true))
// Prints "&#x41;&#x20;&#x71;&#x75;&#x69;&#x63;&#x6B;&#x20;&#x62;&#x72;&#x6F;&#x77;&#x6E;&#x20;&#x66;&#x6F;&#x78;&#x20;&#x6A;&#x75;&#x6D;&#x70;&#x73;&#x20;&#x6F;&#x76;&#x65;&#x72;&#x20;&#x74;&#x68;&#x65;&#x20;&#x6C;&#x61;&#x7A;&#x79;&#x20;&#x64;&#x6F;&#x67;"

// `encodeEverything` overrides `allowUnsafeSymbols`
print(text.htmlEscape(allowUnsafeSymbols: true, encodeEverything: true))
// Prints "&#x41;&#x20;&#x71;&#x75;&#x69;&#x63;&#x6B;&#x20;&#x62;&#x72;&#x6F;&#x77;&#x6E;&#x20;&#x66;&#x6F;&#x78;&#x20;&#x6A;&#x75;&#x6D;&#x70;&#x73;&#x20;&#x6F;&#x76;&#x65;&#x72;&#x20;&#x74;&#x68;&#x65;&#x20;&#x6C;&#x61;&#x7A;&#x79;&#x20;&#x64;&#x6F;&#x67;"

useNamedReferences

默认为 false。 指定应尽可能使用命名字符引用。 设置为 false 以始终使用数字字符引用,即为了与不识别命名字符引用的旧浏览器兼容。

import HTMLEntities

let html = "<script>alert(\"abc\")</script>"

print(html.htmlEscape())
// Prints “&#x3C;script&#x3E;alert(&#x22;abc&#x22;)&#x3C;/script&#x3E;”

print(html.htmlEscape(useNamedReferences: true))
// Prints “&lt;script&gt;alert(&quot;abc&quot;)&lt;/script&gt;”

全局设置转义选项

可以全局设置 HTML 转义选项,这样您就不必每次都设置它们来转义字符串。 这些选项在 String.HTMLEscapeOptions 结构中进行管理。

import HTMLEntities

// set `useNamedReferences` to `true` globally
String.HTMLEscapeOptions.useNamedReferences = true

let html = "<script>alert(\"abc\")</script>"

// Now, the default behavior of `htmlEscape()` is to use named character references
print(html.htmlEscape())
// Prints “&lt;script&gt;alert(&quot;abc&quot;)&lt;/script&gt;”

// And you can still go back to using numeric character references only
print(html.htmlEscape(useNamedReferences: false))
// Prints "&#x3C;script&#x3E;alert(&#x22;abc&#x22;)&#x3C;/script&#x3E;"

取消转义选项

strict

默认为 false。 指定是否应抛出 HTML5 解析错误,或者只是忽略它们。

注意: 如果在调用参数中使用 strict(无论设置为 true 还是 false),htmlUnescape() 是一个抛出函数; 如果未提供任何参数,htmlUnescape() 不是一个抛出函数。

import HTMLEntities

let text = "&#4370&#4449&#4523"

print(text.htmlUnescape())
// Prints "한"

print(try text.htmlUnescape(strict: true))
// Throws a `ParseError.MissingSemicolon` instance

// a throwing function because `strict` is passed in argument
// but no error is thrown because `strict: false`
print(try text.htmlUnescape(strict: false))
// Prints "한"

致谢

HTMLEntities 旨在支持与 he(一个流行的 Javascript HTML 编码器/解码器)相同的一些选项。

许可证

Apache 2.0