-
-
Notifications
You must be signed in to change notification settings - Fork 237
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: automatic output escaping, closes #500
- Loading branch information
Showing
9 changed files
with
188 additions
and
7 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
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 |
---|---|---|
@@ -0,0 +1,51 @@ | ||
--- | ||
title: raw | ||
--- | ||
|
||
{% since %}v9.37.0{% endsince %} | ||
|
||
Liquid filter that directly returns the value of the variable. Useful when [outputEscape](/api/interfaces/liquid_options_.liquidoptions.html#Optional-outputEscape) is set. | ||
|
||
{% note info Auto escape %} | ||
By default `outputEscape` is not set. That means LiquidJS output is not escaped by default, thus `raw` filter is not useful until `outputEscape` is set. | ||
{% endnote %} | ||
|
||
Input (`outputEscape` not set) | ||
```liquid | ||
{{ "<" }} | ||
``` | ||
|
||
Output | ||
```text | ||
< | ||
``` | ||
|
||
Input (`outputEscape="escape"`) | ||
```liquid | ||
{{ "<" }} | ||
``` | ||
|
||
Output | ||
```text | ||
< | ||
``` | ||
|
||
Input (`outputEscape="json"`) | ||
```liquid | ||
{{ "<" }} | ||
``` | ||
|
||
Output | ||
```text | ||
"<" | ||
``` | ||
|
||
Input (`outputEscape="escape"`) | ||
```liquid | ||
{{ "<" | raw }} | ||
``` | ||
|
||
Output | ||
```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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
--- | ||
title: raw | ||
--- | ||
|
||
{% since %}v9.37.0{% endsince %} | ||
|
||
直接返回变量的值。配合 [outputEscape](/api/interfaces/liquid_options_.liquidoptions.html#Optional-outputEscape) 参数使用。 | ||
|
||
{% note info 自动转义 %} | ||
默认情况下 `outputEscape` 为 `undefined`,这意味着 LiquidJS 输出不会默认转义,因此这时使用 `raw` 没有意义。 | ||
{% endnote %} | ||
|
||
输入(未设置 `outputEscape`) | ||
```liquid | ||
{{ "<" }} | ||
``` | ||
|
||
输出 | ||
```text | ||
< | ||
``` | ||
|
||
输入(`outputEscape="escape"`) | ||
```liquid | ||
{{ "<" }} | ||
``` | ||
|
||
输出 | ||
```text | ||
< | ||
``` | ||
|
||
输入(`outputEscape="json"`) | ||
```liquid | ||
{{ "<" }} | ||
``` | ||
|
||
输出 | ||
```text | ||
"<" | ||
``` | ||
|
||
输入(`outputEscape="escape"`) | ||
```liquid | ||
{{ "<" | raw }} | ||
``` | ||
|
||
输出 | ||
```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
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
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 |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import { expect } from 'chai' | ||
import { Liquid } from '../../../src/liquid' | ||
|
||
describe('LiquidOptions#*outputEscape*', function () { | ||
it('when outputEscape is not set', async function () { | ||
const engine = new Liquid() | ||
const html = await engine.parseAndRender('{{"<"}}') | ||
expect(html).to.equal('<') | ||
}) | ||
|
||
it('should escape when outputEscape="escape"', async function () { | ||
const engine = new Liquid({ | ||
outputEscape: 'escape' | ||
}) | ||
const html = await engine.parseAndRender('{{"<"}}') | ||
expect(html).to.equal('<') | ||
}) | ||
|
||
it('should json stringify when outputEscape="json"', async function () { | ||
const engine = new Liquid({ | ||
outputEscape: 'json' | ||
}) | ||
const html = await engine.parseAndRender('{{"<"}}') | ||
expect(html).to.equal('"<"') | ||
}) | ||
|
||
it('should support outputEscape=Function', async function () { | ||
const engine = new Liquid({ | ||
outputEscape: (v: any) => `{${v}}` | ||
}) | ||
const html = await engine.parseAndRender('{{"<"}}') | ||
expect(html).to.equal('{<}') | ||
}) | ||
|
||
it('should skip escape for output with filter "| raw"', async function () { | ||
const engine = new Liquid({ | ||
outputEscape: 'escape' | ||
}) | ||
const html = await engine.parseAndRender('{{"<" | raw}}') | ||
expect(html).to.equal('<') | ||
}) | ||
}) |