From c104cc582d647f5e10b90563cb80907b9e30ec12 Mon Sep 17 00:00:00 2001 From: Evan You Date: Wed, 6 Sep 2017 05:57:00 -0400 Subject: [PATCH] feat(weex): split text into separate module --- .../compiler/modules/recycle-list/index.js | 17 ++------------ .../compiler/modules/recycle-list/text.js | 22 +++++++++++++++++++ 2 files changed, 24 insertions(+), 15 deletions(-) create mode 100644 src/platforms/weex/compiler/modules/recycle-list/text.js diff --git a/src/platforms/weex/compiler/modules/recycle-list/index.js b/src/platforms/weex/compiler/modules/recycle-list/index.js index 1163e4da4a0..82cc6e8f605 100644 --- a/src/platforms/weex/compiler/modules/recycle-list/index.js +++ b/src/platforms/weex/compiler/modules/recycle-list/index.js @@ -1,6 +1,6 @@ /* @flow */ -import { addAttr } from 'compiler/helpers' +import { transformText } from './text' let currentRecycleList = null @@ -20,9 +20,7 @@ function postTransformNode (el: ASTElement) { if (currentRecycleList) { // : transform children text into value attr if (el.tag === 'text') { - addAttr(el, 'value', genText(el.children[0])) - el.children = [] - el.plain = false + transformText(el) } } if (el === currentRecycleList) { @@ -30,17 +28,6 @@ function postTransformNode (el: ASTElement) { } } -function genText (node) { - const value = node.type === 3 - ? node.text - : node.type === 2 - ? node.tokens.length === 1 - ? node.tokens[0] - : node.tokens - : '' - return JSON.stringify(value) -} - export default { preTransformNode, transformNode, diff --git a/src/platforms/weex/compiler/modules/recycle-list/text.js b/src/platforms/weex/compiler/modules/recycle-list/text.js new file mode 100644 index 00000000000..a950aff93a9 --- /dev/null +++ b/src/platforms/weex/compiler/modules/recycle-list/text.js @@ -0,0 +1,22 @@ +/* @flow */ + +import { addAttr } from 'compiler/helpers' + +function genText (node: ASTNode) { + const value = node.type === 3 + ? node.text + : node.type === 2 + ? node.tokens.length === 1 + ? node.tokens[0] + : node.tokens + : '' + return JSON.stringify(value) +} + +export function transformText (el: ASTElement) { + // weex can only contain text, so the parser + // always generates a single child. + addAttr(el, 'value', genText(el.children[0])) + el.children = [] + el.plain = false +}