lib/phlex/sgml.rb
Lines
94%235 of 250 covered
Branches
85%92 of 107 covered
Methods
94%47 of 50 covered
- Not run
- 229234237241324-325332-334414418-419421451-452
- Never called
| 1 | # frozen_string_literal: true | |
| 2 | ||
| 3 | # **Standard Generalized Markup Language** for behaviour common to {HTML} and {SVG}. | |
| 4 | 1 | class Phlex::SGML |
| 5 | 1 | include Phlex::Helpers |
| 6 | ||
| 7 | 1 | class << self |
| 8 | # Render the view to a String. Arguments are delegated to {.new}. | |
| 9 | 1 | def call(...) |
| 10 | 1338 | new(...).call |
| 11 | end | |
| 12 | ||
| 13 | # Create a new instance of the component. | |
| 14 | # @note The block will not be delegated to {#initialize}. Instead, it will be sent to {#view_template} when rendering. | |
| 15 | 1 | def new(*, **, &block) |
| 16 | 2553 | object = allocate |
| 17 | 2553 | object.__send__(:initialize, *, **) |
| 18 | 2541 | object.instance_variable_set(:@_content_block, block) if block |
| 19 | 2540 | object |
| 20 | end | |
| 21 | end | |
| 22 | ||
| 23 | 1 | def view_template |
| 24 | 403 | if block_given? |
| 25 | 393 | yield |
| 26 | else | |
| 27 | 10 | plain "Phlex Warning: Your `#{self.class.name}` class doesn't define a `view_template` method. If you are upgrading to Phlex 2.x make sure to rename your `template` method to `view_template`. See: https://beta.phlex.fun/guides/v2-upgrade.html" |
| 28 | end | |
| 29 | end | |
| 30 | ||
| 31 | 1 | def to_proc |
| 32 | 2 | proc { |c| c.render(self) } |
| 33 | end | |
| 34 | ||
| 35 | # Components can only be rendered once, because Phlex can't reset their instance variables. Pass `rerenderable: true` to allow the component to be rendered again. | |
| 36 | 1 | def call(buffer = +"", context: {}, fragments: nil, rerenderable: false, &) |
| 37 | 2443 | state = Phlex::SGML::State.new( |
| 38 | user_context: context, | |
| 39 | output_buffer: buffer, | |
| 40 | fragments: fragments&.to_set(&:to_s), | |
| 41 | ) | |
| 42 | ||
| 43 | 2443 | internal_call(parent: nil, state:, &) |
| 44 | ||
| 45 | 2293 | state.output_buffer << state.buffer |
| 46 | ensure | |
| 47 | 2443 | @_state = nil if rerenderable && @_state.equal?(state) |
| 48 | end | |
| 49 | ||
| 50 | 1 | def internal_call(parent: nil, state: nil, &block) |
| 51 | 2538 | if @_state |
| 52 | 5 | raise Phlex::DoubleRenderError.new( |
| 53 | "You can't render a #{self.class.name} more than once. To render a component more than once, pass `rerenderable: true` to `call` each time you render it." | |
| 54 | ) | |
| 55 | end | |
| 56 | ||
| 57 | 2533 | @_state = state |
| 58 | ||
| 59 | 2533 | return "" unless render? |
| 60 | ||
| 61 | 2530 | block ||= @_content_block |
| 62 | ||
| 63 | begin | |
| 64 | 2530 | previous_phlex_component = Thread.current[:__phlex_component__] |
| 65 | 2530 | Thread.current[:__phlex_component__] = self |
| 66 | ||
| 67 | 2530 | state.around_render(self) do |
| 68 | 2530 | before_template(&block) |
| 69 | ||
| 70 | 2530 | around_template do |
| 71 | 2530 | if block |
| 72 | 889 | view_template do |*args| |
| 73 | 800 | if args.length > 0 |
| 74 | 385 | __yield_content_with_args__(*args, &block) |
| 75 | else | |
| 76 | 415 | __yield_content__(&block) |
| 77 | end | |
| 78 | end | |
| 79 | else | |
| 80 | 1641 | view_template |
| 81 | end | |
| 82 | end | |
| 83 | ||
| 84 | 2339 | after_template(&block) |
| 85 | end | |
| 86 | ensure | |
| 87 | 2530 | Thread.current[:__phlex_component__] = previous_phlex_component |
| 88 | end | |
| 89 | end | |
| 90 | ||
| 91 | 1 | def context |
| 92 | 6 | if rendering? |
| 93 | 5 | @_state.user_context |
| 94 | else | |
| 95 | 1 | raise Phlex::ArgumentError.new(<<~MESSAGE) |
| 96 | You can’t access the context before the component has started rendering. | |
| 97 | MESSAGE | |
| 98 | end | |
| 99 | end | |
| 100 | ||
| 101 | # Returns `false` before rendering and `true` once the component has started rendering. | |
| 102 | # It will not reset back to false after rendering, unless the component was rendered with `rerenderable: true`. | |
| 103 | 1 | def rendering? |
| 104 | 7 | !!@_state |
| 105 | end | |
| 106 | ||
| 107 | # Output plain text. | |
| 108 | 1 | def plain(content) |
| 109 | 275 | raise Phlex::ArgumentError.new("plain does not accept a block.") if block_given? |
| 110 | ||
| 111 | 273 | unless __text__(content) |
| 112 | 1 | raise Phlex::ArgumentError.new("You've passed an object to plain that is not handled by format_object. See https://rubydoc.info/gems/phlex/Phlex/SGML#format_object-instance_method for more information") |
| 113 | end | |
| 114 | ||
| 115 | 272 | nil |
| 116 | end | |
| 117 | ||
| 118 | # Output a single space character. If a block is given, a space will be output before and after the block. | |
| 119 | 1 | def whitespace(&) |
| 120 | 26 | state = @_state |
| 121 | ||
| 122 | 26 | unless state.should_render? |
| 123 | 9 | yield(self) if block_given? |
| 124 | 8 | return nil |
| 125 | end | |
| 126 | ||
| 127 | 17 | buffer = state.buffer |
| 128 | ||
| 129 | 17 | buffer << " " |
| 130 | ||
| 131 | 17 | if block_given? |
| 132 | 16 | __yield_content__(&) |
| 133 | 16 | buffer << " " |
| 134 | end | |
| 135 | ||
| 136 | 17 | nil |
| 137 | end | |
| 138 | ||
| 139 | # Wrap the output in an HTML comment. | |
| 140 | # | |
| 141 | # [MDN Docs](https://developer.mozilla.org/en-US/docs/Web/HTML/Comments) | |
| 142 | 1 | def comment(&) |
| 143 | 41 | state = @_state |
| 144 | ||
| 145 | 41 | unless state.should_render? |
| 146 | 15 | yield(self) if block_given? |
| 147 | 14 | return nil |
| 148 | end | |
| 149 | ||
| 150 | 26 | buffer = state.buffer |
| 151 | ||
| 152 | 26 | buffer << "<!-- " |
| 153 | 26 | __yield_content__(&) |
| 154 | 26 | buffer << " -->" |
| 155 | ||
| 156 | 26 | nil |
| 157 | end | |
| 158 | ||
| 159 | # Output the given safe object as-is. You may need to use `safe` to mark a string as a safe object. | |
| 160 | 1 | def raw(content) |
| 161 | 1069 | case content |
| 162 | when Phlex::SGML::SafeObject | |
| 163 | 1066 | state = @_state |
| 164 | 1066 | return unless state.should_render?then never taken |
| 165 | ||
| 166 | 1066 | state.append(content.to_s) |
| 167 | when nil, "" # do nothing | |
| 168 | else | |
| 169 | 1 | raise Phlex::ArgumentError.new("You passed an unsafe object to `raw`.") |
| 170 | end | |
| 171 | ||
| 172 | 1068 | nil |
| 173 | end | |
| 174 | ||
| 175 | # Capture the output of the block and returns it as a string. | |
| 176 | 1 | def capture(*args, &block) |
| 177 | 1141 | return "" unless blockthen never taken |
| 178 | ||
| 179 | 1141 | if args.length > 0 |
| 180 | 2 | @_state.capture { __yield_content_with_args__(*args, &block) } |
| 181 | else | |
| 182 | 2280 | @_state.capture { __yield_content__(&block) } |
| 183 | end | |
| 184 | end | |
| 185 | ||
| 186 | # Define a named fragment that can be selectively rendered. | |
| 187 | 1 | def fragment(name) |
| 188 | 1301 | name = name.to_s |
| 189 | 1301 | state = @_state |
| 190 | 1301 | state.begin_fragment(name) |
| 191 | 1301 | completed = false |
| 192 | begin | |
| 193 | 1301 | yield |
| 194 | 1295 | completed = true |
| 195 | ensure | |
| 196 | 1301 | state.end_fragment(name, halt: completed) |
| 197 | end | |
| 198 | 1263 | nil |
| 199 | end | |
| 200 | ||
| 201 | # Mark the given string as safe for HTML output. | |
| 202 | 1 | def safe(value) |
| 203 | 1126 | case value |
| 204 | when String | |
| 205 | 1125 | Phlex::SGML::SafeValue.new(value) |
| 206 | else | |
| 207 | 1 | raise Phlex::ArgumentError.new("Expected a String.") |
| 208 | end | |
| 209 | end | |
| 210 | ||
| 211 | 1 | alias_method :🦺, :safe |
| 212 | ||
| 213 | # Flush the current state to the output buffer. | |
| 214 | 1 | def flush |
| 215 | 40 | @_state.flush |
| 216 | end | |
| 217 | ||
| 218 | 1 | def render(renderable = nil, &) |
| 219 | 100 | case renderable |
| 220 | when Phlex::SGML | |
| 221 | 95 | renderable.internal_call(state: @_state, parent: self, &) |
| 222 | when Class | |
| 223 | 2 | if renderable < Phlex::SGML |
| 224 | 1 | render(renderable.new, &) |
| 225 | else | |
| 226 | 1 | raise Phlex::ArgumentError.new("You can't render a #{renderable.inspect}.") |
| 227 | end | |
| 228 | when Enumerable | |
| 229 | 0 | renderable.each { |r| render(r, &) }when never taken |
| 230 | when Proc, Method | |
| 231 | 2 | if renderable.arity == 0 |
| 232 | 2 | __yield_content_with_no_yield_args__(&renderable) |
| 233 | else | |
| 234 | 0 | __yield_content__(&renderable)else never taken |
| 235 | end | |
| 236 | when String | |
| 237 | 0 | plain(renderable)when never taken |
| 238 | when nil | |
| 239 | 1 | __yield_content__(&) if block_given?else never taken |
| 240 | else | |
| 241 | 0 | raise Phlex::ArgumentError.new("You can't render a #{renderable.inspect}.")else never taken |
| 242 | end | |
| 243 | ||
| 244 | 84 | nil |
| 245 | end | |
| 246 | ||
| 247 | # Cache a block of content. | |
| 248 | # | |
| 249 | # ```ruby | |
| 250 | # @products.each do |product| | |
| 251 | # cache product do | |
| 252 | # h1 { product.name } | |
| 253 | # end | |
| 254 | # end | |
| 255 | # ``` | |
| 256 | 1 | def cache(*cache_key, **, &content) |
| 257 | 39 | location = caller_locations(1, 1)[0] |
| 258 | ||
| 259 | full_key = [ | |
| 260 | 39 | app_version_key, # invalidates the key when deploying new code in case of changes |
| 261 | self.class.name, # prevents collisions between classes | |
| 262 | 39 | (self.class.object_id if enable_cache_reloading?), # enables reloadingthen never taken |
| 263 | location.base_label, # prevents collisions between different methods | |
| 264 | Phlex.__source_line__(location), # prevents collisions between different lines, compiled or not | |
| 265 | cache_key, # allows for custom cache keys | |
| 266 | ].freeze | |
| 267 | ||
| 268 | 39 | low_level_cache(full_key, **, &content) |
| 269 | ||
| 270 | 39 | nil |
| 271 | end | |
| 272 | ||
| 273 | # Cache a block of content where you control the entire cache key. | |
| 274 | # If you really know what you’re doing and want to take full control | |
| 275 | # and responsibility for the cache key, use this method. | |
| 276 | # | |
| 277 | # ```ruby | |
| 278 | # low_level_cache([Commonmarker::VERSION, Digest::MD5.hexdigest(@content)]) do | |
| 279 | # markdown(@content) | |
| 280 | # end | |
| 281 | # ``` | |
| 282 | # | |
| 283 | # Note: To allow you more control, this method does not take a splat of cache keys. | |
| 284 | # If you need to pass multiple cache keys, you should pass an array. | |
| 285 | 1 | def low_level_cache(cache_key, **options, &content) |
| 286 | 223 | state = @_state |
| 287 | ||
| 288 | 333 | cached_buffer, fragment_map = cache_store.fetch(cache_key, **options) { state.caching(&content) } |
| 289 | ||
| 290 | 221 | if state.should_render? |
| 291 | 103 | fragment_map.each do |fragment_name, (offset, length, nested_fragments)| |
| 292 | 1170 | state.record_fragment(fragment_name, offset, length, nested_fragments) |
| 293 | end | |
| 294 | 103 | state.buffer << cached_buffer |
| 295 | else | |
| 296 | 118 | fragment_map.each do |fragment_name, (offset, length, nested_fragments)| |
| 297 | 2263 | if state.fragments.include?(fragment_name) |
| 298 | 110 | state.fragments.delete(fragment_name) |
| 299 | 110 | state.fragments.subtract(nested_fragments) |
| 300 | 110 | state.buffer << cached_buffer.byteslice(offset, length) |
| 301 | end | |
| 302 | end | |
| 303 | end | |
| 304 | ||
| 305 | 221 | nil |
| 306 | end | |
| 307 | ||
| 308 | 1 | def json_escape(string) |
| 309 | 2 | string.gsub(/[&<>\u2028\u2029]/, { |
| 310 | "&" => '\u0026', | |
| 311 | "<" => '\u003c', | |
| 312 | ">" => '\u003e', | |
| 313 | "\u2028" => '\u2028', | |
| 314 | "\u2029" => '\u2029', | |
| 315 | }) | |
| 316 | end | |
| 317 | ||
| 318 | # Override this method to use a different deployment key. | |
| 319 | 1 | private def app_version_key |
| 320 | 39 | Phlex::DEPLOYED_AT |
| 321 | end | |
| 322 | ||
| 323 | # Override this method to use a different cache store. | |
| 324 | 0 | private def cache_store |
| 325 | 0 | raise "Cache store not implemented." |
| 326 | end | |
| 327 | ||
| 328 | 1 | private def enable_cache_reloading? |
| 329 | 39 | false |
| 330 | end | |
| 331 | ||
| 332 | 0 | private def vanish(...) |
| 333 | 0 | capture(...) |
| 334 | 0 | nil |
| 335 | end | |
| 336 | ||
| 337 | 1 | private def render? |
| 338 | 2528 | true |
| 339 | end | |
| 340 | ||
| 341 | 1 | private def format_object(object) |
| 342 | 1093 | case object |
| 343 | when Float, Integer | |
| 344 | 1058 | object.to_s |
| 345 | end | |
| 346 | end | |
| 347 | ||
| 348 | 1 | private def around_template |
| 349 | 2530 | yield |
| 350 | 2339 | nil |
| 351 | end | |
| 352 | ||
| 353 | 1 | private def before_template(&) |
| 354 | 2529 | nil |
| 355 | end | |
| 356 | ||
| 357 | 1 | private def after_template(&) |
| 358 | 2338 | nil |
| 359 | end | |
| 360 | ||
| 361 | # The class or module that registered the element `tag` renders for `name`. | |
| 362 | 1 | private def __element_registrar__(name) |
| 363 | 128 | return unless Phlex::SGML::Elements.registered_name?(name) |
| 364 | ||
| 365 | 206 | self.class.ancestors.find { |ancestor| Phlex::SGML::Elements === ancestor && ancestor.__element_registered__?(name) } |
| 366 | end | |
| 367 | ||
| 368 | 1 | private def __yield_content__ |
| 369 | 1999 | return unless block_given?then never taken |
| 370 | ||
| 371 | 1999 | state = @_state |
| 372 | ||
| 373 | 1999 | original_length = state.output_bytesize |
| 374 | 1999 | content = yield(self) |
| 375 | 1846 | __implicit_output__(content) if original_length == state.output_bytesize |
| 376 | ||
| 377 | 1846 | nil |
| 378 | end | |
| 379 | ||
| 380 | 1 | private def __yield_content_with_no_yield_args__ |
| 381 | 2 | return unless block_given?then never taken |
| 382 | ||
| 383 | 2 | state = @_state |
| 384 | ||
| 385 | 2 | original_length = state.output_bytesize |
| 386 | 2 | content = yield # <-- doesn’t yield self 😉 |
| 387 | 2 | __implicit_output__(content) if original_length == state.output_bytesize |
| 388 | ||
| 389 | 2 | nil |
| 390 | end | |
| 391 | ||
| 392 | 1 | private def __yield_content_with_args__(*a) |
| 393 | 386 | return unless block_given?then never taken |
| 394 | ||
| 395 | 386 | state = @_state |
| 396 | ||
| 397 | 386 | original_length = state.output_bytesize |
| 398 | 386 | content = yield(*a) |
| 399 | 386 | __implicit_output__(content) if original_length == state.output_bytesize |
| 400 | ||
| 401 | 386 | nil |
| 402 | end | |
| 403 | ||
| 404 | 1 | private def __implicit_output__(content) |
| 405 | 425 | state = @_state |
| 406 | 425 | return true unless state.should_render? |
| 407 | ||
| 408 | 411 | case content |
| 409 | when Phlex::SGML::SafeObject | |
| 410 | 16 | state.append(content.to_s) |
| 411 | when String | |
| 412 | 391 | state.buffer << Phlex::Escape.html_escape(content) |
| 413 | when Symbol | |
| 414 | 0 | state.buffer << Phlex::Escape.html_escape(content.name)when never taken |
| 415 | when nil | |
| 416 | nil | |
| 417 | else | |
| 418 | 0 | if (formatted_object = format_object(content))else never taken |
| 419 | 0 | state.buffer << Phlex::Escape.html_escape(formatted_object) |
| 420 | else | |
| 421 | 0 | return false |
| 422 | end | |
| 423 | end | |
| 424 | ||
| 425 | 411 | true |
| 426 | end | |
| 427 | ||
| 428 | # same as __implicit_output__ but escapes even `safe` objects | |
| 429 | 1 | private def __text__(content) |
| 430 | 273 | state = @_state |
| 431 | 273 | return true unless state.should_render? |
| 432 | ||
| 433 | 270 | case content |
| 434 | when String | |
| 435 | 265 | state.buffer << Phlex::Escape.html_escape(content) |
| 436 | when Symbol | |
| 437 | 1 | state.buffer << Phlex::Escape.html_escape(content.name) |
| 438 | when nil | |
| 439 | nil | |
| 440 | else | |
| 441 | 3 | if (formatted_object = format_object(content)) |
| 442 | 2 | state.buffer << Phlex::Escape.html_escape(formatted_object) |
| 443 | else | |
| 444 | 1 | return false |
| 445 | end | |
| 446 | end | |
| 447 | ||
| 448 | 269 | true |
| 449 | end | |
| 450 | ||
| 451 | 0 | private def __attributes__(attributes) |
| 452 | 0 | @_state.attribute_cache.fetch(attributes) { Phlex::SGML::Attributes.generate_attributes(attributes) } |
| 453 | end | |
| 454 | ||
| 455 | 1 | private_class_method def self.method_added(method_name) |
| 456 | 978 | if method_name == :view_template |
| 457 | 813 | location = instance_method(method_name).source_location[0] |
| 458 | ||
| 459 | 813 | if location[0] in "/" | "." |
| 460 | 731 | Phlex.__expand_attribute_cache__(location) |
| 461 | end | |
| 462 | end | |
| 463 | ||
| 464 | 978 | __inlining_changed__(self, [method_name]) |
| 465 | 976 | super |
| 466 | end | |
| 467 | ||
| 468 | 1 | private_class_method def self.method_removed(method_name) |
| 469 | 3 | __inlining_changed__(self, [method_name]) |
| 470 | 3 | super |
| 471 | end | |
| 472 | ||
| 473 | 1 | private_class_method def self.method_undefined(method_name) |
| 474 | 1 | __inlining_changed__(self, [method_name]) |
| 475 | 1 | super |
| 476 | end | |
| 477 | ||
| 478 | 1 | def self.include(*modules) |
| 479 | 29 | super |
| 480 | 59 | __inlining_changed__(self, modules.flat_map { |mod| mod.instance_methods + mod.private_instance_methods }) |
| 481 | 28 | self |
| 482 | end | |
| 483 | ||
| 484 | 1 | def self.prepend(*modules) |
| 485 | 3 | super |
| 486 | 6 | __inlining_changed__(self, modules.flat_map { |mod| mod.instance_methods + mod.private_instance_methods }) |
| 487 | 2 | self |
| 488 | end | |
| 489 | ||
| 490 | # A frozen class can't be recompiled when an element or helper it inlined | |
| 491 | # changes, so its original definitions are put back first. | |
| 492 | 1 | def self.freeze |
| 493 | 3 | Phlex::Compiler.decompile(self) if !Phlex.__compiled_source_maps__.empty? && Phlex::Compiler.compiled?(self) && !frozen? |
| 494 | ||
| 495 | 3 | super |
| 496 | end | |
| 497 | ||
| 498 | # Compiled methods inline elements and helpers as they were at compile | |
| 499 | # time, so a change to one has to be seen. Nothing to do until something | |
| 500 | # has been compiled. | |
| 501 | 1 | private_class_method def self.__inlining_changed__(target, names) |
| 502 | 1033 | if names.include?(:__phlex_equal__?) && !Phlex::SGML.equal?(target) |
| 503 | 6 | raise Phlex::NameError, "__phlex_equal__? is reserved for compiled blocks, so it can't be defined on #{target.inspect}." |
| 504 | end | |
| 505 | ||
| 506 | 1027 | Phlex::Compiler.inlining_changed(target, names) unless Phlex.__compiled_source_maps__.empty? |
| 507 | end | |
| 508 | ||
| 509 | # Inlined svg blocks rely on the class methods that build a Phlex::SVG. | |
| 510 | # Defined before the hooks below, which call it when it's defined. | |
| 511 | 1 | private_class_method def self.__svg_class_changed__(method_name) |
| 512 | 33 | __inlining_changed__(self, [method_name]) if (method_name in :new | :allocate | :===) && Phlex::SVG <= self |
| 513 | end | |
| 514 | ||
| 515 | 1 | private_class_method def self.singleton_method_added(method_name) |
| 516 | 25 | __svg_class_changed__(method_name) |
| 517 | 25 | super |
| 518 | end | |
| 519 | ||
| 520 | 1 | private_class_method def self.singleton_method_removed(method_name) |
| 521 | 7 | __svg_class_changed__(method_name) |
| 522 | 7 | super |
| 523 | end | |
| 524 | ||
| 525 | 1 | private_class_method def self.singleton_method_undefined(method_name) |
| 526 | 1 | __svg_class_changed__(method_name) |
| 527 | 1 | super |
| 528 | end | |
| 529 | ||
| 530 | 1 | private def singleton_method_added(method_name) |
| 531 | 8 | self.class.__send__(:__inlining_changed__, singleton_class, [method_name]) |
| 532 | 5 | super |
| 533 | end | |
| 534 | ||
| 535 | 1 | def extend(*modules) |
| 536 | 3 | super |
| 537 | 6 | self.class.__send__(:__inlining_changed__, singleton_class, modules.flat_map { |mod| mod.instance_methods + mod.private_instance_methods }) |
| 538 | 1 | self |
| 539 | end | |
| 540 | ||
| 541 | # BasicObject#equal? under a name a component can't override. A compiled | |
| 542 | # block runs its compiled body only when the component says the block's | |
| 543 | # receiver is itself, rather than asking the receiver, which could be | |
| 544 | # anything. | |
| 545 | 1 | define_method(:__phlex_equal__?, BasicObject.instance_method(:equal?)) |
| 546 | end |