diff --git a/README.md b/README.md index b5db320..cd67ffd 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/lib/ejs.rb b/lib/ejs.rb index 686649a..4648f5f 100644 --- a/lib/ejs.rb +++ b/lib/ejs.rb @@ -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 @@ -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 diff --git a/test/test_ejs.rb b/test/test_ejs.rb index ed760da..56d572e 100644 --- a/test/test_ejs.rb +++ b/test/test_ejs.rb @@ -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 @@ -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 @@ -95,7 +95,7 @@ class EJSEvaluationTest < Test::Unit::TestCase test "iteration" do template = "" + %>
  • <%- people[i] %>
  • <% } %>" result = EJS.evaluate(template, :people => ["Moe", "Larry", "Curly"]) assert_equal "", result end @@ -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 @@ -157,16 +157,16 @@ class EJSEvaluationTest < Test::Unit::TestCase end test "escaping" do - template = "<%- foobar %>" + template = "<%= foobar %>" assert_equal "<b>Foo Bar</b>", EJS.evaluate(template, { :foobar => "Foo Bar" }) - template = "<%- foobar %>" + template = "<%= foobar %>" assert_equal "Foo & Bar", EJS.evaluate(template, { :foobar => "Foo & Bar" }) - template = "<%- foobar %>" + template = "<%= foobar %>" assert_equal ""Foo Bar"", EJS.evaluate(template, { :foobar => '"Foo Bar"' }) - template = "<%- foobar %>" + template = "<%= foobar %>" assert_equal "'Foo Bar'", EJS.evaluate(template, { :foobar => "'Foo Bar'" }) end