From 8898d08831c57fffee49f3aa7fd6188241f5ff93 Mon Sep 17 00:00:00 2001
From: Yorick Peterse
Date: Mon, 17 Mar 2014 21:03:54 +0100
Subject: [PATCH] Compacter parser AST.
The AST no longer uses the generic `element` type for element nodes but instead
changes the type based on the element type. That is, a
element now results
in an (p) node, in (link), etc.
---
lib/oga/parser.y | 8 ++--
spec/oga/parser/documents_spec.rb | 11 ++----
spec/oga/parser/elements_spec.rb | 46 +++++++++-------------
spec/oga/parser/html_void_elements_spec.rb | 29 ++++----------
4 files changed, 34 insertions(+), 60 deletions(-)
diff --git a/lib/oga/parser.y b/lib/oga/parser.y
index 6c86e5c..d844f1c 100644
--- a/lib/oga/parser.y
+++ b/lib/oga/parser.y
@@ -77,16 +77,18 @@ rule
element
: element_open attributes element_body T_ELEM_CLOSE
{
- s(:element, val[0], val[1], val[2])
+ namespace, name = val[0]
+
+ s(name, namespace, val[1], val[2])
}
;
element_open
#
').should == s(
:document,
- s(:element, nil, 'p', nil, s(:text, 'foo'))
+ s(:p, nil, nil, s(:text, 'foo'))
)
end
example 'parse an element with a single attribute' do
parse('').should == s(
:document,
- s(:element, nil, 'p', s(:attributes, s(:attribute, 'foo')), nil)
+ s(:p, nil, s(:attributes, s(:attribute, 'foo')), nil)
)
end
example 'parse an element with a single attribute with a value' do
parse('').should == s(
:document,
- s(:element, nil, 'p', s(:attributes, s(:attribute, 'foo', 'bar')), nil)
+ s(:p, nil, s(:attributes, s(:attribute, 'foo', 'bar')), nil)
)
end
@@ -34,9 +31,8 @@ describe Oga::Parser do
parse('').should == s(
:document,
s(
- :element,
+ :p,
nil,
- 'p',
s(
:attributes,
s(:attribute, 'foo', 'bar'),
@@ -51,9 +47,8 @@ describe Oga::Parser do
parse('
Bar
').should == s(
:document,
s(
- :element,
+ :p,
nil,
- 'p',
s(:attributes, s(:attribute, 'class', 'foo')),
s(:text, 'Bar')
)
@@ -63,7 +58,7 @@ describe Oga::Parser do
example 'parse an element with a namespace' do
parse('