lib/string_doc/node.rb in pakyow-presenter-1.0.0.rc3 vs lib/string_doc/node.rb in pakyow-presenter-1.0.0.rc4

- old
+ new

@@ -33,11 +33,11 @@ end attr_reader :attributes # @api private - attr_reader :node, :parent, :children, :tag_open_start, :tag_open_end, :tag_close, :transforms, :significance, :labels + attr_reader :node, :parent, :children, :tag_open_start, :tag_open_end, :tag_close, :transforms, :significance # @api private attr_writer :parent include Pakyow::Support::Inspectable @@ -48,17 +48,19 @@ def initialize(tag_open_start = "", attributes = Attributes.new, tag_open_end = "", children = StringDoc.empty, tag_close = "", parent: nil, significance: [], labels: {}) @tag_open_start, @attributes, @tag_open_end, @children, @tag_close = tag_open_start, attributes, tag_open_end, children, tag_close @parent, @labels, @significance = parent, labels, significance @transforms = { high: [], default: [], low: [] } @pipeline = nil + @finalized_labels = {} end # @api private def initialize_copy(_) super @labels = @labels.deep_dup + @finalized_labels = @finalized_labels.deep_dup @attributes = @attributes.dup @children = @children.dup @significance = @significance.dup @transforms = @transforms.each_with_object({}) { |(key, value), hash| @@ -76,19 +78,35 @@ instance.instance_variable_set(:@tag_open_end, @tag_open_end) instance.instance_variable_set(:@tag_close, @tag_close) instance.instance_variable_set(:@parent, @parent) instance.instance_variable_set(:@significance, @significance) instance.instance_variable_set(:@transforms, @transforms) + instance.instance_variable_set(:@finalized_labels, @finalized_labels) instance.instance_variable_set(:@attributes, @attributes.dup) instance.instance_variable_set(:@children, @children.is_a?(StringDoc) ? @children.soft_copy : @children.dup) instance.instance_variable_set(:@labels, @labels.deep_dup) instance.instance_variable_set(:@pipeline, @pipeline.dup) instance end + def finalize_labels(keep: []) + @finalized_labels = @labels + @labels = keep.each_with_object({}) { |key, hash| + hash[key] = @finalized_labels.delete(key).deep_dup + } + + if children.is_a?(StringDoc) + children.finalize_labels(keep: keep) + end + end + + def labels + @labels.merge(@finalized_labels) + end + def freeze(*) pipeline super end @@ -143,13 +161,22 @@ @parent.replace_node(self, replacement) end # Removes the node. # - def remove - set_label(:removed, true) + def remove(label = true, descend = true) + if label + set_label(:removed, true) + end + @parent.remove_node(self) + + if descend && children.is_a?(StringDoc) + children.each do |child| + child.remove(label, descend) + end + end end REGEX_TAGS = /<[^>]*>/ # Returns the text of this node and all children, joined together. @@ -219,25 +246,34 @@ end # Returns the value for label with +name+. # def label(name) - @labels[name.to_sym] + name = name.to_sym + if @labels.key?(name) + @labels[name.to_sym] + else + @finalized_labels[name.to_sym] + end end # Returns true if label exists with +name+. # def labeled?(name) - @labels.key?(name.to_sym) + @labels.key?(name.to_sym) || @finalized_labels.key?(name.to_sym) end # Sets the label with +name+ and +value+. # def set_label(name, value) @labels[name.to_sym] = value end + def removed? + labeled?(:removed) + end + # Delete the label with +name+. # def delete_label(name) @labels.delete(name.to_sym) end @@ -261,10 +297,12 @@ output << children.to_s end output << tag_close end + + output end alias :to_html :render alias :to_xml :render # Returns the node as an xml string, without transforming. @@ -356,20 +394,20 @@ when NilClass return when StringDoc return_value.render(string, context: context); return when Node, MetaNode - current = return_value + if return_value.removed? + return + else + current = return_value + end else string << return_value.to_s; return end end - # Don't render if the node was removed during the transform. - # - if !current.is_a?(Node) || !current.labeled?(:removed) - current.render(string, context: context) - end + current.render(string, context: context) end def string_nodes [@tag_open_start, @attributes, @tag_open_end, @children, @tag_close] end