mirror of
https://github.com/harttle/liquidjs.git
synced 2026-09-15 04:10:40 -07:00
feat: support disable outputEscape for specific filters, #565
This commit is contained in:
@@ -2,11 +2,14 @@ import { expect } from 'chai'
|
||||
import { Liquid } from '../../../src/liquid'
|
||||
|
||||
describe('liquid#registerFilter()', function () {
|
||||
const liquid = new Liquid()
|
||||
let liquid: Liquid
|
||||
beforeEach(() => { liquid = new Liquid() })
|
||||
|
||||
describe('key-value arguments', function () {
|
||||
liquid.registerFilter('obj_test', function (...args) {
|
||||
return JSON.stringify(args)
|
||||
beforeEach(() => {
|
||||
liquid.registerFilter('obj_test', function (...args) {
|
||||
return JSON.stringify(args)
|
||||
})
|
||||
})
|
||||
it('should support key-value arguments', async () => {
|
||||
const src = `{{ "a" | obj_test: k1: "v1", k2: foo }}`
|
||||
@@ -23,14 +26,39 @@ describe('liquid#registerFilter()', function () {
|
||||
})
|
||||
|
||||
describe('async filters', () => {
|
||||
liquid.registerFilter('get_user_data', function (userId) {
|
||||
return Promise.resolve({ userId, userName: userId.toUpperCase() })
|
||||
})
|
||||
it('should support async filter', async () => {
|
||||
liquid.registerFilter('get_user_data', function (userId) {
|
||||
return Promise.resolve({ userId, userName: userId.toUpperCase() })
|
||||
})
|
||||
const src = `{{ userId | get_user_data | json }}`
|
||||
const dst = '{"userId":"alice","userName":"ALICE"}'
|
||||
const html = await liquid.parseAndRender(src, { userId: 'alice' })
|
||||
return expect(html).to.equal(dst)
|
||||
})
|
||||
})
|
||||
|
||||
describe('raw filters', () => {
|
||||
beforeEach(() => {
|
||||
liquid = new Liquid({
|
||||
outputEscape: 'escape'
|
||||
})
|
||||
})
|
||||
it('should escape filter output when outputEscape set to true', async () => {
|
||||
liquid.registerFilter('break', (str) => str.replace(/\n/g, '<br/>'))
|
||||
const src = `{{ "a\nb" | break }}`
|
||||
const dst = 'a<br/>b'
|
||||
const html = await liquid.parseAndRender(src)
|
||||
return expect(html).to.equal(dst)
|
||||
})
|
||||
it('should not escape filter output when registered as "raw"', async () => {
|
||||
liquid.registerFilter('break', {
|
||||
handler: (str) => str.replace(/\n/g, '<br/>'),
|
||||
raw: true
|
||||
})
|
||||
const src = `{{ "a\nb" | break }}`
|
||||
const dst = 'a<br/>b'
|
||||
const html = await liquid.parseAndRender(src)
|
||||
return expect(html).to.equal(dst)
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user