Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove Identical Properties #50

Merged
merged 1 commit into from
Dec 26, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 21 additions & 1 deletion src/css.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 || '';
Expand All @@ -30,7 +47,7 @@ const css = function (options, input, uglify) {
}
};

const classMap = {};
let classMap = {};
const newRules = {};

/* A rule looks like:
Expand Down Expand Up @@ -139,6 +156,8 @@ const css = function (options, input, uglify) {
});
});

classMap = removeIdenticalProperties(classMap);

if (uglify) {
let index = 0;
Object.keys(newRules).forEach(function (key) {
Expand Down Expand Up @@ -176,4 +195,5 @@ const css = function (options, input, uglify) {
};
};

css.removeIdenticalProperties = removeIdenticalProperties;
module.exports = css;
1 change: 0 additions & 1 deletion test/out.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
23 changes: 23 additions & 0 deletions tests/src/css.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down