From a004bc3c7d462b6571e08ec80d72ed6aa1d8efd1 Mon Sep 17 00:00:00 2001 From: eight Date: Tue, 25 Sep 2018 21:10:35 +0800 Subject: [PATCH] Move styleCodeEmpty to util --- background/storage.js | 27 +++------------------------ background/util.js | 24 ++++++++++++++++++++++++ 2 files changed, 27 insertions(+), 24 deletions(-) create mode 100644 background/util.js diff --git a/background/storage.js b/background/storage.js index b57cdf62..e373a8ed 100644 --- a/background/storage.js +++ b/background/storage.js @@ -1,9 +1,8 @@ -/* global getStyleWithNoCode styleSectionsEqual */ +/* + global getStyleWithNoCode styleSectionsEqual styleCodeEmpty RX_CSS_COMMENTS +*/ 'use strict'; -const RX_NAMESPACE = /\s*(@namespace\s+(?:\S+\s+)?url\(http:\/\/.*?\);)\s*/g; -const RX_CHARSET = /\s*@charset\s+(['"]).*?\1\s*;\s*/g; -const RX_CSS_COMMENTS = /\/\*[\s\S]*?(?:\*\/|$)/g; // eslint-disable-next-line no-var var SLOPPY_REGEXP_PREFIX = '\0'; @@ -527,26 +526,6 @@ function getApplicableSections({ } -function styleCodeEmpty(code) { - // Collect the global section if it's not empty, not comment-only, not namespace-only. - const cmtOpen = code && code.indexOf('/*'); - if (cmtOpen >= 0) { - const cmtCloseLast = code.lastIndexOf('*/'); - if (cmtCloseLast < 0) { - code = code.substr(0, cmtOpen); - } else { - code = code.substr(0, cmtOpen) + - code.substring(cmtOpen, cmtCloseLast + 2).replace(RX_CSS_COMMENTS, '') + - code.substr(cmtCloseLast + 2); - } - } - if (!code || !code.trim()) return true; - if (code.includes('@namespace')) code = code.replace(RX_NAMESPACE, '').trim(); - if (code.includes('@charset')) code = code.replace(RX_CHARSET, '').trim(); - return !code; -} - - function invalidateCache({added, updated, deletedId} = {}) { if (!cachedStyles.list) return; const id = added ? added.id : updated ? updated.id : deletedId; diff --git a/background/util.js b/background/util.js new file mode 100644 index 00000000..1bd2f0f5 --- /dev/null +++ b/background/util.js @@ -0,0 +1,24 @@ +'use strict'; + +const RX_NAMESPACE = /\s*(@namespace\s+(?:\S+\s+)?url\(http:\/\/.*?\);)\s*/g; +const RX_CHARSET = /\s*@charset\s+(['"]).*?\1\s*;\s*/g; +const RX_CSS_COMMENTS = /\/\*[\s\S]*?(?:\*\/|$)/g; + +function styleCodeEmpty(code) { + // Collect the global section if it's not empty, not comment-only, not namespace-only. + const cmtOpen = code && code.indexOf('/*'); + if (cmtOpen >= 0) { + const cmtCloseLast = code.lastIndexOf('*/'); + if (cmtCloseLast < 0) { + code = code.substr(0, cmtOpen); + } else { + code = code.substr(0, cmtOpen) + + code.substring(cmtOpen, cmtCloseLast + 2).replace(RX_CSS_COMMENTS, '') + + code.substr(cmtCloseLast + 2); + } + } + if (!code || !code.trim()) return true; + if (code.includes('@namespace')) code = code.replace(RX_NAMESPACE, '').trim(); + if (code.includes('@charset')) code = code.replace(RX_CHARSET, '').trim(); + return !code; +}