UNPKG

953 BJavaScriptView Raw
1const cssSelector = require('./styleSelector');
2const parseCSS = require('css-rules');
3const styleSelector = cssSelector('<style attribute>', [ 1, 0, 0, 0 ]);
4const addProps = require('./addProps');
5
6module.exports = (rule, $) => {
7 const sel = rule[0];
8 const style = rule[1];
9 const selector = cssSelector(sel);
10 const editedElements = [];
11
12 $(sel).each((index, el) => {
13 let cssText;
14
15 if (!el.styleProps) {
16 el.styleProps = {};
17
18 // if the element has inline styles, fake selector with topmost specificity
19 if ($(el).attr('style')) {
20 cssText = `* { ${$(el).attr('style')} } `;
21
22 addProps(el, parseCSS(cssText)[0][1], styleSelector);
23 }
24
25 // store reference to an element we need to compile style="" attr for
26 editedElements.push(el);
27 }
28
29 addProps(el, style, selector);
30 });
31
32 return editedElements;
33};