super-slim 3.6.2 → 3.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: cdd1bc27d23e754183cb4f4e4963a935d56d030a1b73d60bccfc4ee97f8972bc
4
- data.tar.gz: 2419f8247b33000daa959e96470679eb54ef778e5c82aa6c0555323e68e7ed02
3
+ metadata.gz: 52391ec6b8e744b8b883f3f2c7673b0d16c64156869d2d529d4bf37f0d0502cc
4
+ data.tar.gz: bb76a49c673a063e5d8cae8d7a2829776885894673623ba8ee08689533e0be4b
5
5
  SHA512:
6
- metadata.gz: 545d8dd22428f0a94ccb6e36c0f1108427ca607d6242abf1abe1dc69c9969ba6db04cef1404262017214599499813f0a028b3e2392fe17f48eb9a25e5746ef50
7
- data.tar.gz: 339168144fd87e1b8a6ef09e293c1f645d31c0da65c30c3fab686fa63dd6fb9debcbf5f63e92396c244031e75ca0c7aba202c9f9ea0cf0d94a6dd69ceca47018
6
+ metadata.gz: 4b2fe8cf05a6937e42c9bb4e376f34981d9dd0cf522e9b131e0f471d14e612bcc47bb917f789200357824536644a8325f5edf8cc208c73e191a84afb6df97109
7
+ data.tar.gz: a7eba7d8442da3a42c4b97b6d264c69d0d873ed1df12f91c56928361b4af8ae0c2dc1651a65556f83223e4d2592529633702d18101ae23871de09f49312c7377
data/CHANGELOG.md CHANGED
@@ -2,6 +2,30 @@
2
2
 
3
3
  ## [Unreleased]
4
4
 
5
+ ## [3.7.0] — 2026-11-24
6
+
7
+ Faster rendering of splats and data hashes, faster compiling, and one fix.
8
+
9
+ **Faster**
10
+ - Tags with a splat (`*attrs`) or a `data=`/`aria=` hash allocate a third fewer objects per render: a form of twelve
11
+ fields went from 258 to 138 allocations per render, 4% faster without JIT and 9% with YJIT. Rails compiles
12
+ templates without `frozen_string_literal`, so each attribute name passed to the runtime allocated a string.
13
+ - Compiling allocates 30% fewer objects and takes 5% less time on 6,097 public templates, with and without YJIT.
14
+ The compiled Ruby is unchanged.
15
+
16
+ **Fixed**
17
+ - `SuperSlim.compile(source, html_safe: nil)` no longer compiles a dynamic class or a splat into code that raises
18
+ `ArgumentError` when it renders: `nil` works like `false`.
19
+
20
+ ## [3.6.3] — 2026-11-21
21
+
22
+ One fix: a line break inside an interpolation of a quoted attribute no longer shifts the lines that follow.
23
+
24
+ **Fixed**
25
+ - A quoted attribute whose `#{…}` spans lines, such as `p title="#{t(:title,` then `count: 2)}"` on the next line,
26
+ no longer moves every later line one line down per break in backtraces, `__LINE__` and compile errors. The
27
+ attributes of a `ruby:` or `markdown:` block keep their lines too.
28
+
5
29
  ## [3.6.2] — 2026-11-20
6
30
 
7
31
  Template errors found while compiling now point at their template line, in Rails too.
@@ -321,7 +345,9 @@ renders what Slim 5 renders with `format: :html, sort_attrs: false`, byte for by
321
345
  - Ruby 3.3 or later, every Ruby still maintained, Rails 8.0 or 8.1 for the railtie, and strscan 3.1.1 or later,
322
346
  newer than the one Ruby 3.3 ships.
323
347
 
324
- [Unreleased]: https://github.com/levups/super-slim/compare/v3.6.2...HEAD
348
+ [Unreleased]: https://github.com/levups/super-slim/compare/v3.7.0...HEAD
349
+ [3.7.0]: https://github.com/levups/super-slim/compare/v3.6.3...v3.7.0
350
+ [3.6.3]: https://github.com/levups/super-slim/compare/v3.6.2...v3.6.3
325
351
  [3.6.2]: https://github.com/levups/super-slim/compare/v3.6.1...v3.6.2
326
352
  [3.6.1]: https://github.com/levups/super-slim/compare/v3.6.0...v3.6.1
327
353
  [3.6.0]: https://github.com/levups/super-slim/compare/v3.5.0...v3.6.0
data/README.md CHANGED
@@ -461,11 +461,11 @@ bin/release minor --dry-run # shows the notes and the changes, writes nothing
461
461
  bin/release minor # or patch, major, or an exact version such as 1.2.0
