Exercise 1: A Parentless Element With an Empty Stylesheet — Possible Solution ==================================================================== THE TEST ------------------------------ lonely = Element('p', {}) lonely.parent = None empty_sheet = Stylesheet([]) # no rules at all compute_style_tree(lonely, empty_sheet) lonely.computed_style == INITIAL_VALUES RESULT ------------------------------ True -- lonely.computed_style is exactly equal to INITIAL_VALUES, property for property, value for value. WHY THIS IS THE CORRECT RESULT ------------------------------ compute_style_tree calls compute_style(lonely, empty_sheet, None) -- parent_computed defaults to None since no parent_computed argument was passed in for the root call. Inside compute_style: own = cascade(element, stylesheet) # cascade() against an EMPTY # stylesheet has nothing to # loop over -- own == {} base = dict(INITIAL_VALUES) # start from a COPY of the # defaults if parent_computed is not None: # False here -- skipped ... base.update(own) # updating with an empty # dict changes nothing return base Every branch that could have changed base away from a plain copy of INITIAL_VALUES is either skipped (no parent to inherit from) or a no-op (no own rules to apply). The function falls straight through to returning an unmodified copy of the defaults. WHY THIS WORKS AS AN ANSWER ------------------------------ This is the correct baseline case every other test in this chapter implicitly depends on: an element with absolutely nothing influencing it -- no ancestor, no matching rule -- has to fall all the way back to CSS's own initial values, exactly as specified, with nothing else mixed in. It's the "identity" case for compute_style(): remove every possible source of influence, and what's left has to be precisely INITIAL_VALUES, unchanged. If this case failed, it would mean either cascade() was inventing declarations out of nowhere, or compute_style was mutating INITIAL_VALUES itself rather than working from a fresh copy of it -- a real risk, since dict(INITIAL_VALUES) has to make a genuine copy each call, not reuse and quietly corrupt the same shared dict across every element computed during a page's own style pass.