Supported for parsing nested elements.

This commit is contained in:
Yorick Peterse 2014-03-12 23:13:28 +01:00
parent 8cfa81aed9
commit 2fbca93ae8
2 changed files with 31 additions and 23 deletions

View File

@ -76,23 +76,7 @@ rule
# Elements # Elements
element element
# <p></p> : element_open attributes element_body T_ELEM_CLOSE
: element_open T_ELEM_CLOSE { s(:element, val[0]) }
# <p class="foo"></p>
| element_open attributes T_ELEM_CLOSE
{
s(:element, val[0], val[1])
}
# <p>foo</p>
| element_open text T_ELEM_CLOSE
{
s(:element, val[0], nil, val[1])
}
# <p class="foo">Bar</p>
| element_open attributes text T_ELEM_CLOSE
{ {
s(:element, val[0], val[1], val[2]) 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]] } | T_ELEM_OPEN T_ELEM_NS T_ELEM_NAME { [val[1], val[2]] }
; ;
element_body
: text
| element
| /* none */ { nil }
;
# Attributes # Attributes
attributes attributes
: attributes_ { s(:attributes, val[0]) } : attributes_ { s(:attributes, val[0]) }
| /* none */ { nil }
; ;
attributes_ attributes_

View File

@ -3,7 +3,10 @@ require 'spec_helper'
describe Oga::Parser do describe Oga::Parser do
context 'elements' do context 'elements' do
example 'parse an empty element' do example 'parse an empty element' do
parse_html('<p></p>').should == s(:document, s(:element, nil, 'p')) parse_html('<p></p>').should == s(
:document,
s(:element, nil, 'p', nil, nil)
)
end end
example 'parse an element with text' do example 'parse an element with text' do
@ -16,14 +19,14 @@ describe Oga::Parser do
example 'parse an element with a single attribute' do example 'parse an element with a single attribute' do
parse_html('<p foo></p>').should == s( parse_html('<p foo></p>').should == s(
:document, :document,
s(:element, nil, 'p', s(:attributes, s(:attribute, 'foo'))) s(:element, nil, 'p', s(:attributes, s(:attribute, 'foo')), nil)
) )
end end
example 'parse an element with a single attribute with a value' do example 'parse an element with a single attribute with a value' do
parse_html('<p foo="bar"></p>').should == s( parse_html('<p foo="bar"></p>').should == s(
:document, :document,
s(:element, nil, 'p', s(:attributes, s(:attribute, 'foo', 'bar'))) s(:element, nil, 'p', s(:attributes, s(:attribute, 'foo', 'bar')), nil)
) )
end end
@ -38,7 +41,8 @@ describe Oga::Parser do
:attributes, :attributes,
s(:attribute, 'foo', 'bar'), s(:attribute, 'foo', 'bar'),
s(:attribute, 'baz', 'bad') s(:attribute, 'baz', 'bad')
) ),
nil
) )
) )
end end
@ -59,14 +63,27 @@ describe Oga::Parser do
example 'parse an element with a namespace' do example 'parse an element with a namespace' do
parse_html('<foo:p></p>').should == s( parse_html('<foo:p></p>').should == s(
:document, :document,
s(:element, 'foo', 'p') s(:element, 'foo', 'p', nil, nil)
) )
end end
example 'parse an element with a namespace and an attribute' do example 'parse an element with a namespace and an attribute' do
parse_html('<foo:p class="bar"></p>').should == s( parse_html('<foo:p class="bar"></p>').should == s(
:document, :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('<p><a></a></p>').should == s(
:document,
s(:element, nil, 'p', nil, s(:element, nil, 'a', nil, nil))
) )
end end
end end