mirror of
https://github.com/harttle/liquidjs.git
synced 2026-09-15 04:10:40 -07:00
feat: add unregisterFilter method (#946)
* feat: add unregisterFilter method * docs: show how to re-register built-in filters --------- Co-authored-by: Yacov <yacov@noemail>
This commit is contained in:
@@ -62,7 +62,23 @@ See existing filter implementations here: <https://github.com/harttle/liquidjs/t
|
|||||||
|
|
||||||
## Unregister Tags/Filters
|
## Unregister Tags/Filters
|
||||||
|
|
||||||
In some cases it's desirable to disable some tags/filters (see [#324](https://github.com/harttle/liquidjs/issues/324)). You'll need to register a dummy tag/filter that throws a corresponding Error.
|
Filters can be unregistered by name:
|
||||||
|
|
||||||
|
```javascript
|
||||||
|
engine.unregisterFilter('plus')
|
||||||
|
```
|
||||||
|
|
||||||
|
With [`strictFilters`][strict-filters] enabled, using an unregistered filter will throw an error. Otherwise, the filter will be skipped.
|
||||||
|
|
||||||
|
Built-in filters can be registered again using the exported `filters` object:
|
||||||
|
|
||||||
|
```javascript
|
||||||
|
import { filters } from 'liquidjs'
|
||||||
|
|
||||||
|
engine.registerFilter('plus', filters.plus)
|
||||||
|
```
|
||||||
|
|
||||||
|
To disable a tag, or to make a disabled filter throw regardless of `strictFilters`, register a dummy implementation that throws a corresponding error (see [#324](https://github.com/harttle/liquidjs/issues/324)):
|
||||||
|
|
||||||
```javascript
|
```javascript
|
||||||
// disable a tag
|
// disable a tag
|
||||||
@@ -81,3 +97,5 @@ function disabledFilter(name) {
|
|||||||
}
|
}
|
||||||
engine.registerFilter('plus', disabledFilter('plus'));
|
engine.registerFilter('plus', disabledFilter('plus'));
|
||||||
```
|
```
|
||||||
|
|
||||||
|
[strict-filters]: /tutorials/options.html#strict
|
||||||
|
|||||||
@@ -101,6 +101,9 @@ export class Liquid {
|
|||||||
public registerFilter (name: string, filter: FilterImplOptions) {
|
public registerFilter (name: string, filter: FilterImplOptions) {
|
||||||
this.filters[name] = filter
|
this.filters[name] = filter
|
||||||
}
|
}
|
||||||
|
public unregisterFilter (name: string) {
|
||||||
|
delete this.filters[name]
|
||||||
|
}
|
||||||
public registerTag (name: string, tag: TagClass | TagImplOptions) {
|
public registerTag (name: string, tag: TagClass | TagImplOptions) {
|
||||||
this.tags[name] = isFunction(tag) ? tag : createTagClass(tag)
|
this.tags[name] = isFunction(tag) ? tag : createTagClass(tag)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
import { Liquid } from '../../../src/liquid'
|
import { Liquid, filters } from '../../../src'
|
||||||
|
|
||||||
describe('liquid#registerFilter()', function () {
|
describe('liquid#registerFilter()', function () {
|
||||||
let liquid: Liquid
|
let liquid: Liquid
|
||||||
@@ -67,3 +67,32 @@ describe('liquid#registerFilter()', function () {
|
|||||||
await expect(new Liquid({ strictFilters: true }).parseAndRender('{{ 1 | constructor }}')).rejects.toThrow('undefined filter')
|
await expect(new Liquid({ strictFilters: true }).parseAndRender('{{ 1 | constructor }}')).rejects.toThrow('undefined filter')
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
describe('liquid#unregisterFilter()', function () {
|
||||||
|
let liquid: Liquid
|
||||||
|
beforeEach(() => { liquid = new Liquid() })
|
||||||
|
|
||||||
|
it('should unregister a custom filter', async () => {
|
||||||
|
liquid.registerFilter('greet', value => `hello ${value}`)
|
||||||
|
liquid.unregisterFilter('greet')
|
||||||
|
const html = await liquid.parseAndRender('{{ "world" | greet }}')
|
||||||
|
return expect(html).toBe('world')
|
||||||
|
})
|
||||||
|
|
||||||
|
it('should unregister a built-in filter', () => {
|
||||||
|
liquid = new Liquid({ strictFilters: true })
|
||||||
|
liquid.unregisterFilter('upcase')
|
||||||
|
return expect(liquid.parseAndRender('{{ "foo" | upcase }}')).rejects.toThrow('undefined filter: upcase')
|
||||||
|
})
|
||||||
|
|
||||||
|
it('should support re-registering a built-in filter', async () => {
|
||||||
|
liquid.unregisterFilter('upcase')
|
||||||
|
liquid.registerFilter('upcase', filters.upcase)
|
||||||
|
const html = await liquid.parseAndRender('{{ "foo" | upcase }}')
|
||||||
|
return expect(html).toBe('FOO')
|
||||||
|
})
|
||||||
|
|
||||||
|
it('should not throw for an unknown filter', () => {
|
||||||
|
expect(() => liquid.unregisterFilter('unknown')).not.toThrow()
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|||||||
Reference in New Issue
Block a user