From ecf6851711ef8fe02eed6ca4db151281ede1d192 Mon Sep 17 00:00:00 2001 From: Yorick Peterse Date: Thu, 24 Apr 2014 21:16:31 +0200 Subject: [PATCH] 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 0968465f0c8ffc60c37fc5eebe59e76df6e43d4f. --- lib/oga.rb | 1 - lib/oga/xml/child_mixin.rb | 32 -------------------------------- lib/oga/xml/document.rb | 11 ----------- lib/oga/xml/node.rb | 11 ----------- lib/oga/xml/parser.y | 34 ++++++++++++++++++++++++++++++---- 5 files changed, 30 insertions(+), 59 deletions(-) delete mode 100644 lib/oga/xml/child_mixin.rb diff --git a/lib/oga.rb b/lib/oga.rb index b5f1592..7b6b11e 100644 --- a/lib/oga.rb +++ b/lib/oga.rb @@ -1,6 +1,5 @@ require 'set' -require_relative 'oga/xml/child_mixin' require_relative 'oga/xml/lexer' require_relative 'oga/xml/parser' require_relative 'oga/xml/node' diff --git a/lib/oga/xml/child_mixin.rb b/lib/oga/xml/child_mixin.rb deleted file mode 100644 index 152f612..0000000 --- a/lib/oga/xml/child_mixin.rb +++ /dev/null @@ -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 diff --git a/lib/oga/xml/document.rb b/lib/oga/xml/document.rb index 5dad83a..ca2e2ba 100644 --- a/lib/oga/xml/document.rb +++ b/lib/oga/xml/document.rb @@ -17,8 +17,6 @@ module Oga # @return [Oga::XML::XmlDeclaration] # class Document - include ChildMixin - attr_accessor :children, :doctype, :xml_declaration ## @@ -36,15 +34,6 @@ module Oga @children ||= [] end - ## - # @param [Array] nodes - # - def children=(nodes) - @children = nodes - - link_child_nodes - end - ## # Converts the document and its child nodes to XML. # diff --git a/lib/oga/xml/node.rb b/lib/oga/xml/node.rb index 5e1c956..0f2cea3 100644 --- a/lib/oga/xml/node.rb +++ b/lib/oga/xml/node.rb @@ -17,8 +17,6 @@ module Oga # @return [Oga::XML::Node] # class Node - include ChildMixin - attr_accessor :parent, :children, :next, :previous ## @@ -41,15 +39,6 @@ module Oga after_initialize if respond_to?(:after_initialize) end - ## - # @param [Array] nodes - # - def children=(nodes) - @children = nodes - - link_child_nodes - end - ## # Generates the inspect value for the current node. Sub classes can # overwrite the {#extra_inspect_data} method to customize the output diff --git a/lib/oga/xml/parser.y b/lib/oga/xml/parser.y index e007edd..4f132ca 100644 --- a/lib/oga/xml/parser.y +++ b/lib/oga/xml/parser.y @@ -120,6 +120,8 @@ rule element.children = val[1].flatten + link_children(element) + element } ; @@ -285,8 +287,7 @@ Unexpected #{name} with value #{value.inspect} on line #{@line}: children = [children] end - document = Document.new - child_nodes = [] + document = Document.new children.each do |child| if child.is_a?(Doctype) @@ -296,13 +297,38 @@ Unexpected #{name} with value #{value.inspect} on line #{@line}: document.xml_declaration = child else - child_nodes << child + document.children << child end end - document.children = child_nodes + link_children(document) return document 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: