parser: parse css @media queries and @keyframes properly (issue #150)
This commit is contained in:
parent
f5c6e6ce6a
commit
76759568cc
2 changed files with 15 additions and 21 deletions
|
|
@ -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",
|
||||
|
|
|
|||
|
|
@ -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(`<style>${scopedCss}</style>`), widgetInner.widget)
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue