-
Notifications
You must be signed in to change notification settings - Fork 186
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
Refactor specificity #59
Merged
Merged
Changes from 1 commit
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
<?php | ||
namespace TijsVerkoyen\CssToInlineStyles; | ||
|
||
/** | ||
* CSS to Inline Styles Specificity class. | ||
* | ||
* Compare specificity based on the CSS3 spec. | ||
* | ||
* @see http://www.w3.org/TR/selectors/#specificity | ||
* | ||
*/ | ||
class Specificity{ | ||
|
||
/** | ||
* The number of ID selectors in the selector | ||
* | ||
* @var int | ||
*/ | ||
private $a; | ||
|
||
/** | ||
* | ||
* The number of class selectors, attributes selectors, and pseudo-classes in the selector | ||
* | ||
* @var int | ||
*/ | ||
private $b; | ||
|
||
/** | ||
* The number of type selectors and pseudo-elements in the selector | ||
* | ||
* @var int | ||
*/ | ||
private $c; | ||
|
||
/** | ||
* @param int $a The number of ID selectors in the selector | ||
* @param int $b The number of class selectors, attributes selectors, and pseudo-classes in the selector | ||
* @param int $c The number of type selectors and pseudo-elements in the selector | ||
*/ | ||
public function __construct($a=0, $b=0, $c=0) | ||
{ | ||
$this->a = $a; | ||
$this->b = $b; | ||
$this->c = $c; | ||
} | ||
|
||
/** | ||
* Increase the current specificity by adding the three values | ||
* | ||
* @param int $a The number of ID selectors in the selector | ||
* @param int $b The number of class selectors, attributes selectors, and pseudo-classes in the selector | ||
* @param int $c The number of type selectors and pseudo-elements in the selector | ||
*/ | ||
public function increase($a, $b, $c) | ||
{ | ||
$this->a += $a; | ||
$this->b += $b; | ||
$this->c += $c; | ||
} | ||
|
||
/** | ||
* Calculate the specificity based on a CSS Selector string | ||
* | ||
* @param string $selector | ||
* @return static | ||
*/ | ||
public static function fromSelector($selector) | ||
{ | ||
// cleanup selector | ||
$selector = str_replace(array('>', '+'), array(' > ', ' + '), $selector); | ||
|
||
// create a new instance | ||
$specificity = new static; | ||
|
||
// split the selector into chunks based on spaces | ||
$chunks = explode(' ', $selector); | ||
|
||
// loop chunks | ||
foreach ($chunks as $chunk) { | ||
// an ID is important, so give it a high specificity | ||
if(strstr($chunk, '#') !== false) $specificity->increase(1,0,0); | ||
|
||
// classes are more important than a tag, but less important then an ID | ||
elseif(strstr($chunk, '.')) $specificity->increase(0,1,0); | ||
|
||
// anything else isn't that important | ||
else $specificity->increase(0,0,1); | ||
} | ||
|
||
// return | ||
return $specificity; | ||
|
||
} | ||
|
||
/** | ||
* Returns <0 when $specificity is greater, 0 when equal, >0 when smaller | ||
* | ||
* @param Specificity $specificity | ||
* @return int | ||
*/ | ||
public function compare(Specificity $specificity) | ||
{ | ||
if ($this->a !== $specificity->a) { | ||
return $this->a - $specificity->a; | ||
} elseif ($this->b !== $specificity->b) { | ||
return $this->b - $specificity->b; | ||
} else { | ||
return $this->c - $specificity->c; | ||
} | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I tjhink this logic does not work properly for
#id.class1.class2::hover
for instance. And it also break for[data-type="foo bar"]
.In the first case, it will set it to
1, 0, 0
instead of1, 2, 1
. In the second case, it will set it to0, 0, 2
instead of0, 1, 0
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Indeed, but that was also the case earlier.
We could bring in the Selector Parse from the CssSelector component, if that's not a bit overkill?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But we can only use the compare method from the Specificity class when Symfony 2.6 is released, so we would have to use the value (shouldn't be too big a problem, but neither are perfect)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I already thought about getting the specificity from the Symfony parser. I haven't yet tried how much refactoring it would involve in the library (it would not be doable when using
CssSelector::toXPath
static method as this one does not give you the parsed selector at all btw).and bumping the dependency to 2.6+ would also be an issue, because it would make the library unusable in Symfony <2.6 projects. If we use the value, we would face the same bug when you have big selectors
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
however, the first fix is indeed to separate the specificity and the order handling for the comparison, which is making the bug much more likely to happen.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe counting the occurrences of the
#
and.
in a string, as starters?I'm not very good at regexes, otherwise that might be an idea.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
And perhaps better just merge this PR and then create a new issue to focus on getting the actual correct specificity? What do you think @tijsverkoyen ?