Skip to content

Commit 2944515

Browse files
Hanks10100yyx990803
authored andcommitted
[weex] Support unary and left open tags (#5052)
* [weex] Support unary and left open tags * [weex] add test case for unary tag * [compiler] move canBeLeftOpenTag to compiler option
1 parent 2343b90 commit 2944515

File tree

8 files changed

+61
-6
lines changed

8 files changed

+61
-6
lines changed

Diff for: flow/compiler.js

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ declare type CompilerOptions = {
55
staticKeys?: string; // a list of AST properties to be considered static; for optimization
66
directives?: { [key: string]: Function }; // platform specific directives
77
isUnaryTag?: (tag: string) => ?boolean; // check if a tag is unary for the platform
8+
canBeLeftOpenTag?: (tag: string) => ?boolean; // check if a tag can be left opened
89
isReservedTag?: (tag: string) => ?boolean; // check if a tag is a native for the platform
910
mustUseProp?: (tag: string, type: ?string, name: string) => boolean; // check if an attribute should be bound as a property
1011
isPreTag?: (attr: string) => ?boolean; // check if a tag needs to preserve whitespace

Diff for: src/compiler/parser/html-parser.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
*/
1111

1212
import { makeMap, no } from 'shared/util'
13-
import { isNonPhrasingTag, canBeLeftOpenTag } from 'web/compiler/util'
13+
import { isNonPhrasingTag } from 'web/compiler/util'
1414

1515
// Regular Expressions for parsing tags and attributes
1616
const singleAttrIdentifier = /([^\s"'<>/=]+)/
@@ -68,6 +68,7 @@ export function parseHTML (html, options) {
6868
const stack = []
6969
const expectHTML = options.expectHTML
7070
const isUnaryTag = options.isUnaryTag || no
71+
const canBeLeftOpenTag = options.canBeLeftOpenTag || no
7172
let index = 0
7273
let last, lastTag
7374
while (html) {

Diff for: src/compiler/parser/index.js

+1
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ export function parse (
8484
warn,
8585
expectHTML: options.expectHTML,
8686
isUnaryTag: options.isUnaryTag,
87+
canBeLeftOpenTag: options.canBeLeftOpenTag,
8788
shouldDecodeNewlines: options.shouldDecodeNewlines,
8889
start (tag, attrs, unary) {
8990
// check namespace.

Diff for: src/entries/web-server-renderer.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ process.env.VUE_ENV = 'server'
44

55
import { createRenderer as _createRenderer } from 'server/create-renderer'
66
import { createBundleRendererCreator } from 'server/create-bundle-renderer'
7-
import { isUnaryTag } from 'web/compiler/util'
7+
import { isUnaryTag, canBeLeftOpenTag } from 'web/compiler/util'
88
import modules from 'web/server/modules/index'
99
import baseDirectives from 'web/server/directives/index'
1010

@@ -14,6 +14,7 @@ export function createRenderer (options?: Object = {}): {
1414
} {
1515
return _createRenderer({
1616
isUnaryTag,
17+
canBeLeftOpenTag,
1718
modules,
1819
// user can provide server-side implementations for custom directives
1920
// when creating the renderer.

Diff for: src/platforms/web/compiler/index.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/* @flow */
22

3-
import { isUnaryTag } from './util'
3+
import { isUnaryTag, canBeLeftOpenTag } from './util'
44
import { genStaticKeys } from 'shared/util'
55
import { createCompiler } from 'compiler/index'
66

@@ -21,6 +21,7 @@ export const baseOptions: CompilerOptions = {
2121
isPreTag,
2222
isUnaryTag,
2323
mustUseProp,
24+
canBeLeftOpenTag,
2425
isReservedTag,
2526
getTagNamespace,
2627
staticKeys: genStaticKeys(modules)

Diff for: src/platforms/weex/compiler/index.js

+2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
isUnaryTag,
1111
mustUseProp,
1212
isReservedTag,
13+
canBeLeftOpenTag,
1314
getTagNamespace
1415
} from '../util/index'
1516

@@ -18,6 +19,7 @@ export const baseOptions: CompilerOptions = {
1819
directives,
1920
isUnaryTag,
2021
mustUseProp,
22+
canBeLeftOpenTag,
2123
isReservedTag,
2224
getTagNamespace,
2325
preserveWhitespace: false,

Diff for: src/platforms/weex/util/index.js

+19-2
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,27 @@
33
import { makeMap } from 'shared/util'
44

55
export const isReservedTag = makeMap(
6-
'div,img,image,input,switch,indicator,list,scroller,cell,template,text,slider,image'
6+
'template,script,style,element,content,slot,link,meta,svg,view,' +
7+
'a,div,img,image,text,span,richtext,input,switch,textarea,spinner,select,' +
8+
'slider,slider-neighbor,indicator,trisition,trisition-group,canvas,' +
9+
'list,cell,header,loading,loading-indicator,refresh,scrollable,scroller,' +
10+
'video,web,embed,tabbar,tabheader,datepicker,timepicker,marquee,countdown',
11+
true
12+
)
13+
14+
// Elements that you can, intentionally, leave open (and which close themselves)
15+
// more flexable than web
16+
export const canBeLeftOpenTag = makeMap(
17+
'web,spinner,switch,video,textarea,canvas,' +
18+
'indicator,marquee,countdown',
19+
true
20+
)
21+
22+
export const isUnaryTag = makeMap(
23+
'embed,img,image,input,link,meta',
24+
true
725
)
826

9-
export function isUnaryTag () { /* console.log('isUnaryTag') */ }
1027
export function mustUseProp () { /* console.log('mustUseProp') */ }
1128
export function getTagNamespace () { /* console.log('getTagNamespace') */ }
1229
export function isUnknownElement () { /* console.log('isUnknownElement') */ }

Diff for: test/weex/compiler/compile.spec.js

+32-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { compile } from '../../../packages/weex-template-compiler'
2+
import { strToRegExp } from '../helpers/index'
23

34
describe('compile basic', () => {
45
it('should be compiled', () => {
@@ -29,6 +30,36 @@ describe('compile basic', () => {
2930
expect(errors).toEqual([])
3031
})
3132

33+
it('should compile unary tag', () => {
34+
const inputCase = compile(`<div><input><text>abc</text></div>`)
35+
expect(inputCase.render).toMatch(strToRegExp(`return _m(0)`))
36+
expect(inputCase.staticRenderFns).toMatch(strToRegExp(`_c('div',[_c('input'),_c('text',[_v("abc")])])`))
37+
expect(inputCase.errors).toEqual([])
38+
39+
const imageCase = compile(`<div><image src="path"><text>abc</text></div>`)
40+
expect(imageCase.render).toMatch(strToRegExp(`return _m(0)`))
41+
expect(imageCase.staticRenderFns).toMatch(strToRegExp(`_c('div',[_c('image',{attrs:{"src":"path"}}),_c('text',[_v("abc")])])`))
42+
expect(imageCase.errors).toEqual([])
43+
44+
const complexCase = compile(`
45+
<div>
46+
<image src="path">
47+
<image></image>
48+
<div>
49+
<embed>
50+
<text>start</text>
51+
<input type="text">
52+
<input type="url" />
53+
<text>end</text>
54+
</div>
55+
</div>
56+
`)
57+
expect(complexCase.render).toMatch(strToRegExp(`return _m(0)`))
58+
expect(complexCase.staticRenderFns).toMatch(strToRegExp(`_c('image',{attrs:{"src":"path"}}),_c('image'),_c('div'`))
59+
expect(complexCase.staticRenderFns).toMatch(strToRegExp(`_c('div',[_c('embed'),_c('text',[_v("start")]),_c('input',{attrs:{"type":"text"}}),_c('input',{attrs:{"type":"url"}}),_c('text',[_v("end")])]`))
60+
expect(complexCase.errors).toEqual([])
61+
})
62+
3263
it('should compile more complex situation', () => {
3364
// from examples of https://github.com/alibaba/weex
3465
const { render, staticRenderFns, errors } = compile(`
@@ -38,7 +69,7 @@ describe('compile basic', () => {
3869
<text style="margin-left:36px;color:#eee;">Load more...</text>
3970
</refresh>
4071
`)
41-
expect(render).toEqual(`with(this){return _c('refresh',{staticClass:["refresh"],staticStyle:{flexDirection:"row"},attrs:{"display":displayRefresh},on:{"refresh":handleRefresh}},[_c('loading-indicator'),_c('text',{staticStyle:{marginLeft:"36px",color:"#eee"}},[_v("Load more...")])],1)}`)
72+
expect(render).toEqual(`with(this){return _c('refresh',{staticClass:["refresh"],staticStyle:{flexDirection:"row"},attrs:{"display":displayRefresh},on:{"refresh":handleRefresh}},[_c('loading-indicator'),_c('text',{staticStyle:{marginLeft:"36px",color:"#eee"}},[_v("Load more...")])])}`)
4273
expect(staticRenderFns).toEqual([])
4374
expect(errors).toEqual([])
4475
})

0 commit comments

Comments
 (0)