Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
haozi committed Mar 13, 2017
1 parent 1e504cc commit 5c48306
Show file tree
Hide file tree
Showing 13 changed files with 218 additions and 182 deletions.
3 changes: 2 additions & 1 deletion .eslintignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ build/*.js
config/*.js
packages/**/build
node_modules
src/vendor
src/vendor
src/data/exam/index.js
49 changes: 49 additions & 0 deletions .script/buildBecode.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
'use strict'
const PATH = require('path')
const FS = require('fs')

class C {
constructor () {
this.root = PATH.resolve(__dirname, '../')
this.src = `${this.root}/src/data/exam`
this.dist = `${this.root}/src/data/exam/index.js`
this.data = {}
}

run () {
let d = this.ls(this.src).map(item => ({
n: PATH.basename(item, '.js'),
path: item
}))
.filter(item => item.n !== 'index')
.sort((a, b) => a.n > b.n)

d.forEach(item => {
const beCode = this.read(item.path).trim()
beCode && (this.data[item.n] = {
beCode
})
})

this.write(this.dist, `export default ${JSON.stringify(this.data, null, 2)}`)
}

ls (path) {
path = PATH.resolve(path)
const d = FS.readdirSync(path)
return d.map(p => {
return PATH.resolve(path, p)
})
}

read (path) {
return FS.readFileSync(path, 'utf8')
}

write (path, string) {
FS.writeFileSync(path, string.trim() + '\n', 'utf8')
}

}

