"DTD/xhtml1-strict.dtd">
Class Amrita::Element
In: lib/amrita/node_expand.rb
lib/amrita/node.rb
lib/amrita/format.rb
lib/amrita/compiler.rb
Parent: Object

represents HTML element

Methods
<<    ==    []    []=    apply_to_children    clone    delete_attr!    each_element_with_id    hid    hide_hid!    include_attr?    new    put_attr    set_tag    set_text    tagclass    tagname    tagname_symbol    to_ruby   
Attributes
:attrs  [R] 

return attributes as AttrArray

CAUTION! never edit result of this method. use []= instead. because it may be shared by other Elements.

:attrs_hash  [R] 

CAUTION! internal use only

:body  [R] 

return body

:hide_hid  [R] 

CAUTION! internal use only

Included modules
Node
Public Class methods
new(tagname_or_element, *a, &block)

Don't use Element.new. Use Amrita#e instead.

# File lib/amrita/node.rb, line 340
    def initialize(tagname_or_element, *a, &block)
      case tagname_or_element
      when Element
        @tagname = tagname_or_element.tagname_symbol
        @attrs = tagname_or_element.attrs
        @attrs.shared = true
        @attrs_hash = tagname_or_element.attrs_hash
        @hide_hid = tagname_or_element.hide_hid
        if block_given?
          init_body(&block)
        else
          @body = tagname_or_element.body.clone
        end
      when Symbol, String
        set_tag(tagname_or_element)
        @attrs = AttrArray.new
        @attrs_hash = {}
        @hide_hid = false
        if a.size() == 1 and a.kind_of?(AttrArray)
          @attrs = a
          @attrs.shared = true
          @attrs.each do |a|
            @attrs_hash[a.key_symbol] = a
          end
        else
          a.each { |aa| put_attr(aa) }
        end
        if block_given?
          init_body(&block)
        else
          @body = Null
        end
      end
    end
Public Instance methods
apply_to_children(&block) {|body| ...}
# File lib/amrita/node_expand.rb, line 336
    def apply_to_children(&block)
      clone { yield(body) }
    end
==(x)

test if tagname and attributes and body are equal to self. doesn't concern the order of attributes

# File lib/amrita/node.rb, line 377
    def ==(x)
      return false unless x.kind_of?(Element)
      return true if x.id == id
      return false unless x.tagname_symbol == @tagname
      return false unless x.attrs.size == @attrs.size
      @attrs.each do |a|
        return false unless x[a.key] == a.value
      end
      return false unless x.body == @body
      true
    end
set_tag(tagname)
# File lib/amrita/node.rb, line 389
    def set_tag(tagname)
      if tagname
        @tagname = tagname.intern 
      else
        @tagname = nil
      end
    end
clone(&block)
# File lib/amrita/node.rb, line 397
    def clone(&block)
      Element.new(self, &block)
    end
tagname()

return Tag as String

# File lib/amrita/node.rb, line 402
    def tagname
      @tagname.id2name
    end
tagname_symbol()

return Tag as Symbol

# File lib/amrita/node.rb, line 407
    def tagname_symbol
      @tagname
    end
hid()

return id=... attribule value. It can be hide by +hide_hid!

# File lib/amrita/node.rb, line 412
    def hid
      if @hide_hid
        nil
      else
        self[:id] or self[:ID]
      end
    end
hide_hid!()

hide hid for internal use (expand).

# File lib/amrita/node.rb, line 421
    def hide_hid!
      @hide_hid = true
    end
tagclass()
# File lib/amrita/node.rb, line 425
    def tagclass
      self[:class]
    end
put_attr(a)

set attribule.

# File lib/amrita/node.rb, line 430
    def put_attr(a)
      copy_on_write if @attrs.shared
      case a
      when Attr
        if @attrs_hash[a.key_symbol] 
          self[a.key_symbol] = a.value
        else
          a = a.clone
          @attrs << a
          @attrs_hash[a.key_symbol] = a
        end
      when AttrArray
        a.each do |aa|
          put_attr(aa)
        end
      when Hash
        a.each do |k, v|
          put_attr(Attr.new(k, v))
        end
      else
        raise " << not a Attr but a #{a.class}" unless a.kind_of?(Attr)
      end
    end
<<(a, &block)
# File lib/amrita/node.rb, line 454
    def <<(a, &block)
      put_attr(a)
      init_body(&block) if block_given?
      self
    end
include_attr?(key)

test if it has attribule for key

# File lib/amrita/node.rb, line 461
    def include_attr?(key)
      @attrs_hash.include?(key.intern)
    end
[](key)

return attribule value for key

# File lib/amrita/node.rb, line 466
    def [](key)
      a = @attrs_hash[key.intern]
      if a
        a.value
      else
        nil
      end
    end
[]=(key, value)

set attribule. delete it if value is nil

# File lib/amrita/node.rb, line 476
    def []=(key, value)
      copy_on_write if @attrs.shared
      key = key.intern 
      a = @attrs_hash[key]
      if a
        if value
          a.value = value
        else
          delete_attr!(key)
        end
      else
        put_attr(Attr.new(key,value)) if value
      end
      value
    end
delete_attr!(key)

delete attribute of key

# File lib/amrita/node.rb, line 493
    def delete_attr!(key)
      copy_on_write if @attrs.shared
      key = key.intern 
      old_attrs = @attrs
      @attrs = AttrArray.new
      @attrs_hash = {}
      old_attrs.each do |a|
        put_attr(a) if a.key_symbol != key
      end
    end
to_ruby()
# File lib/amrita/node.rb, line 504
    def to_ruby
      ret = "e(:#{tagname}"
      if attrs.size > 0
        ret << ","
        ret << attrs.collect { |a| a.to_ruby}.join(",")
      end
      ret << ") "
      ret << "{ #{body.to_ruby} }" if body and not body.kind_of?(NullNode)
      ret
    end
each_element_with_id(recursive=false, &block) {|self| ...}
# File lib/amrita/node.rb, line 515
    def each_element_with_id(recursive=false, &block)
      if hid
        yield(self)
        super if recursive
      else
        super
      end
    end
set_text(text)

set the text to body of this Element.

# File lib/amrita/node.rb, line 525
    def set_text(text)
      @body = TextElement.new(text)
    end