Support for parsing CDATA tags.

This commit is contained in:
Yorick Peterse 2014-03-11 22:22:02 +01:00
parent c9592856f0
commit 0a396043f8
2 changed files with 33 additions and 1 deletions

View File

@ -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
# <![CDATA[]]>
: T_CDATA_START T_CDATA_END { s(:cdata) }
# <![CDATA[foo]]>
| T_CDATA_START T_TEXT T_CDATA_END { s(:cdata, val[1]) }
;
whitespaces
: whitespaces whitespace
| whitespace

View File

@ -0,0 +1,20 @@
require 'spec_helper'
describe Oga::Parser do
context 'cdata tags' do
example 'parse a cdata tag' do
parse_html('<![CDATA[foo]]>').should == s(:document, s(:cdata, 'foo'))
end
example 'parse an element inside a cdata tag' do
parse_html('<![CDATA[<p>foo</p>]]>').should == s(
:document,
s(:cdata, '<p>foo</p>')
)
end
example 'parse double brackets inside a cdata tag' do
parse_html('<![CDATA[]]]]>').should == s(:document, s(:cdata, ']]'))
end
end
end