new C().run()
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"scripts": {
"start": "npm run dev",
"dev": "sudo PORT=80 node .config/dev-server.js",
"build": "npm run clean && npm run lint && node .config/build.js",
"build": "npm run clean && npm run lint && node .script/buildBecode.js && node .config/build.js",
"unit": "karma start test/unit/karma.conf.js --single-run",
"e2e": "node test/e2e/runner.js",
"lint": "eslint --ext .js,.vue src .config .script test/*_spec.js",
Expand Down
120 changes: 1 addition & 119 deletions src/App.vue
Original file line number Diff line number Diff line change
@@ -1,123 +1,5 @@
<style lang="less">
// 这是网站的框架结构入口,不要在这里写过多业务有关视图代码
// 业务逻辑应该写在具体的 component 里
@import url('./style/main.less');
</style>

<template>
<div id="app" class="i-main">
<div class="i-fe">
<div class="i-browser">
<div class="hd">
<div class="url">
<span>https://xss.test/</span><input type="text">
</div>
</div>
<div class="bd">
<iframe ref="sandbox"></iframe>
</div>
</div>

<div class="i-code">
<h3>input code</h3>
<code-mirror :code="initFeCode" :autofocus="true" @change="updateFeCode" :height="170"></code-mirror>

<h3>html</h3>
<code-mirror :code="raw" :read-only="true" :line-numbers="false" :height="100" mode="application/x-ejs"></code-mirror>
</div>
</div>

<div class="i-be">
<div class="i-code">
<h3>server code</h3>
<code-mirror :code="initBeCode" @change="updateBeCode"></code-mirror>
</div>
</div>

<router-view></router-view>
</div>
</template>

<script>
import codeMirror from './components/codemirror'
import sandboxText from './data/sandbox.raw'
import exam from './data/exam'
const escapeJS = (jsStr) => {
return String(jsStr).trim()
.replace(/'/g, '\\\'')
.replace(/"/g, '\\"')
.replace(/`/g, '\\`')
.replace(/\//g, '\\/')
}
const compile = (tpl, data = {}) => {
return tpl.replace(/{{{(.*?)}}}/g, ($0, $1) => data[$1] || '')
}
export default {
name: 'Home',
data () {
return {
initBeCode: '',
initFeCode: '',
beCode: '',
feCode: ''
}
},
mounted () {
this.initBeCode = this.examData.beCode
this.inject(this.feCode, this.beCode)
},
components: {
codeMirror
},
methods: {
inject (feCode = '', beCode = '') {
const blob = new Blob([
compile(sandboxText, {
FE_CODE: escapeJS(feCode),
BE_CODE: beCode.trim()
})
], { type: 'text/html' })
const blobUrl = URL.createObjectURL(blob)
const sandbox = this.$refs.sandbox.contentWindow
sandbox.location.replace(blobUrl)
},
updateBeCode (newVal) {
this.beCode = newVal
},
updateFeCode (newVal) {
this.feCode = newVal
}
},
computed: {
raw() {
let raw = ''
try {
raw = new Function(`return (${this.beCode.trim()})(\`${escapeJS(this.feCode)}\`)`)()
} catch (e) {}
return raw
},
examData() {
return exam[0]
}
},
watch: {
beCode(beCode) {
this.inject(this.feCode, this.beCode)
},
feCode() {
this.inject(this.feCode, this.beCode)
}
}
}
</script>
</template>
124 changes: 124 additions & 0 deletions src/Home.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
<style lang="less">
// 这是网站的框架结构入口,不要在这里写过多业务有关视图代码
// 业务逻辑应该写在具体的 component 里
@import url('./style/main.less');
</style>

<template>
<div id="app" class="i-main">
<div class="i-fe">
<div class="i-browser">
<div class="hd">
<div class="url">
<span>https://xss.test/</span><input type="text">
</div>
</div>
<div class="bd">
<iframe ref="sandbox"></iframe>
</div>
</div>

<div class="i-code">
<h3>input code</h3>
<code-mirror :code="initFeCode" :autofocus="true" @change="updateFeCode" :height="170"></code-mirror>

<h3>html</h3>
<code-mirror :code="raw" :read-only="true" :line-numbers="false" :height="100" mode="application/x-ejs"></code-mirror>
</div>
</div>

<div class="i-be">
<div class="i-code">
<h3>server code</h3>
<code-mirror :code="initBeCode" @change="updateBeCode"></code-mirror>
</div>
</div>

<router-view></router-view>
</div>
</template>

<script>
import codeMirror from './components/codemirror'
import sandboxText from './data/sandbox.raw'
import exam from './data/exam/index.js'
const escapeJS = (jsStr) => {
return String(jsStr).trim()
.replace(/'/g, '\\\'')
.replace(/"/g, '\\"')
.replace(/`/g, '\\`')
.replace(/\//g, '\\/')
}
const compile = (tpl, data = {}) => {
return tpl.replace(/{{{(.*?)}}}/g, ($0, $1) => data[$1] || '')
}
export default {
name: 'Home',
data () {
return {
initBeCode: '',
initFeCode: '',
beCode: '',
feCode: '',
raw: ''
}
},
mounted () {
let data
data = exam[this.$route.params.id]
if (!data) {
this.$router.push('/404')
return
}
this.initBeCode = data.beCode
},
components: {
codeMirror
},
methods: {
inject (feCode = '', beCode = '') {
const blob = new Blob([
compile(sandboxText, {
BODY: this.serverRender(feCode, beCode)
})
], { type: 'text/html' })
const blobUrl = URL.createObjectURL(blob)
const sandbox = this.$refs.sandbox.contentWindow
sandbox.location.replace(blobUrl)
},
serverRender (feCode, beCode) {
let tpl = '<!-- SERVER_ERROR -->'
try {
tpl = new Function(`return (${beCode.trim()})(\`${escapeJS(feCode)}\`)`)()
} catch (e) {}
this.raw = tpl
console.info(tpl)
return tpl
},
updateBeCode (newVal) {
this.beCode = newVal
},
updateFeCode (newVal) {
this.feCode = newVal
}
},
watch: {
beCode(beCode) {
this.inject(this.feCode, this.beCode)
},
feCode() {
this.inject(this.feCode, this.beCode)
}
}
}
</script>
Empty file removed src/components/Home.vue
Empty file.
2 changes: 1 addition & 1 deletion src/components/codemirror.vue
Original file line number Diff line number Diff line change
Expand Up @@ -74,4 +74,4 @@
}
}
}
</script>
</script>
37 changes: 0 additions & 37 deletions src/data/exam.js

This file was deleted.

3 changes: 3 additions & 0 deletions src/data/exam/0.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
function render (input) {
return '<input type="name" value="' + input + '">'
}
6 changes: 6 additions & 0 deletions src/data/exam/1.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
function render (input) {
const stripTagsRE = /<\/?[^>]+>/gi

input = input.replace(stripTagsRE, '')
return `<article>${input}</article>`
}
8 changes: 8 additions & 0 deletions src/data/exam/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export default {
"0": {
"beCode": "function render (input) {\n return '<input type=\"name\" value=\"' + input + '\">'\n}"
},
"1": {
"beCode": "function render (input) {\n const stripTagsRE = /<\\/?[^>]+>/gi\n\n input = input.replace(stripTagsRE, '')\n return `<article>${input}</article>`\n}"
}
}
Loading

0 comments on commit 5c48306

Please sign in to comment.