From 1839cbaffa5ad33ac6bfc66a393a31ba880ca8c6 Mon Sep 17 00:00:00 2001 From: The Jared Wilcurt Date: Sat, 26 Dec 2020 17:08:45 -0500 Subject: [PATCH] Remove Identical Properties --- src/css.js | 22 +++++++++++++++++++++- test/out.json | 1 - tests/src/css.test.js | 23 +++++++++++++++++++++++ 3 files changed, 44 insertions(+), 2 deletions(-) diff --git a/src/css.js b/src/css.js index 13968c6..43edf91 100644 --- a/src/css.js +++ b/src/css.js @@ -4,6 +4,23 @@ const cssUglifier = require('./css-uglifier.js'); const encodeClassName = require('./css-class-encoding.js'); const helpers = require('./helpers.js'); +/** + * Remove duplicate property/value pairs that are duplicates. + * `display: none; display: none;` becomes `display:none;` + * `display: block; display: none;` is unchanged because they + * are not identical. + * + * @param {object} classMap The class map object used by the HTML/JSON + * @return {object} Returns the classMap object + */ +function removeIdenticalProperties (classMap) { + // Remove identical properties + Object.keys(classMap).forEach(function (selector) { + classMap[selector] = Array.from(new Set(classMap[selector].reverse())).reverse(); + }); + return classMap; +} + const css = function (options, input, uglify) { options = options || {}; input = input || ''; @@ -30,7 +47,7 @@ const css = function (options, input, uglify) { } }; - const classMap = {}; + let classMap = {}; const newRules = {}; /* A rule looks like: @@ -139,6 +156,8 @@ const css = function (options, input, uglify) { }); }); + classMap = removeIdenticalProperties(classMap); + if (uglify) { let index = 0; Object.keys(newRules).forEach(function (key) { @@ -176,4 +195,5 @@ const css = function (options, input, uglify) { }; }; +css.removeIdenticalProperties = removeIdenticalProperties; module.exports = css; diff --git a/test/out.json b/test/out.json index 7039afa..22bbea3 100644 --- a/test/out.json +++ b/test/out.json @@ -10,7 +10,6 @@ ".rp__display__--COLONblock", ".rp__display__--COLONinline-block", ".rp__vertical-align__--COLONbaseline", - ".rp__display__--COLONnone", ".rp__height__--COLON0", ".rp__display__--COLONnone", ".rp__background__--COLONtransparent", diff --git a/tests/src/css.test.js b/tests/src/css.test.js index b8b8715..94253de 100644 --- a/tests/src/css.test.js +++ b/tests/src/css.test.js @@ -14,6 +14,29 @@ describe('CSS', () => { }; }); + describe('removeIdenticalProperties', () => { + test('Removes dupes', () => { + expect(css.removeIdenticalProperties({ + '.duplicates': [ + '.rp__display__--COLONnone', + '.rp__display__--COLONblock', + '.rp__display__--COLONnone', + '.rp__display__--COLONinline-block', + '.rp__display__--COLONnone', + '.rp__display__--COLONflex' + ] + })) + .toEqual({ + '.duplicates': [ + '.rp__display__--COLONblock', + '.rp__display__--COLONinline-block', + '.rp__display__--COLONnone', + '.rp__display__--COLONflex' + ] + }); + }); + }); + describe('Bad inputs', () => { test('Empty', () => { expect(css())