lib/phlex/sgml/state.rb
Lines
100%103 of 103 covered
Branches
100%32 of 32 covered
Methods
100%12 of 12 covered
| 1 | # frozen_string_literal: true | |
| 2 | ||
| 3 | 1 | class Phlex::SGML::State |
| 4 | 1 | def initialize(user_context: {}, output_buffer:, fragments:) |
| 5 | 2445 | @buffer = +"" |
| 6 | 2445 | @flushed_bytesize = 0 |
| 7 | 2445 | @capturing = false |
| 8 | 2445 | @user_context = user_context |
| 9 | 2445 | @fragments = fragments |
| 10 | 2445 | @fragment_depth = 0 |
| 11 | 2445 | @should_render = !fragments |
| 12 | 2445 | @cache_stack = [] |
| 13 | 2445 | @halt_signal = nil |
| 14 | 2445 | @output_buffer = output_buffer |
| 15 | 2445 | @attribute_cache = Phlex.__attribute_cache__ |
| 16 | end | |
| 17 | ||
| 18 | 1 | attr_accessor :capturing, :user_context |
| 19 | ||
| 20 | 1 | attr_reader :fragments, :fragment_depth, :output_buffer, :buffer, :should_render, :attribute_cache |
| 21 | ||
| 22 | # Kept as a stored boolean so the check is a plain attribute read. | |
| 23 | 1 | alias_method :should_render?, :should_render |
| 24 | ||
| 25 | 1 | def around_render(component) |
| 26 | 2530 | if !@fragments || @halt_signal |
| 27 | 2376 | yield |
| 28 | else | |
| 29 | 154 | catch do |signal| |
| 30 | 154 | @halt_signal = signal |
| 31 | 154 | yield |
| 32 | end | |
| 33 | end | |
| 34 | end | |
| 35 | ||
| 36 | 1 | def begin_fragment(id) |
| 37 | 1401 | if @fragments&.include?(id) |
| 38 | 40 | @fragment_depth += 1 |
| 39 | 40 | @should_render = true |
| 40 | end | |
| 41 | ||
| 42 | 1401 | if caching? |
| 43 | 1294 | current_byte_offset = 0 # Start tracking the byte offset of this fragment from the start of the cache buffer |
| 44 | 1294 | cache_stack.reverse_each do |(cache_buffer, fragment_map, open_fragments)| # We'll iterate deepest to shallowest |
| 45 | 1346 | current_byte_offset += cache_buffer.bytesize # Add the length of the cache buffer to the current byte offset |
| 46 | 1346 | fragment_map[id] = [current_byte_offset, nil, []] # Record the byte offset, length, and store a list of the nested fragments |
| 47 | ||
| 48 | # Visit only open ancestors; allocate descendant sets on their first child. | |
| 49 | 1346 | open_fragments.delete(id) |
| 50 | 1412 | open_fragments.each_key { |name| (open_fragments[name] ||= Set.new) << id } |
| 51 | 1346 | open_fragments[id] = nil |
| 52 | end | |
| 53 | end | |
| 54 | end | |
| 55 | ||
| 56 | 1 | def end_fragment(id, halt: true) |
| 57 | 1401 | if caching? |
| 58 | 1294 | byte_length = nil |
| 59 | 1294 | cache_stack.reverse_each do |(cache_buffer, fragment_map, open_fragments)| # We'll iterate deepest to shallowest |
| 60 | 1346 | byte_length ||= cache_buffer.bytesize - fragment_map[id][0] # The byte length is the difference between the current byte offset and the byte offset of the fragment |
| 61 | 1346 | fragment_map[id][1] = byte_length # All cache contexts use the same byte length |
| 62 | 1346 | if (descendants = open_fragments.delete(id)) |
| 63 | 57 | fragment_map[id][2] = descendants.to_a |
| 64 | end | |
| 65 | end | |
| 66 | end | |
| 67 | ||
| 68 | 1401 | return unless @fragments&.include?(id) |
| 69 | ||
| 70 | 40 | @fragments.delete(id) |
| 71 | 40 | @fragment_depth -= 1 |
| 72 | 40 | @should_render = @fragment_depth > 0 |
| 73 | # Don't replace an exception or nonlocal exit with the render halt signal. | |
| 74 | 40 | throw @halt_signal if halt && @fragments.length == 0 |
| 75 | end | |
| 76 | ||
| 77 | 1 | def record_fragment(id, offset, length, nested_fragments) |
| 78 | 2261 | return unless caching? |
| 79 | ||
| 80 | 1154 | cache_stack.reverse_each do |(cache_buffer, fragment_map, open_fragments)| |
| 81 | 1154 | offset += cache_buffer.bytesize |
| 82 | 1154 | fragment_map[id] = [offset, length, nested_fragments] |
| 83 | ||
| 84 | 1154 | open_fragments.delete(id) |
| 85 | 2225 | open_fragments.each_key { |name| (open_fragments[name] ||= Set.new) << id } |
| 86 | end | |
| 87 | end | |
| 88 | ||
| 89 | 1 | def caching(&) |
| 90 | 110 | result = nil |
| 91 | ||
| 92 | 110 | capture(cache: true) do |
| 93 | 110 | @cache_stack.push([buffer, {}, {}].freeze) |
| 94 | begin | |
| 95 | 110 | yield |
| 96 | 108 | result = @cache_stack.last.first(2) |
| 97 | ensure | |
| 98 | 110 | @cache_stack.pop |
| 99 | end | |
| 100 | end | |
| 101 | ||
| 102 | 108 | result |
| 103 | end | |
| 104 | ||
| 105 | 1 | def caching? |
| 106 | 6258 | !(cache_stack = @cache_stack) || cache_stack.length > 0 |
| 107 | end | |
| 108 | ||
| 109 | # A capture has no cache stack until it needs to track a fragment. | |
| 110 | # While it has none, `buffer` is the buffer being captured into. | |
| 111 | 1 | private def cache_stack |
| 112 | 3852 | @cache_stack ||= [[buffer, {}, {}]] |
| 113 | end | |
| 114 | ||
| 115 | 1 | def capture(cache: false) |
| 116 | 1351 | new_buffer = +"" |
| 117 | 1351 | original_buffer = @buffer |
| 118 | 1351 | original_capturing = @capturing |
| 119 | 1351 | original_fragments = @fragments |
| 120 | 1351 | original_should_render = @should_render |
| 121 | 1351 | original_cache_stack = cache ? cache_stack : @cache_stack |
| 122 | 1351 | captured_cache_stack = nil |
| 123 | ||
| 124 | begin | |
| 125 | 1351 | @buffer = new_buffer |
| 126 | 1351 | @capturing = true |
| 127 | 1351 | @fragments = nil |
| 128 | 1351 | @should_render = true |
| 129 | # Captured output may be inserted later, elsewhere, or not at all. | |
| 130 | # Track its fragments locally until the original string is inserted. | |
| 131 | 1351 | @cache_stack = nil unless cache |
| 132 | 1351 | yield |
| 133 | ensure | |
| 134 | 1351 | captured_cache_stack = @cache_stack unless cache |
| 135 | 1351 | @buffer = original_buffer |
| 136 | 1351 | @capturing = original_capturing |
| 137 | 1351 | @fragments = original_fragments |
| 138 | 1351 | @should_render = original_should_render |
| 139 | 1351 | @cache_stack = original_cache_stack |
| 140 | end | |
| 141 | ||
| 142 | 1333 | if captured_cache_stack && !(fragment_map = captured_cache_stack[0][1]).empty? |
| 143 | # Let the string own its metadata so discarded captures can be collected | |
| 144 | # without losing metadata for strings that will be inserted again. | |
| 145 | 1201 | new_buffer.instance_variable_set(:@__phlex_fragments, [new_buffer.dup.freeze, fragment_map]) |
| 146 | end | |
| 147 | ||
| 148 | 1333 | new_buffer |
| 149 | end | |
| 150 | ||
| 151 | # Flushing moves bytes out of the buffer without changing how much was written. | |
| 152 | # Read through `buffer` so overrides that redirect output (e.g. phlex-rails) are measured too. | |
| 153 | 1 | def output_bytesize |
| 154 | 10824 | @flushed_bytesize + buffer.bytesize |
| 155 | end | |
| 156 | ||
| 157 | 1 | def append(content) |
| 158 | # Changed strings no longer have reliable fragment offsets. | |
| 159 | 1195 | if caching? && (captured = content.instance_variable_get(:@__phlex_fragments)) && captured[0] == content |
| 160 | 1086 | captured[1].each do |id, (offset, length, nested_fragments)| |
| 161 | 1091 | record_fragment(id, offset, length, nested_fragments) |
| 162 | end | |
| 163 | end | |
| 164 | ||
| 165 | 1195 | buffer << content |
| 166 | end | |
| 167 | ||
| 168 | 1 | def flush |
| 169 | 90 | return if capturing |
| 170 | ||
| 171 | 88 | buffer = @buffer |
| 172 | 88 | @output_buffer << buffer.dup |
| 173 | ||
| 174 | 88 | @flushed_bytesize += buffer.bytesize |
| 175 | 88 | buffer.clear |
| 176 | 88 | nil |
| 177 | end | |
| 178 | end |