Class: Vident::Phlex::HTML

Inherits:
Phlex::HTML
  • Object
show all
Includes:
Component
Defined in:
lib/vident/phlex/html.rb

Constant Summary collapse

STANDARD_ELEMENTS =
%i[
  a abbr address article aside b bdi bdo blockquote body button caption
  cite code colgroup data datalist dd del details dfn dialog div dl dt
  em fieldset figcaption figure footer form g h1 h2 h3 h4 h5 h6 head
  header hgroup html i iframe ins kbd label legend li main map mark
  menuitem meter nav noscript object ol optgroup option output p path
  picture pre progress q rp rt ruby s samp script section select slot
  small span strong style sub summary sup svg table tbody td template_tag
  textarea tfoot th thead time title tr u ul video wbr
].freeze
VOID_ELEMENTS =
%i[area br embed hr img input link meta param source track col].freeze
VALID_TAGS =
Set[*(STANDARD_ELEMENTS + VOID_ELEMENTS)].freeze

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Class Attribute Details

.component_source_file_pathObject

Returns the value of attribute component_source_file_path.



31
32
33
# File 'lib/vident/phlex/html.rb', line 31

def component_source_file_path
  @component_source_file_path
end

Class Method Details

.cache_component_modified_timeObject

Raises:

  • (::Vident::ConfigurationError)


33
34
35
36
37
# File 'lib/vident/phlex/html.rb', line 33

def cache_component_modified_time
  path = component_source_file_path
  raise ::Vident::ConfigurationError, "No component source file exists #{path}" unless path && ::File.exist?(path)
  ::File.mtime(path).to_i.to_s
end

.inherited(subclass) ⇒ Object

Walks caller_locations to skip the ‘inherited` frame itself.



25
26
27
28
29
# File 'lib/vident/phlex/html.rb', line 25

def inherited(subclass)
  loc = caller_locations(1, 10).reject { |l| l.label == "inherited" }[0]
  subclass.component_source_file_path = loc&.path
  super
end

Instance Method Details

#before_templateObject

DSL procs stay unresolved until ‘helpers` is wired; resolve them here.



41
42
43
44
# File 'lib/vident/phlex/html.rb', line 41

def before_template
  resolve_stimulus_attributes_at_render_time
  super
end

#cache_component(*extra_keys, **options, &block) ⇒ Object

Fragment-cache this component’s render using its Vident-computed ‘cache_key`. Delegates to Phlex’s ‘cache(…)`; pass extra positional keys to invalidate on state not captured by `with_cache_key`.



49
50
51
52
53
54
55
# File 'lib/vident/phlex/html.rb', line 49

def cache_component(*extra_keys, **options, &block)
  unless respond_to?(:cacheable?) && cacheable?
    raise ::Vident::ConfigurationError,
      "#{self.class.name} is not cacheable — `include Vident::Caching` and declare `with_cache_key` first."
  end
  cache([cache_key, *extra_keys], **options, &block)
end

#root_element(**overrides, &block) ⇒ Object

Capture block first so children can mutate this Draft before it seals (outlet-host pattern).



58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/vident/phlex/html.rb', line 58

def root_element(**overrides, &block)
  tag_type = root_element_tag_type
  check_valid_html_tag!(tag_type)
  if block
    content = capture(self, &block).html_safe
    options = build_root_element_attributes(overrides)
    send(tag_type, **options) { content }
  else
    options = build_root_element_attributes(overrides)
    send(tag_type, **options)
  end
end