| 1 | 'use strict';
|
|---|
| 2 |
|
|---|
| 3 | exports.type = 'full';
|
|---|
| 4 |
|
|---|
| 5 | exports.active = true;
|
|---|
| 6 |
|
|---|
| 7 | exports.params = {
|
|---|
| 8 | onlyMatchedOnce: true,
|
|---|
| 9 | removeMatchedSelectors: true,
|
|---|
| 10 | useMqs: ['', 'screen'],
|
|---|
| 11 | usePseudos: ['']
|
|---|
| 12 | };
|
|---|
| 13 |
|
|---|
| 14 | exports.description = 'inline styles (additional options)';
|
|---|
| 15 |
|
|---|
| 16 |
|
|---|
| 17 | var csstree = require('css-tree'),
|
|---|
| 18 | cssTools = require('../lib/css-tools');
|
|---|
| 19 |
|
|---|
| 20 | /**
|
|---|
| 21 | * Moves + merges styles from style elements to element styles
|
|---|
| 22 | *
|
|---|
| 23 | * Options
|
|---|
| 24 | * onlyMatchedOnce (default: true)
|
|---|
| 25 | * inline only selectors that match once
|
|---|
| 26 | *
|
|---|
| 27 | * removeMatchedSelectors (default: true)
|
|---|
| 28 | * clean up matched selectors,
|
|---|
| 29 | * leave selectors that hadn't matched
|
|---|
| 30 | *
|
|---|
| 31 | * useMqs (default: ['', 'screen'])
|
|---|
| 32 | * what media queries to be used
|
|---|
| 33 | * empty string element for styles outside media queries
|
|---|
| 34 | *
|
|---|
| 35 | * usePseudos (default: [''])
|
|---|
| 36 | * what pseudo-classes/-elements to be used
|
|---|
| 37 | * empty string element for all non-pseudo-classes and/or -elements
|
|---|
| 38 | *
|
|---|
| 39 | * @param {Object} document document element
|
|---|
| 40 | * @param {Object} opts plugin params
|
|---|
| 41 | *
|
|---|
| 42 | * @author strarsis <strarsis@gmail.com>
|
|---|
| 43 | */
|
|---|
| 44 | exports.fn = function(document, opts) {
|
|---|
| 45 |
|
|---|
| 46 | // collect <style/>s
|
|---|
| 47 | var styleEls = document.querySelectorAll('style');
|
|---|
| 48 |
|
|---|
| 49 | //no <styles/>s, nothing to do
|
|---|
| 50 | if (styleEls === null) {
|
|---|
| 51 | return document;
|
|---|
| 52 | }
|
|---|
| 53 |
|
|---|
| 54 | var styles = [],
|
|---|
| 55 | selectors = [];
|
|---|
| 56 |
|
|---|
| 57 | for (var styleEl of styleEls) {
|
|---|
| 58 | if (styleEl.isEmpty() || styleEl.closestElem('foreignObject')) {
|
|---|
| 59 | // skip empty <style/>s or <foreignObject> content.
|
|---|
| 60 | continue;
|
|---|
| 61 | }
|
|---|
| 62 |
|
|---|
| 63 | var cssStr = cssTools.getCssStr(styleEl);
|
|---|
| 64 |
|
|---|
| 65 | // collect <style/>s and their css ast
|
|---|
| 66 | var cssAst = {};
|
|---|
| 67 | try {
|
|---|
| 68 | cssAst = csstree.parse(cssStr, {
|
|---|
| 69 | parseValue: false,
|
|---|
| 70 | parseCustomProperty: false
|
|---|
| 71 | });
|
|---|
| 72 | } catch (parseError) {
|
|---|
| 73 | // console.warn('Warning: Parse error of styles of <style/> element, skipped. Error details: ' + parseError);
|
|---|
| 74 | continue;
|
|---|
| 75 | }
|
|---|
| 76 |
|
|---|
| 77 | styles.push({
|
|---|
| 78 | styleEl: styleEl,
|
|---|
| 79 | cssAst: cssAst
|
|---|
| 80 | });
|
|---|
| 81 |
|
|---|
| 82 | selectors = selectors.concat(cssTools.flattenToSelectors(cssAst));
|
|---|
| 83 | }
|
|---|
| 84 |
|
|---|
| 85 |
|
|---|
| 86 | // filter for mediaqueries to be used or without any mediaquery
|
|---|
| 87 | var selectorsMq = cssTools.filterByMqs(selectors, opts.useMqs);
|
|---|
| 88 |
|
|---|
| 89 |
|
|---|
| 90 | // filter for pseudo elements to be used
|
|---|
| 91 | var selectorsPseudo = cssTools.filterByPseudos(selectorsMq, opts.usePseudos);
|
|---|
| 92 |
|
|---|
| 93 | // remove PseudoClass from its SimpleSelector for proper matching
|
|---|
| 94 | cssTools.cleanPseudos(selectorsPseudo);
|
|---|
| 95 |
|
|---|
| 96 |
|
|---|
| 97 | // stable sort selectors
|
|---|
| 98 | var sortedSelectors = cssTools.sortSelectors(selectorsPseudo).reverse();
|
|---|
| 99 |
|
|---|
| 100 |
|
|---|
| 101 | var selector,
|
|---|
| 102 | selectedEl;
|
|---|
| 103 |
|
|---|
| 104 | // match selectors
|
|---|
| 105 | for (selector of sortedSelectors) {
|
|---|
| 106 | var selectorStr = csstree.generate(selector.item.data),
|
|---|
| 107 | selectedEls = null;
|
|---|
| 108 |
|
|---|
| 109 | try {
|
|---|
| 110 | selectedEls = document.querySelectorAll(selectorStr);
|
|---|
| 111 | } catch (selectError) {
|
|---|
| 112 | if (selectError.constructor === SyntaxError) {
|
|---|
| 113 | // console.warn('Warning: Syntax error when trying to select \n\n' + selectorStr + '\n\n, skipped. Error details: ' + selectError);
|
|---|
| 114 | continue;
|
|---|
| 115 | }
|
|---|
| 116 | throw selectError;
|
|---|
| 117 | }
|
|---|
| 118 |
|
|---|
| 119 | if (selectedEls === null) {
|
|---|
| 120 | // nothing selected
|
|---|
| 121 | continue;
|
|---|
| 122 | }
|
|---|
| 123 |
|
|---|
| 124 | selector.selectedEls = selectedEls;
|
|---|
| 125 | }
|
|---|
| 126 |
|
|---|
| 127 |
|
|---|
| 128 | // apply <style/> styles to matched elements
|
|---|
| 129 | for (selector of sortedSelectors) {
|
|---|
| 130 | if(!selector.selectedEls) {
|
|---|
| 131 | continue;
|
|---|
| 132 | }
|
|---|
| 133 |
|
|---|
| 134 | if (opts.onlyMatchedOnce && selector.selectedEls !== null && selector.selectedEls.length > 1) {
|
|---|
| 135 | // skip selectors that match more than once if option onlyMatchedOnce is enabled
|
|---|
| 136 | continue;
|
|---|
| 137 | }
|
|---|
| 138 |
|
|---|
| 139 | // apply <style/> to matched elements
|
|---|
| 140 | for (selectedEl of selector.selectedEls) {
|
|---|
| 141 | if (selector.rule === null) {
|
|---|
| 142 | continue;
|
|---|
| 143 | }
|
|---|
| 144 |
|
|---|
| 145 | // merge declarations
|
|---|
| 146 | csstree.walk(selector.rule, {visit: 'Declaration', enter: function(styleCsstreeDeclaration) {
|
|---|
| 147 |
|
|---|
| 148 | // existing inline styles have higher priority
|
|---|
| 149 | // no inline styles, external styles, external styles used
|
|---|
| 150 | // inline styles, external styles same priority as inline styles, inline styles used
|
|---|
| 151 | // inline styles, external styles higher priority than inline styles, external styles used
|
|---|
| 152 | var styleDeclaration = cssTools.csstreeToStyleDeclaration(styleCsstreeDeclaration);
|
|---|
| 153 | if (selectedEl.style.getPropertyValue(styleDeclaration.name) !== null &&
|
|---|
| 154 | selectedEl.style.getPropertyPriority(styleDeclaration.name) >= styleDeclaration.priority) {
|
|---|
| 155 | return;
|
|---|
| 156 | }
|
|---|
| 157 | selectedEl.style.setProperty(styleDeclaration.name, styleDeclaration.value, styleDeclaration.priority);
|
|---|
| 158 | }});
|
|---|
| 159 | }
|
|---|
| 160 |
|
|---|
| 161 | if (opts.removeMatchedSelectors && selector.selectedEls !== null && selector.selectedEls.length > 0) {
|
|---|
| 162 | // clean up matching simple selectors if option removeMatchedSelectors is enabled
|
|---|
| 163 | selector.rule.prelude.children.remove(selector.item);
|
|---|
| 164 | }
|
|---|
| 165 | }
|
|---|
| 166 |
|
|---|
| 167 |
|
|---|
| 168 | if (!opts.removeMatchedSelectors) {
|
|---|
| 169 | return document; // no further processing required
|
|---|
| 170 | }
|
|---|
| 171 |
|
|---|
| 172 |
|
|---|
| 173 | // clean up matched class + ID attribute values
|
|---|
| 174 | for (selector of sortedSelectors) {
|
|---|
| 175 | if(!selector.selectedEls) {
|
|---|
| 176 | continue;
|
|---|
| 177 | }
|
|---|
| 178 |
|
|---|
| 179 | if (opts.onlyMatchedOnce && selector.selectedEls !== null && selector.selectedEls.length > 1) {
|
|---|
| 180 | // skip selectors that match more than once if option onlyMatchedOnce is enabled
|
|---|
| 181 | continue;
|
|---|
| 182 | }
|
|---|
| 183 |
|
|---|
| 184 | for (selectedEl of selector.selectedEls) {
|
|---|
| 185 | // class
|
|---|
| 186 | var firstSubSelector = selector.item.data.children.first();
|
|---|
| 187 | if(firstSubSelector.type === 'ClassSelector') {
|
|---|
| 188 | selectedEl.class.remove(firstSubSelector.name);
|
|---|
| 189 | }
|
|---|
| 190 | // clean up now empty class attributes
|
|---|
| 191 | if(typeof selectedEl.class.item(0) === 'undefined') {
|
|---|
| 192 | selectedEl.removeAttr('class');
|
|---|
| 193 | }
|
|---|
| 194 |
|
|---|
| 195 | // ID
|
|---|
| 196 | if(firstSubSelector.type === 'IdSelector') {
|
|---|
| 197 | selectedEl.removeAttr('id', firstSubSelector.name);
|
|---|
| 198 | }
|
|---|
| 199 | }
|
|---|
| 200 | }
|
|---|
| 201 |
|
|---|
| 202 |
|
|---|
| 203 | // clean up now empty elements
|
|---|
| 204 | for (var style of styles) {
|
|---|
| 205 | csstree.walk(style.cssAst, {visit: 'Rule', enter: function(node, item, list) {
|
|---|
| 206 | // clean up <style/> atrules without any rulesets left
|
|---|
| 207 | if (node.type === 'Atrule' &&
|
|---|
| 208 | // only Atrules containing rulesets
|
|---|
| 209 | node.block !== null &&
|
|---|
| 210 | node.block.children.isEmpty()) {
|
|---|
| 211 | list.remove(item);
|
|---|
| 212 | return;
|
|---|
| 213 | }
|
|---|
| 214 |
|
|---|
| 215 | // clean up <style/> rulesets without any css selectors left
|
|---|
| 216 | if (node.type === 'Rule' &&
|
|---|
| 217 | node.prelude.children.isEmpty()) {
|
|---|
| 218 | list.remove(item);
|
|---|
| 219 | }
|
|---|
| 220 | }});
|
|---|
| 221 |
|
|---|
| 222 |
|
|---|
| 223 | if (style.cssAst.children.isEmpty()) {
|
|---|
| 224 | // clean up now emtpy <style/>s
|
|---|
| 225 | var styleParentEl = style.styleEl.parentNode;
|
|---|
| 226 | styleParentEl.spliceContent(styleParentEl.content.indexOf(style.styleEl), 1);
|
|---|
| 227 |
|
|---|
| 228 | if (styleParentEl.elem === 'defs' &&
|
|---|
| 229 | styleParentEl.content.length === 0) {
|
|---|
| 230 | // also clean up now empty <def/>s
|
|---|
| 231 | var defsParentEl = styleParentEl.parentNode;
|
|---|
| 232 | defsParentEl.spliceContent(defsParentEl.content.indexOf(styleParentEl), 1);
|
|---|
| 233 | }
|
|---|
| 234 |
|
|---|
| 235 | continue;
|
|---|
| 236 | }
|
|---|
| 237 |
|
|---|
| 238 |
|
|---|
| 239 | // update existing, left over <style>s
|
|---|
| 240 | cssTools.setCssStr(style.styleEl, csstree.generate(style.cssAst));
|
|---|
| 241 | }
|
|---|
| 242 |
|
|---|
| 243 |
|
|---|
| 244 | return document;
|
|---|
| 245 | };
|
|---|