view_component_subtemplates 0.3.0 → 0.4.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: acf9c5ce1fdb01c2939e9e5737cfaf2b66a38a9540f8f24eafadf3cb473aaba5
4
- data.tar.gz: 78bfcfbc62662ab2d5ac93a7f56a1bf4fcdd410132bf23e356c58b9a503336f9
3
+ metadata.gz: 6db77273514e3dbdc8bbcd2b15a3a446ce1bf77887dbfec439bafccdc1958ef6
4
+ data.tar.gz: fd18610333ffa99b8c9fc937a786f53280990b3e659d160cf3d7cf5849e87676
5
5
  SHA512:
6
- metadata.gz: 85de07486ae8017548b8bcef1168967cecb340bfe3060564f33fc9779b8fef323703b364b38d2bde01814d7f4eecb9295f73fc620531ebf14a529108d6a8c7dd
7
- data.tar.gz: 34d0b0f65cac64023cb71eecb06e6001dccb9f1b2144510ea71c21acbaf19cdf13cfaff17a19840ae7be55c09ce52ac3a157810cd11726ce836ab83e0f7800c3
6
+ metadata.gz: 50e2ba1c63fbdc320ed5a673972410992ae3f8561280bce9075cbeb6d5b798eb8bc20b9586b2e40edd5d0a7a0ecac8f791427b931f15d4014d01d99387dd1a4d
7
+ data.tar.gz: 82fb32bf3c9115346aa2b4839655bf484229aac232f017ced62f603dbc634ebe835b0ab7ddfb85ab07941244d40871a9dc9a019d4e13467f9ade0056ca30bdb9
data/CHANGELOG.md CHANGED
@@ -1,5 +1,17 @@
1
1
  ## [Unreleased]
2
2
 
3
+ ## [0.4.0] - 2026-11-20
4
+
5
+ ### Fixed
6
+ - Subtemplates now escape interpolated values. They were compiled with plain `ERB` and the whole output was marked `html_safe`, so user input rendered through `<%= %>` was inserted as raw HTML. Values already marked `html_safe` are still rendered as-is.
7
+
8
+ ### Changed
9
+ - **Breaking:** requires `view_component >= 4.10.0`. Subtemplates render through the component's `capture`, which ViewComponent syncs with the view context's buffer since 4.10.0. With earlier versions, block helpers called through `helpers` render their content outside the container.
10
+ - **Breaking:** block helpers that return their output (`fields_for`, `simple_fields_for`, `form_with`, `content_tag`, etc.) must be called with `<%=`, as in any Rails template. With plain `ERB`, `<% fields_for :x do |f| %>` rendered its block by accident; it now renders nothing, without raising an error. Change those tags to `<%= fields_for :x do |f| %>` when upgrading. This change can't be made before upgrading: plain `ERB` raises a syntax error on `<%= helper do %>`.
11
+ - Subtemplates are compiled with the ActionView handler registered for their extension, the same way ViewComponent compiles the main template. Block helpers (`<%= content_tag :div do %>`) and `capture` now work inside subtemplates, and `.haml`/`.slim` subtemplates use their own handler instead of being compiled as ERB.
12
+ - The trim marker in `<%# locals: (...) -%>` now removes the line break that follows it, so subtemplate output no longer starts with `"\n"`.
13
+ - Subtemplates in a non-HTML format (e.g. `notes.text.erb`) are not HTML-escaped, following Rails' behavior for text templates. Their output is not marked `html_safe`, so it is escaped when embedded in an HTML template.
14
+
3
15
  ## [0.3.0] - 2026-06-12
4
16
 
5
17
  ### Added
data/README.md CHANGED
@@ -152,7 +152,7 @@ templates, `call_<name>` keeps taking locals exactly as shown in the Quick Start
152
152
 
153
153
  - Ruby >= 3.1.0
154
154
  - Rails >= 7.0.0
155
- - ViewComponent >= 4.2.0
155
+ - ViewComponent >= 4.10.0
156
156
 
157
157
  ## Development
158
158
 
@@ -15,6 +15,10 @@ module ViewComponentSubtemplates
15
15
  #
16
16
  # PRIVATE API - All other methods are subject to change without notice.
17
17
  class SubTemplate
18
+ # Minimal template object accepted by ActionView template handlers.
19
+ TemplateDetails = Struct.new(:format, :identifier, :short_identifier, :type, keyword_init: true)
20
+ private_constant :TemplateDetails
21
+
18
22
  # PUBLIC API
19
23
  attr_reader :component, :path, :template_name
20
24
 
@@ -37,7 +41,7 @@ module ViewComponentSubtemplates
37
41
  # This is the main entry point for the class from the compiler extension
38
42
  def compile_to_component
39
43
  validate_file_exists!
