Rough DOM building setup.

This commit is contained in:
Yorick Peterse 2014-03-28 08:59:48 +01:00
parent 6ae52c1b12
commit 2b250bbf42
7 changed files with 129 additions and 17 deletions

View File

@ -6,6 +6,9 @@ require_relative 'oga/xml/lexer'
require_relative 'oga/xml/parser' require_relative 'oga/xml/parser'
require_relative 'oga/xml/node' require_relative 'oga/xml/node'
require_relative 'oga/xml/element' require_relative 'oga/xml/element'
require_relative 'oga/xml/comment'
require_relative 'oga/xml/text'
require_relative 'oga/xml/document'
require_relative 'oga/xml/builder' require_relative 'oga/xml/builder'
require_relative 'oga/html/parser' require_relative 'oga/html/parser'

View File

@ -9,19 +9,68 @@ module Oga
class Builder < ::AST::Processor class Builder < ::AST::Processor
attr_reader :ast attr_reader :ast
## def on_document(node)
# @param [Oga::AST::Node] ast document = Document.new
# document.children = process_all(node)
def initialize(ast)
@ast = ast document.children.each do |child|
child.parent = document
end
return document
end
def on_comment(node)
return Comment.new(:text => node.children[0])
end end
def on_element(node) def on_element(node)
ns, name, attr, *children = *node
if attr
attr = process(attr)
end
if children
children = process_all(children)
end
element = Element.new(
:name => name,
:namespace => ns,
:attributes => attr,
:children => children
)
element.children.each_with_index do |child, index|
if index > 0
child.previous = element.children[index - 1]
end
if index + 1 <= element.children.length
child.next = element.children[index + 1]
end
child.parent = element
end
return element
end end
def on_text(node) def on_text(node)
return Text.new(:text => node.children[0])
end
alias_method :on_cdata, :on_text
def on_attributes(node)
pairs = process_all(node)
return Hash[pairs]
end
def on_attribute(node)
return *node
end end
end # Builder end # Builder
end # XML end # XML

13
lib/oga/xml/comment.rb Normal file
View File

@ -0,0 +1,13 @@
module Oga
module XML
##
#
class Comment < Node
attr_accessor :text
def to_xml
return "<!--#{text}-->"
end
end # Comment
end # XML
end # Oga

14
lib/oga/xml/document.rb Normal file
View File

@ -0,0 +1,14 @@
module Oga
module XML
##
# Class description
#
class Document < Node
attr_accessor :dtd, :xml_version, :encoding
def to_xml
return children.map(&:to_xml).join('')
end
end # Document
end # XML
end # Oga

View File

@ -5,14 +5,30 @@ module Oga
# attributes and child nodes. # attributes and child nodes.
# #
class Element < Node class Element < Node
## attr_accessor :name, :namespace, :attributes
# @param [String] name The name of the element
# @see Oga::XML::Node#initialize
#
def initialize(name, options = {})
super(options)
@name = name def after_initialize
@attributes ||= {}
end
def attribute(name)
return attributes[name]
end
alias_method :attr, :attribute
def to_xml
ns = namespace ? "#{namespace}:" : ''
body = children.map(&:to_xml).join('')
attrs = ''
attributes.each do |key, value|
attrs << "#{key}=#{value.inspect}"
end
attrs = " #{attrs}" unless attrs.empty?
return "<#{ns}#{name}#{attrs}>#{body}</#{name}>"
end end
end # Element end # Element
end # XML end # XML

View File

@ -1,20 +1,20 @@
module Oga module Oga
module XML module XML
## ##
# @!attribute [r] parent # @!attribute [rw] parent
# @return [Oga::XML::Node] # @return [Oga::XML::Node]
# #
# @!attribute [r] children # @!attribute [rw] children
# @return [Array<Oga::XML::Node>] # @return [Array<Oga::XML::Node>]
# #
# @!attribute [r] next # @!attribute [rw] next
# @return [Oga::XML::NOde] # @return [Oga::XML::NOde]
# #
# @!attribute [r] previous # @!attribute [rw] previous
# @return [Oga::XML::Node] # @return [Oga::XML::Node]
# #
class Node class Node
attr_reader :parent, :children, :next, :previous attr_accessor :parent, :children, :next, :previous
## ##
# @param [Hash] options # @param [Hash] options
@ -30,6 +30,10 @@ module Oga
options.each do |key, value| options.each do |key, value|
instance_variable_set("@#{key}", value) if respond_to?(key) instance_variable_set("@#{key}", value) if respond_to?(key)
end end
@children ||= []
after_initialize if respond_to?(:after_initialize)
end end
end # Element end # Element
end # XML end # XML

13
lib/oga/xml/text.rb Normal file
View File

@ -0,0 +1,13 @@
module Oga
module XML
##
#
class Text < Node
attr_accessor :text
def to_xml
return text.to_s
end
end # Text
end # XML
end # Oga