diff options
author | seki <seki@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2016-01-02 07:19:58 +0000 |
---|---|---|
committer | seki <seki@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2016-01-02 07:19:58 +0000 |
commit | 1fa0581e87a848d6e84651dafb30e2e81ed8d487 (patch) | |
tree | 63f3e1b69d1fd82bafb1d20f637b49a0544a223a /test | |
parent | 5f19087c5343f8eed31599e6342bb8c4feb1c093 (diff) | |
download | ruby-1fa0581e87a848d6e84651dafb30e2e81ed8d487.tar.gz |
Allow ERB subclass to add token easily. [Feature #11936]
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@53412 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'test')
-rw-r--r-- | test/erb/test_erb.rb | 53 |
1 files changed, 53 insertions, 0 deletions
diff --git a/test/erb/test_erb.rb b/test/erb/test_erb.rb index 4e2d49a866..ee3333f66e 100644 --- a/test/erb/test_erb.rb +++ b/test/erb/test_erb.rb @@ -481,6 +481,59 @@ EOS def test_percent_after_etag assert_equal("1%", @erb.new("<%= 1 %>%", nil, "%").result) end + + def test_token_extension + extended_erb = Class.new(ERB) + extended_erb.module_eval do + def make_compiler(trim_mode) + compiler = Class.new(ERB::Compiler) + compiler.module_eval do + def compile_stag(stag, out, scanner) + case stag + when '<%==' + scanner.stag = stag + add_put_cmd(out, content) if content.size > 0 + self.content = '' + else + super + end + end + + def compile_content(stag, out) + case stag + when '<%==' + out.push("#{@insert_cmd}(::ERB::Util.html_escape(#{content}))") + else + super + end + end + + def make_scanner(src) + scanner = Class.new(ERB::Compiler::SimpleScanner) + scanner.module_eval do + def stags + ['<%=='] + super + end + end + scanner.new(src, @trim_mode, @percent) + end + end + compiler.new(trim_mode) + end + end + + src = <<~EOS + <% tag = '<>' %> + <%= tag %> + <%== tag %> + EOS + ans = <<~EOS + + <> + <> + EOS + assert_equal(ans, extended_erb.new(src).result) + end end class TestERBCoreWOStrScan < TestERBCore |