Amrita::Element (Class)

In: lib/amrita/compiler.rb
lib/amrita/format.rb
lib/amrita/node.rb
lib/amrita/node_expand.rb
Parent: Object

represents HTML element

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

Public Class methods

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

[Source]

# 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

[Source]

# File lib/amrita/node.rb, line 454
    def <<(a, &block)
      put_attr(a)
      init_body(&block) if block_given?
      self
    end

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

[Source]

# 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

return attribule value for key

[Source]

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

set attribule. delete it if value is nil

[Source]

# 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

[Source]

# File lib/amrita/node_expand.rb, line 336
    def apply_to_children(&block)
      clone { yield(body) }
    end

[Source]

# File lib/amrita/node.rb, line 397
    def clone(&block)
      Element.new(self, &block)
    end

[Source]

# File lib/amrita/compiler.rb, line 82
    def compile(c)
      if hid =~ /^\w+$/
        c.compile_id_element(self) 
      else
        c.compile_var_attr(self) or
          c.put_static_element(self) do
          body.compile(c)
        end
      end
    end

delete attribute of key

[Source]

# 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

[Source]

# 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

[Source]

# File lib/amrita/compiler.rb, line 93
    def expand_and_format(data, context, formatter)
      data.amrita_expand_and_format(self, context, formatter)
    end

[Source]

# File lib/amrita/format.rb, line 548
    def format(f)
      if f.delete_span and tagname_symbol == :span and attrs.size == 0 
        return body.format(f)
      else
        f.format_element(self)
      end
    end

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

[Source]

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

hide hid for internal use (expand).

[Source]

# File lib/amrita/node.rb, line 421
    def hide_hid!
      @hide_hid = true
    end

test if it has attribule for key

[Source]

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

[Source]

# File lib/amrita/format.rb, line 556
    def pre_format1(prf)
      f = prf.formatter
      if hid or (prf.expand_attr and has_expandable_attr?)
        prf << clone do
          body.pre_format(f).result
        end
      else
        f.format_element(self) do
          body.pre_format1(prf)
        end
      end
    end

set attribule.

[Source]

# 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

[Source]

# File lib/amrita/node.rb, line 389
    def set_tag(tagname)
      if tagname
        @tagname = tagname.intern 
      else
        @tagname = nil
      end
    end

set the text to body of this Element.

[Source]

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

[Source]

# File lib/amrita/node.rb, line 425
    def tagclass
      self[:class]
    end

return Tag as String

[Source]

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

return Tag as Symbol

[Source]

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

[Source]

# 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

[Validate]