Skip to content

Commit 19a9271

Browse files
authored
Remove selective rendering (#838)
Thought I’d open this up so we can look at what exactly it would involve to remove selective rendering. Closes #836
1 parent 170fb27 commit 19a9271

File tree

5 files changed

+15
-200
lines changed

5 files changed

+15
-200
lines changed

lib/phlex/context.rb

Lines changed: 2 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -6,53 +6,24 @@ def initialize(user_context: {}, view_context: nil)
66
@buffer = +""
77
@capturing = false
88
@user_context = user_context
9-
@fragments = nil
10-
@in_target_fragment = false
11-
@halt_signal = nil
129
@view_context = view_context
1310
end
1411

15-
attr_accessor :buffer, :capturing, :user_context, :in_target_fragment
12+
attr_accessor :buffer, :capturing, :user_context
1613

17-
attr_reader :fragments, :view_context
18-
19-
def target_fragments(fragments)
20-
@fragments = fragments.to_h { |it| [it, true].freeze }
21-
end
22-
23-
def around_render
24-
return yield if !@fragments || @halt_signal
25-
26-
catch do |signal|
27-
@halt_signal = signal
28-
yield
29-
end
30-
end
31-
32-
def begin_target(id)
33-
@in_target_fragment = id
34-
end
35-
36-
def end_target
37-
@fragments.delete(@in_target_fragment)
38-
@in_target_fragment = false
39-
throw @halt_signal if @fragments.length == 0
40-
end
14+
attr_reader :view_context
4115

4216
def capturing_into(new_buffer)
4317
original_buffer = @buffer
4418
original_capturing = @capturing
45-
original_fragments = @fragments
4619

4720
begin
4821
@buffer = new_buffer
4922
@capturing = true
50-
@fragments = nil
5123
yield
5224
ensure
5325
@buffer = original_buffer
5426
@capturing = original_capturing
55-
@fragments = original_fragments
5627
end
5728

5829
new_buffer

lib/phlex/html.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ class Phlex::HTML < Phlex::SGML
1010
# Output an HTML doctype.
1111
def doctype
1212
context = @_context
13-
return if context.fragments && !context.in_target_fragment
1413

1514
context.buffer << "<!doctype html>"
1615
nil

lib/phlex/sgml.rb

Lines changed: 13 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -56,44 +56,38 @@ def to_proc
5656
proc { |c| c.render(self) }
5757
end
5858

59-
def call(buffer = +"", context: {}, view_context: nil, parent: nil, fragments: nil, &block)
59+
def call(buffer = +"", context: {}, view_context: nil, parent: nil, &block)
6060
@_buffer = buffer
6161
@_context = phlex_context = parent&.__context__ || Phlex::Context.new(user_context: context, view_context:)
6262
@_parent = parent
6363

6464
raise Phlex::DoubleRenderError.new("You can't render a #{self.class.name} more than once.") if @_rendered
6565
@_rendered = true
6666

67-
if fragments
68-
phlex_context.target_fragments(fragments)
69-
end
70-
7167
block ||= @_content_block
7268

7369
return "" unless render?
7470

7571
Thread.current[:__phlex_component__] = [self, Fiber.current.object_id].freeze
7672

77-
phlex_context.around_render do
78-
before_template(&block)
73+
before_template(&block)
7974

80-
around_template do
81-
if block
82-
view_template do |*args|
83-
if args.length > 0
84-
__yield_content_with_args__(*args, &block)
85-
else
86-
__yield_content__(&block)
87-
end
75+
around_template do
76+
if block
77+
view_template do |*args|
78+
if args.length > 0
79+
__yield_content_with_args__(*args, &block)
80+
else
81+
__yield_content__(&block)
8882
end
89-
else
90-
view_template
9183
end
84+
else
85+
view_template
9286
end
93-
94-
after_template(&block)
9587
end
9688

89+
after_template(&block)
90+
9791
unless parent
9892
buffer << phlex_context.buffer
9993
end
@@ -119,7 +113,6 @@ def plain(content)
119113
# Output a single space character. If a block is given, a space will be output before and after the block.
120114
def whitespace(&)
121115
context = @_context
122-
return if context.fragments && !context.in_target_fragment
123116

124117
buffer = context.buffer
125118

@@ -138,7 +131,6 @@ def whitespace(&)
138131
# [MDN Docs](https://developer.mozilla.org/en-US/docs/Web/HTML/Comments)
139132
def comment(&)
140133
context = @_context
141-
return if context.fragments && !context.in_target_fragment
142134

143135
buffer = context.buffer
144136

@@ -154,7 +146,6 @@ def raw(content)
154146
case content
155147
when Phlex::SGML::SafeObject
156148
context = @_context
157-
return if context.fragments && !context.in_target_fragment
158149

159150
context.buffer << content.to_s
160151
when nil, "" # do nothing
@@ -234,7 +225,6 @@ def render(renderable = nil, &)
234225
# ```
235226
def cache(*cache_key, **options, &content)
236227
context = @_context
237-
return if context.fragments && !context.in_target_fragment
238228

239229
location = caller_locations(1, 1)[0]
240230

@@ -263,7 +253,6 @@ def cache(*cache_key, **options, &content)
263253
# If you need to pass multiple cache keys, you should pass an array.
264254
def low_level_cache(cache_key, **options, &content)
265255
context = @_context
266-
return if context.fragments && !context.in_target_fragment
267256

268257
context.buffer << cache_store.fetch(cache_key, **options) { capture(&content) }
269258
end
@@ -351,7 +340,6 @@ def __yield_content_with_args__(*a)
351340

352341
def __implicit_output__(content)
353342
context = @_context
354-
return true if context.fragments && !context.in_target_fragment
355343

356344
case content
357345
when Phlex::SGML::SafeObject
@@ -376,7 +364,6 @@ def __implicit_output__(content)
376364
# same as __implicit_output__ but escapes even `safe` objects
377365
def __text__(content)
378366
context = @_context
379-
return true if context.fragments && !context.in_target_fragment
380367

381368
case content
382369
when String

lib/phlex/sgml/elements.rb

Lines changed: 0 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -12,26 +12,8 @@ def register_element(method_name, tag: method_name.name.tr("_", "-"))
1212
def #{method_name}(**attributes)
1313
context = @_context
1414
buffer = context.buffer
15-
fragment = context.fragments
16-
target_found = false
1715
block_given = block_given?
1816
19-
if fragment
20-
return if fragment.length == 0 # we found all our fragments already
21-
22-
id = attributes[:id]
23-
24-
if !context.in_target_fragment
25-
if fragment[id]
26-
context.begin_target(id)
27-
target_found = true
28-
else
29-
yield(self) if block_given
30-
return nil
31-
end
32-
end
33-
end
34-
3517
if attributes.length > 0 # with attributes
3618
if block_given # with content block
3719
buffer << "<#{tag}" << (Phlex::ATTRIBUTE_CACHE[attributes] ||= __attributes__(attributes)) << ">"
@@ -90,8 +72,6 @@ def #{method_name}(**attributes)
9072
9173
#{'flush' if tag == 'head'}
9274
93-
context.end_target if target_found
94-
9575
nil
9676
end
9777
RUBY
@@ -108,31 +88,13 @@ def __register_void_element__(method_name, tag: method_name.name.tr("_", "-"))
10888
def #{method_name}(**attributes)
10989
context = @_context
11090
buffer = context.buffer
111-
fragment = context.fragments
112-
113-
if fragment
114-
return if fragment.length == 0 # we found all our fragments already
115-
116-
id = attributes[:id]
117-
118-
if !context.in_target_fragment
119-
if fragment[id]
120-
context.begin_target(id)
121-
target_found = true
122-
else
123-
return nil
124-
end
125-
end
126-
end
12791
12892
if attributes.length > 0 # with attributes
12993
buffer << "<#{tag}" << (::Phlex::ATTRIBUTE_CACHE[attributes] ||= __attributes__(attributes)) << ">"
13094
else # without attributes
13195
buffer << "<#{tag}>"
13296
end
13397
134-
context.end_target if target_found
135-
13698
nil
13799
end
138100

quickdraw/sgml/selective_rendering.test.rb

Lines changed: 0 additions & 104 deletions
This file was deleted.

0 commit comments

Comments
 (0)