Class: EJX::Template::HTMLTag

Inherits:
Node
  • Object
show all
Defined in:
lib/ejx/template/html_tag.rb

Defined Under Namespace

Classes: AttributeValue

Instance Attribute Summary collapse

Attributes inherited from Node

#children

Instance Method Summary collapse

Methods inherited from Node

#push

Constructor Details

#initializeHTMLTag

Returns a new instance of HTMLTag.



7
8
9
10
# File 'lib/ejx/template/html_tag.rb', line 7

def initialize
  super
  @attrs = []
end

Instance Attribute Details

#attrsObject

Returns the value of attribute attrs.



5
6
7
# File 'lib/ejx/template/html_tag.rb', line 5

def attrs
  @attrs
end

#namespaceObject

Returns the value of attribute namespace.



5
6
7
# File 'lib/ejx/template/html_tag.rb', line 5

def namespace
  @namespace
end

#tag_nameObject

Returns the value of attribute tag_name.



5
6
7
# File 'lib/ejx/template/html_tag.rb', line 5

def tag_name
  @tag_name
end

Instance Method Details

#inspectObject



16
17
18
# File 'lib/ejx/template/html_tag.rb', line 16

def inspect
  "#<EJX::HTMLTag:#{self.object_id} @tag_name=#{tag_name}>"
end

#to_js(append: "__output", var_generator:, indentation: 4, namespace: nil, promises: '__promises') ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/ejx/template/html_tag.rb', line 20

def to_js(append: "__output", var_generator:, indentation: 4, namespace: nil, promises: '__promises')
  namespace ||= self.namespace
  
  output_var = var_generator.next
  js = "#{' '*indentation}var #{output_var} = document.createElement"
  js << if namespace
    "NS(#{namespace.to_js}, #{JSON.generate(tag_name)});\n"
  else
    "(#{JSON.generate(tag_name)});\n"
  end

  @attrs.each do |attr|
    if attr.is_a?(Hash)
      attr.each do |k, v|
        js << "#{' '*indentation}#{output_var}.setAttribute(#{JSON.generate(k)}, #{v.to_js});\n"
      end
    else
      js << "#{' '*indentation}#{output_var}.setAttribute(#{JSON.generate(attr)}, \"\");\n"
    end
  end

  @children.each do |child|
    js << if child.is_a?(EJX::Template::String)
      "#{' '*indentation}__ejx_append(#{child.to_js}, #{output_var}, 'unescape', #{promises});\n"
    elsif child.is_a?(EJX::Template::HTMLTag)
      child.to_js(var_generator: var_generator, indentation: indentation, append: output_var, namespace: namespace, promises: promises)
    else
      child.to_js(var_generator: var_generator, indentation: indentation, append: output_var, promises: promises)
    end
  end

  js << "#{' '*indentation}__ejx_append(#{output_var}, #{append}, 'unescape', #{promises});\n"
  js
end

#to_sObject



12
13
14
# File 'lib/ejx/template/html_tag.rb', line 12

def to_s
  @value
end