-
Notifications
You must be signed in to change notification settings - Fork 0
/
css-functions.js
54 lines (49 loc) · 1.58 KB
/
css-functions.js
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
42
43
44
45
46
47
48
49
50
51
52
53
54
import { CssPointer } from "./css-helper-class.js"
export const customFunctions = {
/**
* @private
* @type {[() => void]}
*/
functions: [],
/**
* @param {(pointer: CssPointer) => void} func
* @param {string} optional_name
* #param {boolean} global_or_parameterized
*/
add(func, optional_name = "") {
func.regex = new RegExp(`(;|,|\\b)${optional_name || func.name}\\s*\\(\\s*(['"]?)(.+?)\\2\\s*\\)(;|,|!|\\b|$)`)
this.functions.push(func)
},
run(doc = document) {
doc.querySelectorAll("style.functions").forEach(style => {
this.functions.forEach(fn => {
do var start = execute(style, fn, start)
while(start)
})
})
}
}
customFunctions.run()
/**
* execute()
* -
* - look at a part of a stylesheet to see if a function is used in it.
* - if found, run the function assoc.'d.
* - use CSS var(--) pointers to allow for slotted / computed CSS values.
*
* @version ALPHA 1.5.0.0
* @param {HTMLStyleElement} style
* @param {{regex: RegExp}} fn
* @param {number} start
*/
function execute(style, fn, start = 0) {
const parts = fn.regex.exec(style.innerHTML.substr(start))
if(!parts) return false
const pointer = new CssPointer(style, parts, start)
const {key, end, index} = pointer.private
const result = fn(pointer)
if(result) pointer.private.set(result)
const insert = `${style.innerHTML.substr(0, index)}var(${key});`
style.innerHTML = `${insert}${style.innerHTML.substr(end)}`
return insert.length
}