462
462
  ```
463
463
 
464
- Nobody pushes to `main` directly, so `bin/release` dates the Unreleased section and bumps the version on a
465
- `release/vX.Y.Z` branch, opens a pull request that merges itself once CI passes, and waits for it. It then pushes
466
- a signed `vX.Y.Z` tag on the merge commit (if you stopped it while it waited, `bin/release --tag X.Y.Z` does that
467
- step). The tag starts the Release workflow, which publishes the gem to RubyGems through trusted publishing and
468
- creates the GitHub release with the notes and the `.gem` file.
464
+ `main` takes no direct push, so `bin/release` dates the notes and bumps the version on a `release/vX.Y.Z` branch,
465
+ opens a pull request that merges itself once CI is green, and waits for it (`bin/release --wait X.Y.Z` if you
466
+ stopped it). The merge starts the Release workflow, which waits for a maintainer's approval, tags the merge commit
467
+ `vX.Y.Z`, publishes the gem to RubyGems through trusted publishing and creates the GitHub release with the notes
468
+ and the `.gem`.
469
469
 
470
470
  ## License
471
471
 
@@ -8,6 +8,7 @@ module SuperSlim
8
8
 
9
9
  VOID_ELEMENTS = %w[area base br col embed hr img input keygen link menuitem meta param source track wbr].to_h { [_1, true] }.freeze
10
10
  HYPHENATED_ATTRIBUTES = %w[data aria].freeze
11
+ MAX_SCANNED_ATTRIBUTES = 8
11
12
  DOCTYPES = {
12
13
  html: {
13
14
  "5" => "<!DOCTYPE html>",
@@ -48,7 +49,7 @@ module SuperSlim
48
49
  @xml = format == :xml
49
50
  @doctypes = DOCTYPES.fetch(format)
50
51
  @rails = rails
51
- @html_safe = html_safe
52
+ @html_safe = html_safe ? true : false
52
53
  @escape_function = html_safe ? "::SuperSlim::Runtime.escape_html_safe" : "::ERB::Escape.html_escape"
53
54
  @preamble = preamble
54
55
  @postamble = postamble
@@ -106,6 +107,13 @@ module SuperSlim
106
107
  parts
107
108
  end
108
109
 
110
+ # The line breaks inside the text's interpolations, which its compiled code keeps.
111
+ def self.interpolation_newlines(text)
112
+ return 0 unless text.include?("\n") && text.include?('#{')
113
+
114
+ interpolation_parts(text).sum { |kind, code| (kind == :dynamic) ? code.count("\n") : 0 }
115
+ end
116
+
109
117
  private
110
118
 
111
119
  def compile_children(nodes)
@@ -219,31 +227,51 @@ module SuperSlim
219
227
  return if attributes.empty?
220
228
  return emit_output(compile_runtime_attributes(attributes), false) if attributes.any? { runtime_attribute?(_1) }
221
229
 
222
- if attributes.size > 1
223
- seen_names = {}
224
- attributes.each do |attribute|
225
- next if attribute.name == "class"
226
- if seen_names.key?(attribute.name)
227
- # Reported on the last line of the attributes, like the tag's compiled code, which follows their code.
228
- code_newlines = attributes.sum { _1.is_a?(CodeAttribute) ? _1.code.count("\n") : 0 }
229
- compile_error!("Multiple #{attribute.name} attributes specified", lines_ahead: code_newlines)
230
- end
231
-
232
- seen_names[attribute.name] = true
230
+ # A tag has a handful of attributes: scanning them again costs less than a hash of their names, which only a
231
+ # long list needs, as the scans grow with its square.
232
+ seen_names = {} if attributes.size > MAX_SCANNED_ATTRIBUTES
233
+ attributes.each_with_index do |attribute, index|
234
+ name = attribute.name
235
+ next if name == "class"
236
+
237
+ if seen_names
238
+ next seen_names[name] = true unless seen_names.key?(name)
239
+ elsif attributes.index { _1.name == name } == index
240
+ next
233
241
  end
242
+
243
+ # Reported on the last line of the attributes, like the tag's compiled code, which follows their code.
244
+ lines_ahead = attributes.sum { |other| code_newlines(other) }
245
+ compile_error!("Multiple #{name} attributes specified", lines_ahead:)
234
246
  end
235
247
 
236
248
  # Every class value renders at the place of the first class attribute.
237
- class_attributes = nil
249
+ classes_compiled = false
238
250
  attributes.each do |attribute|
239
251
  next compile_attribute(attribute) unless attribute.name == "class"
240
- next if class_attributes
252
+ next if classes_compiled
241
253
 
242
- class_attributes = attributes.select { _1.name == "class" }
243
- compile_class_attribute(class_attributes.map { attribute_parts(_1) })
254
+ classes_compiled = true
255
+ if attributes.all? { _1.name != "class" || _1.is_a?(StaticAttribute) }
256
+ compile_static_classes(attributes)
257
+ else
258
+ compile_class_attribute(attributes.filter_map { attribute_parts(_1) if _1.name == "class" })
259
+ end
244
260
  end
245
261
  end
246
262
 
263
+ # Shortcuts, the most common classes: their values join at compile time.
264
+ def compile_static_classes(attributes)
265
+ opened = false
266
+ attributes.each do |attribute|
267
+ next if attribute.name != "class" || attribute.value.empty?
268
+
269
+ @static << (opened ? " " : ' class="') << attribute.value
270
+ opened = true
271
+ end
272
+ @static << '"' if opened
273
+ end
274
+
247
275
  def runtime_attribute?(attribute)
248
276
  attribute.is_a?(Splat) || (attribute.is_a?(CodeAttribute) && HYPHENATED_ATTRIBUTES.include?(attribute.name))
249
277
  end
@@ -252,6 +280,7 @@ module SuperSlim
252
280
  case attribute
253
281
  when BooleanAttribute then emit_boolean_attribute(attribute.name)
254
282
  when CodeAttribute then compile_code_attribute(attribute)
283
+ when StaticAttribute then @static << " " << attribute.name << '="' << attribute.value << '"'
255
284
  else emit_attribute(attribute.name, attribute_parts(attribute))
256
285
  end
257
286
  end
@@ -314,15 +343,15 @@ module SuperSlim
314
343
 
315
344
  def compile_runtime_attributes(attributes)
316
345
  builder = new_variable
317
- emit_code("#{builder} = ::SuperSlim::Runtime::Attributes.new(#{@html_safe})")
346
+ emit_code("#{builder} = ::SuperSlim::Runtime::#{@html_safe ? "HtmlSafeAttributes" : "Attributes"}.new")
318
347
  attributes.each do |attribute|
319
348
  case attribute
320
349
  when Splat
321
350
  emit_code("#{builder}.splat_attrs((#{attribute.code}))")
322
351
  when CodeAttribute
323
- emit_code("#{builder}.code_attr(#{attribute.name.inspect}, #{attribute.escape}, (#{attribute.code}))")
352
+ emit_code("#{builder}.code_attr(#{frozen_literal(attribute.name)}, #{attribute.escape}, (#{attribute.code})#{unchecked_name(attribute.name)})")
324
353
  else
325
- emit_code("#{builder}.attr(#{attribute.name.inspect}, #{string_expression(attribute_parts(attribute))})")
354
+ emit_code("#{builder}.attr(#{frozen_literal(attribute.name)}, #{string_expression(attribute_parts(attribute))}#{unchecked_name(attribute.name)})")
326
355
  end
327
356
  end
328
357
  builder
@@ -344,13 +373,14 @@ module SuperSlim
344
373
  end
345
374
 
346
375
  def emit_attribute(name, parts)
347
- emit_static(%( #{name}="))
376
+ @static << " " << name << '="'
348
377
  emit_parts(parts)
349
- emit_static('"')
378
+ @static << '"'
350
379
  end
351
380
 
352
381
  def emit_boolean_attribute(name)
353
- emit_static(@xml ? %( #{name}="") : " #{name}")
382
+ @static << " " << name
383
+ @static << '=""' if @xml
354
384
  end
355
385
 
356
386
  def emit_parts(parts)
@@ -369,6 +399,7 @@ module SuperSlim
369
399
  return "#{@escape_function}((#{value}))" if kind == :dynamic && escape
370
400
  return value if kind == :raw
371
401
  end
402
+ return frozen_literal(parts.map { _1[1] }.join) if parts.all? { _1[0] == :static }
372
403
 
373
404
  literal = parts.map do |kind, value, escape|
374
405
  case kind
@@ -380,11 +411,27 @@ module SuperSlim
380
411
  %("#{literal.join}")
381
412
  end
382
413
 
414
+ # A name found valid now needs no check at render time; the parser lets through some the runtime refuses, such
415
+ # as control characters, which still raise when they render.
416
+ def unchecked_name(name)
417
+ ", false" unless name.match?(Runtime::Attributes::INVALID_NAME)
418
+ end
419
+
420
+ # Rails compiles templates without `frozen_string_literal`: a bare literal would allocate on every render.
421
+ def frozen_literal(text)
422
+ "#{text.inspect}.freeze"
423
+ end
424
+
383
425
  def emit_interpolated(text)
384
- emit_parts(Compiler.interpolation_parts(text))
426
+ text.include?('#{') ? emit_parts(Compiler.interpolation_parts(text)) : emit_static(text)
385
427
  end
386
428
 
387
429
  def compile_embedded(embedded)
430
+ case embedded.engine
431
+ when "ruby", "markdown"
432
+ # Their attributes are dropped, and with them the lines their interpolations go on to.
433
+ @code << ("\n" * embedded.attributes.sum { code_newlines(_1) })
434
+ end
388
435
  case embedded.engine
389
436
  when "ruby" then compile_ruby(embedded.children)
390
437
  when "javascript" then compile_tag(Tag.new("script", embedded.attributes, [TextBlock.new(embedded.children)]))
@@ -439,6 +486,16 @@ module SuperSlim
439
486
  end
440
487
  end
441
488
 
489
+ # The lines an attribute's Ruby code goes on to: a code attribute's, and those of a quoted attribute's
490
+ # interpolations, which the parser leaves to the code.
491
+ def code_newlines(attribute)
492
+ case attribute
493
+ when CodeAttribute then attribute.code.count("\n")
494
+ when QuotedAttribute then Compiler.interpolation_newlines(attribute.text)
495
+ else 0
496
+ end
497
+ end
498
+
442
499
  # The compiled code keeps each template line on its own line, so the newlines written so far give the line of
443
500
  # the node being compiled: no node carries its line, which would cost every template line. Code not written
444
501
  # yet may still hold `lines_ahead` of them.
@@ -466,25 +523,29 @@ module SuperSlim
466
523
  @static << text
467
524
  end
468
525
 
526
+ # The buffers write their statements straight into the compiled code: no string for each one.
469
527
  def emit_output(code, escape)
470
- emit_code(escape ? @buffer.escaped(code) : @buffer.raw(code))
528
+ flush_static
529
+ separate_statement
530
+ escape ? @buffer.escaped(@code, code) : @buffer.raw(@code, code)
471
531
  end
472
532
 
473
533
  def emit_code(code)
474
534
  flush_static
475
- append(code)
535
+ separate_statement
536
+ @code << code
476
537
  end
477
538
 
478
539
  def flush_static
479
540
  return if @static.empty?
480
541
 
481
- append(@buffer.static(@static))
482
- @static = +""
542
+ separate_statement
543
+ @buffer.static(@code, @static)
544
+ @static.clear
483
545
  end
484
546
 
485
- def append(code)
547
+ def separate_statement
486
548
  @code << "; " unless @code.empty? || @code.end_with?("\n")
487
- @code << code
488
549
  end
489
550
 
490
551
  def new_variable
@@ -499,17 +560,35 @@ module SuperSlim
499
560
 
500
561
  def open = "#{@name} = +\"\""
501
562
  def close = @name
502
- def static(text) = "#{@name} << #{text.inspect}.freeze"
503
- def escaped(code) = "#{@name} << #{@escape_function}((#{code}))"
504
- def raw(code) = "#{@name} << (#{code}).to_s"
563
+
564
+ def static(code, text)
565
+ code << @name << " << " << text.inspect << ".freeze"
566
+ end
567
+
568
+ def escaped(code, ruby)
569
+ code << @name << " << " << @escape_function << "((" << ruby << "))"
570
+ end
571
+
572
+ def raw(code, ruby)
573
+ code << @name << " << (" << ruby << ").to_s"
574
+ end
505
575
  end
506
576
 
507
577
  class OutputBuffer
508
578
  def open = "@output_buffer = output_buffer || ActionView::OutputBuffer.new"
509
579
  def close = "@output_buffer"
510
- def static(text) = "@output_buffer.safe_concat(#{text.inspect}.freeze)"
511
- def escaped(code) = "@output_buffer << (#{code})"
512
- def raw(code) = "@output_buffer.safe_concat((#{code}).to_s)"
580
+
581
+ def static(code, text)
582
+ code << "@output_buffer.safe_concat(" << text.inspect << ".freeze)"
583
+ end
584
+
585
+ def escaped(code, ruby)
586
+ code << "@output_buffer << (" << ruby << ")"
587
+ end
588
+
589
+ def raw(code, ruby)
590
+ code << "@output_buffer.safe_concat((" << ruby << ").to_s)"
591
+ end
513
592
  end
514
593
  end
515
594
  end
@@ -47,6 +47,7 @@ module SuperSlim
47
47
  QUOTED_CHUNK_BY_QUOTE = %w[" '].to_h { |quote| [quote, /(?:[^{}\\#{quote}]|\\(?!\z))+/] }.freeze
48
48
  SLASH, PIPE, APOSTROPHE, LESS_THAN, GREATER_THAN, DASH, EQUALS = "/|'<>-=".bytes
49
49
  OPENING_BRACE, CLOSING_BRACE = "{}".bytes
50
+ HASH, DOT, STAR = "#.*".bytes
50
51
  WHITESPACE_MARKER_BYTES = "<>'".bytes.freeze
51
52
  BOOLEAN_ATTRIBUTE_BY_CLOSING = DELIMITERS.values.to_h { |closing| [closing, /#{ATTRIBUTE_NAME}(?=(\s|#{Regexp.escape(closing)}|\z))/] }.freeze
52
53
  ATTRIBUTE_LIST_END_BY_CLOSING = DELIMITERS.values.to_h { |closing| [closing, /\s*#{Regexp.escape(closing)}/] }.freeze
@@ -364,11 +365,18 @@ module SuperSlim
364
365
  end
365
366
  end
366
367
 
367
- # A tag name is consumed with its match; a shortcut is left for parse_tag to read as an attribute.
368
+ # A tag name is consumed with its match; a shortcut is left for parse_tag to read as an attribute of a div, and
369
+ # a splat as the attributes of a dynamic tag.
368
370
  def scan_tag
369
- tag = @scanner.check(Shortcuts::TAG_PATTERN) or return
370
- @scanner.pos += tag.bytesize if @scanner[1]
371
- tag
371
+ length = @scanner.match?(Shortcuts::TAG_PATTERN) or return
372
+ case @scanner.peek_byte
373
+ when HASH, DOT then Shortcuts::DEFAULT_TAG
374
+ when STAR then "*"
375
+ else
376
+ tag = @scanner.peek(length)
377
+ @scanner.pos += length
378
+ tag
379
+ end
372
380
  end
373
381
 
374
382
  def parse_embedded(engine, attributes_source, text)
@@ -473,18 +481,18 @@ module SuperSlim
473
481
 
474
482
  def parse_tag(tag)
475
483
  tag_lineno = @lineno
476
- tag = Shortcuts::DEFAULT_TAG if Shortcuts::ATTRIBUTE_BY_SHORTCUT.key?(tag)
477
484
 
478
485
  attributes = []
479
486
  # Read before it is consumed, so that an illegal shortcut is reported where it starts.
480
487
  while (shortcut_length = @scanner.match?(@attribute_shortcut))
481
- shortcut = @scanner[1]
482
- value = @scanner[2]
483
- syntax_error!("Illegal shortcut") unless (name = Shortcuts::ATTRIBUTE_BY_SHORTCUT[shortcut])
488
+ # A shortcut is a single `#` or `.`: read by bytes, without a string for it.
489
+ next_byte = @scanner.string.getbyte(@scanner.pos + 1)
490
+ syntax_error!("Illegal shortcut") if next_byte == HASH || next_byte == DOT
484
491
 
