all files / dom-utils/ index.js

100% Statements 143/143
100% Branches 56/56
100% Functions 0/0
100% Lines 130/130
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 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247                        23×     14×                       24× 24×   24×   24× 20×   10×                 26×   12×   11×     10×                                 16×                         17×   13×   12×   12×   10×   10×                                                                                        
'use strict'
 
const is = require( '@mojule/is' )
const nodeUtils = require( '@mojule/node-utils' )
 
const { walk, removeAll } = nodeUtils
 
const select = ( node, selector ) => {
  if( node.nodeType === 1 && node.matches( selector ) )
    return node
 
  return node.querySelector( selector )
}
 
const selectAll = ( node, selector ) => {
  const result = []
 
  if( node.nodeType === 1 && node.matches( selector ) )
    result.push( node )
 
  const results = Array.from( node.querySelectorAll( selector ) )
 
  return result.concat( results )
}
 
const isWhitespaceNode = node =>
  node.nodeType === 3 && node.nodeValue.replace( /[\t\n\r ]/g, '' ).length === 0
 
const removeWhitespace = node => {
  const remove = []
 
  walk( node, current => {
    if( current.nodeType !== 3 ) return
    if( !isWhitespaceNode( current ) ) return
 
    remove.push( current )
  })
 
  remove.forEach( current => current.parentNode.removeChild( current ) )
}
 
//needs to be modified to leave whitespace alone in pre, script, textarea etc
const normalizeWhitespace = node => {
  node.normalize()
 
  walk( node, current => {
    if( current.nodeType !== 3 ) return
 
    current.nodeValue = current.nodeValue.replace( /[\t\n\r ]+/g, ' ' )
  })
}
 
const parse = ( document, str ) => {
  const fragment = document.createDocumentFragment()
  const temp = document.createElement( 'div' )
 
  temp.innerHTML = str
 
  if( temp.childNodes.length === 1 )
    return temp.removeChild( temp.firstChild )
 
  while( temp.firstChild )
    fragment.appendChild( temp.firstChild )
 
  return fragment
}
 
const parseDocument = ( document, str ) => {
  const doc = createEmptyDocument( document )
  const docType = document.implementation.createDocumentType( 'html', '', '' )
 
  const { documentElement } = document.implementation.createHTMLDocument( '' )
  documentElement.innerHTML = str
 
  doc.appendChild( docType )
  doc.appendChild( documentElement )
 
  return doc
}
 
const stringify = node => {
  if( node.nodeType === 1 ) return node.outerHTML
 
  if( node.nodeType === 3 ) return node.nodeValue
 
  if( node.nodeType === 8 ) return `<!--${ node.nodeValue }-->`
 
  // force HTML5 doctypes - for now
  if( node.nodeType === 10 ) return '<!doctype html>'
 
  return [ ...node.childNodes ].map( stringify ).join( '' )
}
 
const getAttributes = element => {
  const attributes = {}
  const { length } = element.attributes
 
  for( let i = 0; i < length; i++ ){
    const { name, value } = element.attributes[ i ]
 
    attributes[ name ] = value
  }
 
  return attributes
}
 
const setAttributes = ( element, attributes ) =>
  Object.keys( attributes ).forEach( name => {
    element.setAttribute( name, attributes[ name ] )
  })
 
const createEmptyDocument = document => {
  const doc = document.implementation.createDocument( 'http://www.w3.org/1999/xhtml', 'html' )
 
  removeAll( doc )
 
  return doc
}
 
const serializeChildren = node => [ ...node.childNodes ].map( serialize )
 
const serialize = node => {
  if( node.nodeType === 1 ){
    const tagName = node.tagName.toLowerCase()
 
    if( node.hasAttributes() )
      return [ tagName, getAttributes( node ), ...serializeChildren( node ) ]
 
    return [ tagName, ...serializeChildren( node ) ]
  }
 
  if( node.nodeType === 3 ) return node.nodeValue
 
  if( node.nodeType === 8 ) return [ node.nodeName, node.nodeValue ]
 
  if( node.nodeType === 10 ) return [
    '#document-type', node.name, node.publicId, node.systemId
  ]
 
  if( node.hasChildNodes() )
    return [ node.nodeName, ...serializeChildren( node ) ]
 
  throw Error( 'Unexpected node' )
}
 
const deserialize = ( document, value ) => {
  if( is.string( value ) )
    return document.createTextNode( value )
 
  if( !is.array( value ) )
    throw Error( 'Expected the serialized node to be a string or array' )
 
  const nodeName = value[ 0 ]
 
  if( !nodeName.startsWith( '#' ) ){
    const element = document.createElement( nodeName )
 
    for( let i = 1; i < value.length; i++ ){
      const current = value[ i ]
 
      if( is.object( current ) ){
        setAttributes( element, current )
      } else {
        element.appendChild( deserialize( document, current ) )
      }
    }
 
    return element
  }
 
  const args = value.slice( 1 )
 
  if( nodeName === '#comment' )
    return document.createComment( ...args )
 
  if( nodeName === '#document-type' )
    return document.implementation.createDocumentType( ...args )
 
  if( nodeName === '#document' ){
    const doc = createEmptyDocument( document )
 
    args.forEach( current =>
      doc.appendChild( deserialize( document, current ) )
    )
 
    return doc
  }
 
  if( nodeName === '#document-fragment' ){
    const fragment = document.createDocumentFragment()
 
    args.forEach( current =>
      fragment.appendChild( deserialize( document, current ) )
    )
 
    return fragment
  }
 
  throw Error( 'Unexpected node' )
}
 
const eachAttribute = ( node, callback ) => {
  const { attributes } = node
  const { length } = attributes
 
  for( let i = 0; i < length; i++ ){
    if( callback( attributes[ i ], i ) ) break
  }
}
 
const rename = ( document, node, tagName ) => {
  const tag = document.createElement( tagName )
 
  while( node.firstChild )
    tag.appendChild( node.firstChild )
 
  eachAttribute( node, attribute => {
    const { name, value } = attribute
 
    tag.setAttribute( name, value )
  })
 
  node.parentNode.insertBefore( tag, node )
  node.parentNode.removeChild( node )
 
  return tag
}
 
const empty = node => {
  const removed = []
 
  while( node.firstChild )
    removed.push( node.removeChild( node.firstChild ) )
 
  return removed
}
 
const domUtils = {
  select, selectAll, isWhitespaceNode, removeWhitespace, normalizeWhitespace,
  parse, parseDocument, stringify, getAttributes, setAttributes,
  createEmptyDocument, serialize, deserialize, eachAttribute, rename, empty
}
 
const utils = Object.assign( {}, nodeUtils, domUtils )
 
module.exports = utils