Module: Lilac::Directives::Grammar

Defined in:
lib/lilac/directives/grammar.rb

Overview

Predicates for the name-shaped tokens that appear in directives โ€” event-handler method names, the X parts of data-attr-X / data-css-X, and the banned-attribute deny-list.

Duplicate pair (build-time / runtime). See decisions ยง17.

Reactive values (@ivar / bare ident) live on Value so they can carry their kind polymorphically rather than as a string-prefix check.

Anchors: mruby-regexp-compat supports ^/$ but not \A/\z. Since directive values are always single-line strings, ^/$ are equivalent here.

Module methods are defined via class << self instead of module_function because mruby's module_function doesn't behave like MRI's (see runtime/mruby-lilac/mrblib/html.rb:60).

Constant Summary collapse

METHOD_IDENT =

? predicate suffix allowed (used by @active? ivars and bare-ident predicate field reads). Bang ! is rejected โ€” the regex stops before any trailing !.

/^[a-zA-Z_][a-zA-Z0-9_]*$/
KEBAB_NAME =

X part of data-attr-X / data-css-X: kebab-lowercase, letter-first, no -- prefix (clashes with CSS variable prefix the framework auto-prepends).

/^[a-z][a-z0-9-]*$/
BANNED_ATTR =

Inline event handlers (on*), srcdoc (iframe HTML injection vector), and style (use data-css-X or RefElement#set_style instead) are banned from data-attr-X.

/^on[a-z]+$|^srcdoc$|^style$/
DIRECTIVE_ATTR =

data-* attribute names that map to a directive (one of the Directive::Kind values aside from :component). This is the SSOT for both:

* build-time: TemplateAST uses it to identify directive-bearing
elements that need a synthetic `lilN` slot.
* runtime: `Refs` / `TemplateRefs` use it to walk the mounted
subtree in DFS and resolve `refs.lilN` positionally.

Anything matching here counts toward a scope's directive-bearing index; markers like data-ref / data-component / data-template / data-arg-* are excluded.

/^data-(?:text|unsafe-html|bind|show|hide|each|key|class|form|field|button|on-.+|attr-.+|css-.+)$/

Class Method Summary collapse

Class Method Details

.banned_attr?(s) ⇒ Boolean

Returns:

  • (Boolean)


62
63
64
# File 'lib/lilac/directives/grammar.rb', line 62

def banned_attr?(s)
  !!BANNED_ATTR.match?(s.to_s)
end

.directive_attribute?(s) ⇒ Boolean

True when s is a data-* attribute name that triggers codegen (i.e. occupies a lilN slot in its scope). Used by both build-time TemplateAST and runtime Refs to keep their DFS counters in lockstep.

Returns:

  • (Boolean)


70
71
72
# File 'lib/lilac/directives/grammar.rb', line 70

def directive_attribute?(s)
  !!DIRECTIVE_ATTR.match?(s.to_s)
end

.kebab_name?(s) ⇒ Boolean

progress / theme-color. Rejects uppercase, digit-start, leading - (which would collide with the auto-prepended --).

Returns:

  • (Boolean)


57
58
59
60
# File 'lib/lilac/directives/grammar.rb', line 57

def kebab_name?(s)
  str = s.to_s
  !!KEBAB_NAME.match?(str) && !str.start_with?("-")
end

.method_ident?(s) ⇒ Boolean

increment / add_todo. Event-handler method names โ€” no ? suffix so a typoed save? is caught at mount time.

Returns:

  • (Boolean)


51
52
53
# File 'lib/lilac/directives/grammar.rb', line 51

def method_ident?(s)
  !!METHOD_IDENT.match?(s.to_s)
end