feature_detection.js (2994B)
1 import { elt, range, removeChildren, removeChildrenAndAdd } from "./dom.js" 2 import { ie, ie_version } from "./browser.js" 3 4 // Detect drag-and-drop 5 export let dragAndDrop = function() { 6 // There is *some* kind of drag-and-drop support in IE6-8, but I 7 // couldn't get it to work yet. 8 if (ie && ie_version < 9) return false 9 let div = elt('div') 10 return "draggable" in div || "dragDrop" in div 11 }() 12 13 let zwspSupported 14 export function zeroWidthElement(measure) { 15 if (zwspSupported == null) { 16 let test = elt("span", "\u200b") 17 removeChildrenAndAdd(measure, elt("span", [test, document.createTextNode("x")])) 18 if (measure.firstChild.offsetHeight != 0) 19 zwspSupported = test.offsetWidth <= 1 && test.offsetHeight > 2 && !(ie && ie_version < 8) 20 } 21 let node = zwspSupported ? elt("span", "\u200b") : 22 elt("span", "\u00a0", null, "display: inline-block; width: 1px; margin-right: -1px") 23 node.setAttribute("cm-text", "") 24 return node 25 } 26 27 // Feature-detect IE's crummy client rect reporting for bidi text 28 let badBidiRects 29 export function hasBadBidiRects(measure) { 30 if (badBidiRects != null) return badBidiRects 31 let txt = removeChildrenAndAdd(measure, document.createTextNode("A\u062eA")) 32 let r0 = range(txt, 0, 1).getBoundingClientRect() 33 let r1 = range(txt, 1, 2).getBoundingClientRect() 34 removeChildren(measure) 35 if (!r0 || r0.left == r0.right) return false // Safari returns null in some cases (#2780) 36 return badBidiRects = (r1.right - r0.right < 3) 37 } 38 39 // See if "".split is the broken IE version, if so, provide an 40 // alternative way to split lines. 41 export let splitLinesAuto = "\n\nb".split(/\n/).length != 3 ? string => { 42 let pos = 0, result = [], l = string.length 43 while (pos <= l) { 44 let nl = string.indexOf("\n", pos) 45 if (nl == -1) nl = string.length 46 let line = string.slice(pos, string.charAt(nl - 1) == "\r" ? nl - 1 : nl) 47 let rt = line.indexOf("\r") 48 if (rt != -1) { 49 result.push(line.slice(0, rt)) 50 pos += rt + 1 51 } else { 52 result.push(line) 53 pos = nl + 1 54 } 55 } 56 return result 57 } : string => string.split(/\r\n?|\n/) 58 59 export let hasSelection = window.getSelection ? te => { 60 try { return te.selectionStart != te.selectionEnd } 61 catch(e) { return false } 62 } : te => { 63 let range 64 try {range = te.ownerDocument.selection.createRange()} 65 catch(e) {} 66 if (!range || range.parentElement() != te) return false 67 return range.compareEndPoints("StartToEnd", range) != 0 68 } 69 70 export let hasCopyEvent = (() => { 71 let e = elt("div") 72 if ("oncopy" in e) return true 73 e.setAttribute("oncopy", "return;") 74 return typeof e.oncopy == "function" 75 })() 76 77 let badZoomedRects = null 78 export function hasBadZoomedRects(measure) { 79 if (badZoomedRects != null) return badZoomedRects 80 let node = removeChildrenAndAdd(measure, elt("span", "x")) 81 let normal = node.getBoundingClientRect() 82 let fromRange = range(node, 0, 1).getBoundingClientRect() 83 return badZoomedRects = Math.abs(normal.left - fromRange.left) > 1 84 }