Exercise 1: A UA Rule With No Competing Author Rule at All — Possible Solution ==================================================================== THE TEST ------------------------------ elem3 = Element('b', {}) result3 = cascade_with_origin(elem3, UA_STYLESHEET, Stylesheet([])) result3['font-weight'] RESULT ------------------------------ 'bold' WHY THIS IS THE CORRECT RESULT ------------------------------ cascade_with_origin loops over both origins in turn: for origin, sheet in ((0, ua_sheet), (1, author_sheet)): for rule in sheet.rules: ... For origin=1 (the author sheet), sheet.rules is an empty list -- Stylesheet([]).rules == [] -- so that inner loop simply never executes at all; nothing gets appended to matches from the author side. For origin=0 (the UA sheet), UA_STYLESHEET's own 'b' rule matches elem3 directly, contributing (0, (0,0,1), , [Declaration('font-weight','bold')]) to matches. After the loop, matches contains exactly ONE entry -- the UA rule. Sorting a single-element list changes nothing, and the final result dict is built purely from that one entry's own declarations: font-weight becomes 'bold'. WHY ORIGIN-AWARE CASCADING GIVES THE SAME ANSWER AS PLAIN CASCADING HERE ------------------------------ The whole POINT of checking origin before specificity is to resolve a CONFLICT -- deciding which of two competing values, from two different origins, should win. When only one origin has anything to say about a given element and property at all, there is no conflict to resolve in the first place: matches has only one candidate, and that candidate wins by default, regardless of which origin it happens to belong to or what sort key is used to rank it. Origin-aware cascading and plain specificity-only cascading are mathematically guaranteed to agree in every case where at most one rule matches -- they can only possibly diverge once BOTH sides have a genuine, competing answer for the same property on the same element, which is exactly the scenario the chapter's own main margin example was built to demonstrate.