Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/soft-liquid-endtag.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@shopify/liquid-html-parser': patch
---

Stop parsing an incomplete HTML start tag before its enclosing Liquid block terminator so tolerant formatting preserves the original source.
24 changes: 19 additions & 5 deletions crates/liquid-parser/src/html.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,20 @@ impl<'a> Parser<'a> {
self.consume(TT::HtmlTagOpen)?;
let name = self.parse_compound_name()?;
let attributes = self.parse_attributes()?;

if self.is_block_terminator() {
let tag_name = self.peek_tag_name().unwrap();
let open_end = self.tokens[self.p - 1].end;
return Err(error(
format!(
"Attempting to close LiquidTag '{tag_name}' before HtmlElement '{}' was closed",
display_name(plain_name(&name))
),
open_start,
open_end,
));
}

if self.accept(TT::HtmlSelfClose).is_some() {
let bsp = Span::new(open_start, self.tokens[self.p - 1].end);
return Ok(Node::HtmlSelfClosingElement(HtmlSelfClosingElement {
Expand Down Expand Up @@ -408,19 +422,19 @@ impl<'a> Parser<'a> {
}

pub(crate) fn parse_branch_attributes(&mut self) -> PResult<Vec<Node<'a>>> {
self.parse_attribute_list(true)
self.parse_attribute_list()
}

fn parse_attributes(&mut self) -> PResult<Vec<Node<'a>>> {
let saved = self.in_attribute_context;
self.in_attribute_context = true;
let result = self.parse_attribute_list(false);
let result = self.parse_attribute_list();
self.in_attribute_context = saved;
result
}

/// `parseAttributeList`, or `parseBranchAttributesImpl` when `in_branch`.
fn parse_attribute_list(&mut self, in_branch: bool) -> PResult<Vec<Node<'a>>> {
/// Parse HTML attributes, stopping before a Liquid block terminator.
fn parse_attribute_list(&mut self) -> PResult<Vec<Node<'a>>> {
let mut attrs = Vec::new();
while !self.is_at_end() {
self.skip_space();
Expand All @@ -430,7 +444,7 @@ impl<'a> Parser<'a> {
{
break;
}
if in_branch && self.is_block_terminator() {
if self.is_block_terminator() {
break;
}
if self.check(TT::LiquidTagOpen, 0) {
Expand Down
12 changes: 12 additions & 0 deletions packages/liquid-html-parser/src/document/html.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,17 @@ export function parseHtmlElement(
const name = parseCompoundName(parser);
const attributes = parseAttributes(parser);

if (parser.isBlockTerminator()) {
const tagName = parser.peekTagName()!;
const openTagEnd = parser.tokenAt(parser.getPosition() - 1).end;
throw new LiquidHTMLASTParsingError(
`Attempting to close LiquidTag '${tagName}' before HtmlElement '${extractPlainName(name) || 'compound'}' was closed`,
source,
openTagStart,
openTagEnd,
);
}

if (parser.accept(TokenType.HtmlSelfClose)) {
const openTagEnd = parser.tokenAt(parser.getPosition() - 1).end;
const blockStartPosition: Position = { start: openTagStart, end: openTagEnd };
Expand Down Expand Up @@ -513,6 +524,7 @@ function parseAttributeList(parser: HtmlParserDelegate): AttributeNode[] {
skipSpace(parser);
if (parser.isAtEnd()) break;
if (parser.check(TokenType.HtmlTagClose) || parser.check(TokenType.HtmlSelfClose)) break;
if (parser.isBlockTerminator()) break;

// Liquid tag between attributes
if (parser.check(TokenType.LiquidTagOpen)) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{% capture a %}<svg h="1"{% endcapture %}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{% capture a %}<svg h="1"{% endcapture %}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { test } from 'vitest';
import { assertFormattedEqualsFixed } from '../test-helpers';

test('issue-1382', async () => {
await assertFormattedEqualsFixed(__dirname);
});
7 changes: 7 additions & 0 deletions specs/parser/unclosed.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
cases:
- name: should report an endcapture inside an unclosed HTML start tag
source: "{% capture a %}<svg h=\"1\"{% endcapture %}\nx"
parse: [html]
error:
message: Attempting to close LiquidTag 'endcapture' before HtmlElement 'svg' was closed
start: [0, 15]
text: '<svg h="1"'
- name: should parse nested unclosed tags
source: "{% for a in b %} <div> {% if true %}"
parse: [liquid]
Expand Down
Loading