492
+ value = @scanner[1]
485
493
  # Tailwind arbitrary values may hold quotes and ampersands, which Slim names never do.
486
494
  value = ERB::Escape.html_escape(value) if value.match?(SHORTCUT_VALUE_TO_ESCAPE)
487
- attributes << StaticAttribute.new(name, value)
495
+ attributes << StaticAttribute.new((@scanner.peek_byte == HASH) ? "id" : "class", value)
488
496
  @scanner.pos += shortcut_length
489
497
  end
490
498
 
@@ -629,6 +637,7 @@ module SuperSlim
629
637
  first = source_offset if @outline
630
638
  value = +""
631
639
  depth = 0
640
+ breaks = 0
632
641
  quote_byte = quote.ord
633
642
 
634
643
  until depth == 0 && @scanner.peek_byte == quote_byte
@@ -636,7 +645,7 @@ module SuperSlim
636
645
  # Unlike a stripped continuation line, a line read whole keeps its trailing whitespace in the value.
637
646
  @outline[@lineno].text = true if @outline && @scanner.eos? && @scanner.string.equal?(@line)
638
647
  value << (@scanner.eos? ? "\n" : " ")
639
- @stacks.last << NEWLINE
648
+ breaks += 1
640
649
  expect_next_line
641
650
  elsif (chunk = @scanner.scan(QUOTED_CHUNK_BY_QUOTE[quote]))
