openrat-cms

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README

dom.js (3309B)


      1 import { ie, ios } from "./browser.js"
      2 
      3 export function classTest(cls) { return new RegExp("(^|\\s)" + cls + "(?:$|\\s)\\s*") }
      4 
      5 export let rmClass = function(node, cls) {
      6   let current = node.className
      7   let match = classTest(cls).exec(current)
      8   if (match) {
      9     let after = current.slice(match.index + match[0].length)
     10     node.className = current.slice(0, match.index) + (after ? match[1] + after : "")
     11   }
     12 }
     13 
     14 export function removeChildren(e) {
     15   for (let count = e.childNodes.length; count > 0; --count)
     16     e.removeChild(e.firstChild)
     17   return e
     18 }
     19 
     20 export function removeChildrenAndAdd(parent, e) {
     21   return removeChildren(parent).appendChild(e)
     22 }
     23 
     24 export function elt(tag, content, className, style) {
     25   let e = document.createElement(tag)
     26   if (className) e.className = className
     27   if (style) e.style.cssText = style
     28   if (typeof content == "string") e.appendChild(document.createTextNode(content))
     29   else if (content) for (let i = 0; i < content.length; ++i) e.appendChild(content[i])
     30   return e
     31 }
     32 // wrapper for elt, which removes the elt from the accessibility tree
     33 export function eltP(tag, content, className, style) {
     34   let e = elt(tag, content, className, style)
     35   e.setAttribute("role", "presentation")
     36   return e
     37 }
     38 
     39 export let range
     40 if (document.createRange) range = function(node, start, end, endNode) {
     41   let r = document.createRange()
     42   r.setEnd(endNode || node, end)
     43   r.setStart(node, start)
     44   return r
     45 }
     46 else range = function(node, start, end) {
     47   let r = document.body.createTextRange()
     48   try { r.moveToElementText(node.parentNode) }
     49   catch(e) { return r }
     50   r.collapse(true)
     51   r.moveEnd("character", end)
     52   r.moveStart("character", start)
     53   return r
     54 }
     55 
     56 export function contains(parent, child) {
     57   if (child.nodeType == 3) // Android browser always returns false when child is a textnode
     58     child = child.parentNode
     59   if (parent.contains)
     60     return parent.contains(child)
     61   do {
     62     if (child.nodeType == 11) child = child.host
     63     if (child == parent) return true
     64   } while (child = child.parentNode)
     65 }
     66 
     67 export function activeElt() {
     68   // IE and Edge may throw an "Unspecified Error" when accessing document.activeElement.
     69   // IE < 10 will throw when accessed while the page is loading or in an iframe.
     70   // IE > 9 and Edge will throw when accessed in an iframe if document.body is unavailable.
     71   let activeElement
     72   try {
     73     activeElement = document.activeElement
     74   } catch(e) {
     75     activeElement = document.body || null
     76   }
     77   while (activeElement && activeElement.shadowRoot && activeElement.shadowRoot.activeElement)
     78     activeElement = activeElement.shadowRoot.activeElement
     79   return activeElement
     80 }
     81 
     82 export function addClass(node, cls) {
     83   let current = node.className
     84   if (!classTest(cls).test(current)) node.className += (current ? " " : "") + cls
     85 }
     86 export function joinClasses(a, b) {
     87   let as = a.split(" ")
     88   for (let i = 0; i < as.length; i++)
     89     if (as[i] && !classTest(as[i]).test(b)) b += " " + as[i]
     90   return b
     91 }
     92 
     93 export let selectInput = function(node) { node.select() }
     94 if (ios) // Mobile Safari apparently has a bug where select() is broken.
     95   selectInput = function(node) { node.selectionStart = 0; node.selectionEnd = node.value.length }
     96 else if (ie) // Suppress mysterious IE10 errors
     97   selectInput = function(node) { try { node.select() } catch(_e) {} }