Module: HoTModuLe
- Defined in:
- lib/hot_module.rb,
lib/hot_module/petite.rb,
lib/hot_module/version.rb,
lib/hot_module/fragment.rb,
lib/hot_module/shadow_effects.rb,
lib/hot_module/query_selection.rb,
lib/hot_module/component_renderer.rb
Overview
Include this module into your own component class
Defined Under Namespace
Modules: ClassMethods, ContentMethod, JSTemplateLiterals, Petite, QuerySelection, ShadowEffects
Classes: AttributeBinding, ComponentRenderer, Error, Fragment
Constant Summary
collapse
- VERSION =
"1.0.0.alpha14"
Class Method Summary
collapse
Instance Method Summary
collapse
Class Method Details
.included(klass) ⇒ void
This method returns an undefined value.
48
49
50
51
52
53
54
55
56
57
58
59
60
|
# File 'lib/hot_module.rb', line 48
def self.included(klass)
klass.extend ClassMethods
klass.attribute_binding "hmod:children", :_hmod_children_binding, only: :template
klass.attribute_binding "hmod:replace", :_hmod_replace_binding
klass.attribute_binding "hmod:text", :_hmod_expr_binding
klass.attribute_binding "hmod:html", :_hmod_expr_binding
return if klass.instance_methods.include?(:content)
klass.include ContentMethod
end
|
.register_element(component) ⇒ Object
41
42
43
44
|
# File 'lib/hot_module.rb', line 41
def self.register_element(component)
@registered_elements ||= Concurrent::Set.new
@registered_elements << component
end
|
.registered_elements ⇒ Object
27
28
29
30
31
32
33
34
35
36
37
38
39
|
# File 'lib/hot_module.rb', line 27
def self.registered_elements
@registered_elements ||= Concurrent::Set.new
@registered_elements.each do |component|
begin
next if Kernel.const_get(component.to_s) == component rescue NameError; end
@registered_elements.delete component
end
@registered_elements
end
|
Instance Method Details
#_check_stack(node) ⇒ Object
345
346
347
348
349
350
351
352
353
354
355
356
357
358
|
# File 'lib/hot_module.rb', line 345
def _check_stack(node)
node_and_ancestors = [node, *node.ancestors.to_a]
stack_misses = 0
_context_nodes.each do |stack_node|
if node_and_ancestors.none? { _1["hmod-added"] } && node_and_ancestors.none? { _1 == stack_node }
stack_misses += 1
end
end
stack_misses.times { _context_nodes.pop }
node_and_ancestors.any? { _context_nodes.include?(_1) }
end
|
#_context_locals ⇒ Object
343
|
# File 'lib/hot_module.rb', line 343
def _context_locals = @_context_locals ||= {}
|
#_context_nodes ⇒ Object
341
|
# File 'lib/hot_module.rb', line 341
def _context_nodes = @_context_nodes ||= []
|
#_hmod_children_binding(attribute:, node:) ⇒ Object
rubocop:disable Lint/UnusedMethodArgument
366
367
368
369
|
# File 'lib/hot_module.rb', line 366
def _hmod_children_binding(attribute:, node:) self.replaced_content = node.children[0]
node.remove
end
|
#_hmod_expr_binding(attribute:, node:) ⇒ Object
381
382
383
384
385
386
387
388
389
|
# File 'lib/hot_module.rb', line 381
def _hmod_expr_binding(attribute:, node:)
if attribute.name.end_with?(":text")
node.content = node_or_string(evaluate_attribute_expression(attribute))
attribute.parent.delete(attribute.name)
elsif attribute.name.end_with?(":html")
node.inner_html = node_or_string(evaluate_attribute_expression(attribute))
attribute.parent.delete(attribute.name)
end
end
|
#_hmod_replace_binding(attribute:, node:) ⇒ Object
371
372
373
374
375
376
377
378
379
|
# File 'lib/hot_module.rb', line 371
def _hmod_replace_binding(attribute:, node:)
if node.name == "template"
node.children[0].inner_html = node_or_string(evaluate_attribute_expression(attribute))
node.replace(node.children[0].children)
else
node.inner_html = node_or_string(evaluate_attribute_expression(attribute))
node.replace(node.children)
end
end
|
#_in_context_nodes {|previous_context| ... } ⇒ Object
360
361
362
363
364
|
# File 'lib/hot_module.rb', line 360
def _in_context_nodes
previous_context = _context_locals
yield previous_context
@_context_locals = previous_context
end
|
#attributes ⇒ Hash
155
|
# File 'lib/hot_module.rb', line 155
def attributes = {}
|
#call ⇒ Object
268
|
# File 'lib/hot_module.rb', line 268
def call(...) = render_element(...)
|
#class_list_for(obj) ⇒ Object
329
330
331
332
333
334
335
336
337
338
339
|
# File 'lib/hot_module.rb', line 329
def class_list_for(obj)
case obj
when Hash
obj.filter { |_k, v| v }.keys
when Array
obj
else
Array[obj]
end.join(" ")
end
|
#evaluate_attribute_expression(attribute, eval_code = attribute.value) ⇒ Object
321
322
323
324
325
326
327
|
# File 'lib/hot_module.rb', line 321
def evaluate_attribute_expression(attribute, eval_code = attribute.value)
eval_code = eval_code.gsub(/\${(.*)}/, "\#{\\1}")
_context_locals.keys.reverse_each do |name|
eval_code = "#{name} = _context_locals[\"#{name}\"];" + eval_code
end
instance_eval(eval_code, self.class.html_module, self.class.line_number_of_node(attribute))
end
|
#inspect ⇒ Object
270
|
# File 'lib/hot_module.rb', line 270
def inspect = "#<#{self.class.name} #{attributes}>"
|
#node_or_string(val) ⇒ Object
283
284
285
|
# File 'lib/hot_module.rb', line 283
def node_or_string(val)
val.is_a?(Nokolexbor::Node) ? val : val.to_s
end
|
#process_fragment(fragment) ⇒ void
This method returns an undefined value.
Override in component if need be, otherwise we’ll use the node walker/binding pipeline
291
|
# File 'lib/hot_module.rb', line 291
def process_fragment(fragment) = Fragment.new(fragment, self).process
|
#process_list(attribute:, node:, item_node:, for_in:) ⇒ Object
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
|
# File 'lib/hot_module.rb', line 293
def process_list(attribute:, node:, item_node:, for_in:) _context_nodes.push(node)
lh = for_in[0].strip.delete_prefix("(").delete_suffix(")").split(",").map!(&:strip)
rh = for_in[1].strip
list_items = evaluate_attribute_expression(attribute, rh)
return unless list_items
_in_context_nodes do |previous_context|
list_items.each_with_index do |list_item, index|
new_node = item_node.clone
node.parent << new_node
new_node["hmod-added"] = ""
@_context_locals = { **(previous_context || {}) }
_context_locals[lh[0]] = list_item
_context_locals[lh[1]] = index if lh[1]
Fragment.new(new_node, self).process
end
end
end
|
#render_element(attributes: self.attributes, content: self.content) ⇒ Object
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
|
# File 'lib/hot_module.rb', line 168
def render_element(attributes: self.attributes, content: self.content) doc = self.class.doc.clone
@_content = content
tmpl_el = doc.css("> template").find do |node|
node.attributes.empty? ||
(node.attributes.count == 1 && node.attributes.any? { |k| k[0].start_with?("data-") })
end
unless tmpl_el
tmpl_el = doc.document.create_element("template")
immediate_children = doc.css("> :not(style):not(script)")
tmpl_el.children[0] << immediate_children
doc.prepend_child(tmpl_el)
end
process_fragment(tmpl_el)
HoTModuLe.registered_elements.each do |component|
tmpl_el.children[0].css(component.tag_name).reverse.each do |node|
if node["hmod:ignore"]
node.remove_attribute("hmod:ignore")
next
end
attrs = node.attributes.transform_values(&:value)
attrs.reject! { |k| k.start_with?("hmod:") }
new_attrs = {}
attrs.each do |k, v|
next unless k.start_with?("arg:")
new_key = k.delete_prefix("arg:")
attrs.delete(k)
new_attrs[new_key] = instance_eval(v, self.class.html_module, self.class.line_number_of_node(node))
end
attrs.merge!(new_attrs)
attrs.transform_keys!(&:to_sym)
new_node = node.replace(
component.new(**attrs).render_element(content: node.children)
)
new_node.remove_attribute("hmod:ignore")
end
end
attributes.each { |k, v| doc[k.to_s.tr("_", "-")] = value_to_attribute(v) if v }
output_styles = ""
external_styles = doc.css("link[rel=stylesheet]")
external_styles.each do |external_style|
next unless external_style["hmod:process"]
output_styles += File.read(File.expand_path(external_style["href"], File.dirname(self.class.html_module)))
external_style.remove
rescue StandardError => e
raise e.class, e.message.lines.first,
["#{self.class.html_module}:#{external_style.line}", *e.backtrace]
end
sidecar_file = "#{File.join(
File.dirname(self.class.html_module), File.basename(self.class.html_module, ".*")
)}.#{self.class.processed_css_extension}"
output_styles += if File.exist?(sidecar_file)
File.read(sidecar_file)
else
doc.css("> style:not([scope])").map(&:content).join
end
doc.children.each do |node|
node.remove unless node == tmpl_el
end
style_tag = nil
if output_styles.length.positive?
style_tag = tmpl_el.document.create_element("style")
style_tag.content = output_styles
end
child_content = @_replaced_content || content
if self.class.shadow_root
tmpl_el["shadowrootmode"] = "open"
tmpl_el.children[0] << style_tag if style_tag
doc << child_content if child_content
else
tmpl_el.children[0] << style_tag if style_tag
tmpl_el.children[0].at_css("slot:not([name])")&.swap(child_content) if child_content
tmpl_el.children[0].children.each do |node|
doc << node
end
tmpl_el.remove
end
rendering_mode == :node ? doc : doc.to_html
end
|
#rendering_mode ⇒ Object
157
|
# File 'lib/hot_module.rb', line 157
def rendering_mode = @_rendering_mode || :node
|
#rendering_mode=(mode) ⇒ Object
159
160
161
162
163
164
|
# File 'lib/hot_module.rb', line 159
def rendering_mode=(mode)
@_rendering_mode = case mode
when :node, :string
mode
end
end
|
#replaced_content=(new_content) ⇒ Object
148
149
150
|
# File 'lib/hot_module.rb', line 148
def replaced_content=(new_content)
@_replaced_content = new_content
end
|
#value_to_attribute(val) ⇒ Object
272
273
274
275
276
277
278
279
280
281
|
# File 'lib/hot_module.rb', line 272
def value_to_attribute(val)
case val
when String, Numeric
val
when TrueClass
""
else
val.to_json
end
end
|