3 ms·
> to select a <p> which has only a <img> as a child To basics, this selector p > img { display:block } has O(0) complexity (+/-) - for any IMG element yo
by c-smile 6y ago
> to select a <p> which has only a <img> as a child
To basics, this selector
p > img { display:block }
has O(0) complexity (+/-) - for any IMG element you can instantly tell if the selector applies to it. By inspecting its tag and tag of its parent (any element always have valid parent).
But this selector:
p:has(> img) { display:block }
has complexity O(N) - in order to test applicability you need to scan all N children of the P.
And now consider some JS code that does this
p.append(<img />);
If we would have that :has() feature then that simple statement will force rescan of the whole DOM tree. That will lead to O(N*N) complex updates.
> If Builders Built Buildings the Way Programmers Wrote Programs, Then the First Woodpecker That Came Along Would Destroy Civilization
Programmers who write browsers do care about such things, trust me. We have plenty of creative Web desickers who are managing to push browsers to extremes already ...