From e82127a524d68cd198cfa9aedc59fc6ef2f43541 Mon Sep 17 00:00:00 2001 From: Hanks Date: Fri, 7 Jul 2017 20:01:43 +0800 Subject: [PATCH 1/2] feat(weex): add "weex.supports" api for feature detection --- src/platforms/weex/entry-framework.js | 42 +++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/src/platforms/weex/entry-framework.js b/src/platforms/weex/entry-framework.js index cc5181d342f..87a4dc95eb6 100644 --- a/src/platforms/weex/entry-framework.js +++ b/src/platforms/weex/entry-framework.js @@ -79,6 +79,7 @@ export function createInstance ( const weexInstanceVar = { config, document, + supports, requireModule: moduleGetter } Object.freeze(weexInstanceVar) @@ -216,6 +217,18 @@ export function registerModules (newModules) { } } +/** + * Check whether the module or the method has been registered. + * @param {String} module name + * @param {String} method name (optional) + */ +function isRegisteredModule (name, method) { + if (typeof method === 'string') { + return !!(modules[name] && modules[name][method]) + } + return !!modules[name] +} + /** * Register native components information. * @param {array} newComponents @@ -235,6 +248,35 @@ export function registerComponents (newComponents) { } } +/** + * Check whether the component has been registered. + * @param {String} component name + */ +function isRegisteredComponent (name) { + return !!components[name] +} + +/** + * Detects whether Weex supports specific features. + * @param {String} condition + */ +function supports (condition) { + if (typeof condition !== 'string') return null + + const res = condition.match(/^@(\w+)\/(\w+)(\.(\w+))?$/i) + if (res) { + const type = res[1] + const name = res[2] + const method = res[4] + switch (type) { + case 'module': return isRegisteredModule(name, method) + case 'component': return isRegisteredComponent(name) + } + } + + return null +} + /** * Create a fresh instance of Vue for each Weex instance. */ From 70c035613c539cd446c5c0d76e9917e6c7606b0d Mon Sep 17 00:00:00 2001 From: Hanks Date: Fri, 7 Jul 2017 20:33:29 +0800 Subject: [PATCH 2/2] test(weex): add test case for weex.supports and related methods --- src/platforms/weex/entry-framework.js | 6 +-- test/weex/runtime/framework.spec.js | 60 +++++++++++++++++++++++++++ 2 files changed, 63 insertions(+), 3 deletions(-) diff --git a/src/platforms/weex/entry-framework.js b/src/platforms/weex/entry-framework.js index 87a4dc95eb6..4314a81c6e4 100644 --- a/src/platforms/weex/entry-framework.js +++ b/src/platforms/weex/entry-framework.js @@ -222,7 +222,7 @@ export function registerModules (newModules) { * @param {String} module name * @param {String} method name (optional) */ -function isRegisteredModule (name, method) { +export function isRegisteredModule (name, method) { if (typeof method === 'string') { return !!(modules[name] && modules[name][method]) } @@ -252,7 +252,7 @@ export function registerComponents (newComponents) { * Check whether the component has been registered. * @param {String} component name */ -function isRegisteredComponent (name) { +export function isRegisteredComponent (name) { return !!components[name] } @@ -260,7 +260,7 @@ function isRegisteredComponent (name) { * Detects whether Weex supports specific features. * @param {String} condition */ -function supports (condition) { +export function supports (condition) { if (typeof condition !== 'string') return null const res = condition.match(/^@(\w+)\/(\w+)(\.(\w+))?$/i) diff --git a/test/weex/runtime/framework.spec.js b/test/weex/runtime/framework.spec.js index 8b824266445..f280c345f19 100644 --- a/test/weex/runtime/framework.spec.js +++ b/test/weex/runtime/framework.spec.js @@ -409,6 +409,30 @@ describe('framework APIs', () => { ).toEqual(['b(1)']) }) + it('isRegisteredModule', () => { + framework.registerModules({ + foo: ['a', 'b'], + bar: [ + { name: 'x', args: ['string'] }, + { name: 'y', args: ['number'] } + ] + }) + expect(framework.isRegisteredModule('foo')).toBe(true) + expect(framework.isRegisteredModule('bar')).toBe(true) + expect(framework.isRegisteredModule('foo', 'a')).toBe(true) + expect(framework.isRegisteredModule('foo', 'b')).toBe(true) + expect(framework.isRegisteredModule('bar', 'x')).toBe(true) + expect(framework.isRegisteredModule('bar', 'y')).toBe(true) + expect(framework.isRegisteredModule('FOO')).toBe(false) + expect(framework.isRegisteredModule(' bar ')).toBe(false) + expect(framework.isRegisteredModule('unknown')).toBe(false) + expect(framework.isRegisteredModule('#}{)=}')).toBe(false) + expect(framework.isRegisteredModule('foo', '')).toBe(false) + expect(framework.isRegisteredModule('foo', 'c')).toBe(false) + expect(framework.isRegisteredModule('bar', 'z')).toBe(false) + expect(framework.isRegisteredModule('unknown', 'unknown')).toBe(false) + }) + it('registerComponents', () => { framework.registerComponents(['foo', { type: 'bar' }, 'text']) const instance = new Instance(runtime) @@ -431,6 +455,42 @@ describe('framework APIs', () => { }) }) + it('isRegisteredComponent', () => { + framework.registerComponents(['foo', { type: 'bar' }, 'text']) + expect(framework.isRegisteredComponent('foo')).toBe(true) + expect(framework.isRegisteredComponent('bar')).toBe(true) + expect(framework.isRegisteredComponent('text')).toBe(true) + expect(framework.isRegisteredComponent('FOO')).toBe(false) + expect(framework.isRegisteredComponent(' bar ')).toBe(false) + expect(framework.isRegisteredComponent('')).toBe(false) + expect(framework.isRegisteredComponent('#}{)=}')).toBe(false) + }) + + it('weex.supports', () => { + framework.registerComponents(['apple', { type: 'banana' }]) + framework.registerModules({ + cat: ['eat', 'sleep'], + dog: [ + { name: 'bark', args: ['string'] } + ] + }) + expect(framework.supports('@component/apple')).toBe(true) + expect(framework.supports('@component/banana')).toBe(true) + expect(framework.supports('@module/cat')).toBe(true) + expect(framework.supports('@module/cat.eat')).toBe(true) + expect(framework.supports('@module/cat.sleep')).toBe(true) + expect(framework.supports('@module/dog.bark')).toBe(true) + expect(framework.supports('@component/candy')).toBe(false) + expect(framework.supports('@module/bird')).toBe(false) + expect(framework.supports('@module/bird.sing')).toBe(false) + expect(framework.supports('@module/dog.sleep')).toBe(false) + expect(framework.supports('apple')).toBe(null) + expect(framework.supports('')).toBe(null) + expect(framework.supports('cat')).toBe(null) + expect(framework.supports('@dog')).toBe(null) + expect(framework.supports('@component/dog#bark')).toBe(null) + }) + it('vm.$getConfig', () => { const instance = new Instance(runtime) instance.$create(`