Skip to content

Commit 226d190

Browse files
committed
Create function to apply styles via CSSOM
1 parent 1290ba3 commit 226d190

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

src/instance/functions/style.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,3 +215,20 @@ export default function style(element) {
215215
transition
216216
}
217217
}
218+
219+
/**
220+
* apply a CSS string to an element using the CSSOM (element.style) rather
221+
* than setAttribute, which may violate the content security policy.
222+
*
223+
* @param {Node} [el] Element to receive styles.
224+
* @param {string} [declaration] Styles to apply.
225+
*/
226+
export function applyStyle (el, declaration) {
227+
declaration.split(';').forEach(pair => {
228+
const [property, value] = pair.split(':').map(s => s.trim())
229+
if (property && value) {
230+
el.style[property] = value
231+
}
232+
})
233+
}
234+

0 commit comments

Comments
 (0)