You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

handleRule.js 953B

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