Something went wrong. Try again.
Exploring the idea of generated web rendering engine.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263//! Spec text for the stacking-order seam (`layout::stacking`, `specs/stacking-order.md`),//! the shape of `provenance::css21_absolute_positioning` kept inside this crate rather//! than beside it: `crates/provenance` is not in this seam's file set (a live track is//! rebuilding its spec fetch), and the `DefinitionIndex` trait it exports is all the//! provenance record needs. Three sections feed one seam: CSS 2.1 §9.9.1 (the `z-index`//! property and stacking contexts), CSS 2.1 Appendix E.2 (the painting order within a//! stacking context) and CSS Color Level 3 §3.2 (`opacity < 1` as a stacking-context//! trigger). Everything a candidate needs is in these; transforms, `isolation`, blend//! modes and filters are out of this seam's scope and are not quoted.
use provenance::DefinitionIndex;
/// `"9.9.1"` names this seam the way `"10.3.7"` names the absolute-positioning seam's/// primary section; `Fixture::references` reaches Appendix E and the opacity rule.pub const SECTION_ID: &str = "9.9.1";pub const SPEC_URL: &str = "https://www.w3.org/TR/CSS21/visuren.html#z-index";pub const SPEC_LEVEL: &str = "CSS 2.1";
/// CSS 2.1 §9.9.1 and Appendix E.2's normative text on stacking contexts and painting/// order, plus CSS Color 3's opacity rule, HTML-wrapped and narrowed to the block-level,/// no-text, no-float case this seam's corpus exercises/// (`crates/harness/src/stacking_cases.rs`). The `<wpt>` block and the "Changes" section/// are synthetic, added to exercise the normalizer the same way every other seam's/// `SPEC_TEXT` does; everything else is the sections' actual prose, with bracketed prose/// standing in for sentences not quoted verbatim.pub const SPEC_TEXT: &str = r#"<h4 id="z-index">9.9.1 Specifying the stack level: the 'z-index' property</h4>
<p>'z-index': Value: auto | <integer> | inherit. Initial: auto. Applies to: positionedelements. Inherited: no.</p>
<p>For a positioned box, the 'z-index' property specifies:</p><ol><li>The stack level of the box in the current stacking context.</li><li>Whether the box establishes a stacking context.</li></ol>
<p>Values have the following meanings:</p><dl><dt><integer></dt><dd>This integer is the stack level of the generated box in the current stackingcontext. The box also establishes a new stacking context.</dd><dt>auto</dt><dd>The stack level of the generated box in the current stacking context is 0. The boxdoes not establish a new stacking context unless it is the root element.</dd></dl>
<p>In this section, the expression "in front of" means closer to the user as the userfaces the screen.</p>
<p>The order in which the rendering tree is painted onto the canvas is described in termsof stacking contexts. Stacking contexts can contain further stacking contexts. A stackingcontext is atomic from the point of view of its parent stacking context; boxes in otherstacking contexts may not come between any of its boxes.</p>
<p>Each box belongs to one stacking context. Each positioned box in a given stackingcontext has an integer stack level, which is its position on the z-axis relative otherstack levels within the same stacking context. Boxes with greater stack levels are alwaysformatted in front of boxes with lower stack levels. Boxes may have negative stack levels.Boxes with the same stack level in a stacking context are stacked back-to-front accordingto document tree order.</p>
<p>The root element forms the root stacking context. Other stacking contexts aregenerated by any positioned element (including relatively positioned elements) having acomputed value of 'z-index' other than 'auto'. Stacking contexts are not necessarilyrelated to containing blocks. In future levels of CSS, other properties may introducestacking contexts, for example 'opacity'.</p>
<p>Within each stacking context, the following layers are painted in back-to-frontorder:</p><ol><li>the background and borders of the element forming the stacking context.</li><li>the child stacking contexts with negative stack levels (most negative first).</li><li>the in-flow, non-inline-level, non-positioned descendants.</li><li>the non-positioned floats.</li><li>the in-flow, inline-level, non-positioned descendants, including inline tables andinline blocks.</li><li>the child stacking contexts with stack level 0 and the positioned descendants withstack level 0.</li><li>the child stacking contexts with positive stack levels (least positive first).</li></ol>
<p>Within each stacking context, positioned elements with stack level 0 (in layer 6),non-positioned floats (layer 4), inline blocks (layer 5), and inline tables (layer 5),are painted as if those elements themselves generated new stacking contexts, except thattheir positioned descendants and any would-be child stacking contexts take part in thecurrent stacking context.</p>
<p>This painting order is applied recursively to each stacking context.</p>
<h4 id="elaborate-stacking-contexts">Appendix E.2 Painting order</h4>
<p>The bottom of the stack is the furthest from the user, the top of the stack is thenearest. The stacking context of the root element is the root stacking context. [...]</p>
<p>When traversing the tree, each stacking context is painted as follows:</p><ol><li>If the element is a root element or an element that establishes a stacking context:paint its background color and background image, then its border.</li><li>Stacking contexts formed by positioned descendants with negative z-indices (excluding0) in z-index order (most negative first) then tree order.</li><li>For all its in-flow, non-positioned, block-level descendants in tree order: paintthe background color, background image and border of the element [and, for each,continue with its own in-flow, non-positioned, block-level descendants in tree order].</li><li>All non-positioned floating descendants, in tree order. (not exercised: this seam'scorpus has no floats.)</li><li>[Inline-level content. (not exercised: this seam's corpus has no inline content.)]</li><li>All positioned, opacity or transform descendants, in tree order that fall into thefollowing categories:<ol><li>All positioned descendants with 'z-index: auto' or 'z-index: 0', in tree order. Forthose with 'z-index: auto', treat the element as if it created a new stacking context,but any positioned descendants and descendants which actually create a new stackingcontext should be considered part of the parent stacking context, not this new one. Forthose with 'z-index: 0' treat the stacking context generated atomically.</li><li>All opacity descendants with opacity less than 1, in tree order, create a stackingcontext generated atomically.</li><li>[Transform descendants (not exercised: this seam's corpus has no transforms).]</li></ol></li><li>Stacking contexts formed by positioned descendants with z-indices greater than orequal to 1 in z-index order (smallest first) then tree order.</li></ol>
<p>[A 'z-index' declared on a box that is not positioned has no effect: the propertyapplies to positioned elements only, so such a box is an ordinary in-flow descendant ofits stacking context, painted in step 3 in tree order.]</p>
<h4 id="transparency">CSS Color Level 3, 3.2 Transparency: the 'opacity' property</h4>
<p>Since elements with opacity less than 1 are composited from a single offscreen image,content outside of it cannot be layered in z-order between pieces of content inside ofit. For the same reason, implementations must create a new stacking context for anyelement with opacity less than 1. If an element with opacity less than 1 is notpositioned, implementations must paint the layer it creates, within its parent stackingcontext, at the same stacking order that would be used if it were a positioned elementwith 'z-index: 0' and 'opacity: 1'. If an element with opacity less than 1 is positioned,the 'z-index' property applies as described in [CSS21], except that 'auto' is treated as'0' since a new stacking context is always created.</p>
<wpt><file>css/CSS2/zindex/z-index-001.html</file><file>css/CSS2/zindex/z-index-negative-001.html</file></wpt>
<h4>Changes</h4><p>Folded Appendix E.2 and CSS Color 3 §3.2 in beside 9.9.1 rather than pulling in thewhole of each (E.2 also covers floats, inline content, tables and transforms, none ofwhich this seam's corpus reaches); bracketed prose stands in for sentences this excerptdoes not quote verbatim. No other normative effect.</p>"#;
/// Abbreviated stand-ins for the real definition text a webref `curated` fetch would/// supply, mirroring `provenance::css21_absolute_positioning::Fixture`'s shape.struct Definition { id: &'static str, text: &'static str, references: &'static [&'static str],}
const DEFINITIONS: &[Definition] = &[ Definition { id: "stacking-context", text: "A stacking context is atomic from the point of view of its parent stacking \ context; boxes in other stacking contexts may not come between any of its \ boxes.", references: &["stack-level"], }, Definition { id: "stack-level", text: "Each positioned box in a given stacking context has an integer stack level, \ which is its position on the z-axis relative to other stack levels within \ the same stacking context.", references: &["positioned-element"], }, Definition { id: "positioned-element", text: "An element is said to be positioned if its 'position' property has a value \ other than 'static'.", references: &[], }, Definition { id: "tree-order", text: "Document tree order: a pre-order, depth-first traversal of the document \ tree, parent before children, earlier siblings before later ones.", references: &[], }, Definition { id: "in-flow", text: "An element is called out of flow if it is floated, absolutely positioned, \ or is the root element. An element is called in-flow if it is not \ out-of-flow.", references: &["positioned-element"], },];
fn find(id: &str) -> Option<&'static Definition> { DEFINITIONS.iter().find(|d| d.id == id)}
/// The `DefinitionIndex` for CSS 2.1 §9.9.1 / Appendix E.2's stacking rules.pub struct Fixture;
impl DefinitionIndex for Fixture { fn references(&self, section_id: &str) -> Vec<String> { if section_id == SECTION_ID { vec![ "stacking-context".to_string(), "tree-order".to_string(), "in-flow".to_string(), ] } else { Vec::new() } }
fn definition_references(&self, definition_id: &str) -> Vec<String> { find(definition_id) .map(|d| d.references.iter().map(|s| s.to_string()).collect()) .unwrap_or_default() }
fn definition_text(&self, definition_id: &str) -> Option<String> { find(definition_id).map(|d| d.text.to_string()) }}
#[cfg(test)]mod tests { use super::*; use provenance::{closure_for, closure_hash, normalize_spec_text};
#[test] fn closure_reaches_the_transitive_definitions() { let closure = closure_for(SECTION_ID, &Fixture); assert_eq!( closure, std::collections::BTreeSet::from([ "stacking-context".to_string(), "stack-level".to_string(), "positioned-element".to_string(), "tree-order".to_string(), "in-flow".to_string(), ]) ); }
#[test] fn normalized_spec_text_drops_synthetic_wpt_and_changes() { let normalized = normalize_spec_text(SPEC_TEXT); assert!(!normalized.contains("z-index-001.html")); assert!(!normalized.contains("Folded Appendix E.2")); assert!(normalized.contains("most negative first")); assert!(normalized.contains("opacity less than 1")); }
#[test] fn definition_closure_hash_is_deterministic() { let closure = closure_for(SECTION_ID, &Fixture); assert_eq!(closure_hash(&closure, &Fixture), closure_hash(&closure, &Fixture)); }}