Skip to content

Remove Duplicate CSS Selectors from the final content#236

Open
emadha wants to merge 2 commits into
matthiasmullie:masterfrom
emadha:master
Open

Remove Duplicate CSS Selectors from the final content#236
emadha wants to merge 2 commits into
matthiasmullie:masterfrom
emadha:master

Conversation

@emadha

@emadha emadha commented Feb 7, 2018

Copy link
Copy Markdown

Removing Duplicate CSS Selectors

i noticed that if i add two files of the same content, the script will still show them and that will add up to the final file size (content).

for instance if i add default.css and default.css?v=1 and both have body{font-family:xx}, the final file will contain both files contents resulting in duplicate css selectors.

i added a method which gets all css selectors and array_unique that will implode with a NULL glue.

that will save a lot of data. for the example files i gave here it will cut the file size (and content) to half.

i noticed that if i add two of the same (or content) the script will still show them, and that will add up to the final file size (content).

for instance if i add default.css and default.css?v=1 and both have 'body{font-family:xx}, the final file will contain both files contents resulting in duplicate css selectors.

i added a method which gets all css selectors and array_unique that with implode of a NULL glue.

that will save a lot of data. for the example files i gave here it will cut the file size (and content) to half.
@emadha emadha changed the title Remove Duplicate CSS Selectors from the final content Remove Duplicate CSS Selectors from the final content + Adding some Minify Customizations Feb 8, 2018
@PeeHaa

PeeHaa commented Feb 8, 2018

Copy link
Copy Markdown

Out of curiosity. Does this properly handle overridden styles?

E.g.:

file1.css

body {font-family: xxx}

file2.css

body {font-family: yyy}

file3.css

body {font-family: xxx}

@PeeHaa

PeeHaa commented Feb 8, 2018

Copy link
Copy Markdown

Also it seems this PR actually adds / changes two different unrelated things?

You may want to split it up in 2 PRs if that is indeed the case.

@emadha

emadha commented Feb 8, 2018

Copy link
Copy Markdown
Author

@PeeHaa this method will keep the last selectors (just like the browser). + i should make it clear that selectors must be identical in order to be removed since i'm using array unique on the list of all css selectors

preg_match_all('/(?ims)([a-z0-9, \s\.\:#_\-@]+)\{([^\}]*)\}/', $contents, $selectors);

so it's not about a single property thing, it's about identical selector+properties+values
a selector of body{font-size:13px;} and body{font-size:13px;color:#fff} will not considered identical, and won't be removed.

@emadha

emadha commented Feb 8, 2018

Copy link
Copy Markdown
Author

@PeeHaa i noticed some changed cuz i'm using PHPStorm IDE, it reformatted parts of the code.

Adding $options to minify, usefull for customizing behaviours, etc.
@emadha

emadha commented Feb 8, 2018

Copy link
Copy Markdown
Author

@PeeHaa about PRs, i'm actually new to github however i created a new branch for the customization thing and separated them, i hope i did it the right way.

@emadha emadha changed the title Remove Duplicate CSS Selectors from the final content + Adding some Minify Customizations Remove Duplicate CSS Selectors from the final content Feb 8, 2018
@PeeHaa

PeeHaa commented Feb 8, 2018

Copy link
Copy Markdown

about PRs, i'm actually new to github however i created a new branch for the customization thing and separated them, i hope i did it the right way.

No worries. Looks fine to me.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants