-
Notifications
You must be signed in to change notification settings - Fork 184
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
#74068 nth child specificity #369
Merged
+89
−3
Merged
Changes from 4 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
d519ee9
fix(): nth-child specificity
SStranks fc1db40
fix(selectorPrinting.test): added more tests
SStranks 8d81e52
fix(selectorPrinting): complete
SStranks b99246f
fix(): comment formatting
SStranks c7b2ea9
fix(selectorPrinting): different logical approach
SStranks 733bb34
chore(): tidying up
SStranks 1bc7121
fix(selectorPrinting.ts): formatting
SStranks 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -456,10 +456,9 @@ export class SelectorPrinting { | |
continue elementLoop; | ||
} | ||
|
||
if (text.match(/^:(?:nth-child|nth-last-child|host|host-context)/i) && childElements.length > 0) { | ||
if (text.match(/^:(?:host|host-context)/i) && childElements.length > 0) { | ||
// The specificity of :host() is that of a pseudo-class, plus the specificity of its argument. | ||
// The specificity of :host-context() is that of a pseudo-class, plus the specificity of its argument. | ||
// The specificity of an :nth-child() or :nth-last-child() selector is the specificity of the pseudo class itself (counting as one pseudo-class selector) plus the specificity of the most specific complex selector in its selector list argument. | ||
specificity.attr++; | ||
|
||
let mostSpecificListItem = calculateMostSpecificListItem(childElements); | ||
|
@@ -470,6 +469,27 @@ export class SelectorPrinting { | |
continue elementLoop; | ||
} | ||
|
||
if (text.match(/^:(?:nth-child|nth-last-child)/i) && childElements.length > 0) { | ||
/* The specificity of the :nth-child(An+B [of S]?) pseudo-class is the specificity of a single pseudo-class plus, if S is specified, the specificity of the most specific complex selector in S */ | ||
// https://www.w3.org/TR/selectors-4/#the-nth-child-pseudo | ||
specificity.attr++; | ||
|
||
const selectorText = text.slice(text.indexOf('(') + 1, text.length -1); | ||
|
||
// Test for presence of complex-selector-list S; find highest specificity | ||
const keyword = text.indexOf(' of '); | ||
if (keyword !== -1) { | ||
const psuedoSelector = element.getChildren(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Typo - should be named 'pseudoSelector' (e/u inversion) |
||
const complexSelectorList = psuedoSelector[psuedoSelector.length - 1].getChildren(); | ||
let mostSpecificListItem = calculateMostSpecificListItem(complexSelectorList); | ||
|
||
specificity.id += mostSpecificListItem.id; | ||
specificity.attr += mostSpecificListItem.attr; | ||
specificity.tag += mostSpecificListItem.tag; | ||
} | ||
continue elementLoop; | ||
} | ||
|
||
specificity.attr++; //pseudo class | ||
continue elementLoop; | ||
} | ||
|
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
Oops, something went wrong.
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.
Testing for a space before and after
of
is not clean. There could also be a tab, or even a non word characterwould be valid too.
I suggest to jeust use the scanner to find out if there's an
of
ident in the string.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.
Reworked the logic in the latest commit. When the 'An + B [of S]?' syntax is respected then we can just grab the last childElement as it will contain the entire complex-selector list (S).
The edge case where 'n' has no integer prefix (the parser does not recognise it as a binary expression token). Extract out the complex-selector list, and re-parse the selectors. This also handles the different syntax possibilities; added more tests for completeness.