From 6ae52c1b12bf4685fb5edde8667ea35e99bae06e Mon Sep 17 00:00:00 2001 From: Yorick Peterse Date: Wed, 26 Mar 2014 18:12:00 +0100 Subject: [PATCH] Initial rough sketches for the DOM API. --- lib/oga.rb | 3 +++ lib/oga/xml/builder.rb | 28 ++++++++++++++++++++++++++++ lib/oga/xml/element.rb | 19 +++++++++++++++++++ lib/oga/xml/node.rb | 36 ++++++++++++++++++++++++++++++++++++ 4 files changed, 86 insertions(+) create mode 100644 lib/oga/xml/builder.rb create mode 100644 lib/oga/xml/element.rb create mode 100644 lib/oga/xml/node.rb diff --git a/lib/oga.rb b/lib/oga.rb index 2a433c3..657670f 100644 --- a/lib/oga.rb +++ b/lib/oga.rb @@ -4,5 +4,8 @@ require_relative 'oga/ast/node' require_relative 'oga/xml/lexer' require_relative 'oga/xml/parser' +require_relative 'oga/xml/node' +require_relative 'oga/xml/element' +require_relative 'oga/xml/builder' require_relative 'oga/html/parser' diff --git a/lib/oga/xml/builder.rb b/lib/oga/xml/builder.rb new file mode 100644 index 0000000..f497196 --- /dev/null +++ b/lib/oga/xml/builder.rb @@ -0,0 +1,28 @@ +module Oga + module XML + ## + # Class for building a DOM tree of XML/HTML nodes. + # + # @!attribute [r] ast + # @return [Oga::AST::Node] + # + class Builder < ::AST::Processor + attr_reader :ast + + ## + # @param [Oga::AST::Node] ast + # + def initialize(ast) + @ast = ast + end + + def on_element(node) + + end + + def on_text(node) + + end + end # Builder + end # XML +end # Oga diff --git a/lib/oga/xml/element.rb b/lib/oga/xml/element.rb new file mode 100644 index 0000000..bb55eb5 --- /dev/null +++ b/lib/oga/xml/element.rb @@ -0,0 +1,19 @@ +module Oga + module XML + ## + # Class that contains information about an XML element such as the name, + # attributes and child nodes. + # + class Element < Node + ## + # @param [String] name The name of the element + # @see Oga::XML::Node#initialize + # + def initialize(name, options = {}) + super(options) + + @name = name + end + end # Element + end # XML +end # Oga diff --git a/lib/oga/xml/node.rb b/lib/oga/xml/node.rb new file mode 100644 index 0000000..8c087cf --- /dev/null +++ b/lib/oga/xml/node.rb @@ -0,0 +1,36 @@ +module Oga + module XML + ## + # @!attribute [r] parent + # @return [Oga::XML::Node] + # + # @!attribute [r] children + # @return [Array] + # + # @!attribute [r] next + # @return [Oga::XML::NOde] + # + # @!attribute [r] previous + # @return [Oga::XML::Node] + # + class Node + attr_reader :parent, :children, :next, :previous + + ## + # @param [Hash] options + # + # @option options [Array] :children The child nodes of the current + # element. + # + # @option options [Oga::XML::Node] :parent The parent node. + # @option options [Oga::XML::Node] :next The following node. + # @option options [Oga::XML::Node] :previous The previous node. + # + def initialize(options = {}) + options.each do |key, value| + instance_variable_set("@#{key}", value) if respond_to?(key) + end + end + end # Element + end # XML +end # Oga