In CSSStyleSelector, after rules are matched they are sorted according to specificity. This currently happens in 2 different places and thus should be refactored. This will also help in the implementation of <style scoped> (see bug 67720).
:P Turns out, that code has changed since I worked on it (in unsavory ways, IMHO), so marking this as invalid.
Upon further thought, a refactoring may still make sense, esp. in view of <style scoped> later on. It just won't be quite as elegant anymore. :p
Created attachment 115349 [details] Patch
Comment on attachment 115349 [details] Patch This seems like a bad way to factor this. You are adding a function to share code but very little is actually used in the other case.
FWIW, I completely agree! :P Originally the function only took a collectRulesOnly parameter that was set to false for page rules. See the patch for 67720 for the original version. FWIW, no CSS test breaks with this, even with the link style twiddling added, but since matchPageRules bypasses the twiddling it felt dangerous to re-use that branch in the refactoring.
Given the code changes, this refactoring is probably meaningless without the context of the larger patch after all. Marking INVALID.