642
651
  value << chunk
@@ -648,6 +657,9 @@ module SuperSlim
648
657
  end
649
658
  end
650
659
 
660
+ # A break inside an interpolation is already a line of the compiled code, which keeps it: only the others get
661
+ # a newline node.
662
+ (breaks - Compiler.interpolation_newlines(value)).times { @stacks.last << NEWLINE } if breaks > 0
651
663
  record_ruby(:text, first, source_offset) if @outline
652
664
  @scanner.pos += 1
653
665
  value
@@ -17,10 +17,10 @@ module SuperSlim
17
17
 
18
18
  def class_value(value, escape, html_safe)
19
19
  if value.is_a?(Array)
20
- value = value.flatten.filter_map do |element|
21
- string = element.to_s
22
- string unless string.empty?
23
- end.join(" ")
20
+ # `flatten` returns a new array: changing it in place spares one more.
21
+ classes = value.flatten.map!(&:to_s)
22
+ classes.reject!(&:empty?)
23
+ value = classes.join(" ")
24
24
  end
25
25
  return value.to_s unless escape
26
26
 
@@ -28,11 +28,13 @@ module SuperSlim
28
28
  end
29
29
 
30
30
  def join_classes(*values)
31
- values.reject(&:empty?).join(" ")
31
+ values.reject!(&:empty?)
32
+ values.join(" ")
32
33
  end
