LibWeb: Apply style rules in order of specificity (kinda)

We now sort the matched rules by the specificity of the first selector
in them. This is not perfect, since a rule can have multiple selectors,
but it is a nice chin-related progression on ACID2. :^)
This commit is contained in:
Andreas Kling 2020-06-10 16:41:30 +02:00
parent 4e1939c635
commit 7fe2f5f170
Notes: sideshowbarker 2024-07-19 05:43:36 +09:00
2 changed files with 11 additions and 0 deletions

View File

@ -24,6 +24,7 @@
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <AK/QuickSort.h>
#include <LibWeb/CSS/SelectorEngine.h>
#include <LibWeb/CSS/StyleResolver.h>
#include <LibWeb/CSS/StyleSheet.h>
@ -439,6 +440,13 @@ NonnullRefPtr<StyleProperties> StyleResolver::resolve_style(const Element& eleme
element.apply_presentational_hints(*style);
auto matching_rules = collect_matching_rules(element);
// FIXME: We need to look at the specificity of the matching *selector*, not just the matched *rule*!
// FIXME: It's really awkward that NonnullRefPtrVector cannot be quick_sort()'ed
quick_sort(reinterpret_cast<Vector<NonnullRefPtr<StyleRule>>&>(matching_rules), [&](auto& a, auto& b) {
return a->selectors().first().specificity() < b->selectors().first().specificity();
});
for (auto& rule : matching_rules) {
for (auto& property : rule.declaration().properties()) {
set_property_expanding_shorthands(style, property.property_id, property.value);

View File

@ -33,6 +33,9 @@
namespace Web {
class StyleRule : public RefCounted<StyleRule> {
AK_MAKE_NONCOPYABLE(StyleRule);
AK_MAKE_NONMOVABLE(StyleRule);
public:
static NonnullRefPtr<StyleRule> create(Vector<Selector>&& selectors, NonnullRefPtr<StyleDeclaration>&& declaration)
{