-
Notifications
You must be signed in to change notification settings - Fork 82
/
Copy pathcss-style.ts
130 lines (103 loc) · 3.89 KB
/
css-style.ts
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
/// <reference path="../requirements.ts" />
interface ModuleHub { (file: 'core:css-style'): CSSStyle.CSSStyleModule; }
module CSSStyle {
/**
* Provides an easier way to deal with the different css style objects of an element.
*/
export interface CSSStyleModule extends Object {
/**
* Returns the available css style the closest possible to the used style of an element.
*
* NOTE: polyfilled properties from cssCascade.polyfillStyleInterface should work on this object.
* @param element The element for which the style should be returned.
*/
usedStyleOf(element : HTMLElement) : CSSStyleDeclaration
/**
* Returns the available css style the closest possible to the cascaded style of an element.
*
* NOTE: polyfilled properties from cssCascade.polyfillStyleInterface should work on this object.
* @param element The element for which the style should be returned.
*/
currentStyleOf(element : HTMLElement) : CSSStyleDeclaration
/**
* Returns the available css style the closest possible to the override style of an element.
*
* NOTE: polyfilled properties from cssCascade.polyfillStyleInterface should work on this object.
* @param element The element for which the style should be returned.
*/
styleOf(element : HTMLElement) : CSSStyleDeclaration
/**
* Returns the available css style the closest possible to the runtime style of an element.
*
* NOTE: polyfilled properties from cssCascade.polyfillStyleInterface should work on this object.
* @param element The element for which the style should be returned.
*/
runtimeStyleOf(element : HTMLElement) : CSSStyleDeclaration
/**
* Sets the runtime style of an element to the specified value, and return a backup for restore with restoreStyle.
*
* @param element The element for which the style should be modified.
* @param property The (css) name of the property to override.
* @param value The value the property will be set to.
*/
enforceStyle(
element : HTMLElement,
property : string,
value : string
)
: StyleBackup
/**
* Sets the runtime style of an element to the specified values, and return a backup for restore with restoreStyles.
*
* @param element The element for which the style should be modified.
* @param propertyValues A dictionnary whose names represent css property names, and whose value will be used for that property.
*/
enforceStyles(
element : HTMLElement,
propertyValues : Object
) : Array<StyleBackup>
/**
* Restore the runtime style of an element to the specified value.
*
* @param element The element for which the style should be modified.
* @param backup The backup obtained from enforceStyle.
*/
restoreStyle(
element: HTMLElement,
backup: StyleBackup
)
: void
/**
* Restore the runtime style of an element to the specified values.
*
* @param element The element for which the style should be modified.
* @param backup The backup obtained from enforceStyles.
*/
restoreStyles(
element: HTMLElement,
backup: Array<StyleBackup>
)
: void
}
export interface StyleBackup {
property : string
value : string
priority: string
}
}
/////////////////////////////////////////////////////////////////////////
//// EXAMPLES ///////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////
/** How do you obtain the used value of a property? */
function obtain_css_value() {
var cssStyle = require('core:css-style');
return cssStyle.usedStyleOf(document.body).alignContent;
}
/** How do you override a style temporarily? */
function override_css_value() {
var cssStyle = require('core:css-style');
// set the runtime style
var backup = cssStyle.enforceStyle(document.body, "color", "red");
// reset the previous value
cssStyle.restoreStyle(document.body, backup);
}