mirror of
https://github.com/harttle/liquidjs.git
synced 2026-09-15 04:10:40 -07:00
+14
-14
@@ -86,7 +86,7 @@ export class Context {
|
||||
public * _getFromScope (scope: unknown, paths: (PropertyKey | Drop)[] | string, strictVariables = this.strictVariables): IterableIterator<unknown> {
|
||||
if (isString(paths)) paths = paths.split('.')
|
||||
for (let i = 0; i < paths.length; i++) {
|
||||
scope = yield readProperty(scope as object, paths[i], this.ownPropertyOnly)
|
||||
scope = yield this.readProperty(scope as object, paths[i])
|
||||
if (strictVariables && isUndefined(scope)) {
|
||||
throw new InternalUndefinedVariableError((paths as string[]).slice(0, i + 1).join!('.'))
|
||||
}
|
||||
@@ -120,21 +120,21 @@ export class Context {
|
||||
if (key in this.environments) return this.environments
|
||||
return this.globals
|
||||
}
|
||||
readProperty (obj: Scope, key: (PropertyKey | Drop)) {
|
||||
obj = toLiquid(obj)
|
||||
key = toValue(key) as PropertyKey
|
||||
if (isNil(obj)) return obj
|
||||
if (isArray(obj) && (key as number) < 0) return obj[obj.length + +key]
|
||||
const value = readJSProperty(obj, key, this.ownPropertyOnly)
|
||||
if (value === undefined && obj instanceof Drop) return obj.liquidMethodMissing(key, this)
|
||||
if (isFunction(value)) return value.call(obj)
|
||||
if (key === 'size') return readSize(obj)
|
||||
else if (key === 'first') return readFirst(obj)
|
||||
else if (key === 'last') return readLast(obj)
|
||||
return value
|
||||
}
|
||||
}
|
||||
|
||||
export function readProperty (obj: Scope, key: (PropertyKey | Drop), ownPropertyOnly: boolean) {
|
||||
obj = toLiquid(obj)
|
||||
key = toValue(key) as PropertyKey
|
||||
if (isNil(obj)) return obj
|
||||
if (isArray(obj) && (key as number) < 0) return obj[obj.length + +key]
|
||||
const value = readJSProperty(obj, key, ownPropertyOnly)
|
||||
if (value === undefined && obj instanceof Drop) return obj.liquidMethodMissing(key)
|
||||
if (isFunction(value)) return value.call(obj)
|
||||
if (key === 'size') return readSize(obj)
|
||||
else if (key === 'first') return readFirst(obj)
|
||||
else if (key === 'last') return readLast(obj)
|
||||
return value
|
||||
}
|
||||
export function readJSProperty (obj: Scope, key: PropertyKey, ownPropertyOnly: boolean) {
|
||||
if (ownPropertyOnly && !hasOwnProperty.call(obj, key) && !(obj instanceof Drop)) return undefined
|
||||
return obj[key]
|
||||
|
||||
+3
-1
@@ -1,5 +1,7 @@
|
||||
import { Context } from '../context'
|
||||
|
||||
export abstract class Drop {
|
||||
public liquidMethodMissing (key: string | number): Promise<any> | any {
|
||||
public liquidMethodMissing (key: string | number, context: Context): Promise<any> | any {
|
||||
return undefined
|
||||
}
|
||||
}
|
||||
|
||||
+28
-16
@@ -1,25 +1,37 @@
|
||||
import { Drop, Liquid } from '../..'
|
||||
|
||||
class SettingsDrop extends Drop {
|
||||
public foo = 'FOO'
|
||||
public bar () {
|
||||
return 'BAR'
|
||||
}
|
||||
public liquidMethodMissing (key: string) {
|
||||
return key.toUpperCase()
|
||||
}
|
||||
}
|
||||
import { Context, Drop, Liquid } from '../..'
|
||||
|
||||
describe('drop', function () {
|
||||
const settings = new SettingsDrop()
|
||||
let engine: Liquid
|
||||
beforeEach(function () {
|
||||
engine = new Liquid()
|
||||
})
|
||||
it('should support liquidMethodMissing', async function () {
|
||||
const src = `{{settings.foo}},{{settings.bar}},{{settings.coo}}`
|
||||
const html = await engine.parseAndRender(src, { settings })
|
||||
return expect(html).toBe('FOO,BAR,COO')
|
||||
|
||||
describe('liquidMethodMissing', () => {
|
||||
it('should support liquidMethodMissing', async function () {
|
||||
class SettingsDrop extends Drop {
|
||||
public foo = 'FOO'
|
||||
public bar () {
|
||||
return 'BAR'
|
||||
}
|
||||
public liquidMethodMissing (key: string) {
|
||||
return key.toUpperCase()
|
||||
}
|
||||
}
|
||||
const src = `{{settings.foo}},{{settings.bar}},{{settings.coo}}`
|
||||
const html = await engine.parseAndRender(src, { settings: new SettingsDrop() })
|
||||
return expect(html).toBe('FOO,BAR,COO')
|
||||
})
|
||||
|
||||
it('should expose context', async function () {
|
||||
class SettingsDrop extends Drop {
|
||||
public liquidMethodMissing (key: string, context: Context) {
|
||||
return key + ':' + context.getSync([key])
|
||||
}
|
||||
}
|
||||
const src = `{{settings.foo}}`
|
||||
const html = await engine.parseAndRender(src, { settings: new SettingsDrop(), foo: 'FOO' })
|
||||
return expect(html).toBe('foo:FOO')
|
||||
})
|
||||
})
|
||||
|
||||
describe('BlandDrop', function () {
|
||||
|
||||
Reference in New Issue
Block a user