UNPKG

2.15 kBJavaScriptView Raw
1/* @flow */
2declare var document: WeexDocument;
3
4import TextNode from 'weex/runtime/text-node'
5
6export const namespaceMap = {}
7
8export function createElement (tagName: string): WeexElement {
9 return document.createElement(tagName)
10}
11
12export function createElementNS (namespace: string, tagName: string): WeexElement {
13 return document.createElement(namespace + ':' + tagName)
14}
15
16export function createTextNode (text: string) {
17 return new TextNode(text)
18}
19
20export function createComment (text: string) {
21 return document.createComment(text)
22}
23
24export function insertBefore (
25 node: WeexElement,
26 target: WeexElement,
27 before: WeexElement
28) {
29 if (target.nodeType === 3) {
30 if (node.type === 'text') {
31 node.setAttr('value', target.text)
32 target.parentNode = node
33 } else {
34 const text = createElement('text')
35 text.setAttr('value', target.text)
36 node.insertBefore(text, before)
37 }
38 return
39 }
40 node.insertBefore(target, before)
41}
42
43export function removeChild (node: WeexElement, child: WeexElement) {
44 if (child.nodeType === 3) {
45 node.setAttr('value', '')
46 return
47 }
48 node.removeChild(child)
49}
50
51export function appendChild (node: WeexElement, child: WeexElement) {
52 if (child.nodeType === 3) {
53 if (node.type === 'text') {
54 node.setAttr('value', child.text)
55 child.parentNode = node
56 } else {
57 const text = createElement('text')
58 text.setAttr('value', child.text)
59 node.appendChild(text)
60 }
61 return
62 }
63
64 node.appendChild(child)
65}
66
67export function parentNode (node: WeexElement): WeexElement | void {
68 return node.parentNode
69}
70
71export function nextSibling (node: WeexElement): WeexElement | void {
72 return node.nextSibling
73}
74
75export function tagName (node: WeexElement): string {
76 return node.type
77}
78
79export function setTextContent (node: WeexElement, text: string) {
80 if (node.parentNode) {
81 node.parentNode.setAttr('value', text)
82 }
83}
84
85export function setAttribute (node: WeexElement, key: string, val: any) {
86 node.setAttr(key, val)
87}
88
89export function setStyleScope (node: WeexElement, scopeId: string) {
90 node.setAttr('@styleScope', scopeId)
91}