From 762f5ab61d5655443986c8668f9956b15ee51247 Mon Sep 17 00:00:00 2001 From: jean-emmanuel Date: Mon, 12 Feb 2018 13:27:50 +0100 Subject: [PATCH] parser: parse css @media queries and @keyframes properly (issue #150) --- package.json | 1 + src/browser/js/app/parser.js | 35 ++++++++++++++--------------------- 2 files changed, 15 insertions(+), 21 deletions(-) diff --git a/package.json b/package.json index 23ef7e8d..ca5185a7 100644 --- a/package.json +++ b/package.json @@ -39,6 +39,7 @@ "minimatch": "^3.0.4", "node-sass": "4.7.2", "nodemon": "1.14.12", + "scope-css": "^1.0.5", "screenfull": "3.3.2", "shortid": "2.2.8", "stacktrace-js": "^2.0.0", diff --git a/src/browser/js/app/parser.js b/src/browser/js/app/parser.js index c3a697f5..279bf395 100644 --- a/src/browser/js/app/parser.js +++ b/src/browser/js/app/parser.js @@ -1,6 +1,7 @@ var widgetManager = require('./managers/widgets'), stateManager = require('./managers/state'), - {iconify} = require('./ui/utils') + {iconify} = require('./ui/utils'), + scope = require('scope-css') var Parser = class Parser { @@ -117,29 +118,21 @@ var Parser = class Parser { DOM.get(widgetContainer, '> .label')[0].innerHTML = label } - // parse css - var css = ';' + widgetInner.getProp('css'), - scopedCss = '' - - css = css.replace('\n', ' ') - // parse scoped css - css = css.replace(/[;\}\s]*([^;\{]*\{[^\}]*\})/g, (m)=>{ - m = m.replace(/^[;\}\s]*/,'') - if (m[0]=='&') { - scopedCss += m - } else { - scopedCss += '& ' + m - } - return '' - }) - // apply styles - widgetContainer.setAttribute('style', styleW + styleH + styleL + styleT + css) + var css = widgetInner.getProp('css') + + widgetContainer.setAttribute('style', styleW + styleH + styleL + styleT + css.replace(/;{[^\}]}/g),'') // apply scoped css - if (scopedCss.length) { - widgetContainer.setAttribute('id', 'widget-' + widgetInner.hash) - scopedCss = scopedCss.split('&').join('#widget-' + widgetInner.hash) + var prefix = '#' + widgetInner.hash, + scopedCss = scope(css, prefix) + + if (scopedCss.indexOf(prefix) > -1) { + + if (scopedCss.indexOf('@keyframes') > -1) scopedCss = scopedCss.replace(new RegExp(prefix + '(\\s+[0-9]+%)', 'g'), '$1') + if (scopedCss.indexOf('&') > -1) scopedCss = scopedCss.replace(new RegExp(prefix + '\\s&', 'g'), prefix) + + widgetContainer.setAttribute('id', widgetInner.hash) widgetContainer.insertBefore(DOM.create(``), widgetInner.widget) }