feat: escape filters from Jekyll, #443

This commit is contained in:
Yang Jun
2024-05-12 15:02:04 +08:00
parent 4955e75be7
commit 4561ba3d53
13 changed files with 169 additions and 14 deletions
+6
View File
@@ -26,6 +26,12 @@ describe('filters/html', function () {
it('should escape nil value to empty string', () =>
test('{{ undefinedValue | escape_once }}', ''))
})
describe('xml_escape', function () {
it('should xml_escape \' and &', function () {
return test('{{ "Have you read \'James & the Giant Peach\'?" | xml_escape }}',
'Have you read 'James & the Giant Peach'?')
})
})
describe('newline_to_br', function () {
it('should support string_with_newlines', function () {
const src = '{% capture string_with_newlines %}\n' +
+40 -10
View File
@@ -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('{{ "[email protected]" | 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('{{ "[email protected]" | 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)
})
})
})