You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@weex.apache.org by da...@apache.org on 2017/08/17 03:59:19 UTC
[02/43] incubator-weex git commit: + [jsfm] add directive filter in
runtime
+ [jsfm] add directive filter in runtime
Project: http://git-wip-us.apache.org/repos/asf/incubator-weex/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-weex/commit/2ecf8565
Tree: http://git-wip-us.apache.org/repos/asf/incubator-weex/tree/2ecf8565
Diff: http://git-wip-us.apache.org/repos/asf/incubator-weex/diff/2ecf8565
Branch: refs/heads/0.16-dev
Commit: 2ecf8565f8da844caea3069f551aba6c5a79a54b
Parents: f40f596
Author: Hanks <zh...@gmail.com>
Authored: Thu Aug 3 11:28:42 2017 +0800
Committer: Hanks <zh...@gmail.com>
Committed: Thu Aug 3 11:28:42 2017 +0800
----------------------------------------------------------------------
html5/runtime/vdom/directive.js | 53 ++++++++++++++++++++++
html5/test/unit/vdom/directive.js | 82 ++++++++++++++++++++++++++++++++++
2 files changed, 135 insertions(+)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/incubator-weex/blob/2ecf8565/html5/runtime/vdom/directive.js
----------------------------------------------------------------------
diff --git a/html5/runtime/vdom/directive.js b/html5/runtime/vdom/directive.js
new file mode 100644
index 0000000..97371ca
--- /dev/null
+++ b/html5/runtime/vdom/directive.js
@@ -0,0 +1,53 @@
+function typof (v) {
+ const s = Object.prototype.toString.call(v)
+ return s.substring(8, s.length - 1)
+}
+
+// match the binding delimiter
+const delimiterRE = /\[\[((?:.|\n)+?)\]\]/g
+
+export function generateBinding (text) {
+ if (typof(text) === 'String') {
+ return { '@binding': text }
+ }
+ return text
+}
+
+export function parseString (string) {
+ const tokens = []
+ let lastIndex = delimiterRE.lastIndex = 0
+ let match, index
+ while ((match = delimiterRE.exec(string))) {
+ index = match.index
+ if (index > lastIndex) {
+ tokens.push(string.slice(lastIndex, index))
+ }
+ const binding = generateBinding(match[1].trim())
+ tokens.push(binding)
+ lastIndex = index + match[0].length
+ }
+ if (lastIndex < string.length) {
+ tokens.push(string.slice(lastIndex))
+ }
+ if (tokens.length === 1) {
+ return tokens[0]
+ }
+ return tokens
+}
+
+export function filterDirective (value) {
+ if (typof(value) === 'String' && delimiterRE.test(value)) {
+ return parseString(value)
+ }
+ if (typof(value) === 'Object') {
+ const realData = {}
+ for (const key in value) {
+ realData[key] = filterDirective(value[key])
+ }
+ return realData
+ }
+ if (typof(value) === 'Array') {
+ return value.map(filterDirective)
+ }
+ return value
+}
http://git-wip-us.apache.org/repos/asf/incubator-weex/blob/2ecf8565/html5/test/unit/vdom/directive.js
----------------------------------------------------------------------
diff --git a/html5/test/unit/vdom/directive.js b/html5/test/unit/vdom/directive.js
new file mode 100644
index 0000000..5d7d037
--- /dev/null
+++ b/html5/test/unit/vdom/directive.js
@@ -0,0 +1,82 @@
+import { expect } from 'chai'
+import { filterDirective } from '../../../runtime/vdom/directive'
+
+describe('filterDirective', () => {
+ it('other type', () => {
+ expect(filterDirective(0)).to.be.equal(0)
+ expect(filterDirective(53)).to.be.equal(53)
+ expect(filterDirective(null)).to.be.equal(null)
+ const reg = /\w+/i
+ expect(filterDirective(reg)).to.be.equal(reg)
+ })
+
+ it('normal string', () => {
+ expect(filterDirective('')).to.be.equal('')
+ expect(filterDirective('-1')).to.be.equal('-1')
+ expect(filterDirective('abc')).to.be.equal('abc')
+ expect(filterDirective(' a bc d')).to.be.equal(' a bc d')
+ expect(filterDirective(' a {{ bc }}')).to.be.equal(' a {{ bc }}')
+ })
+
+ it('binding', () => {
+ expect(filterDirective('[[abc]]')).to.deep.equal({ '@binding': 'abc' })
+ expect(filterDirective('[[ xyz ]]')).to.deep.equal({ '@binding': 'xyz' })
+ expect(filterDirective('[[ x y z ]]')).to.deep.equal({ '@binding': 'x y z' })
+ })
+
+ it('binding and normal string', () => {
+ expect(filterDirective('xyz[[abc]]ttt')).to.deep.equal(['xyz', { '@binding': 'abc' }, 'ttt'])
+ expect(filterDirective(' x [[ w ]] t ')).to.deep.equal([' x ', { '@binding': 'w' }, ' t '])
+ expect(filterDirective('[[ wn]][xx{uur}]')).to.deep.equal([{ '@binding': 'wn' }, '[xx{uur}]'])
+ })
+
+ it('multi-binding', () => {
+ expect(filterDirective('[[abc]][[wpc]]')).to.deep.equal([{ '@binding': 'abc' }, { '@binding': 'wpc' }])
+ expect(filterDirective('abcd[[ xyz ]]ef[[w]]gh')).to.deep.equal([
+ 'abcd',
+ { '@binding': 'xyz' },
+ 'ef',
+ { '@binding': 'w' },
+ 'gh'
+ ])
+ expect(filterDirective(' a [[ b ]] [[c]] d [[e]][[f]]g')).to.deep.equal([
+ ' a ',
+ { '@binding': 'b' },
+ ' ',
+ { '@binding': 'c' },
+ ' d ',
+ { '@binding': 'e' },
+ { '@binding': 'f' },
+ 'g'
+ ])
+ })
+
+ it('parse object', () => {
+ expect(filterDirective({ key: '[[x]]' })).to.deep.equal({ key: { '@binding': 'x' } })
+ expect(filterDirective({ a: '[[A]]', b: '[[B]]' })).to.deep.equal({
+ a: { '@binding': 'A' },
+ b: { '@binding': 'B' }
+ })
+ expect(filterDirective({
+ a: '[[A]]',
+ x: 'X',
+ y: {
+ b: ' - [[B]] - ',
+ z: {
+ c: ['[[C]] + [[C]]', 'cc'],
+ w: 24
+ }
+ }
+ })).to.deep.equal({
+ a: { '@binding': 'A' },
+ x: 'X',
+ y: {
+ b: [' - ', { '@binding': 'B' }, ' - '],
+ z: {
+ c: [[{ '@binding': 'C' }, ' + ', { '@binding': 'C' }], 'cc'],
+ w: 24
+ }
+ }
+ })
+ })
+})