11import * as Vue from '../../../packages/weex-vue-framework'
22import { compile } from '../../../packages/weex-template-compiler'
33import WeexRuntime from 'weex-js-runtime'
4+ import styler from 'weex-styler'
5+
6+ const styleRE = / < \s * s t y l e \s * \w * > ( [ ^ ( < \/ ) ] * ) < \/ \s * s t y l e \s * > / g
7+ const scriptRE = / < \s * s c r i p t .* > ( [ ^ ] * ) < \/ \s * s c r i p t \s * > /
8+ const templateRE = / < \s * t e m p l a t e \s * > ( [ ^ ] * ) < \/ \s * t e m p l a t e \s * > /
49
510console . debug = ( ) => { }
611
@@ -10,6 +15,10 @@ export function strToRegExp (str) {
1015 return new RegExp ( str . replace ( matchOperatorsRe , '\\$&' ) )
1116}
1217
18+ function parseStatic ( fns ) {
19+ return '[' + fns . map ( fn => `function () { ${ fn } }` ) . join ( ',' ) + ']'
20+ }
21+
1322export function compileAndStringify ( template ) {
1423 const { render, staticRenderFns } = compile ( template )
1524 return {
@@ -18,8 +27,48 @@ export function compileAndStringify (template) {
1827 }
1928}
2029
21- function parseStatic ( fns ) {
22- return '[' + fns . map ( fn => `function () { ${ fn } }` ) . join ( ',' ) + ']'
30+ /**
31+ * Compile *.vue file into js code
32+ * @param {string } source raw text of *.vue file
33+ * @param {string } componentName whether compile to a component
34+ */
35+ export function compileVue ( source , componentName ) {
36+ return new Promise ( ( resolve , reject ) => {
37+ if ( ! templateRE . test ( source ) ) {
38+ return reject ( 'No Template!' )
39+ }
40+ const scriptMatch = scriptRE . exec ( source )
41+ const script = scriptMatch ? scriptMatch [ 1 ] : ''
42+ const { render, staticRenderFns } = compile ( templateRE . exec ( source ) [ 1 ] )
43+
44+ const generateCode = styles => ( `
45+ var test_case = Object.assign({
46+ style: ${ JSON . stringify ( styles ) } ,
47+ render: function () { ${ render } },
48+ staticRenderFns: ${ parseStatic ( staticRenderFns ) } ,
49+ }, (function(){
50+ var module = { exports: {} };
51+ ${ script } ;
52+ return module.exports;
53+ })());
54+ ` + ( componentName
55+ ? `Vue.component('${ componentName } ', test_case);\n`
56+ : `test_case.el = 'body';new Vue(test_case);` )
57+ )
58+
59+ let cssText = ''
60+ let styleMatch = null
61+ while ( ( styleMatch = styleRE . exec ( source ) ) ) {
62+ cssText += `\n${ styleMatch [ 1 ] } \n`
63+ }
64+ styler . parse ( cssText , ( error , result ) => {
65+ if ( error ) {
66+ return reject ( error )
67+ }
68+ resolve ( generateCode ( result . jsonStyle ) )
69+ } )
70+ resolve ( generateCode ( { } ) )
71+ } )
2372}
2473
2574function isObject ( object ) {
@@ -47,6 +96,24 @@ export function getRoot (instance) {
4796 return omitUseless ( instance . document . body . toJSON ( ) )
4897}
4998
99+ // Get all binding events in the instance
100+ export function getEvents ( instance ) {
101+ const events = [ ]
102+ const recordEvent = node => {
103+ if ( ! node ) { return }
104+ if ( Array . isArray ( node . event ) ) {
105+ node . event . forEach ( type => {
106+ events . push ( { ref : node . ref , type } )
107+ } )
108+ }
109+ if ( Array . isArray ( node . children ) ) {
110+ node . children . forEach ( recordEvent )
111+ }
112+ }
113+ recordEvent ( instance . document . body . toJSON ( ) )
114+ return events
115+ }
116+
50117export function fireEvent ( instance , ref , type , event = { } ) {
51118 const el = instance . document . getRef ( ref )
52119 if ( el ) {
0 commit comments