This repository has been archived by the owner on Jun 20, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #187 from Shopify/fix/strict-mode
Add support for strict Liquid parsing
- Loading branch information
Showing
15 changed files
with
344 additions
and
117 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,89 +1,125 @@ | ||
import { expect } from 'chai'; | ||
import { liquidHtmlGrammar, liquidStatementsGrammar } from '~/parser/grammar'; | ||
import { strictGrammars, tolerantGrammars } from '~/parser/grammar'; | ||
|
||
describe('Unit: liquidHtmlGrammar', () => { | ||
it('should parse or not parse HTML+Liquid', () => { | ||
expectMatchSucceeded('<h6 data-src="hello world">').to.be.true; | ||
expectMatchSucceeded('<a src="https://product"></a>').to.be.true; | ||
expectMatchSucceeded('<a src="https://google.com"></b>').to.be.true; | ||
expectMatchSucceeded(`<img src="hello" loading='lazy' enabled=true disabled>`).to.be.true; | ||
expectMatchSucceeded(`<img src="hello" loading='lazy' enabled=true disabled />`).to.be.true; | ||
expectMatchSucceeded(`<{{header_type}}-header>`).to.be.true; | ||
expectMatchSucceeded(`<header--{{header_type}}>`).to.be.true; | ||
expectMatchSucceeded(`<-nope>`).to.be.false; | ||
expectMatchSucceeded(`<:nope>`).to.be.false; | ||
expectMatchSucceeded(`<1nope>`).to.be.false; | ||
expectMatchSucceeded(`{{ product.feature }}`).to.be.true; | ||
expectMatchSucceeded(`{{product.feature}}`).to.be.true; | ||
expectMatchSucceeded(`{%- if A -%}`).to.be.true; | ||
expectMatchSucceeded(`{%-if A-%}`).to.be.true; | ||
expectMatchSucceeded(`{%- else-%}`).to.be.true; | ||
expectMatchSucceeded(`{%- liquid-%}`).to.be.true; | ||
expectMatchSucceeded(`{%- schema-%}`).to.be.true; | ||
expectMatchSucceeded(`{%- form-%}`).to.be.true; | ||
expectMatchSucceeded(`{{ true-}}`).to.be.true; | ||
expectMatchSucceeded(` | ||
<html> | ||
<head> | ||
{{ 'foo' | script_tag }} | ||
</head> | ||
<body> | ||
{% if true %} | ||
<div> | ||
hello world | ||
</div> | ||
{% else %} | ||
nope | ||
{% endif %} | ||
</body> | ||
</html> | ||
`).to.be.true; | ||
expectMatchSucceeded(` | ||
<input | ||
class="[[ cssClasses.checkbox ]] form-checkbox sm:text-[8px]" | ||
type="checkbox" | ||
const grammars = [ | ||
{ mode: 'strict', grammar: strictGrammars }, | ||
{ mode: 'tolerant', grammar: tolerantGrammars }, | ||
]; | ||
|
||
[[# isRefined ]] | ||
checked | ||
[[/ isRefined ]] | ||
/> | ||
`).to.be.true; | ||
expectMatchSucceeded(` | ||
<svg> | ||
<svg a=1><svg b=2> | ||
<path d="M12"></path> | ||
</svg></svg> | ||
</svg> | ||
`).to.be.true; | ||
expectMatchSucceeded(`<div data-popup-{{ section.id }}="size-{{ section.id }}">`).to.be.true; | ||
expectMatchSucceeded('<img {% if aboveFold %} loading="lazy"{% endif %} />').to.be.true; | ||
expectMatchSucceeded('<svg><use></svg>').to.be.true; | ||
expectMatchSucceeded('<6h>').to.be.false; | ||
grammars.forEach(({ mode, grammar }) => { | ||
describe(`Case: ${mode}`, () => { | ||
it('should parse or not parse HTML+Liquid', () => { | ||
expectMatchSucceeded('<h6 data-src="hello world">').to.be.true; | ||
expectMatchSucceeded('<a src="https://product"></a>').to.be.true; | ||
expectMatchSucceeded('<a src="https://google.com"></b>').to.be.true; | ||
expectMatchSucceeded(`<img src="hello" loading='lazy' enabled=true disabled>`).to.be.true; | ||
expectMatchSucceeded(`<img src="hello" loading='lazy' enabled=true disabled />`).to.be.true; | ||
expectMatchSucceeded(`<{{header_type}}-header>`).to.be.true; | ||
expectMatchSucceeded(`<header--{{header_type}}>`).to.be.true; | ||
expectMatchSucceeded(`<-nope>`).to.be.false; | ||
expectMatchSucceeded(`<:nope>`).to.be.false; | ||
expectMatchSucceeded(`<1nope>`).to.be.false; | ||
expectMatchSucceeded(`{{ product.feature }}`).to.be.true; | ||
expectMatchSucceeded(`{{product.feature}}`).to.be.true; | ||
expectMatchSucceeded(`{%- if A -%}`).to.be.true; | ||
expectMatchSucceeded(`{%-if A-%}`).to.be.true; | ||
expectMatchSucceeded(`{%- else-%}`).to.be.true; | ||
expectMatchSucceeded(`{%- break-%}`).to.be.true; | ||
expectMatchSucceeded(`{%- continue -%}`).to.be.true; | ||
expectMatchSucceeded(`{%- liquid-%}`).to.be.true; | ||
expectMatchSucceeded(`{%- schema-%}{% endschema %}`).to.be.true; | ||
expectMatchSucceeded(`{%- form 'form-type'-%}`).to.be.true; | ||
expectMatchSucceeded(`{%- # a comment -%}`).to.be.true; | ||
expectMatchSucceeded(`{%- javascript -%}{% endjavascript %}`).to.be.true; | ||
expectMatchSucceeded(`{%- include 'layout' -%}`).to.be.true; | ||
expectMatchSucceeded(`{%- layout 'full-width' -%}`).to.be.true; | ||
expectMatchSucceeded(`{%- layout none -%}`).to.be.true; | ||
expectMatchSucceeded(`{% render 'filename' for array as item %}`).to.be.true; | ||
expectMatchSucceeded(`{% section 'name' %}`).to.be.true; | ||
expectMatchSucceeded(`{% sections 'name' %}`).to.be.true; | ||
expectMatchSucceeded(`{% style %}{% endstyle %}`).to.be.true; | ||
expectMatchSucceeded(`{% stylesheet %}{% endstylesheet %}`).to.be.true; | ||
expectMatchSucceeded(`{% assign variable_name = value %}`).to.be.true; | ||
expectMatchSucceeded(` | ||
{% capture variable %} | ||
value | ||
{% endcapture %} | ||
`).to.be.true; | ||
expectMatchSucceeded(` | ||
{% for variable in array limit: number %} | ||
expression | ||
{% endfor %} | ||
`).to.be.true; | ||
|
||
function expectMatchSucceeded(text: string) { | ||
const match = liquidHtmlGrammar.match(text, 'Node'); | ||
return expect(match.succeeded()); | ||
} | ||
}); | ||
expectMatchSucceeded(`{% decrement variable_name %}`).to.be.true; | ||
expectMatchSucceeded(`{% increment variable_name %}`).to.be.true; | ||
expectMatchSucceeded(`{{ true-}}`).to.be.true; | ||
expectMatchSucceeded(` | ||
<html> | ||
<head> | ||
{{ 'foo' | script_tag }} | ||
</head> | ||
<body> | ||
{% if true %} | ||
<div> | ||
hello world | ||
</div> | ||
{% else %} | ||
nope | ||
{% endif %} | ||
</body> | ||
</html> | ||
`).to.be.true; | ||
expectMatchSucceeded(` | ||
<input | ||
class="[[ cssClasses.checkbox ]] form-checkbox sm:text-[8px]" | ||
type="checkbox" | ||
[[# isRefined ]] | ||
checked | ||
[[/ isRefined ]] | ||
/> | ||
`).to.be.true; | ||
expectMatchSucceeded(` | ||
<svg> | ||
<svg a=1><svg b=2> | ||
<path d="M12"></path> | ||
</svg></svg> | ||
</svg> | ||
`).to.be.true; | ||
expectMatchSucceeded(`<div data-popup-{{ section.id }}="size-{{ section.id }}">`).to.be | ||
.true; | ||
expectMatchSucceeded('<img {% if aboveFold %} loading="lazy"{% endif %} />').to.be.true; | ||
expectMatchSucceeded('<svg><use></svg>').to.be.true; | ||
expectMatchSucceeded('<6h>').to.be.false; | ||
|
||
function expectMatchSucceeded(text: string) { | ||
const match = grammar.LiquidHTML.match(text, 'Node'); | ||
return expect(match.succeeded(), text); | ||
} | ||
}); | ||
|
||
it('should parse or not parse {% liquid %} lines', () => { | ||
expectMatchSucceeded(` | ||
layout none | ||
it('should parse or not parse {% liquid %} lines', () => { | ||
expectMatchSucceeded(` | ||
layout none | ||
paginate search.results by 28 | ||
for item in search.results | ||
if item.object_type != 'product' | ||
continue | ||
endif | ||
paginate search.results by 28 | ||
for item in search.results | ||
if item.object_type != 'product' | ||
continue | ||
endif | ||
render 'product-item', product: item | ||
endfor | ||
endpaginate | ||
`).to.be.true; | ||
render 'product-item', product: item | ||
endfor | ||
endpaginate | ||
`).to.be.true; | ||
|
||
function expectMatchSucceeded(text: string) { | ||
const match = liquidStatementsGrammar.match(text.trimStart(), 'Node'); | ||
return expect(match.succeeded()); | ||
} | ||
function expectMatchSucceeded(text: string) { | ||
const match = grammar.LiquidStatement.match(text.trimStart(), 'Node'); | ||
return expect(match.succeeded(), text); | ||
} | ||
}); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.