From 2fbca93ae85912c1d9a67e056795d50c114fe7c6 Mon Sep 17 00:00:00 2001 From: Yorick Peterse Date: Wed, 12 Mar 2014 23:13:28 +0100 Subject: [PATCH] Supported for parsing nested elements. --- lib/oga/parser.y | 25 ++++++++----------------- spec/oga/parser/elements_spec.rb | 29 +++++++++++++++++++++++------ 2 files changed, 31 insertions(+), 23 deletions(-) diff --git a/lib/oga/parser.y b/lib/oga/parser.y index 8d77ca1..9a5a518 100644 --- a/lib/oga/parser.y +++ b/lib/oga/parser.y @@ -76,23 +76,7 @@ rule # Elements element - #

- : element_open T_ELEM_CLOSE { s(:element, val[0]) } - - #

- | element_open attributes T_ELEM_CLOSE - { - s(:element, val[0], val[1]) - } - - #

foo

- | element_open text T_ELEM_CLOSE - { - s(:element, val[0], nil, val[1]) - } - - #

Bar

- | element_open attributes text T_ELEM_CLOSE + : element_open attributes element_body T_ELEM_CLOSE { s(:element, val[0], val[1], val[2]) } @@ -106,10 +90,17 @@ rule | T_ELEM_OPEN T_ELEM_NS T_ELEM_NAME { [val[1], val[2]] } ; + element_body + : text + | element + | /* none */ { nil } + ; + # Attributes attributes : attributes_ { s(:attributes, val[0]) } + | /* none */ { nil } ; attributes_ diff --git a/spec/oga/parser/elements_spec.rb b/spec/oga/parser/elements_spec.rb index fb5b25f..a36c81e 100644 --- a/spec/oga/parser/elements_spec.rb +++ b/spec/oga/parser/elements_spec.rb @@ -3,7 +3,10 @@ require 'spec_helper' describe Oga::Parser do context 'elements' do example 'parse an empty element' do - parse_html('

').should == s(:document, s(:element, nil, 'p')) + parse_html('

').should == s( + :document, + s(:element, nil, 'p', nil, nil) + ) end example 'parse an element with text' do @@ -16,14 +19,14 @@ describe Oga::Parser do example 'parse an element with a single attribute' do parse_html('

').should == s( :document, - s(:element, nil, 'p', s(:attributes, s(:attribute, 'foo'))) + s(:element, nil, 'p', s(:attributes, s(:attribute, 'foo')), nil) ) end example 'parse an element with a single attribute with a value' do parse_html('

').should == s( :document, - s(:element, nil, 'p', s(:attributes, s(:attribute, 'foo', 'bar'))) + s(:element, nil, 'p', s(:attributes, s(:attribute, 'foo', 'bar')), nil) ) end @@ -38,7 +41,8 @@ describe Oga::Parser do :attributes, s(:attribute, 'foo', 'bar'), s(:attribute, 'baz', 'bad') - ) + ), + nil ) ) end @@ -59,14 +63,27 @@ describe Oga::Parser do example 'parse an element with a namespace' do parse_html('

').should == s( :document, - s(:element, 'foo', 'p') + s(:element, 'foo', 'p', nil, nil) ) end example 'parse an element with a namespace and an attribute' do parse_html('

').should == s( :document, - s(:element, 'foo', 'p', s(:attributes, s(:attribute, 'class', 'bar'))) + s( + :element, + 'foo', + 'p', + s(:attributes, s(:attribute, 'class', 'bar')), + nil + ) + ) + end + + example 'parse an element nested inside another element' do + parse_html('

').should == s( + :document, + s(:element, nil, 'p', nil, s(:element, nil, 'a', nil, nil)) ) end end