diff --git a/lib/oga/parser.y b/lib/oga/parser.y index 9a5a518..8c03af9 100644 --- a/lib/oga/parser.y +++ b/lib/oga/parser.y @@ -1,6 +1,5 @@ class Oga::Parser -token T_NEWLINE T_SPACE token T_STRING T_TEXT token T_DOCTYPE_START T_DOCTYPE_END T_DOCTYPE_TYPE token T_CDATA_START T_CDATA_END @@ -90,10 +89,19 @@ rule | T_ELEM_OPEN T_ELEM_NS T_ELEM_NAME { [val[1], val[2]] } ; - element_body - : text + elements + : elements element { val } | element - | /* none */ { nil } + ; + + element_body + : texts + | texts elements { val } + | texts elements texts { val } + | elements + | elements texts { val } + | elements texts elements { val } + | /* none */ { nil } ; # Attributes @@ -122,16 +130,9 @@ rule : T_TEXT { s(:text, val[0]) } ; - # Whitespace - - whitespaces - : whitespaces whitespace - | whitespace - ; - - whitespace - : T_NEWLINE - | T_SPACE + texts + : texts text { val } + | text ; end diff --git a/spec/oga/lexer/documents_spec.rb b/spec/oga/lexer/documents_spec.rb index 5cd305c..629a53c 100644 --- a/spec/oga/lexer/documents_spec.rb +++ b/spec/oga/lexer/documents_spec.rb @@ -1,6 +1,6 @@ require 'spec_helper' -describe Oga::Parser do +describe Oga::Lexer do context 'HTML documents' do example 'lex a basic HTML document' do html = <<-EOF diff --git a/spec/oga/parser/documents_spec.rb b/spec/oga/parser/documents_spec.rb new file mode 100644 index 0000000..cfc5b4e --- /dev/null +++ b/spec/oga/parser/documents_spec.rb @@ -0,0 +1,51 @@ +require 'spec_helper' + +describe Oga::Parser do + context 'HTML documents' do + example 'parse a basic HTML document' do + html = <<-EOF + + + +Title + + + + EOF + + parse_html(html).should == s( + :document, + s(:doctype), + + # + s( + :element, + nil, + 'html', + nil, + + # + s( + :element, + nil, + 'head', + nil, + + # + s( + :element, + nil, + 'title', + nil, + s(:text, 'Title') + ) + ), + + # <body> + s(:element, nil, 'body', nil, nil) + ), + s(:text, "\n") + ) + end + end +end diff --git a/spec/oga/parser/elements_spec.rb b/spec/oga/parser/elements_spec.rb index a36c81e..e366850 100644 --- a/spec/oga/parser/elements_spec.rb +++ b/spec/oga/parser/elements_spec.rb @@ -86,5 +86,63 @@ describe Oga::Parser do s(:element, nil, 'p', nil, s(:element, nil, 'a', nil, nil)) ) end + + example 'parse an element with children text, element' do + parse_html('<p>Foo<a>Bar</a></p>').should == s( + :document, + s( + :element, + nil, + 'p', + nil, + s(:text, 'Foo'), + s(:element, nil, 'a', nil, s(:text, 'Bar')) + ) + ) + end + + example 'parse an element with children text, element, text' do + parse_html('<p>Foo<a>Bar</a>Baz</p>').should == s( + :document, + s( + :element, + nil, + 'p', + nil, + s(:text, 'Foo'), + s(:element, nil, 'a', nil, s(:text, 'Bar')), + s(:text, 'Baz') + ) + ) + end + + example 'parse an element with children element, text' do + parse_html('<p><a>Bar</a>Baz</p>').should == s( + :document, + s( + :element, + nil, + 'p', + nil, + s(:element, nil, 'a', nil, s(:text, 'Bar')), + s(:text, 'Baz') + ) + ) + end + + example 'parse an element with children element, text, element' do + parse_html('<p><a>Bar</a>Baz<span>Da</span></p>').should == s( + :document, + s( + :element, + nil, + 'p', + nil, + s(:element, nil, 'a', nil, s(:text, 'Bar')), + s(:text, 'Baz'), + s(:element, nil, 'span', nil, s(:text, 'Da')) + ) + ) + end end end diff --git a/spec/oga/parser/general_spec.rb b/spec/oga/parser/general_spec.rb index fcbf3c0..9dd2fef 100644 --- a/spec/oga/parser/general_spec.rb +++ b/spec/oga/parser/general_spec.rb @@ -4,4 +4,8 @@ describe Oga::Parser do example 'parse regular text' do parse_html('foo').should == s(:document, s(:text, 'foo')) end + + example 'parse a newline' do + parse_html("\n").should == s(:document, s(:text, "\n")) + end end