Skip to content
This repository has been archived by the owner on Jun 10, 2018. It is now read-only.

Replace escape pattern #19

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
7 changes: 3 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,9 @@ template evaluation.

The EJS tag syntax is as follows:

* `<% ... %>` silently evaluates the statement inside the tags.
* `<%= ... %>` evaluates the expression inside the tags and inserts
its string value into the template output.
* `<%- ... %>` behaves like `<%= ... %>` but HTML-escapes its output.
* Unescaped buffering with `<%- code %>`
* Escapes html by default with `<%= code %>`
* Unbuffered code for conditionals etc `<% code %>`

If you have the [ExecJS](https://github.com/sstephenson/execjs/)
library and a suitable JavaScript runtime installed, you can pass a
Expand Down
8 changes: 4 additions & 4 deletions lib/ejs.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ module EJS
'u2029' => "\u2029"
}
JS_ESCAPES = JS_UNESCAPES.invert
JS_UNESCAPE_PATTERN = /\\(#{Regexp.union(JS_UNESCAPES.keys)})/
JS_ESCAPE_PATTERN = Regexp.union(JS_ESCAPES.keys)
JS_UNESCAPE_PATTERN = /\\(#{Regexp.union(JS_UNESCAPES.keys)})/

class << self
attr_accessor :escape_pattern
attr_accessor :evaluation_pattern
attr_accessor :interpolation_pattern
attr_accessor :escape_pattern

# Compiles an EJS template to a JavaScript function. The compiled
# function takes an optional argument, an object specifying local
Expand Down Expand Up @@ -95,7 +95,7 @@ def escape_function
end
end

self.escape_pattern = /<%=([\s\S]+?)%>/
self.evaluation_pattern = /<%([\s\S]+?)%>/
self.interpolation_pattern = /<%=([\s\S]+?)%>/
self.escape_pattern = /<%-([\s\S]+?)%>/
self.interpolation_pattern = /<%-([\s\S]+?)%>/
end
24 changes: 12 additions & 12 deletions test/test_ejs.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,13 @@ class EJSCompilationTest < Test::Unit::TestCase
extend TestHelper

test "compile" do
result = EJS.compile("Hello <%= name %>")
result = EJS.compile("Hello <%- name %>")
assert_match FUNCTION_PATTERN, result
assert_no_match(/Hello \<%= name %\>/, result)
assert_no_match(/Hello \<%- name %\>/, result)
end

test "compile with custom syntax" do
standard_result = EJS.compile("Hello <%= name %>")
standard_result = EJS.compile("Hello <%- name %>")
braced_result = EJS.compile("Hello {{= name }}", BRACE_SYNTAX)

assert_match FUNCTION_PATTERN, braced_result
Expand Down Expand Up @@ -73,17 +73,17 @@ class EJSEvaluationTest < Test::Unit::TestCase
extend TestHelper

test "quotes" do
template = "<%= thing %> is gettin' on my noives!"
template = "<%- thing %> is gettin' on my noives!"
assert_equal "This is gettin' on my noives!", EJS.evaluate(template, :thing => "This")
end

test "backslashes" do
template = "<%= thing %> is \\ridanculous"
template = "<%- thing %> is \\ridanculous"
assert_equal "This is \\ridanculous", EJS.evaluate(template, :thing => "This")
end

test "backslashes into interpolation" do
template = %q{<%= "Hello \"World\"" %>}
template = %q{<%- "Hello \"World\"" %>}
assert_equal 'Hello "World"', EJS.evaluate(template)
end

Expand All @@ -95,7 +95,7 @@ class EJSEvaluationTest < Test::Unit::TestCase
test "iteration" do
template = "<ul><%
for (var i = 0; i < people.length; i++) {
%><li><%= people[i] %></li><% } %></ul>"
%><li><%- people[i] %></li><% } %></ul>"
result = EJS.evaluate(template, :people => ["Moe", "Larry", "Curly"])
assert_equal "<ul><li>Moe</li><li>Larry</li><li>Curly</li></ul>", result
end
Expand All @@ -118,7 +118,7 @@ class EJSEvaluationTest < Test::Unit::TestCase
end

test "newlines and tabs" do
template = "This\n\t\tis: <%= x %>.\n\tok.\nend."
template = "This\n\t\tis: <%- x %>.\n\tok.\nend."
assert_equal "This\n\t\tis: that.\n\tok.\nend.", EJS.evaluate(template, :x => "that")
end

Expand Down Expand Up @@ -157,16 +157,16 @@ class EJSEvaluationTest < Test::Unit::TestCase
end

test "escaping" do
template = "<%- foobar %>"
template = "<%= foobar %>"
assert_equal "&lt;b&gt;Foo Bar&lt;&#x2F;b&gt;", EJS.evaluate(template, { :foobar => "<b>Foo Bar</b>" })

template = "<%- foobar %>"
template = "<%= foobar %>"
assert_equal "Foo &amp; Bar", EJS.evaluate(template, { :foobar => "Foo & Bar" })

template = "<%- foobar %>"
template = "<%= foobar %>"
assert_equal "&quot;Foo Bar&quot;", EJS.evaluate(template, { :foobar => '"Foo Bar"' })

template = "<%- foobar %>"
template = "<%= foobar %>"
assert_equal "&#x27;Foo Bar&#x27;", EJS.evaluate(template, { :foobar => "'Foo Bar'" })
end

Expand Down