diff --git a/.storybook/vcssom/createUseCssDiffWithNano.ts b/.storybook/vcssom/createUseCssDiffWithNano.ts index 34e91a0e..3dc4684d 100644 --- a/.storybook/vcssom/createUseCssDiffWithNano.ts +++ b/.storybook/vcssom/createUseCssDiffWithNano.ts @@ -1,4 +1,4 @@ -import {cssToTree} from '../../addon/vcssom'; +import {cssToTree} from '../../addon/vcssom/cssToTree'; import {HookUseMemo} from './types'; const createUseCssDiffWithNano = (nano: any) => (useMemo: HookUseMemo) => { diff --git a/addon/vcssom.js b/addon/vcssom.js index 72e37b89..73f561bf 100644 --- a/addon/vcssom.js +++ b/addon/vcssom.js @@ -1,60 +1,6 @@ 'use strict'; -var cssToTree = function (tree, css, selector, prelude) { - var declarations = {}; - var hasDeclarations = false; - var key, value; - - for (key in css) { - value = css[key]; - if (typeof value !== 'object') { - hasDeclarations = true; - declarations[key] = value; - } - } - - if (hasDeclarations) { - if (!tree[prelude]) tree[prelude] = {}; - tree[prelude][selector] = declarations; - } - - for (key in css) { - value = css[key]; - if (typeof value === 'object') { - if (key[0] === '@') { - cssToTree(tree, value, selector, key); - } else { - var hasCurrentSymbol = key.indexOf('&') > -1; - var selectorParts = selector.split(','); - if (hasCurrentSymbol) { - for (var i = 0; i < selectorParts.length; i++) { - selectorParts[i] = key.replace(/&/g, selectorParts[i]); - } - } else { - for (var i = 0; i < selectorParts.length; i++) { - selectorParts[i] = selectorParts[i] + ' ' + key; - } - } - cssToTree(tree, value, selectorParts.join(','), prelude); - } - } - } -}; - -exports.cssToTree = cssToTree; - -function removeRule (sh, rule) { - var maxIndex = rule.index; - var rules = sh.cssRules || sh.rules; - maxIndex = Math.max(maxIndex, rules.length - 1); - while (maxIndex >= 0) { - if (rules[maxIndex] === rule) { - sh.deleteRule(maxIndex); - break; - } - maxIndex--; - } -} +var removeRule = require('./vcssom/removeRule').removeRule; exports.addon = function (renderer) { if (process.env.NODE_ENV !== 'production') { diff --git a/addon/__tests__/vcssom.test.js b/addon/vcssom/__tests__/cssToTree.test.js similarity index 98% rename from addon/__tests__/vcssom.test.js rename to addon/vcssom/__tests__/cssToTree.test.js index 2f13119d..ac69a43b 100644 --- a/addon/__tests__/vcssom.test.js +++ b/addon/vcssom/__tests__/cssToTree.test.js @@ -1,7 +1,7 @@ /* eslint-disable */ 'use strict'; -var cssToTree = require('../vcssom').cssToTree; +var cssToTree = require('../cssToTree').cssToTree; describe('cssToTree', function () { test('exist', function () { diff --git a/addon/vcssom/cssToTree.js b/addon/vcssom/cssToTree.js new file mode 100644 index 00000000..84f633fd --- /dev/null +++ b/addon/vcssom/cssToTree.js @@ -0,0 +1,42 @@ +function cssToTree (tree, css, selector, prelude) { + var declarations = {}; + var hasDeclarations = false; + var key, value; + + for (key in css) { + value = css[key]; + if (typeof value !== 'object') { + hasDeclarations = true; + declarations[key] = value; + } + } + + if (hasDeclarations) { + if (!tree[prelude]) tree[prelude] = {}; + tree[prelude][selector] = declarations; + } + + for (key in css) { + value = css[key]; + if (typeof value === 'object') { + if (key[0] === '@') { + cssToTree(tree, value, selector, key); + } else { + var hasCurrentSymbol = key.indexOf('&') > -1; + var selectorParts = selector.split(','); + if (hasCurrentSymbol) { + for (var i = 0; i < selectorParts.length; i++) { + selectorParts[i] = key.replace(/&/g, selectorParts[i]); + } + } else { + for (var i = 0; i < selectorParts.length; i++) { + selectorParts[i] = selectorParts[i] + ' ' + key; + } + } + cssToTree(tree, value, selectorParts.join(','), prelude); + } + } + } +}; + +exports.cssToTree = cssToTree; diff --git a/addon/vcssom/removeRule.js b/addon/vcssom/removeRule.js new file mode 100644 index 00000000..d1ac6864 --- /dev/null +++ b/addon/vcssom/removeRule.js @@ -0,0 +1,14 @@ +function removeRule (sh, rule) { + var maxIndex = rule.index; + var rules = sh.cssRules || sh.rules; + maxIndex = Math.max(maxIndex, rules.length - 1); + while (maxIndex >= 0) { + if (rules[maxIndex] === rule) { + sh.deleteRule(maxIndex); + break; + } + maxIndex--; + } +} + +exports.removeRule = removeRule;