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

Base module for HTML elements

Methods

Included Modules

Enumerable

Public Instance methods

Copy a Node n times and generate NodeArray

[Source]

# File lib/amrita/node.rb, line 302
    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

Node can be added and they become NodeArray

[Source]

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

[Source]

# File lib/amrita/node_expand.rb, line 299
    def amrita_expand_node(n, context)
      self
    end

[Source]

# File lib/amrita/node_expand.rb, line 303
    def apply_to_children(hash, context)
      self
    end

a Node has NullNode as body before init_body was called.

[Source]

# File lib/amrita/node.rb, line 245
    def body
      if defined? @body
        @body
      else
        Null
      end
    end

@return [Array] child nodes or an empty Array if it does not have a body

[Source]

# File lib/amrita/node.rb, line 259
    def children
      if no_child?
        []
      else
        [ body ]
      end
    end
each(&block)

Alias for each_element

iterate on HTML elements (self and children).

[Source]

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

iterate on self and children

[Source]

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

expand self as a template with a model data.

[Source]

# File lib/amrita/node_expand.rb, line 278
    def expand(data, context=DefaultContext.clone)
      case data
      when true
        self
      when nil, false
        Null
      when DictionaryData
        expand1(data, context)
      else
        raise TypeError, "Amrita::Node#expand accepts only Hash or ExpandByMember as model data (#{data.class} was passed)"
      end
    end

set the block ‘s result to body

[Source]

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

[Source]

# File lib/amrita/node.rb, line 292
    def inspect
      to_ruby
    end

test if it has any children

[Source]

# File lib/amrita/node.rb, line 254
    def no_child?
      body.kind_of?(NullNode)
    end

converts an Element without id to TextElement to make tree low for performance.

A pre-formatted Node tree will be expanded faster than original. But, it produces the same output .

[Source]

# File lib/amrita/format.rb, line 524
    def pre_format(formatter, expand_attr=false)
      raise "pre_format dose not suport pretty-print" if formatter.kind_of?(PrettyPrintFormatter)
      prf = PreFormatter.new(formatter, expand_attr)
      prf.pre_format(self)
      prf
    end

[Source]

# File lib/amrita/format.rb, line 531
    def pre_format1(prf)
      prf << self
    end

generate a Node object 要素をcloneしない。

[Source]

# File lib/amrita/node.rb, line 269
    def to_node(n)
      case n
      when nil, false
        Null
      when Node
        n
      when Array, NodeArray
        case n.size()
        when 0
          Null
        when 1
          to_node(n[0])
        else
          r = NodeArray.new
          n.each {|node| r << node}
          r
        end
      else
        TextElement.new(n.to_s) 
      end
    end

[Source]

# File lib/amrita/format.rb, line 513
    def to_s
      ret = ""
      SingleLineFormatter.new(ret).format(self)
      ret
    end

[Validate]