All files domBuilder.js

97.69% Statements 169/173
100% Branches 2/2
100% Functions 0/0
97.69% Lines 169/173

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 1731x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x         1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x
import { watchFunction } from './remodel.js'
 
function _constructValue (parts, node) {
  if (parts == null) return null
  if (typeof parts === 'function') {
    parts = parts(node)
  }
  if (Array.isArray(parts)) {
    const mappedParts = parts.map(part => {
      if (typeof part === 'function') {
        part = part(node)
      }
      if (part == null) return ''
      if (part && part.type === 'part') {
        return part.value
      } else {
        return part
      }
    })
    if (mappedParts.length === 1) {
      return mappedParts[0]
    }
    return mappedParts.join('')
  }
  return parts
}
 
function _renderAttributes (attributes, node) {
  let obj = {}
  attributes.forEach(attribute => {
    if (typeof attribute === 'function') {
      attribute = attribute(node)
    }
    if (attribute == null) return
    if (attribute && attribute.type === 'attribute') {
      const name = attribute.name
      if (Object.prototype.hasOwnProperty.call(obj, name)) return
      const value = _constructValue(attribute.value, node)
      if (value == null) {
        obj[name] = name
      } else {
        obj[name] = value
      }
    } else if (Array.isArray(attribute)) {
      obj = Object.assign(_renderAttributes(attribute, node), obj)
    } else if (typeof attribute === 'object') {
      Object.entries(attribute).forEach(([name, value]) => {
        if (Object.prototype.hasOwnProperty.call(obj, name)) return
        obj[name] = value
      })
    } else {
      const name = attribute.toString()
      if (Object.prototype.hasOwnProperty.call(obj, name)) return
      obj[name] = name
    }
  })
  return obj
}
 
function _setAttribute (element, name, value) {
  if (element[name] !== value) {
    try {
      element[name] = value
    } catch (e) {
      // SVGs don't like getting their properties set and that's okay...
    }
  }
  if (!(typeof value).match(/(?:boolean|number|string)/)) {
    value = name
  }
  const str = value.toString()
  if (element.getAttribute(name) !== str) {
    element.setAttribute(name, str)
  }
  return element
}
 
function _setAttributes (element, attributes) {
  Object.entries(attributes).forEach(([name, value]) => {
    _setAttribute(element, name, value)
  })
  return element
}
 
function _pruneAttributes (element, newAttributes, oldAttributes) {
  const orphans = new Set(Object.keys(oldAttributes))
  Object.keys(newAttributes).forEach(attribute => orphans.delete(attribute))
  orphans.forEach(attribute => {
    element.removeAttribute(attribute)
    delete element[attribute]
  })
}
 
const _descriptionMap = new Map()
function _descriptionsToNodes (descriptions, xmlns) {
  if (!Array.isArray(descriptions)) {
    descriptions = [descriptions]
  }
  const nodes = []
  descriptions.forEach(description => {
    if (typeof description === 'function') {
      description = description()
    }
    if (description != null) {
      if (Array.isArray(description)) {
        nodes.push(..._descriptionsToNodes(description, xmlns))
      } else {
        if (description.tag === null || description.tag === '') {
          nodes.push(..._descriptionsToNodes(description.children, xmlns))
        } else if (typeof description.tag === 'function') {
          const attributes = _renderAttributes(description.attributes)
          nodes.push(..._descriptionsToNodes(description.tag(attributes, description.children, description), attributes.xmlns || xmlns))
        } else if (description.type) {
          if (!_descriptionMap.has(description)) {
            let node
            if (description.type === 'textnode') {
              node = document.createTextNode(description.value)
            } else {
              let oldAttributes = {}
              let newAttributes = _renderAttributes(description.attributes, node)
              node = document.createElementNS(newAttributes.xmlns || xmlns, description.tag, { is: description.attributes.is })
              _setAttributes(node, newAttributes)
              render(node, description.children, newAttributes.xmlns || xmlns)
              watchFunction(() => {
                newAttributes = _renderAttributes(description.attributes, node)
                _setAttributes(node, newAttributes)
                _pruneAttributes(node, newAttributes, oldAttributes)
                oldAttributes = newAttributes
              })
            }
            _descriptionMap.set(description, node)
          }
          nodes.push(_descriptionMap.get(description))
        } else {
          console.log(description)
          nodes.push(document.createTextNode(description.toString()))
        }
      }
    }
  })
  return nodes
}
 
function _setChildren (element, descriptions, xmlns) {
  const newNodes = _descriptionsToNodes(descriptions, xmlns)
  newNodes.forEach((newNode, index) => {
    while (element.childNodes[index] !== newNode) {
      const oldNode = element.childNodes[index]
      if (!oldNode) {
        element.appendChild(newNode)
      } else if (newNodes.indexOf(oldNode) > index) {
        element.insertBefore(newNode, oldNode)
      } else {
        element.removeChild(oldNode)
      }
    }
  })
  while (element.childNodes.length > newNodes.length) {
    element.removeChild(element.lastChild)
  }
  return element
}
 
export function render (element, descriptions, xmlns = 'http://www.w3.org/1999/xhtml') {
  if (!descriptions) {
    return _descriptionsToNodes(element, xmlns)
  }
  function f () {
    return _setChildren(element, descriptions, xmlns)
  }
  watchFunction(f)
}