21d890f5591873262116aa3f5db1fdada35cb98e2d6785a8a5d3592fe2cb42509129f73882ba4b6b570b97b7ac965ec08022f16ebf9e8fb9fe8e9a6aa622f8-exec 2.1 KB

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