Class: Condenser::SVGTransformer::Tag

Inherits:
Object
  • Object
show all
Defined in:
lib/condenser/transformers/svg_transformer/tag.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ Tag

Returns a new instance of Tag.



5
6
7
8
9
# File 'lib/condenser/transformers/svg_transformer/tag.rb', line 5

def initialize(name)
  @tag_name = name
  @attrs = []
  @children = []
end

Instance Attribute Details

#attrsObject

Returns the value of attribute attrs.



3
4
5
# File 'lib/condenser/transformers/svg_transformer/tag.rb', line 3

def attrs
  @attrs
end

#childrenObject

Returns the value of attribute children.



3
4
5
# File 'lib/condenser/transformers/svg_transformer/tag.rb', line 3

def children
  @children
end

#namespaceObject

Returns the value of attribute namespace.



3
4
5
# File 'lib/condenser/transformers/svg_transformer/tag.rb', line 3

def namespace
  @namespace
end

#tag_nameObject

Returns the value of attribute tag_name.



3
4
5
# File 'lib/condenser/transformers/svg_transformer/tag.rb', line 3

def tag_name
  @tag_name
end

Instance Method Details

#inspectObject



15
16
17
# File 'lib/condenser/transformers/svg_transformer/tag.rb', line 15

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

#to_js(append: nil, var_generator:, indentation: 4, namespace: nil) ⇒ Object



19
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
# File 'lib/condenser/transformers/svg_transformer/tag.rb', line 19

def to_js(append: nil, var_generator:, indentation: 4, namespace: nil)
  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.is_a?(String) ? v : 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?(Condenser::SVGTransformer::Tag)
      child.to_js(var_generator: var_generator, indentation: indentation, append: output_var, namespace: namespace)
    else
      child.to_js(var_generator: var_generator, indentation: indentation, append: output_var)
    end
  end

  js << "#{' '*indentation}#{append}.append(#{output_var});\n" if append

  js
end

#to_sObject



11
12
13
# File 'lib/condenser/transformers/svg_transformer/tag.rb', line 11

def to_s
  @value
end