Revert "Move linking of child nodes to a dedicated mixin."

This doesn't actually make things any easier. It also introduces a weirdly
named mixin.

This reverts commit 0968465f0c.
This commit is contained in:
Yorick Peterse 2014-04-24 21:16:31 +02:00
parent 0968465f0c
commit ecf6851711
5 changed files with 30 additions and 59 deletions

View File

@ -1,6 +1,5 @@
require 'set' require 'set'
require_relative 'oga/xml/child_mixin'
require_relative 'oga/xml/lexer' 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'

View File

@ -1,32 +0,0 @@
module Oga
module XML
##
# Mixin that can be used to link child nodes together with their parent,
# previous and next following.
#
module ChildMixin
##
# Links child nodes together with each other and sets the parent node of
# each child.
#
def link_child_nodes
amount = children.length
children.each_with_index do |child, index|
prev_index = index - 1
next_index = index + 1
if index > 0
child.previous = children[prev_index]
end
if next_index <= amount
child.next = children[next_index]
end
child.parent = self
end
end
end # NodeHierarchy
end # XML
end # Oga

View File

@ -17,8 +17,6 @@ module Oga
# @return [Oga::XML::XmlDeclaration] # @return [Oga::XML::XmlDeclaration]
# #
class Document class Document
include ChildMixin
attr_accessor :children, :doctype, :xml_declaration attr_accessor :children, :doctype, :xml_declaration
## ##
@ -36,15 +34,6 @@ module Oga
@children ||= [] @children ||= []
end end
##
# @param [Array] nodes
#
def children=(nodes)
@children = nodes
link_child_nodes
end
## ##
# Converts the document and its child nodes to XML. # Converts the document and its child nodes to XML.
# #

View File

@ -17,8 +17,6 @@ module Oga
# @return [Oga::XML::Node] # @return [Oga::XML::Node]
# #
class Node class Node
include ChildMixin
attr_accessor :parent, :children, :next, :previous attr_accessor :parent, :children, :next, :previous
## ##
@ -41,15 +39,6 @@ module Oga
after_initialize if respond_to?(:after_initialize) after_initialize if respond_to?(:after_initialize)
end end
##
# @param [Array] nodes
#
def children=(nodes)
@children = nodes
link_child_nodes
end
## ##
# Generates the inspect value for the current node. Sub classes can # Generates the inspect value for the current node. Sub classes can
# overwrite the {#extra_inspect_data} method to customize the output # overwrite the {#extra_inspect_data} method to customize the output

View File

@ -120,6 +120,8 @@ rule
element.children = val[1].flatten element.children = val[1].flatten
link_children(element)
element element
} }
; ;
@ -285,8 +287,7 @@ Unexpected #{name} with value #{value.inspect} on line #{@line}:
children = [children] children = [children]
end end
document = Document.new document = Document.new
child_nodes = []
children.each do |child| children.each do |child|
if child.is_a?(Doctype) if child.is_a?(Doctype)
@ -296,13 +297,38 @@ Unexpected #{name} with value #{value.inspect} on line #{@line}:
document.xml_declaration = child document.xml_declaration = child
else else
child_nodes << child document.children << child
end end
end end
document.children = child_nodes link_children(document)
return document return document
end end
##
# Links the child nodes together by setting attributes such as the
# previous, next and parent node.
#
# @param [Oga::XML::Node] node
#
def link_children(node)
amount = node.children.length
node.children.each_with_index do |child, index|
prev_index = index - 1
next_index = index + 1
if index > 0
child.previous = node.children[prev_index]
end
if next_index <= amount
child.next = node.children[next_index]
end
child.parent = node
end
end
# vim: set ft=racc: # vim: set ft=racc: