Module: VirtualDOM::DOM

Included in:
Porous::Component
Defined in:
lib/virtual_dom/dom.rb

Constant Summary collapse

HTML_TAGS =
%w[a abbr address area article aside audio b base bdi bdo big blockquote body br
button canvas caption cite code col colgroup data datalist dd del details dfn
dialog div dl dt em embed fieldset figcaption figure footer form h1 h2 h3 h4 h5
h6 head header hr html i iframe img input ins kbd keygen label legend li link
main map mark menu menuitem meta meter nav noscript object ol optgroup option
output p param picture pre progress q rp rt ruby s samp script section select
small source span strong style sub summary sup table tbody td textarea tfoot th
thead time title tr track u ul var video wbr].freeze
SVG_TAGS =
%w[animate animateMotion animateTransform circle clipPath defs desc ellipse filter
foreignObject g image line linearGradient marker mask metadata mpath path pattern
polygon polyline radialGradient rect set stop svg switch symbol textPath tspan use view].freeze

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(klass, params = {}, &block) ⇒ Object

rubocop:disable Style/MissingRespondToMissing, Metrics/MethodLength, Metrics/AbcSize



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/virtual_dom/dom.rb', line 45

def method_missing(klass, params = {}, &block)
  return unless @__last_virtual_node__
  return unless @__virtual_nodes__

  @__virtual_nodes__.pop
  children = []

  if params.is_a?(String)
    children = [params]
    params = {}
  end

  class_params = @__last_virtual_node__.params.delete(:className)
  method_params = if klass.end_with?('!')
                    { id: klass[0..-2],
                      class: merge_string(class_params, params[:class]) }
                  else
                    { class: merge_string(class_params, params[:class], klass.to_s.gsub('_', '-').gsub('--', '_')) }
                  end
  params = @__last_virtual_node__.params.merge(params).merge(method_params)
  process_tag(@__last_virtual_node__.name, params, block, children)
end

Instance Method Details

#class_names(hash) ⇒ Object



110
111
112
113
114
115
116
# File 'lib/virtual_dom/dom.rb', line 110

def class_names(hash)
  class_names = []
  hash.each do |key, value|
    class_names << key if value
  end
  class_names.join(' ')
end

#merge_string(*params) ⇒ Object

rubocop:enable Style/MissingRespondToMissing, Metrics/MethodLength, Metrics/AbcSize



69
70
71
72
73
74
75
76
77
# File 'lib/virtual_dom/dom.rb', line 69

def merge_string(*params)
  arr = []
  params.each do |string|
    next unless string

    arr << string.split
  end
  arr.join(' ')
end

#process_params(params) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/virtual_dom/dom.rb', line 79

def process_params(params)
  params.dup.each_key do |k|
    case k
    when 'for'
      params['htmlFor'] = params.delete('for')
    when 'class'
      params['className'] = params.delete('class')
    when 'data'
      params['dataset'] = params.delete('data')
    when 'default'
      params['defaultValue'] = params.delete('default')
    when /^on/
      # Events are ignored server-side
      params.delete k
    end
  end
  params
end

#process_tag(tag, params, block, children = []) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/virtual_dom/dom.rb', line 27

def process_tag(tag, params, block, children = [])
  @__virtual_nodes__ ||= []
  if block
    current = @__virtual_nodes__
    @__virtual_nodes__ = []
    result = block.call || children
    vnode = VirtualNode.new(tag, process_params(params),
                            @__virtual_nodes__.count.zero? ? result : @__virtual_nodes__)
    @__virtual_nodes__ = current
  else
    vnode = VirtualNode.new(tag, process_params(params), children)
  end
  @__last_virtual_node__ = vnode
  @__virtual_nodes__ << @__last_virtual_node__
  self
end

#text(string) ⇒ Object



98
99
100
# File 'lib/virtual_dom/dom.rb', line 98

def text(string)
  @__virtual_nodes__ << string.to_s
end

#to_vnodeObject



102
103
104
105
106
107
108
# File 'lib/virtual_dom/dom.rb', line 102

def to_vnode
  if @__virtual_nodes__.one?
    @__virtual_nodes__.first
  else
    VirtualNode.new('div', {}, @__virtual_nodes__)
  end
end