dom.js 3.2 KB

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