Skip to content

Commit

Permalink
feat: extraAttr
Browse files Browse the repository at this point in the history
  • Loading branch information
oct16 committed Mar 15, 2020
1 parent dd29c5a commit 7f88887
Show file tree
Hide file tree
Showing 10 changed files with 3,497 additions and 25 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -102,5 +102,4 @@ typings/
# TernJS port file
.tern-port


dist
2 changes: 1 addition & 1 deletion dist/packages/player/src/container.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,5 @@ export declare class Container {
initSandbox(): void;
initTemplate(): void;
createContainer(): HTMLElement;
createStyle(): HTMLElement;
createStyle(s: string): HTMLElement;
}
2 changes: 1 addition & 1 deletion dist/replay.cjs.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/replay.cjs.js.map

Large diffs are not rendered by default.

3,462 changes: 3,461 additions & 1 deletion dist/replay.esm.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/replay.esm.js.map

Large diffs are not rendered by default.

17 changes: 13 additions & 4 deletions packages/player/src/container.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { VNode, convertVNode } from '@WebReplay/virtual-dom'
import HTML from './ui.html'
import STYLE from './ui.css'
import FIXED from './fixed.css'

export class Container {
container: HTMLElement
Expand All @@ -27,11 +28,19 @@ export class Container {
this.sandBox.style.width = this.width + 'px'
this.sandBox.style.height = this.height + 'px'
const sandBoxDoc = (this.sandBox.contentWindow as Window).document
sandBoxDoc.replaceChild(convertVNode(this.vNode, null)!, sandBoxDoc.documentElement)

const child = convertVNode(this.vNode, null)
if (child) {
const head = child.firstChild
if (head) {
head.appendChild(this.createStyle(FIXED))
}
sandBoxDoc.replaceChild(child, sandBoxDoc.documentElement)
}
}

initTemplate() {
document.head.appendChild(this.createStyle())
document.head.appendChild(this.createStyle(STYLE))
document.body.appendChild(this.createContainer())
}

Expand All @@ -45,9 +54,9 @@ export class Container {
return (this.container = element)
}

createStyle() {
createStyle(s: string) {
const parser = new DOMParser()
const style = parser.parseFromString(`<style>${STYLE}</style>`, 'text/html').head.firstChild as HTMLElement
const style = parser.parseFromString(`<style>${s}</style>`, 'text/html').head.firstChild as HTMLElement
return style
}
}
3 changes: 3 additions & 0 deletions packages/player/src/fixed.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
input[type="checkbox"] {
pointer-events: none;
}
1 change: 0 additions & 1 deletion packages/player/src/ui.css
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ body {

#wr-player {
position: relative;
pointer-events: none;
}

#wr-pointer {
Expand Down
30 changes: 16 additions & 14 deletions packages/virtual-dom/src/serialize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,32 +15,34 @@ const getVNodeByEl = (el: Element, isSVG?: boolean): VNode => {
const getAttr = (el: HTMLElement & { checked: boolean }) => {
const resAttr: { [key: string]: string } = {}
const attrs = el.attributes

if (el.checked) {
resAttr.checked = 'true'
}

if (attrs && attrs.length) {
return Object.values(attrs).reduce((ret: any, attr) => {
// const [name, value] = extraAttr(attr)
const { name, value } = attr
ret[name] = value
const [name, value] = extraAttr(attr)
if (name) {
ret[name] = value
}
return ret
}, resAttr)
}
return resAttr
}

// const extraAttr = (attr: Attr) => {
// let { name, value } = attr
// if (name === 'href' || name === 'src') {
// if (/^\/(?!\/)/.test(value)) {
// const host = `https://github.com`
// value = host + value
// }
// }
// return [name, value]
// }
const extraAttr = (attr: Attr) => {
let { name, value } = attr
if (name === 'href' || name === 'src') {
if (/^\/(?!\/)/.test(value)) {
const origin = location.origin
value = origin + value
} else if (value.startsWith('#/')) {
return []
}
}
return [name, value]
}

export const createElement = (el: Element, inheritSVG?: boolean): VNode | null => {
if (el.nodeType === Node.TEXT_NODE) {
Expand Down

0 comments on commit 7f88887

Please sign in to comment.