diff --git a/src/parser/model/ParserCss.js b/src/parser/model/ParserCss.js index 5b6a8b945..dbe6cd038 100644 --- a/src/parser/model/ParserCss.js +++ b/src/parser/model/ParserCss.js @@ -117,7 +117,7 @@ module.exports = config => ({ for (var s = 0, lens = subRules.length; s < lens; s++) { var subRule = subRules[s]; - subRule.mediaText = condition; + condition && (subRule.mediaText = condition); subRule.atRuleType = atRules[type]; } result = result.concat(subRules); @@ -180,22 +180,15 @@ module.exports = config => ({ * @return {Object|Array} */ parse(str) { - var el = document.createElement('style'); - /* - el.innerHTML = ".cssClass {border: 2px solid black; background-color: blue;} " + - ".red, .red2 {color:red; padding:5px} .test1.red {color:black} .red:hover{color: blue} " + - "@media screen and (min-width: 480px){ .red{color:white} }"; - */ + const el = document.createElement('style'); el.innerHTML = str; - // There is no .sheet without adding it to the + // There is no .sheet before adding it to the document.head.appendChild(el); - var sheet = el.sheet; + const sheet = el.sheet; document.head.removeChild(el); - var result = this.parseNode(sheet); + const result = this.parseNode(sheet); - if (result.length == 1) result = result[0]; - - return result; + return result.length == 1 ? result[0] : result; } }); diff --git a/test/specs/parser/model/ParserCss.js b/test/specs/parser/model/ParserCss.js index 0d0583bf5..6b126f8b8 100644 --- a/test/specs/parser/model/ParserCss.js +++ b/test/specs/parser/model/ParserCss.js @@ -238,6 +238,43 @@ module.exports = { }; expect(obj.parse(str)).toEqual(result); }); + + // Can't test keyframes https://github.com/NV/CSSOM/issues/95 + it.skip('Parse rule with a keyframes at-rule', () => { + var str = `@keyframes { + from {opacity: 0;} + to {opacity: 1;} + }`; + var result = [ + { + selectors: [], + atRuleType: 'keyframes', + selectorsAdd: 'from', + style: { opacity: '0' } + }, + { + selectors: [], + atRuleType: 'keyframes', + selectorsAdd: 'to', + style: { opacity: '1' } + } + ]; + expect(obj.parse(str)).toEqual(result); + }); + + it('Parse rule with font-face at-rule', () => { + var str = `@font-face { + font-family: "Open Sans"; + }`; + var result = { + selectors: [], + selectorsAdd: '', + atRuleType: 'font-face', + singleAtRule: 1, + style: { 'font-family': '"Open Sans"' } + }; + expect(obj.parse(str)).toEqual(result); + }); }); } };