33
34
 
34
35
  # Attributes only known at render time: splats (`*hash`), `data=`/`aria=` hashes and dynamic tags (`*{tag: "a"}`).
35
- class Attributes
36
+ # A hash of each attribute's rendered value by name, in the order they render: one object per tag.
37
+ class Attributes < Hash
36
38
  # Slim lets tabs and line breaks through: they end a name, so `data: {"x\tonclick" => code}` set onclick.
37
39
  INVALID_NAME = /[[:cntrl:] "'>\/=]/
38
40
  # Read after escaping, which turns markup into entities: `&` is refused with the rest.
@@ -41,19 +43,15 @@ module SuperSlim
41
43
  # Far deeper than any page needs, and short of the stack a hash referring to itself would exhaust.
42
44
  MAX_HYPHENATED_DEPTH = 100
43
45
 
44
- def initialize(html_safe)
45
- @html_safe = html_safe
46
- @value_by_name = {}
47
- end
48
-
49
- def code_attr(name, escape, value)
46
+ # Names are checked unless the compiler already found them valid: those from render-time data always are.
47
+ def code_attr(name, escape, value, check = true)
50
48
  if name == "class"
51
49
  value = value.is_a?(Array) ? value.join(" ") : value.to_s
52
- attr(name, escape(escape, value)) unless value.empty?
50
+ attr(name, escape(escape, value), false) unless value.empty?
53
51
  elsif HYPHENATED.include?(name) && value.is_a?(Hash)
54
52
  hyphenated_attr(name, escape, value)
55
53
  elsif value != false && !value.nil?
56
- attr(name, escape(escape, value))
54
+ attr(name, escape(escape, value), check)
57
55
  end
58
56
  end
59
57
 
@@ -61,20 +59,20 @@ module SuperSlim
61
59
  splat.each { |name, value| code_attr(name.is_a?(Symbol) ? name.name : name.to_s, true, value) }
62
60
  end
63
61
 
64
- def attr(name, value)
65
- raise InvalidAttributeNameError, "Invalid attribute name '#{name}' was rendered" if name.empty? || name.match?(INVALID_NAME)
62
+ def attr(name, value, check = true)
63
+ raise InvalidAttributeNameError, "Invalid attribute name '#{name}' was rendered" if check && (name.empty? || name.match?(INVALID_NAME))
66
64
 
67
- if !@value_by_name.key?(name)
68
- @value_by_name[name] = value
65
+ if !key?(name)
66
+ self[name] = value
69
67
  elsif name == "class"
70
- @value_by_name[name] = "#{@value_by_name[name]} #{value}"
68
+ self[name] = "#{self[name]} #{value}"
71
69
  else
72
70
  raise "Multiple #{name} attributes specified"
73
71
  end
74
72
  end
75
73
 
76
74
  def build_tag(content = nil)
77
- tag = @value_by_name.delete("tag").to_s
75
+ tag = delete("tag").to_s
78
76
  tag = "div" if tag.empty?
79
77
  raise InvalidTagNameError, "Invalid tag name '#{tag}' was rendered" unless tag.match?(VALID_TAG)
80
78
  content ? "<#{tag}#{self}>#{content}</#{tag}>" : "<#{tag}#{self} />"
@@ -82,7 +80,7 @@ module SuperSlim
82
80
 
83
81
  def to_s
84
82
  html = +""
85
- @value_by_name.each do |name, value|
83
+ each do |name, value|
86
84
  html << " " << name
87
85
  html << %(=") << value.to_s << %(") unless value == true
88
86
  end
@@ -102,9 +100,16 @@ module SuperSlim
102
100
  end
103
101
 
104
102
  def escape(escape, value)
105
- return value if !escape || value == true
103
+ (!escape || value == true) ? value : Runtime.escape_html(value)
104
+ end
105
+ end
106
+
107
+ # Values that are `html_safe?` render as they are.
108
+ class HtmlSafeAttributes < Attributes
109
+ private
106
110
 
107
- @html_safe ? Runtime.escape_html_safe(value) : Runtime.escape_html(value)
111
+ def escape(escape, value)
112
+ (!escape || value == true) ? value : Runtime.escape_html_safe(value)
108
113
  end
109
114
  end
110
115
  end
@@ -3,7 +3,6 @@
3
3
  module SuperSlim
4
4
  # `#name` sets the id and `.name` a class, on a div unless a tag name comes first.
5
5
  module Shortcuts
6
- ATTRIBUTE_BY_SHORTCUT = {"#" => "id", "." => "class"}.freeze
7
6
  DEFAULT_TAG = "div"
8
7
 
9
8
  SLIM_NAME_PART = /\p{Word}|-|\/\d+|:(?:\w|-)+/
@@ -21,8 +20,8 @@ module SuperSlim
21
20
  | (?<=-)\( (?:[^()\s] | \([^()\s]*\))+ \)
22
21
  }x
