Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
titaniumnetwork-dev
GitHub Repository: titaniumnetwork-dev/Ultraviolet
Path: blob/main/src/client/dom/style.js
305 views
1
import EventEmitter from "events";
2
import HookEvent from "../hook.js";
3
4
/**
5
* @typedef {import('../index').default} UVClient
6
*/
7
8
class StyleApi extends EventEmitter {
9
/**
10
*
11
* @param {UVClient} ctx
12
*/
13
constructor(ctx) {
14
super();
15
this.ctx = ctx;
16
this.window = ctx.window;
17
this.CSSStyleDeclaration = this.window.CSSStyleDeclaration || {};
18
this.cssStyleProto = this.CSSStyleDeclaration.prototype || {};
19
this.getPropertyValue = this.cssStyleProto.getPropertyValue || null;
20
this.setProperty = this.cssStyleProto.setProperty || null;
21
this.cssText -
22
ctx.nativeMethods.getOwnPropertyDescriptors(
23
this.cssStyleProto,
24
"cssText"
25
);
26
this.urlProps = [
27
"background",
28
"backgroundImage",
29
"borderImage",
30
"borderImageSource",
31
"listStyle",
32
"listStyleImage",
33
"cursor",
34
];
35
this.dashedUrlProps = [
36
"background",
37
"background-image",
38
"border-image",
39
"border-image-source",
40
"list-style",
41
"list-style-image",
42
"cursor",
43
];
44
this.propToDashed = {
45
background: "background",
46
backgroundImage: "background-image",
47
borderImage: "border-image",
48
borderImageSource: "border-image-source",
49
listStyle: "list-style",
50
listStyleImage: "list-style-image",
51
cursor: "cursor",
52
};
53
}
54
overrideSetGetProperty() {
55
this.ctx.override(
56
this.cssStyleProto,
57
"getPropertyValue",
58
(target, that, args) => {
59
if (!args.length) return target.apply(that, args);
60
61
let [property] = args;
62
63
const event = new HookEvent({ property }, target, that);
64
this.emit("getPropertyValue", event);
65
66
if (event.intercepted) return event.returnValue;
67
return event.target.call(event.that, event.data.property);
68
}
69
);
70
this.ctx.override(
71
this.cssStyleProto,
72
"setProperty",
73
(target, that, args) => {
74
if (2 > args.length) return target.apply(that, args);
75
let [property, value] = args;
76
77
const event = new HookEvent({ property, value }, target, that);
78
this.emit("setProperty", event);
79
80
if (event.intercepted) return event.returnValue;
81
return event.target.call(
82
event.that,
83
event.data.property,
84
event.data.value
85
);
86
}
87
);
88
}
89
overrideCssText() {
90
this.ctx.overrideDescriptor(this.cssStyleProto, "cssText", {
91
get: (target, that) => {
92
const event = new HookEvent({ value: target.call(that) }, target, that);
93
this.emit("getCssText", event);
94
95
if (event.intercepted) return event.returnValue;
96
return event.data.value;
97
},
98
set: (target, that, [val]) => {
99
const event = new HookEvent({ value: val }, target, that);
100
this.emit("setCssText", event);
101
102
if (event.intercepted) return event.returnValue;
103
return event.target.call(event.that, event.data.value);
104
},
105
});
106
}
107
}
108
109
export default StyleApi;
110
111