diff --git a/browser_tests.rb b/browser_tests.rb index 11766bbc..27bbe615 100755 --- a/browser_tests.rb +++ b/browser_tests.rb @@ -105,6 +105,10 @@ def view_template File.open("fixtures/xss.txt") do |file| file.each_line do |line| div(class: line) { line } + svg do |s| + s.cdata(line) + s.cdata { line } + end end end end diff --git a/lib/phlex/svg.rb b/lib/phlex/svg.rb index e5536fe3..33cdfbb9 100644 --- a/lib/phlex/svg.rb +++ b/lib/phlex/svg.rb @@ -15,6 +15,20 @@ def filename nil end + def cdata(content = nil, &block) + state = @_state + return unless state.should_render? + + if !block && String === content + state.buffer << "", "]]>]]" + elsif block && nil == content + state.buffer << "", "]]>]]" + else + + raise Phlex::ArgumentError.new("Expected a String or block.") + end + end + def tag(name, **attributes, &) state = @_state block_given = block_given? diff --git a/quickdraw/svg.test.rb b/quickdraw/svg.test.rb index 756c76b2..7e533876 100644 --- a/quickdraw/svg.test.rb +++ b/quickdraw/svg.test.rb @@ -16,3 +16,25 @@ def view_template component = Class.new(Phlex::SVG) assert_equal component.new.content_type, "image/svg+xml" end + +test "cdata with string" do + component = Class.new(Phlex::SVG) do + def view_template + cdata("Hello, <[[test]]> World!") + end + end + + assert_equal component.call, %(]]) +end + +test "cdata with block" do + component = Class.new(Phlex::SVG) do + def view_template + cdata do + path(d: "123") + end + end + end + + assert_equal component.call, %(]]>) +end