Class: CompEx::Template::Renderer
- Inherits:
-
Object
- Object
- CompEx::Template::Renderer
- Defined in:
- lib/compex/template/renderer.rb
Constant Summary collapse
- MERGEABLE_ATTRIBUTES =
The following attributes will have their values merged when we find the same tag already defined by an embedded component
Set.new(%w[class])
Instance Attribute Summary collapse
-
#embeddings ⇒ Object
Returns the value of attribute embeddings.
-
#frag ⇒ Object
Returns the value of attribute frag.
-
#root_classes ⇒ Object
Returns the value of attribute root_classes.
-
#tokens ⇒ Object
Returns the value of attribute tokens.
Instance Method Summary collapse
- #define_css_prefix(prefix) ⇒ Object
- #define_js_module(name) ⇒ Object
- #handle_inner_components ⇒ Object
- #handle_multiple_root ⇒ Object
- #handle_non_element ⇒ Object
-
#initialize(assembler, context) ⇒ Renderer
constructor
A new instance of Renderer.
- #prepare_template ⇒ Object
- #process_embedding(key, quoted) ⇒ Object
- #render ⇒ Object
- #walk(node) {|node| ... } ⇒ Object
Constructor Details
#initialize(assembler, context) ⇒ Renderer
14 15 16 17 18 19 20 21 22 23 |
# File 'lib/compex/template/renderer.rb', line 14 def initialize(assembler, context) @tokens = assembler.result = assembler. @inner_components = assembler.components @frag = nil @root_classes = [] @context = context @context_id = @context.component_id prepare_template end |
Instance Attribute Details
#embeddings ⇒ Object
Returns the value of attribute embeddings.
12 13 14 |
# File 'lib/compex/template/renderer.rb', line 12 def end |
#frag ⇒ Object
Returns the value of attribute frag.
12 13 14 |
# File 'lib/compex/template/renderer.rb', line 12 def frag @frag end |
#root_classes ⇒ Object
Returns the value of attribute root_classes.
12 13 14 |
# File 'lib/compex/template/renderer.rb', line 12 def root_classes @root_classes end |
#tokens ⇒ Object
Returns the value of attribute tokens.
12 13 14 |
# File 'lib/compex/template/renderer.rb', line 12 def tokens @tokens end |
Instance Method Details
#define_css_prefix(prefix) ⇒ Object
93 94 95 |
# File 'lib/compex/template/renderer.rb', line 93 def define_css_prefix(prefix) @frag.children.first.add_class(prefix) end |
#define_js_module(name) ⇒ Object
97 98 99 |
# File 'lib/compex/template/renderer.rb', line 97 def define_js_module(name) @frag.children.first.set_attribute("cx-controller", name) end |
#handle_inner_components ⇒ Object
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/compex/template/renderer.rb', line 56 def handle_inner_components walk(@frag.children.first) do |node| next unless node.name.start_with? "compex-component-" inner_name = @inner_components[node.name] next unless inner_name c_node = CompEx::ComponentRegistry.resolve(inner_name, context: @context) next unless c_node args = c_node.descriptor.args.map(&:to_s) component_props = node.attributes.slice(*args) component_props.each_key { node.remove_attribute(it) } child = node.children replacement = Nokogiri::HTML5.fragment("<#{node.name}></#{node.name}>") node.attributes.each_value { replacement.set_attribute(it.name, it.value) } node.replace(replacement) @inner_components[node.name] = { name: node.name, attrs: node.attributes.values, children: child, args: component_props.values.to_h { [it.name.to_sym, it.value] }, component: c_node } end end |
#handle_multiple_root ⇒ Object
148 149 150 151 152 153 154 |
# File 'lib/compex/template/renderer.rb', line 148 def handle_multiple_root raise MultipleRootError, "Element contains multiple root elements" if CompEx.config.on_multiple_root == :raise wrapper = @frag.document.create_element(CompEx.config.multiple_root_wrap_element) @frag.children.each { wrapper << it } @frag.add_child(wrapper) end |
#handle_non_element ⇒ Object
156 157 158 159 160 161 |
# File 'lib/compex/template/renderer.rb', line 156 def handle_non_element raise NonElementRootError, "Element contains multiple root elements" if CompEx.config.on_non_element == :raise wrapper = @frag.document.create_element(CompEx.config.multiple_root_wrap_element) @frag.children.first.wrap(wrapper) end |
#prepare_template ⇒ Object
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 52 53 54 |
# File 'lib/compex/template/renderer.rb', line 25 def prepare_template .transform_values! do |v| RubyVM::InstructionSequence.compile("-> { #{v} }").eval end source = @tokens.map do |item| case item[:kind] when :literal item[:value] when :executable item[:id] else raise "[BUG] Unexpected token kind #{item[:kind]}" end end.join @frag = Nokogiri::HTML5.fragment(source.strip) return if @frag.children.empty? if @frag.children.length > 1 handle_multiple_root elsif !@frag.children.first.element? handle_non_element end @root_classes = @frag.children.first.classes.uniq handle_inner_components @frag end |
#process_embedding(key, quoted) ⇒ Object
137 138 139 140 141 142 143 144 145 146 |
# File 'lib/compex/template/renderer.rb', line 137 def (key, quoted) unless .key? key warn "[CompEx] BUG: Embedding not found: #{key} @ #{@context}" return key end ret = yield [key] ret = ret.inspect if quoted ret end |
#render ⇒ Object
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 |
# File 'lib/compex/template/renderer.rb', line 101 def render(&) dom = @frag.dup @inner_components.each do |k, v| = dom.at_css(k) next unless result = Nokogiri::HTML5.fragment(yield v).children.first v[:attrs].each do |attr| val = attr.value val = "#{result.get_attribute(attr.name)} #{val}" if MERGEABLE_ATTRIBUTES.include?(attr.name) && result.has_attribute?(attr.name) result.set_attribute(attr.name, val) end .replace(result) end .each do |k, v| next if k.start_with? "__cx_embedding" = dom.at_css("compex-embedding[id=\"#{k}\"]") next unless result = yield v .replace(result) end slot = dom.at_css("slot") # WARNING: The safe navigation operator below WILL PREVENT yield when # slot is nil. slot&.replace((yield :slot) || "") dom.to_html .gsub(/__cx_embedding_#{@context_id}_\d+__/) { (it, false, &) } .gsub(/__cx_embedding_quoted_#{@context_id}_\d+__/) { (it, true, &) } end |
#walk(node) {|node| ... } ⇒ Object
83 84 85 86 87 88 89 90 91 |
# File 'lib/compex/template/renderer.rb', line 83 def walk(node, &) return unless node.element? yield node # give caller a chance to handle/replace this node node.children.each do |child| walk(child, &) end end |