In: |
lib/amrita/compiler.rb
lib/amrita/format.rb lib/amrita/node.rb lib/amrita/node_expand.rb |
Parent: | Object |
represents HTML element
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 |
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
# 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
# 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
# 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
# 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
# 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
# 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
# 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
# File lib/amrita/compiler.rb, line 93 def expand_and_format(data, context, formatter) data.amrita_expand_and_format(self, context, formatter) end
# 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
hide hid for internal use (expand).
# File lib/amrita/node.rb, line 421 def hide_hid! @hide_hid = true end
test if it has attribule for key
# File lib/amrita/node.rb, line 461 def include_attr?(key) @attrs_hash.include?(key.intern) end
# 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.
# 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
# File lib/amrita/node.rb, line 389 def set_tag(tagname) if tagname @tagname = tagname.intern else @tagname = nil end end