Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support automatic escaping HTML #80

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Sources/Extension.swift
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ class DefaultExtension: Extension {
registerFilter("uppercase", filter: uppercase)
registerFilter("lowercase", filter: lowercase)
registerFilter("join", filter: joinFilter)
registerFilter("safe", filter: safeFilter)
}
}

Expand Down
5 changes: 5 additions & 0 deletions Sources/Filters.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,8 @@ func joinFilter(value: Any?, arguments: [Any?]) throws -> Any? {

return value
}


func safeFilter(value: Any?) throws -> Any? {
return escaped(html: stringify(value))
}
26 changes: 26 additions & 0 deletions Sources/HTML.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
public protocol HTMLString {
var html: String { get }
}


struct EscapedHTML: HTMLString, CustomStringConvertible {
let value: String

var html: String { return value }
var description: String { return value }
}


func escaped(html: String) -> HTMLString {
return EscapedHTML(value: html)
}


func escapeHTML(_ value: String) -> String {
return value
.replacingOccurrences(of: "&", with: "&")
.replacingOccurrences(of: "'", with: "&39;")
.replacingOccurrences(of: "<", with: "&lt;")
.replacingOccurrences(of: ">", with: "&gt;")
.replacingOccurrences(of: "\"", with: "&quot;")
}
8 changes: 6 additions & 2 deletions Sources/Node.swift
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,15 @@ public class VariableNode : NodeType {

public func render(_ context: Context) throws -> String {
let result = try variable.resolve(context)
return stringify(result)

if let result = result as? EscapedHTML {
return result.html
}

return escapeHTML(stringify(result))
}
}


func stringify(_ result: Any?) -> String {
if let result = result as? String {
return result
Expand Down
9 changes: 9 additions & 0 deletions Tests/StencilTests/FilterSpec.swift
Original file line number Diff line number Diff line change
Expand Up @@ -147,4 +147,13 @@ func testFilter() {
try expect(result) == "OneTwo"
}
}

describe("safe filter") {
let template = Template(templateString: "{{ \"<'>\"|safe }}")

$0.it("prevents auto escaping") {
let result = try template.render()
try expect(result) == "<'>"
}
}
}
13 changes: 13 additions & 0 deletions Tests/StencilTests/NodeSpec.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ class ErrorNode : NodeType {
func testNode() {
describe("Node") {
let context = Context(dictionary: [
"title": escaped(html: "'Hello World'"),
"name": "Kyle",
"age": 27,
"items": [1, 2, 3],
Expand All @@ -34,6 +35,18 @@ func testNode() {
let node = VariableNode(variable: Variable("age"))
try expect(try node.render(context)) == "27"
}

$0.describe("escaping") {
$0.it("automatically escapes unescaped html") {
let node = VariableNode(variable: Variable("\"'Hello World'\""))
try expect(try node.render(context)) == "&39;Hello World&39;"
}

$0.it("doesn't double escape already escaped HTML") {
let node = VariableNode(variable: Variable("title"))
try expect(try node.render(context)) == "'Hello World'"
}
}
}

$0.describe("rendering nodes") {
Expand Down
17 changes: 17 additions & 0 deletions docs/builtins.rst
Original file line number Diff line number Diff line change
Expand Up @@ -286,3 +286,20 @@ Join an array of items.
{{ value|join:", " }}

.. note:: The value MUST be an array.

``safe``
~~~~~~~~

Marks a value as safe and thus prevents the value from being automatically
escaped.

.. code-block:: html+django

{{ name|safe }}

Other filters may remove the safe state, for example filtering through `safe`
and then `lowercase` will result in an unsafe lowercased string.

.. code-block:: html+django

{{ name|safe|lowercase }}