40
- compiled_source = compile_erb_source
44
+ compiled_source = compile_source
41
45
  define_render_method(compiled_source, explicit_locals)
42
46
  end
43
47
 
@@ -57,10 +61,40 @@ module ViewComponentSubtemplates
57
61
  locals_string.scan(/(\w+):/).flatten.map(&:to_sym)
58
62
  end
59
63
 
60
- def compile_erb_source
61
- erb = ERB.new(source)
62
- erb.filename = path
63
- erb.src
64
+ # Compiles the source with the ActionView handler registered for the file
65
+ # extension, the same way ViewComponent compiles the main template. This keeps
66
+ # Rails' HTML escaping for `<%= %>` and support for block helpers and capture.
67
+ def compile_source
68
+ template = TemplateDetails.new(
69
+ format: template_format,
70
+ identifier: path,
71
+ short_identifier: short_identifier,
72
+ type: template_type
73
+ )
74
+
75
+ template_handler.call(template, source)
76
+ end
77
+
78
+ def template_type
79
+ ActionView::Template::Types[template_format]
80
+ end
81
+
82
+ def escaped_output?
83
+ !ActionView::Template::Handlers::ERB.escape_ignore_list.include?(template_type)
84
+ end
85
+
86
+ def template_handler
87
+ extension = File.basename(path).split(".").last
88
+ ActionView::Template.registered_template_handler(extension)
89
+ end
90
+
91
+ def template_format
92
+ parts = File.basename(path).split(".")
93
+ parts.length > 2 ? parts[-2].to_sym : :html
94
+ end
95
+
96
+ def short_identifier
97
+ defined?(Rails.root) && Rails.root ? path.sub("#{Rails.root}/", "") : path
64
98
  end
65
99
 
66
100
  def define_render_method(compiled_source, expected_args)
@@ -71,11 +105,18 @@ module ViewComponentSubtemplates
71
105
  define_public_call_method(call_method_name, render_method_name, expected_args)
72
106
  end
73
107
 
108
+ # `capture` renders into a fresh string inside the shared buffer, not the
109
+ # caller's output. ViewComponent's override syncs it with the view context's
110
+ # buffer (they differ inside partials), so `helpers` blocks capture correctly.
111
+ # It returns nil for an empty subtemplate.
74
112
  def define_private_render_method(name, compiled_source, args)
113
+ output = escaped_output? ? "output" : "String.new(output)"
114
+
75
115
  @component.class_eval <<~RUBY, __FILE__, __LINE__ + 1
76
116
  private
77
117
  def #{name}(#{args.join(", ")})
78
- (#{compiled_source}).html_safe
118
+ output = capture { #{compiled_source} } || "".html_safe
119
+ #{output}
79
120
  end
80
121
  RUBY
81
122
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module ViewComponentSubtemplates
4
- VERSION = "0.3.0"
4
+ VERSION = "0.4.0"
5
5
  end
metadata CHANGED
@@ -1,14 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: view_component_subtemplates
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - jsolas
8
- autorequire:
9
8
  bindir: exe
10
9
  cert_chain: []
11
- date: 2026-06-16 00:00:00.000000000 Z
10
+ date: 1980-01-02 00:00:00.000000000 Z
12
11
  dependencies:
13
12
  - !ruby/object:Gem::Dependency
14
13
  name: rails
@@ -30,14 +29,14 @@ dependencies:
30
29
  requirements:
31
30
  - - ">="
32
31
  - !ruby/object:Gem::Version
33
- version: 4.2.0
32
+ version: 4.10.0
34
33
  type: :runtime
35
34
  prerelease: false
36
35
  version_requirements: !ruby/object:Gem::Requirement
37
36
  requirements:
38
37
  - - ">="
39
38
  - !ruby/object:Gem::Version
40
- version: 4.2.0
39
+ version: 4.10.0
41
40
  description: Adds support for sub-templates with arguments to ViewComponent, allowing
42
41
  components to have multiple templates in their sidecar directory.
43
42
  email:
@@ -64,7 +63,6 @@ metadata:
64
63
  homepage_uri: https://github.com/bukhr/view_component_subtemplates
65
64
  source_code_uri: https://github.com/bukhr/view_component_subtemplates
66
65
  changelog_uri: https://github.com/bukhr/view_component_subtemplates/blob/main/CHANGELOG.md
67
- post_install_message:
68
66
  rdoc_options: []
69
67
  require_paths:
70
68
  - lib
@@ -79,8 +77,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
79
77
  - !ruby/object:Gem::Version
80
78
  version: '0'
81
79
  requirements: []
82
- rubygems_version: 3.5.18
83
- signing_key:
80
+ rubygems_version: 4.0.15
84
81
  specification_version: 4
85
82
  summary: Sub-templates functionality for ViewComponent
86
83
  test_files: []