Class Amrita::Element
In: lib/amrita/node_expand.rb
lib/amrita/format.rb
lib/amrita/node.rb
Parent: Object

represents HTML element

Methods

<<   ==   []   []=   add   apply_to_children   clone   delete_attr!   format   hide_hid!   include_attr?   new   pre_format1   put_attr   set_tag   set_text   tagclass   tagname   tagname_symbol   to_ruby  

Included Modules

Node

Attributes

attrs  [R]  return attributes as AttrArray

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

body  [R]  return body
hide_hid  [R]  CAUTION! internal use only
multi  [RW]  internal use only

Public Class methods

create or copy a Element instance. @overload initialize aElement

    copy of aElement.
    @param [Element] aElement       コピー元のHTML要素インスタンス

@overload initialize tagname, attr, …

    create a Element.
    @param [String, Symbol] tagname HTML要素名
    @param [Attr] attr              属性インスタンス

@overload initialize tagname, aAttrArray

    create a Element.
    @param [String, Symbol] tagname HTML要素名
    @param [AttrArray] aAttrArray   属性セット

[Source]

# File lib/amrita/node.rb, line 395
    def initialize tagname_or_element, *a, &block
      case tagname_or_element
      when Element
        @tagname = tagname_or_element.tagname_symbol
        @attrs = tagname_or_element.attrs.clone
        @multi = tagname_or_element.multi
        @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.intern
        @hide_hid = false
        if a.size() == 1 and a.kind_of?(AttrArray)
          @attrs = a.clone
        else
          @attrs = AttrArray.new
          a.each { |aa| put_attr(aa) }
        end
        if block_given?
          init_body(&block)
        else
          @body = Null
        end
      else
        raise TypeError, "tagname must be a Symbol/String, but #{tagname_or_element.inspect}"
      end
    end

Public Instance methods

[Source]

# File lib/amrita/node.rb, line 492
    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 428
    def ==(x)
      return false unless x.kind_of?(Element)
      return true if x.object_id == object_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 504
    def [] key
      raise TypeError, "key must be a String/Symbol, but #{key.class}" if !key.is_a?(String) && !key.is_a?(Symbol)

      key = key.to_s
      a = @attrs[key]
      a ? a.value : nil
    end

set attribule. delete it if value is nil

[Source]

# File lib/amrita/node.rb, line 513
    def []= key, value
      raise TypeError, "key must be a String/Symbol, but #{key.class}" if !key.is_a?(String) && !key.is_a?(Symbol)

      key = key.to_s
      if !value
        delete_attr!(key)
      else
        put_attr(Attr.new(key,value))
      end
      value
    end

[Source]

# File lib/amrita/node.rb, line 549
    def add(node)
      case @body
      when Null
        @body = node
      when NodeArray
        @body << node
      when Element, TextElement, SpecialElement
        a = NodeArray.new()
        a << @body << node
        @body = a
      else
        raise TypeError
      end
    end

[Source]

# File lib/amrita/node_expand.rb, line 346
    def apply_to_children(hash, context)
      clone { yield(body) }
    end

[Source]

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

delete attribute of key

[Source]

# File lib/amrita/node.rb, line 526
    def delete_attr!(key)
      raise TypeError, "key must be a String/Symbol, but #{key.class}" if !key.is_a?(String) && !key.is_a?(Symbol)

      @attrs.delete(key.to_s)
      self
    end

[Source]

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

hide hid for internal use (expand).

[Source]

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

test if it has attribule for key

[Source]

# File lib/amrita/node.rb, line 499
    def include_attr?(key)
      @attrs[key.to_s] ? true : false
    end

[Source]

# File lib/amrita/format.rb, line 553
    def pre_format1(prf)
      f = prf.formatter
      if self[:id] 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 474
    def put_attr(a)
      case a
      when Attr
        @attrs[a.key] = a
      when AttrArray
        a.each do |aa|
          put_attr(aa)
        end
      when Hash
        a.each do |k, v|
          k = k.id2name if k.is_a?(Symbol)
          put_attr(Attr.new(k, v))
        end
      else
        raise "not a Attr, AttrArray or Hash, but a #{a.class}"
      end
    end

[Source]

# File lib/amrita/node.rb, line 440
    def set_tag(tagname)
      raise TypeError if !tagname.is_a?(Symbol)
      if tagname
        @tagname = tagname
      else
        @tagname = nil
      end
    end

set the text to body of this Element.

[Source]

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

@return class属性値

[Source]

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

@return [String] HTML要素名

[Source]

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

@return [Symbol] HTML要素名

[Source]

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

[Source]

# File lib/amrita/node.rb, line 533
    def to_ruby
      ret = "e(:#{tagname}#{multi ? " multi" : ""}"
      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]