From 0a396043f85fc30134dcfff08b74f773b1545da0 Mon Sep 17 00:00:00 2001 From: Yorick Peterse Date: Tue, 11 Mar 2014 22:22:02 +0100 Subject: [PATCH] Support for parsing CDATA tags. --- lib/oga/parser.y | 14 +++++++++++++- spec/oga/parser/cdata_spec.rb | 20 ++++++++++++++++++++ 2 files changed, 33 insertions(+), 1 deletion(-) create mode 100644 spec/oga/parser/cdata_spec.rb diff --git a/lib/oga/parser.y b/lib/oga/parser.y index de2abd7..e67623d 100644 --- a/lib/oga/parser.y +++ b/lib/oga/parser.y @@ -1,8 +1,9 @@ class Oga::Parser token T_NEWLINE T_SPACE -token T_STRING +token T_STRING T_TEXT token T_DOCTYPE_START T_DOCTYPE_END T_DOCTYPE_TYPE +token T_CDATA_START T_CDATA_END options no_result_var @@ -19,6 +20,7 @@ rule expression : doctype + | cdata ; # Doctypes @@ -46,6 +48,16 @@ rule } ; + # CDATA tags + + cdata + # + : T_CDATA_START T_CDATA_END { s(:cdata) } + + # + | T_CDATA_START T_TEXT T_CDATA_END { s(:cdata, val[1]) } + ; + whitespaces : whitespaces whitespace | whitespace diff --git a/spec/oga/parser/cdata_spec.rb b/spec/oga/parser/cdata_spec.rb new file mode 100644 index 0000000..d7aecce --- /dev/null +++ b/spec/oga/parser/cdata_spec.rb @@ -0,0 +1,20 @@ +require 'spec_helper' + +describe Oga::Parser do + context 'cdata tags' do + example 'parse a cdata tag' do + parse_html('').should == s(:document, s(:cdata, 'foo')) + end + + example 'parse an element inside a cdata tag' do + parse_html('foo

]]>').should == s( + :document, + s(:cdata, '

foo

') + ) + end + + example 'parse double brackets inside a cdata tag' do + parse_html('').should == s(:document, s(:cdata, ']]')) + end + end +end