Revert "Compacter parser AST."

Although this AST is compacter it will result in conflicts between (text),
(attributes) and (attribute) nodes in regular XML documents. This is due to XML
allowing elements with these names (unlike in HTML).

This reverts commit 8898d08831.
This commit is contained in:
Yorick Peterse 2014-03-18 18:55:16 +01:00
parent 9687dd379f
commit 7271e74396
4 changed files with 60 additions and 34 deletions

View File

@ -77,18 +77,16 @@ rule
element
: element_open attributes element_body T_ELEM_CLOSE
{
namespace, name = val[0]
s(name, namespace, val[1], val[2])
s(:element, val[0], val[1], val[2])
}
;
element_open
# <p>
: T_ELEM_OPEN T_ELEM_NAME { [nil, val[1].to_sym] }
: T_ELEM_OPEN T_ELEM_NAME { [nil, val[1]] }
# <foo:p>
| T_ELEM_OPEN T_ELEM_NS T_ELEM_NAME { [val[1], val[2].to_sym] }
| T_ELEM_OPEN T_ELEM_NS T_ELEM_NAME { [val[1], val[2]] }
;
elements

View File

@ -20,24 +20,27 @@ describe Oga::Parser do
# <html>
s(
:html,
:element,
nil,
'html',
nil,
s(:text, "\n"),
# <head>
s(
:head,
:element,
nil,
'head',
nil,
s(:text, "\n"),
# <title>
s(
:title,
:element,
nil,
'title',
nil,
s(:text, 'Title')
),
@ -47,7 +50,7 @@ describe Oga::Parser do
# <body>
s(:text, "\n"),
s(:body, nil, nil, nil),
s(:element, nil, 'body', nil, nil),
s(:text, "\n")
),
s(:text, "\n")

View File

@ -3,27 +3,30 @@ require 'spec_helper'
describe Oga::Parser do
context 'elements' do
example 'parse an empty element' do
parse('<p></p>').should == s(:document, s(:p, nil, nil, nil))
parse('<p></p>').should == s(
:document,
s(:element, nil, 'p', nil, nil)
)
end
example 'parse an element with text' do
parse('<p>foo</p>').should == s(
:document,
s(:p, nil, nil, s(:text, 'foo'))
s(:element, nil, 'p', nil, s(:text, 'foo'))
)
end
example 'parse an element with a single attribute' do
parse('<p foo></p>').should == s(
:document,
s(:p, nil, s(:attributes, s(:attribute, 'foo')), nil)
s(:element, nil, 'p', s(:attributes, s(:attribute, 'foo')), nil)
)
end
example 'parse an element with a single attribute with a value' do
parse('<p foo="bar"></p>').should == s(
:document,
s(:p, nil, s(:attributes, s(:attribute, 'foo', 'bar')), nil)
s(:element, nil, 'p', s(:attributes, s(:attribute, 'foo', 'bar')), nil)
)
end
@ -31,8 +34,9 @@ describe Oga::Parser do
parse('<p foo="bar" baz="bad"></p>').should == s(
:document,
s(
:p,
:element,
nil,
'p',
s(
:attributes,
s(:attribute, 'foo', 'bar'),
@ -47,8 +51,9 @@ describe Oga::Parser do
parse('<p class="foo">Bar</p>').should == s(
:document,
s(
:p,
:element,
nil,
'p',
s(:attributes, s(:attribute, 'class', 'foo')),
s(:text, 'Bar')
)
@ -58,7 +63,7 @@ describe Oga::Parser do
example 'parse an element with a namespace' do
parse('<foo:p></p>').should == s(
:document,
s(:p, 'foo', nil, nil)
s(:element, 'foo', 'p', nil, nil)
)
end
@ -66,8 +71,9 @@ describe Oga::Parser do
parse('<foo:p class="bar"></p>').should == s(
:document,
s(
:p,
:element,
'foo',
'p',
s(:attributes, s(:attribute, 'class', 'bar')),
nil
)
@ -77,7 +83,7 @@ describe Oga::Parser do
example 'parse an element nested inside another element' do
parse('<p><a></a></p>').should == s(
:document,
s(:p, nil, nil, s(:a, nil, nil, nil))
s(:element, nil, 'p', nil, s(:element, nil, 'a', nil, nil))
)
end
@ -85,11 +91,12 @@ describe Oga::Parser do
parse('<p>Foo<a>Bar</a></p>').should == s(
:document,
s(
:p,
:element,
nil,
'p',
nil,
s(:text, 'Foo'),
s(:a, nil, nil, s(:text, 'Bar'))
s(:element, nil, 'a', nil, s(:text, 'Bar'))
)
)
end
@ -98,11 +105,12 @@ describe Oga::Parser do
parse('<p>Foo<a>Bar</a>Baz</p>').should == s(
:document,
s(
:p,
:element,
nil,
'p',
nil,
s(:text, 'Foo'),
s(:a, nil, nil, s(:text, 'Bar')),
s(:element, nil, 'a', nil, s(:text, 'Bar')),
s(:text, 'Baz')
)
)
@ -112,10 +120,11 @@ describe Oga::Parser do
parse('<p><a>Bar</a>Baz</p>').should == s(
:document,
s(
:p,
:element,
nil,
'p',
nil,
s(:a, nil, nil, s(:text, 'Bar')),
s(:element, nil, 'a', nil, s(:text, 'Bar')),
s(:text, 'Baz')
)
)
@ -125,12 +134,13 @@ describe Oga::Parser do
parse('<p><a>Bar</a>Baz<span>Da</span></p>').should == s(
:document,
s(
:p,
:element,
nil,
'p',
nil,
s(:a, nil, nil, s(:text, 'Bar')),
s(:element, nil, 'a', nil, s(:text, 'Bar')),
s(:text, 'Baz'),
s(:span, nil, nil, s(:text, 'Da'))
s(:element, nil, 'span', nil, s(:text, 'Da'))
)
)
end

View File

@ -5,14 +5,14 @@ describe Oga::Parser do
example 'parse a void element that omits the closing /' do
parse('<link>', :html => true).should == s(
:document,
s(:link, nil, nil, nil)
s(:element, nil, 'link', nil, nil)
)
end
example 'parse a void element inside another element' do
parse('<head><link></head>', :html => true).should == s(
:document,
s(:head, nil, nil, s(:link, nil, nil, nil))
s(:element, nil, 'head', nil, s(:element, nil, 'link', nil, nil))
)
end
@ -20,12 +20,14 @@ describe Oga::Parser do
parse('<head><link href="foo.css"></head>', :html => true).should == s(
:document,
s(
:head,
:element,
nil,
'head',
nil,
s(
:link,
:element,
nil,
'link',
s(:attributes, s(:attribute, 'href', 'foo.css')),
nil
)
@ -37,11 +39,24 @@ describe Oga::Parser do
parse('<head><link><title>Foo</title></head>', :html => true).should == s(
:document,
s(
:head,
:element,
nil,
'head',
nil,
s(:link, nil, nil, nil),
s(:title, nil, nil, s(:text, 'Foo'))
s(
:element,
nil,
'link',
nil,
nil
),
s(
:element,
nil,
'title',
nil,
s(:text, 'Foo')
)
)
)
end