From 8ae5c1884ee3fefb5fc6d84fdfe82929313423d6 Mon Sep 17 00:00:00 2001 From: jean-emmanuel Date: Wed, 14 Feb 2018 20:13:43 +0100 Subject: [PATCH] props: css & color fixes --- src/browser/js/app/widgets/common/widget.js | 27 +++++++++------------ 1 file changed, 12 insertions(+), 15 deletions(-) diff --git a/src/browser/js/app/widgets/common/widget.js b/src/browser/js/app/widgets/common/widget.js index 11dc487a..ed120eb9 100644 --- a/src/browser/js/app/widgets/common/widget.js +++ b/src/browser/js/app/widgets/common/widget.js @@ -124,12 +124,11 @@ class Widget extends EventEmitter { if (options.container) { this.container = DOM.create(` -
+
`) this.label = DOM.create(`
`) this.container.appendChild(this.label) this.container.appendChild(this.widget) - this.container.setAttribute('data-widget', this.hash) this.container._widget_instance = this this.setContainerStyles() } else { @@ -486,22 +485,20 @@ class Widget extends EventEmitter { // css var prefix = '#' + this.hash, scopedCss = scopeCss(this.getProp('css'), prefix), - unScopedCss = prefix + '{' + this.getProp('css').replace(/(^|\n)([^\{]*)\{([^\}]*)\}/g, '') + '}' + unScopedCss = '' - if (scopedCss.indexOf(prefix) > -1) { + this.getProp('css').replace(/^[^\{\}]*/m, m => unScopedCss += m) - if (scopedCss.indexOf('@keyframes') > -1) scopedCss = scopedCss.replace(new RegExp(prefix + '\\s+([0-9]+%|to|from)', 'g'), ' $1') - if (scopedCss.indexOf('&') > -1) scopedCss = scopedCss.replace(new RegExp(prefix + '\\s&', 'g'), prefix) + if (scopedCss.indexOf('@keyframes') > -1) scopedCss = scopedCss.replace(new RegExp(prefix + '\\s+([0-9]+%|to|from)', 'g'), ' $1') + if (scopedCss.indexOf('&') > -1) scopedCss = scopedCss.replace(new RegExp(prefix + '\\s&', 'g'), prefix) - this.container.setAttribute('id', this.hash) + var style = DOM.create(``), + oldStyle = DOM.get(this.container, '> style')[0] - var style = DOM.create(``), - oldStyle = DOM.get(this.container, '> style')[0] - if (oldStyle) { - this.container.replaceChild(style, oldStyle) - } else { - this.container.insertBefore(style, this.widget) - } + if (oldStyle) { + this.container.replaceChild(style, oldStyle) + } else if (scopedCss.length || unScopedCss.length){ + this.container.insertBefore(style, this.widget) } } @@ -509,7 +506,7 @@ class Widget extends EventEmitter { if (styles.includes('color')) { // color - if (this.getProp('color') && this.getProp('color')!='auto') this.container.style.setProperty('--color-custom',this.getProp('color')) + this.container.style.setProperty('--color-custom', this.getProp('color') != 'auto' ? this.getProp('color') : '') }