From 51fb0994eddad4bd4c0d687fce4b2c24ae03ac7d Mon Sep 17 00:00:00 2001 From: WangXinhai <18907191365@189.cn> Date: Sun, 1 Apr 2018 00:59:19 +0800 Subject: [PATCH] theme replacing should cut tons of irrelevant css MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 风格替换既然是颜色,那么应该削减大量无关的代码. 可以通过regexp 捕捉相关的,以上代码经过测试 --- src/components/ThemePicker/index.vue | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/src/components/ThemePicker/index.vue b/src/components/ThemePicker/index.vue index b366d963..a5e7059a 100644 --- a/src/components/ThemePicker/index.vue +++ b/src/components/ThemePicker/index.vue @@ -65,12 +65,18 @@ export default { }, methods: { - updateStyle(style, oldCluster, newCluster) { - let newStyle = style + updateStyle(style, oldCluster, newCluster) { + let colorOverrides = [] // only capture color overides oldCluster.forEach((color, index) => { - newStyle = newStyle.replace(new RegExp(color, 'ig'), newCluster[index]) + let value = newCluster[index] + let repl = new RegExp(`(^|})([^{]+{[^{}]+)${color}\\b([^}]*)(?=})`, 'gi') + let nestRepl = new RegExp(color, 'ig') // for greed matching before the 'color' + let v + while ((v = repl.exec(data))) { + colorOverrides.push(v[2].replace(nestRepl, value) + value + v[3] + '}') // '}' not captured in the regexp repl to reserve it as locator-boundary + } }) - return newStyle + return colorOverrides.join('') }, getCSSString(url, callback, variable) {