Repository navigation
Expand file tree
/
Copy pathpolyfills.ts
More file actions
41 lines (38 loc) · 1.66 KB
/
Copy pathpolyfills.ts
File metadata and controls
41 lines (38 loc) · 1.66 KB
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
// @ts-ignore-file
// issue: https://github.com/maurizzzio/function-plot/issues/6
// solution: the line type is selecting the derivative line when the content is re-drawn, then when the
// derivative was redrawn an already selected line (by the line type) was used thus making a single line
// disappear from the graph, to avoid the selection of the derivative line the selector needs to
// work only for immediate children which is done with `:scope >`
// src: http://stackoverflow.com/questions/6481612/queryselector-search-immediate-children
/*eslint-disable */
if (typeof window !== 'undefined')
(function (doc, proto) {
try {
// check if browser supports :scope natively
doc.querySelector(':scope body')
} catch (err) {
// polyfill native methods if it doesn't
;['querySelector', 'querySelectorAll'].forEach(function (method) {
// @ts-ignore
const native = proto[method]
// @ts-ignore
proto[method] = function (selectors) {
if (/(^|,)\s*:scope/.test(selectors)) {
// only if selectors contains :scope
const id = this.id // remember current element id
this.id = 'ID_' + Date.now() // assign new unique id
selectors = selectors.replace(/((^|,)\s*):scope/g, '$1#' + this.id) // replace :scope with #ID
// @ts-ignore
const result = doc[method](selectors)
this.id = id // restore previous id
return result
} else {
return native.call(this, selectors) // use native code for other selectors
}
}
})
}
})(window.document, Element.prototype)
// @ts-ignore-end
/* eslint-enable */