用于 Swift 的纯 Swift HTML 编码/解码实用工具。
包含对 HTML5 命名字符引用的支持。 您可以在这里找到所有 2231 个 HTML5 命名字符引用的列表。
HTMLEntities 可以转义所有非 ASCII 字符以及字符 <、>、&、"、’,因为这五个字符是 HTML 标签和 HTML 属性语法的一部分。
此外,HTMLEntities 可以取消转义包含十进制、十六进制或 HTML5 命名字符引用的已编码 HTML 文本。
HTMLEntities 的 API 文档位于这里。
NegativeMediumSpace; 等)String 类添加的最新版本的 HTMLEntities 需要 Swift 4.0 及更高版本。
将 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
)
将 HTMLEntities 添加到您的 Podfile 中
target '<project-name>' do
pod 'HTMLEntities', :git => 'https://github.com/Kitura/swift-html-entities.git'
end
将 HTMLEntities 添加到您的 Cartfile 中
github "Kitura/swift-html-entities"
import HTMLEntities
// encode example
let html = "<script>alert(\"abc\")</script>"
print(html.htmlEscape())
// Prints "<script>alert("abc")</script>"
// decode example
let htmlencoded = "<script>alert("abc")</script>"
print(htmlencoded.htmlUnescape())
// Prints "<script>alert(\"abc\")</script>"
HTMLEntities 在转义和取消转义 HTML 字符时支持各种选项。
默认为 false。 指定是否应跳过不安全的 ASCII 字符。
import HTMLEntities
let html = "<p>\"café\"</p>"
print(html.htmlEscape())
// Prints "<p>"café"</p>"
print(html.htmlEscape(allowUnsafeSymbols: true))
// Prints "<p>\"café\"</p>"
默认为 false。 指定在使用数字字符转义时,应使用十进制字符转义而不是十六进制字符转义(即,不影响命名字符引用转义)。 建议使用十六进制字符转义。
import HTMLEntities
let text = "한, 한, ế, ế, 🇺🇸"
print(text.htmlEscape())
// Prints "한, 한, ế, ế, 🇺🇸"
print(text.htmlEscape(decimal: true))
// Prints "한, 한, ế, ế, 🇺🇸"
默认为 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 "A quick brown fox jumps over the lazy dog"
// `encodeEverything` overrides `allowUnsafeSymbols`
print(text.htmlEscape(allowUnsafeSymbols: true, encodeEverything: true))
// Prints "A quick brown fox jumps over the lazy dog"
默认为 false。 指定应尽可能使用命名字符引用。 设置为 false 以始终使用数字字符引用,即为了与不识别命名字符引用的旧浏览器兼容。
import HTMLEntities
let html = "<script>alert(\"abc\")</script>"
print(html.htmlEscape())
// Prints “<script>alert("abc")</script>”
print(html.htmlEscape(useNamedReferences: true))
// Prints “<script>alert("abc")</script>”
可以全局设置 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 “<script>alert("abc")</script>”
// And you can still go back to using numeric character references only
print(html.htmlEscape(useNamedReferences: false))
// Prints "<script>alert("abc")</script>"
默认为 false。 指定是否应抛出 HTML5 解析错误,或者只是忽略它们。
注意: 如果在调用参数中使用 strict(无论设置为 true 还是 false),htmlUnescape() 是一个抛出函数; 如果未提供任何参数,htmlUnescape() 不是一个抛出函数。
import HTMLEntities
let text = "한"
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