Module Amrita::Node
In: lib/amrita/node.rb

Base module for HTML elements

Methods
*    +    amrita_id    apply_to_children    body    children    delete_if    each_element    each_element_with_id    each_node    has_id_element?    init_body    inspect    no_child?    to_node   
Included modules
Enumerable
Public Instance methods
init_body(&block) {|| ...}

set the block 's result to body

# File lib/amrita/node.rb, line 260
    def init_body(&block)
      if block_given?
        @body = to_node(yield)
      else
        @body = Null
      end
    end
body()

a Node has NullNode as body before init_body was called.

# File lib/amrita/node.rb, line 269
    def body
      if defined? @body
        @body
      else
        Null
      end
    end
no_child?()

test if it has any children

# File lib/amrita/node.rb, line 278
    def no_child?
      body == Null
    end
children()

return an Array of child Node or an empty Array if it does not have a body

# File lib/amrita/node.rb, line 283
    def children
      if no_child?
        []
      else
        [ body ]
      end
    end
apply_to_children(&block)
# File lib/amrita/node.rb, line 291
    def apply_to_children(&block)
      self
    end
delete_if(&block) {|self| ...}
# File lib/amrita/node.rb, line 295
    def delete_if(&block)
      if yield(self)
        Null
      else
        self
      end
    end
to_node(n)

generate a Node object

# File lib/amrita/node.rb, line 304
    def to_node(n)
      case n
      when nil, false
        Null
      when Node
        n
      when Array
        case n.size()
        when 0
          Null
        when 1
          to_node(n[0])
        else
          NodeArray.new(*n)
        end
      else
        TextElement.new(n.to_s) 
      end
    end
amrita_id()
# File lib/amrita/node.rb, line 325
    def amrita_id 
      nil
    end
inspect()
# File lib/amrita/node.rb, line 329
    def inspect
      to_ruby
    end
+(node)

Node can be added and they become NodeArray

# File lib/amrita/node.rb, line 334
    def +(node)
      NodeArray.new(self, to_node(node))
    end
*(n)

Copy a Node n times and generate NodeArray

# File lib/amrita/node.rb, line 339
    def *(n)
      raise "can't #{self.class} * #{n}(#{n.class})" unless n.kind_of?(Integer)
      a = (0...n).collect { |i| self }
      NodeArray.new(*a)
    end
each_node(&block) {|self| ...}

iterate on self and children

# File lib/amrita/node.rb, line 346
    def each_node(&block)
      c = children # save children before yield
      yield(self)
      c.each do |n|
        n.each_node(&block)
      end
    end
each_element(&block) {|node| ...}

iterate on child Elements

# File lib/amrita/node.rb, line 355
    def each_element(&block)
      each_node do |node|
        yield(node) if node.kind_of?(Element)
      end
    end
each_element_with_id(recursive=false, &block)

iterate on child Elements with id. If recursive == false, don't go to children of an Element with id.

# File lib/amrita/node.rb, line 364
    def each_element_with_id(recursive=false, &block)
      children.each do |node|
        node.each_element_with_id(recursive, &block)
      end
    end
has_id_element?()

test if an Element or children has any id

# File lib/amrita/node.rb, line 371
    def has_id_element?
      each_node do |n|
        next unless n.kind_of?(Element)
        return true if n.amrita_id
        return true if n.get_expandable_attrs.size > 0
      end
      false
    end