-
-
Notifications
You must be signed in to change notification settings - Fork 238
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: escape filters from Jekyll, #443
- Loading branch information
Showing
13 changed files
with
169 additions
and
14 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 |
---|---|---|
@@ -0,0 +1,17 @@ | ||
--- | ||
title: cgi_escape | ||
--- | ||
|
||
{% since %}v10.13.0{% endsince %} | ||
|
||
CGI escape a string for use in a URL. Replaces any special characters with appropriate `%XX` replacements. CGI escape normally replaces a space with a plus `+` sign. | ||
|
||
Input | ||
```liquid | ||
{{ "foo, bar; baz?" | cgi_escape }} | ||
``` | ||
|
||
Output | ||
```text | ||
foo%2C+bar%3B+baz%3F | ||
``` |
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,19 @@ | ||
--- | ||
title: uri_escape | ||
--- | ||
|
||
{% since %}v10.13.0{% endsince %} | ||
|
||
Percent encodes any special characters in a URI. URI escape normally replaces a space with `%20`. [Reserved characters][reserved] will not be escaped. | ||
|
||
Input | ||
```liquid | ||
{{ "http://foo.com/?q=foo, \bar?" | uri_escape }} | ||
``` | ||
|
||
Output | ||
```text | ||
http://foo.com/?q=foo,%20%5Cbar? | ||
``` | ||
|
||
[reserved]: https://en.wikipedia.org/wiki/Percent-encoding#Types_of_URI_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,17 @@ | ||
--- | ||
title: xml_escape | ||
--- | ||
|
||
{% since %}v10.13.0{% endsince %} | ||
|
||
Escape some text for use in XML. | ||
|
||
Input | ||
```liquid | ||
{{ "Have you read \'James & the Giant Peach\'?" | xml_escape }} | ||
``` | ||
|
||
Output | ||
```text | ||
Have you read 'James & the Giant Peach'? | ||
``` |
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,17 @@ | ||
--- | ||
title: cgi_escape | ||
--- | ||
|
||
{% since %}v10.13.0{% endsince %} | ||
|
||
把字符串 CGI 转义,用于 URL。用对应的 `%XX` 替换特殊字符,空格会被转义为 `+` 号。 | ||
|
||
输入 | ||
```liquid | ||
{{ "foo, bar; baz?" | cgi_escape }} | ||
``` | ||
|
||
输出 | ||
```text | ||
foo%2C+bar%3B+baz%3F | ||
``` |
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,19 @@ | ||
--- | ||
title: uri_escape | ||
--- | ||
|
||
{% since %}v10.13.0{% endsince %} | ||
|
||
把 URI 中的特殊字符做百分号编码,空格会变成 `%20`。[保留字][reserved] 不会被转义。 | ||
|
||
输入 | ||
```liquid | ||
{{ "http://foo.com/?q=foo, \bar?" | uri_escape }} | ||
``` | ||
|
||
输出 | ||
```text | ||
http://foo.com/?q=foo,%20%5Cbar? | ||
``` | ||
|
||
[reserved]: https://en.wikipedia.org/wiki/Percent-encoding#Types_of_URI_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,17 @@ | ||
--- | ||
title: xml_escape | ||
--- | ||
|
||
{% since %}v10.13.0{% endsince %} | ||
|
||
把文本做 XML 转义。 | ||
|
||
输入 | ||
```liquid | ||
{{ "Have you read \'James & the Giant Peach\'?" | xml_escape }} | ||
``` | ||
|
||
输出 | ||
```text | ||
Have you read 'James & the Giant Peach'? | ||
``` |
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,4 +1,10 @@ | ||
import { stringify } from '../util/underscore' | ||
|
||
export const url_decode = (x: string) => stringify(x).split('+').map(decodeURIComponent).join(' ') | ||
export const url_encode = (x: string) => stringify(x).split(' ').map(encodeURIComponent).join('+') | ||
export const url_decode = (x: string) => decodeURIComponent(stringify(x)).replace(/\+/g, ' ') | ||
export const url_encode = (x: string) => encodeURIComponent(stringify(x)).replace(/%20/g, '+') | ||
export const cgi_escape = (x: string) => encodeURIComponent(stringify(x)) | ||
.replace(/%20/g, '+') | ||
.replace(/[!'()*]/g, c => '%' + c.charCodeAt(0).toString(16).toUpperCase()) | ||
export const uri_escape = (x: string) => encodeURI(stringify(x)) | ||
.replace(/%5B/g, '[') | ||
.replace(/%5D/g, ']') |
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,15 +1,45 @@ | ||
import { test } from '../../stub/render' | ||
import { Liquid } from '../../../src' | ||
|
||
describe('filters/url', function () { | ||
describe('url_decode', function () { | ||
it('should decode %xx and +', | ||
() => test('{{ "%27Stop%21%27+said+Fred" | url_decode }}', "'Stop!' said Fred")) | ||
describe('filters/url', () => { | ||
const liquid = new Liquid() | ||
describe('url_decode', () => { | ||
it('should decode %xx and +', () => { | ||
const html = liquid.parseAndRenderSync('{{ "%27Stop%21%27+said+Fred" | url_decode }}') | ||
expect(html).toEqual("'Stop!' said Fred") | ||
}) | ||
}) | ||
|
||
describe('url_encode', function () { | ||
it('should encode @', | ||
() => test('{{ "john@liquid.com" | url_encode }}', 'john%40liquid.com')) | ||
it('should encode <space>', | ||
() => test('{{ "Tetsuro Takara" | url_encode }}', 'Tetsuro+Takara')) | ||
describe('url_encode', () => { | ||
it('should encode @', () => { | ||
const html = liquid.parseAndRenderSync('{{ "john@liquid.com" | url_encode }}') | ||
expect(html).toEqual('john%40liquid.com') | ||
}) | ||
it('should encode <space>', () => { | ||
const html = liquid.parseAndRenderSync('{{ "Tetsuro Takara" | url_encode }}') | ||
expect(html).toEqual('Tetsuro+Takara') | ||
}) | ||
}) | ||
|
||
describe('cgi_escape', () => { | ||
it('should escape CGI chars', () => { | ||
const html = liquid.parseAndRenderSync('{{ "!\',()*\\"!" | cgi_escape }}') | ||
expect(html).toEqual('%21%27%2C%28%29%2A%22%21') | ||
}) | ||
it('should escape space as +', () => { | ||
const html = liquid.parseAndRenderSync('{{ "foo, bar; baz?" | cgi_escape }}') | ||
expect(html).toEqual('foo%2C+bar%3B+baz%3F') | ||
}) | ||
}) | ||
|
||
describe('uri_escape', () => { | ||
it('should escape unsupported chars for uri', () => { | ||
const html = liquid.parseAndRenderSync('{{ "http://foo.com/?q=foo, \\\\bar?" | uri_escape }}') | ||
expect(html).toEqual('http://foo.com/?q=foo,%20%5Cbar?') | ||
}) | ||
it('should not escape reserved characters', () => { | ||
const reserved = "!#$&'()*+,/:;=?@[]" | ||
const html = liquid.parseAndRenderSync('{{ reserved | uri_escape }}', { reserved }) | ||
expect(html).toEqual(reserved) | ||
}) | ||
}) | ||
}) |