Skip to content

Commit aebd75e

Browse files
authored
Merge pull request #1954 from Shopify/jm/doc_body
Expose tag body in the Doc tag
2 parents fc96e66 + 7f2f8a2 commit aebd75e

File tree

2 files changed

+114
-7
lines changed

2 files changed

+114
-7
lines changed

lib/liquid/tags/doc.rb

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,15 +36,19 @@ def initialize(tag_name, markup, parse_context)
3636
end
3737

3838
def parse(tokens)
39+
@body = +""
40+
3941
while (token = tokens.shift)
4042
tag_name = token =~ BlockBody::FullTokenPossiblyInvalid && Regexp.last_match(2)
4143

4244
raise_nested_doc_error if tag_name == @tag_name
4345

4446
if tag_name == block_delimiter
4547
parse_context.trim_whitespace = (token[-3] == WhitespaceControl)
48+
@body << Regexp.last_match(1) if Regexp.last_match(1) != ""
4649
return
4750
end
51+
@body << token unless token.empty?
4852
end
4953

5054
raise_tag_never_closed(block_name)
@@ -55,11 +59,11 @@ def render_to_output_buffer(_context, output)
5559
end
5660

5761
def blank?
58-
true
62+
@body.empty?
5963
end
6064

6165
def nodelist
62-
[]
66+
[@body]
6367
end
6468

6569
private

test/unit/tags/doc_tag_unit_test.rb

Lines changed: 108 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,21 @@ def test_doc_tag
2020
assert_template_result('', template)
2121
end
2222

23+
def test_doc_tag_body_content
24+
doc_content = " Documentation content\n @param {string} foo - test\n"
25+
template_source = "{% doc %}#{doc_content}{% enddoc %}"
26+
27+
doc_tag = nil
28+
ParseTreeVisitor
29+
.for(Template.parse(template_source).root)
30+
.add_callback_for(Liquid::Doc) do |tag|
31+
doc_tag = tag
32+
end
33+
.visit
34+
35+
assert_equal(doc_content, doc_tag.nodelist.first.to_s)
36+
end
37+
2338
def test_doc_tag_does_not_support_extra_arguments
2439
error = assert_raises(Liquid::SyntaxError) do
2540
template = <<~LIQUID.chomp
@@ -116,6 +131,20 @@ def test_doc_tag_ignores_malformed_syntax
116131
assert_template_result('', template)
117132
end
118133

134+
def test_doc_tag_captures_token_before_enddoc
135+
template_source = "{% doc %}{{ incomplete{% enddoc %}"
136+
137+
doc_tag = nil
138+
ParseTreeVisitor
139+
.for(Template.parse(template_source).root)
140+
.add_callback_for(Liquid::Doc) do |tag|
141+
doc_tag = tag
142+
end
143+
.visit
144+
145+
assert_equal("{{ incomplete", doc_tag.nodelist.first.to_s)
146+
end
147+
119148
def test_doc_tag_preserves_error_line_numbers
120149
template = Liquid::Template.parse(<<~LIQUID.chomp, line_numbers: true)
121150
{% doc %}
@@ -145,11 +174,11 @@ def test_doc_tag_whitespace_control
145174

146175
def test_doc_tag_delimiter_handling
147176
assert_template_result('', <<~LIQUID.chomp)
148-
{% if true %}
149-
{% doc %}
150-
{% docEXTRA %}wut{% enddocEXTRA %}xyz
151-
{% enddoc %}
152-
{% endif %}
177+
{%- if true -%}
178+
{%- doc -%}
179+
{%- docEXTRA -%}wut{% enddocEXTRA -%}xyz
180+
{%- enddoc -%}
181+
{%- endif -%}
153182
LIQUID
154183

155184
assert_template_result('', "{% doc %}123{% enddoc xyz %}")
@@ -167,6 +196,80 @@ def test_doc_tag_visitor
167196
)
168197
end
169198

199+
def test_doc_tag_blank_with_empty_content
200+
template_source = "{% doc %}{% enddoc %}"
201+
202+
doc_tag = nil
203+
ParseTreeVisitor
204+
.for(Template.parse(template_source).root)
205+
.add_callback_for(Liquid::Doc) do |tag|
206+
doc_tag = tag
207+
end
208+
.visit
209+
210+
assert_equal(true, doc_tag.blank?)
211+
end
212+
213+
def test_doc_tag_blank_with_content
214+
template_source = "{% doc %}Some documentation{% enddoc %}"
215+
216+
doc_tag = nil
217+
ParseTreeVisitor
218+
.for(Template.parse(template_source).root)
219+
.add_callback_for(Liquid::Doc) do |tag|
220+
doc_tag = tag
221+
end
222+
.visit
223+
224+
assert_equal(false, doc_tag.blank?)
225+
end
226+
227+
def test_doc_tag_blank_with_whitespace_only
228+
template_source = "{% doc %} {% enddoc %}"
229+
230+
doc_tag = nil
231+
ParseTreeVisitor
232+
.for(Template.parse(template_source).root)
233+
.add_callback_for(Liquid::Doc) do |tag|
234+
doc_tag = tag
235+
end
236+
.visit
237+
238+
assert_equal(false, doc_tag.blank?)
239+
end
240+
241+
def test_doc_tag_nodelist_returns_array_with_body
242+
doc_content = "Documentation content\n@param {string} foo"
243+
template_source = "{% doc %}#{doc_content}{% enddoc %}"
244+
245+
doc_tag = nil
246+
ParseTreeVisitor
247+
.for(Template.parse(template_source).root)
248+
.add_callback_for(Liquid::Doc) do |tag|
249+
doc_tag = tag
250+
end
251+
.visit
252+
253+
assert_equal([doc_content], doc_tag.nodelist)
254+
assert_equal(1, doc_tag.nodelist.length)
255+
assert_equal(doc_content, doc_tag.nodelist.first)
256+
end
257+
258+
def test_doc_tag_nodelist_with_empty_content
259+
template_source = "{% doc %}{% enddoc %}"
260+
261+
doc_tag = nil
262+
ParseTreeVisitor
263+
.for(Template.parse(template_source).root)
264+
.add_callback_for(Liquid::Doc) do |tag|
265+
doc_tag = tag
266+
end
267+
.visit
268+
269+
assert_equal([""], doc_tag.nodelist)
270+
assert_equal(1, doc_tag.nodelist.length)
271+
end
272+
170273
private
171274

172275
def traversal(template)

0 commit comments

Comments
 (0)