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 Duplicate CSS Selectors from the final content #236

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open

Remove Duplicate CSS Selectors from the final content #236

wants to merge 2 commits into from

Conversation

emadha
Copy link

@emadha emadha commented Feb 7, 2018

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
Copy link

PeeHaa commented Feb 8, 2018

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
Copy link

PeeHaa commented Feb 8, 2018

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
Copy link
Author

emadha commented Feb 8, 2018

@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
Copy link
Author

emadha commented Feb 8, 2018

@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
Copy link
Author

emadha commented Feb 8, 2018

@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
Copy link

PeeHaa commented Feb 8, 2018

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.

None yet

2 participants