23
22
 
24
- ATTRIBUTE_PATTERN = /\A([#.]+)((?:#{SLIM_NAME_PART})*)/
25
- TAILWIND_ATTRIBUTE_PATTERN = /\A([#.]+)((?:#{TAILWIND_NAME_PART})*)/
26
- TAG_PATTERN = /\A(?:[#.]|\*(?=[^\s]+)|(\p{Word}(?:\p{Word}|:|-)*\p{Word}|\p{Word}+))/
23
+ ATTRIBUTE_PATTERN = /\A[#.]+((?:#{SLIM_NAME_PART})*)/
24
+ TAILWIND_ATTRIBUTE_PATTERN = /\A[#.]+((?:#{TAILWIND_NAME_PART})*)/
25
+ TAG_PATTERN = /\A(?:[#.]|\*(?=[^\s]+)|(?:\p{Word}(?:\p{Word}|:|-)*\p{Word}|\p{Word}+))/
27
26
  end
28
27
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module SuperSlim
4
- VERSION = "3.6.2"
4
+ VERSION = "3.7.0"
5
5
  end
data/lib/super_slim.rb CHANGED
@@ -70,8 +70,9 @@ module SuperSlim
70
70
  # Located like the parser's errors, its backtrace starting at the template line, which Rails reports.
71
71
  def compile_syntax_error(exception, source, file)
72
72
  lineno = exception.lineno
73
- # The source parsed, so it is valid UTF-8 whatever its encoding says. A construct whose compiled code takes
74
- # more lines than its template can move the count past the end: the error still raises, without that line.
73
+ # The source parsed, so it is valid UTF-8 whatever its encoding says. A count past the end guards against
74
+ # compiled code taking more lines than its template, which no construct is known to do: the error still
75
+ # raises, without that line.
75
76
  line = String.new(Parser.split_lines(source.b)[lineno - 1].to_s, encoding: Encoding::UTF_8)
76
77
  line = line.delete_prefix("") if lineno == 1
77
78
  error = SyntaxError.new(exception.message, file, line, lineno, line[/\A[ \t]*/].size)
metadata CHANGED
@@ -1,90 +1,90 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: super-slim
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.6.2
4
+ version: 3.7.0
5
5
  platform: ruby
6
6
  authors:
7
- - Level UP Solutions
7
+ - Level UP Solutions
8
8
  bindir: exe
9
9
  cert_chain: []
10
10
  date: 1980-01-02 00:00:00.000000000 Z
11
11
  dependencies:
12
- - !ruby/object:Gem::Dependency
13
- name: strscan
14
- requirement: !ruby/object:Gem::Requirement
15
- requirements:
16
- - - ">="
17
- - !ruby/object:Gem::Version
18
- version: 3.1.1
19
- type: :runtime
20
- prerelease: false
21
- version_requirements: !ruby/object:Gem::Requirement
22
- requirements:
23
- - - ">="
24
- - !ruby/object:Gem::Version
25
- version: 3.1.1
26
- description: A Slim template engine written from scratch, without Temple. It renders
27
- the same HTML as Slim 5, compiles and renders faster with fewer allocations, and
28
- comes with a formatter that cannot change a page, a linter with HTML and accessibility
29
- rules, and a RuboCop plugin for the Ruby inside templates.
12
+ - !ruby/object:Gem::Dependency
13
+ name: strscan
14
+ requirement: !ruby/object:Gem::Requirement
15
+ requirements:
16
+ -
17
+ - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: 3.1.1
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ -
25
+ - ">="
26
+ - !ruby/object:Gem::Version
27
+ version: 3.1.1
28
+ description: "A Slim template engine written from scratch, without Temple. It renders the same HTML as Slim 5, compiles and renders faster with fewer allocations, and comes with a formatter that cannot change a page, a linter with HTML and accessibility rules, and a RuboCop plugin for the Ruby inside templates."
30
29
  executables:
31
- - super-slim
30
+ - super-slim
32
31
  extensions: []
33
32
  extra_rdoc_files: []
34
33
  files:
35
- - CHANGELOG.md
36
- - LICENSE
37
- - LICENSE-slim
38
- - README.md
39
- - config/rubocop.yml
40
- - exe/super-slim
41
- - lib/super-slim.rb
42
- - lib/super_slim.rb
43
- - lib/super_slim/ast.rb
44
- - lib/super_slim/cli.rb
45
- - lib/super_slim/compiler.rb
46
- - lib/super_slim/formatter.rb
47
- - lib/super_slim/formatter/attribute_arranger.rb
48
- - lib/super_slim/formatter/code_wrapper.rb
49
- - lib/super_slim/formatter/line_wrapper.rb
50
- - lib/super_slim/linter.rb
51
- - lib/super_slim/linter/presets.rb
52
- - lib/super_slim/linter/rules.rb
53
- - lib/super_slim/parallel.rb
54
- - lib/super_slim/parser.rb
55
- - lib/super_slim/parser/ruby_brackets.rb
56
- - lib/super_slim/rails_handler.rb
57
- - lib/super_slim/railtie.rb
58
- - lib/super_slim/rubocop.rb
59
- - lib/super_slim/runtime.rb
60
- - lib/super_slim/shortcuts.rb
61
- - lib/super_slim/version.rb
62
- homepage: https://github.com/levups/super-slim
34
+ - CHANGELOG.md
35
+ - LICENSE
36
+ - LICENSE-slim
37
+ - README.md
38
+ - config/rubocop.yml
39
+ - exe/super-slim
40
+ - lib/super-slim.rb
41
+ - lib/super_slim.rb
42
+ - lib/super_slim/ast.rb
43
+ - lib/super_slim/cli.rb
44
+ - lib/super_slim/compiler.rb
45
+ - lib/super_slim/formatter.rb
46
+ - lib/super_slim/formatter/attribute_arranger.rb
47
+ - lib/super_slim/formatter/code_wrapper.rb
48
+ - lib/super_slim/formatter/line_wrapper.rb
49
+ - lib/super_slim/linter.rb
50
+ - lib/super_slim/linter/presets.rb
51
+ - lib/super_slim/linter/rules.rb
52
+ - lib/super_slim/parallel.rb
53
+ - lib/super_slim/parser.rb
54
+ - lib/super_slim/parser/ruby_brackets.rb
55
+ - lib/super_slim/rails_handler.rb
56
+ - lib/super_slim/railtie.rb
57
+ - lib/super_slim/rubocop.rb
58
+ - lib/super_slim/runtime.rb
59
+ - lib/super_slim/shortcuts.rb
60
+ - lib/super_slim/version.rb
61
+ homepage: "https://github.com/levups/super-slim"
63
62
  licenses:
64
- - MIT
63
+ - MIT
65
64
  metadata:
66
- source_code_uri: https://github.com/levups/super-slim
67
- changelog_uri: https://github.com/levups/super-slim/blob/main/CHANGELOG.md
68
- documentation_uri: https://levups.github.io/super-slim/
69
- bug_tracker_uri: https://github.com/levups/super-slim/issues
70
- rubygems_mfa_required: 'true'
71
- default_lint_roller_plugin: SuperSlim::RuboCop::Plugin
65
+ source_code_uri: "https://github.com/levups/super-slim"
66
+ changelog_uri: "https://github.com/levups/super-slim/blob/main/CHANGELOG.md"
67
+ documentation_uri: "https://levups.github.io/super-slim/"
68
+ bug_tracker_uri: "https://github.com/levups/super-slim/issues"
69
+ rubygems_mfa_required: "true"
70
+ default_lint_roller_plugin: "SuperSlim::RuboCop::Plugin"
72
71
  rdoc_options: []
73
72
  require_paths:
74
- - lib
73
+ - lib
75
74
  required_ruby_version: !ruby/object:Gem::Requirement
76
75
  requirements:
77
- - - ">="
78
- - !ruby/object:Gem::Version
79
- version: '3.3'
76
+ -
77
+ - ">="
78
+ - !ruby/object:Gem::Version
79
+ version: "3.3"
80
80
  required_rubygems_version: !ruby/object:Gem::Requirement
81
81
  requirements:
82
- - - ">="
83
- - !ruby/object:Gem::Version
84
- version: '0'
82
+ -
83
+ - ">="
84
+ - !ruby/object:Gem::Version
85
+ version: "0"
85
86
  requirements: []
86
- rubygems_version: 4.0.20
87
+ rubygems_version: 4.1.0
87
88
  specification_version: 4
88
- summary: A Slim template engine that compiles and renders faster, with a formatter
89
- and a linter
89
+ summary: "A Slim template engine that compiles and renders faster, with a formatter and a linter"
90
90
  test_files: []