comparison .cms/lib/codemirror/src/util/dom.js @ 0:78edf6b517a0 draft

24.10
author Coffee CMS <info@coffee-cms.ru>
date Fri, 11 Oct 2024 22:40:23 +0000
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:78edf6b517a0
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(rootNode) {
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 doc = rootNode.ownerDocument || rootNode
72 let activeElement
73 try {
74 activeElement = rootNode.activeElement
75 } catch(e) {
76 activeElement = doc.body || null
77 }
78 while (activeElement && activeElement.shadowRoot && activeElement.shadowRoot.activeElement)
79 activeElement = activeElement.shadowRoot.activeElement
80 return activeElement
81 }
82
83 export function addClass(node, cls) {
84 let current = node.className
85 if (!classTest(cls).test(current)) node.className += (current ? " " : "") + cls
86 }
87 export function joinClasses(a, b) {
88 let as = a.split(" ")
89 for (let i = 0; i < as.length; i++)
90 if (as[i] && !classTest(as[i]).test(b)) b += " " + as[i]
91 return b
92 }
93
94 export let selectInput = function(node) { node.select() }
95 if (ios) // Mobile Safari apparently has a bug where select() is broken.
96 selectInput = function(node) { node.selectionStart = 0; node.selectionEnd = node.value.length }
97 else if (ie) // Suppress mysterious IE10 errors
98 selectInput = function(node) { try { node.select() } catch(_e) {} }
99
100 export function doc(cm) { return cm.display.wrapper.ownerDocument }
101
102 export function root(cm) {
103 return rootNode(cm.display.wrapper)
104 }
105
106 export function rootNode(element) {
107 // Detect modern browsers (2017+).
108 return element.getRootNode ? element.getRootNode() : element.ownerDocument
109 }
110
111 export function win(cm) { return doc(cm).defaultView }