Skip to content

Pass the tag markup and tokenizer to Document#unknown_tag#1290

Merged
dylanahsmith merged 2 commits into
masterfrom
document-unknown-tag-refactor
Sep 11, 2020
Merged

Pass the tag markup and tokenizer to Document#unknown_tag#1290
dylanahsmith merged 2 commits into
masterfrom
document-unknown-tag-refactor

Conversation

@dylanahsmith
Copy link
Copy Markdown
Contributor

Depends on #1289

Problem

Shopify is currently overriding Liquid::Document#registered_tags to implement theme section tags, since they are only supposed to be available at the top-level of the section document. However, registered_tags is actually defined in Liquid::BlockBody and #1289 will make Liquid::Document no longer inherit from Liquid::BlockBody, so we need another way to enforce this constraint.

Solution

Liquid already has builtin context sensitive tags, such as elsif and else within the if tag. These are implemented using Liquid::Block#unknown_tag. Liquid::Document also has an unknown_tag method, but it isn't provided the markup or tokenizer needed to the information we need, so the first commit of this pull request changes Liquid::Document#unknown_tag to take the same parameters as Liquid::Block#unknown_tag. Liquid::Document#parse was also changed to be more like Liquid::Block#parse by continuing to parse if unknown_tag handles the tag without raising.

The second commit merely changes the Liquid::Block#unknown_tag parameter names for clarify since we use markup elsewhere to refer to the text within the tag after the tag name and the third parameter is actually a tokenizer.

@dylanahsmith dylanahsmith force-pushed the refactor-for-c-block-body branch from e146906 to dfbbf87 Compare September 8, 2020 18:01
@dylanahsmith dylanahsmith force-pushed the document-unknown-tag-refactor branch from ea97ca3 to a372baa Compare September 8, 2020 18:08
Comment thread lib/liquid/document.rb
@body.parse(tokens, parse_context) do |end_tag_name, _end_tag_params|
unknown_tag(end_tag_name, parse_context) if end_tag_name
def parse(tokenizer, parse_context)
while parse_body(tokenizer)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are we adding a loop?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because we don't want the handled unknown tag (e.g. section tags) to end the parsing of the document, we still need to parse the rest of the document.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, but why wasn't there a loop in the previous implementation? Sorry if I'm missing something obvious 😅 I reviewed expecting a refactor without change in behavior.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Previously unknown_tag was expected to always raise when called on a Liquid::Document, unlike Liquid::Block which has its unknown_tag method overridden by child classes which don't always raise. This won't change any existing code, since Liquid::Document#unknown_tag itself still always raises. It just means that now we can override unknown_tag in a child class of Liquid::Document and not raise to have it continue parsing.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It just means that now we can override unknown_tag in a child class of Liquid::Document and not raise to have it continue parsing.

Ahhh that explains it. 👍

Comment thread lib/liquid/document.rb
@body.parse(tokens, parse_context) do |end_tag_name, _end_tag_params|
unknown_tag(end_tag_name, parse_context) if end_tag_name
def parse(tokenizer, parse_context)
while parse_body(tokenizer)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It just means that now we can override unknown_tag in a child class of Liquid::Document and not raise to have it continue parsing.

Ahhh that explains it. 👍

Base automatically changed from refactor-for-c-block-body to master September 11, 2020 13:32
The parse_context no longer needs to be passed in because it is available
through through an attr_reader on the instance. However, the markup and
tokenizer weren't made available.  This refactor also makes the parameters
given to Document#unknown_tag consistent with Block#unknown_tag.
@dylanahsmith dylanahsmith force-pushed the document-unknown-tag-refactor branch from a372baa to 0d02dea Compare September 11, 2020 13:33
@dylanahsmith dylanahsmith merged commit fb77921 into master Sep 11, 2020
@dylanahsmith dylanahsmith deleted the document-unknown-tag-refactor branch September 11, 2020 13:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants