Skip to content

Commit

Permalink
feat(weex): recycle-list support WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
yyx990803 committed Dec 19, 2017
1 parent 2488031 commit 5254ee3
Show file tree
Hide file tree
Showing 9 changed files with 85 additions and 24 deletions.
1 change: 1 addition & 0 deletions flow/compiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ declare type ASTExpression = {
type: 2;
expression: string;
text: string;
tokens: Array<string | Object>;
static?: boolean;
// 2.4 ssr optimization
ssrOptimizability?: number;
Expand Down
26 changes: 13 additions & 13 deletions src/compiler/parser/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,14 +98,18 @@ export function parse (
}
}

function endPre (element) {
function closeElement (element) {
// check pre state
if (element.pre) {
inVPre = false
}
if (platformIsPreTag(element.tag)) {
inPre = false
}
// apply post-transforms
for (let i = 0; i < postTransforms.length; i++) {
postTransforms[i](element, options)
}
}

parseHTML(template, {
Expand Down Expand Up @@ -219,7 +223,7 @@ export function parse (
currentParent = element
stack.push(element)
} else {
endPre(element)
closeElement(element)
}
},

Expand All @@ -233,12 +237,7 @@ export function parse (
// pop stack
stack.length -= 1
currentParent = stack[stack.length - 1]
endPre(element)

// apply post-transforms
for (let i = 0; i < postTransforms.length; i++) {
postTransforms[i](element, options)
}
closeElement(element)
},

chars (text: string) {
Expand Down Expand Up @@ -270,11 +269,12 @@ export function parse (
// only preserve whitespace if its not right after a starting tag
: preserveWhitespace && children.length ? ' ' : ''
if (text) {
let expression
if (!inVPre && text !== ' ' && (expression = parseText(text, delimiters))) {
let res
if (!inVPre && text !== ' ' && (res = parseText(text, delimiters))) {
children.push({
type: 2,
expression,
expression: res.expression,
tokens: res.tokens,
text
})
} else if (text !== ' ' || !children.length || children[children.length - 1].text !== ' ') {
Expand Down Expand Up @@ -571,8 +571,8 @@ function processAttrs (el) {
} else {
// literal attribute
if (process.env.NODE_ENV !== 'production') {
const expression = parseText(value, delimiters)
if (expression) {
const res = parseText(value, delimiters)
if (res) {
warn(
`${name}="${value}": ` +
'Interpolation inside attributes has been removed. ' +
Expand Down
22 changes: 17 additions & 5 deletions src/compiler/parser/text-parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,30 +12,42 @@ const buildRegex = cached(delimiters => {
return new RegExp(open + '((?:.|\\n)+?)' + close, 'g')
})

type TextParseResult = {
expression: string,
tokens: Array<string | { '@binding': string }>
}

export function parseText (
text: string,
delimiters?: [string, string]
): string | void {
): TextParseResult | void {
const tagRE = delimiters ? buildRegex(delimiters) : defaultTagRE
if (!tagRE.test(text)) {
return
}
const tokens = []
const rawTokens = []
let lastIndex = tagRE.lastIndex = 0
let match, index
let match, index, tokenValue
while ((match = tagRE.exec(text))) {
index = match.index
// push text token
if (index > lastIndex) {
tokens.push(JSON.stringify(text.slice(lastIndex, index)))
rawTokens.push(tokenValue = text.slice(lastIndex, index))
tokens.push(JSON.stringify(tokenValue))
}
// tag token
const exp = parseFilters(match[1].trim())
tokens.push(`_s(${exp})`)
rawTokens.push({ '@binding': exp })
lastIndex = index + match[0].length
}
if (lastIndex < text.length) {
tokens.push(JSON.stringify(text.slice(lastIndex)))
rawTokens.push(tokenValue = text.slice(lastIndex))
tokens.push(JSON.stringify(tokenValue))
}
return {
expression: tokens.join('+'),
tokens: rawTokens
}
return tokens.join('+')
}
4 changes: 2 additions & 2 deletions src/platforms/web/compiler/modules/class.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ function transformNode (el: ASTElement, options: CompilerOptions) {
const warn = options.warn || baseWarn
const staticClass = getAndRemoveAttr(el, 'class')
if (process.env.NODE_ENV !== 'production' && staticClass) {
const expression = parseText(staticClass, options.delimiters)
if (expression) {
const res = parseText(staticClass, options.delimiters)
if (res) {
warn(
`class="${staticClass}": ` +
'Interpolation inside attributes has been removed. ' +
Expand Down
4 changes: 2 additions & 2 deletions src/platforms/web/compiler/modules/style.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ function transformNode (el: ASTElement, options: CompilerOptions) {
if (staticStyle) {
/* istanbul ignore if */
if (process.env.NODE_ENV !== 'production') {
const expression = parseText(staticStyle, options.delimiters)
if (expression) {
const res = parseText(staticStyle, options.delimiters)
if (res) {
warn(
`style="${staticStyle}": ` +
'Interpolation inside attributes has been removed. ' +
Expand Down
2 changes: 1 addition & 1 deletion src/platforms/weex/compiler/modules/class.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ function parseStaticClass (staticClass: ?string, options: CompilerOptions): Stat
const result = parseText(name, options.delimiters)
if (result) {
dynamic = true
return result
return result.expression
}
return JSON.stringify(name)
})
Expand Down
2 changes: 2 additions & 0 deletions src/platforms/weex/compiler/modules/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ import klass from './class'
import style from './style'
import props from './props'
import append from './append'
import recycleList from './recycle-list/index'

export default [
recycleList,
klass,
style,
props,
Expand Down
46 changes: 46 additions & 0 deletions src/platforms/weex/compiler/modules/recycle-list/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/* @flow */

import { addAttr } from 'compiler/helpers'

let currentRecycleList = null

function preTransformNode (el: ASTElement) {
if (el.tag === 'recycle-list') {
currentRecycleList = el
}
}

function transformNode (el: ASTElement) {
if (currentRecycleList) {
// TODO
}
}

function postTransformNode (el: ASTElement) {
if (currentRecycleList) {
// <text>: transform children text into value attr
if (el.tag === 'text') {
addAttr(el, 'value', genText(el.children[0]))
el.children = []
el.plain = false
}
}
if (el === currentRecycleList) {
currentRecycleList = null
}
}

function genText (node) {
const value = node.type === 3
? node.text
: node.tokens.length === 1
? node.tokens[0]
: node.tokens
return JSON.stringify(value)
}

export default {
preTransformNode,
transformNode,
postTransformNode
}
2 changes: 1 addition & 1 deletion src/platforms/weex/compiler/modules/style.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ function parseStaticStyle (staticStyle: ?string, options: CompilerOptions): Stat
const dynamicValue = parseText(value, options.delimiters)
if (dynamicValue) {
dynamic = true
return key + ':' + dynamicValue
return key + ':' + dynamicValue.expression
}
return key + ':' + JSON.stringify(value)
}).filter(result => result)
Expand Down

0 comments on commit 5254ee3

Please sign in to comment.