Repository: servo/servo
File: components/script/dom/window.rs (Rust)

Code before:
        // >  - the document has pending stylesheet requests
        // >  - the document has pending layout operations which might cause the user agent to request
        // >    a font, or which depend on recently-loaded fonts
        //
        // Thus, we are queueing promise resolution here. This reflow should have been triggered by
        // a "rendering opportunity" in `ScriptThread::handle_web_font_loaded, which should also
        // make sure a microtask checkpoint happens, triggering the promise callback.
        if !pending_web_fonts && is_ready_state_complete {
            font_face_set.fulfill_ready_promise_if_needed();
        }

        // If writing a screenshot, check if the script has reached a state
        // where it's safe to write the image. This means that:
        // 1) The reflow is for display (otherwise it could be a query)
        // 2) The html element doesn't contain the 'reftest-wait' class
        // 3) The load event has fired.
        // When all these conditions are met, notify the constellation
        // that this pipeline is ready to write the image (from the script thread
        // perspective at least).
        if self.prepare_for_screenshot && for_display {
            // Checks if the html element has reftest-wait attribute present.
            // See http://testthewebforward.org/docs/reftests.html
            // and https://web-platform-tests.org/writing-tests/crashtest.html
            let html_element = document.GetDocumentElement();
            let reftest_wait = html_element.map_or(false, |elem| {
                elem.has_class(&atom!("reftest-wait"), CaseSensitivity::CaseSensitive) || elem.has_class(&atom!("test-wait"), CaseSensitivity::CaseSensitive)
            });

            let has_sent_idle_message = self.has_sent_idle_message.get();
            let pending_images = !self.pending_layout_images.borrow().is_empty();

            if !has_sent_idle_message &&
                is_ready_state_complete &&
                !reftest_wait &&
                !pending_images &&
                !pending_web_fonts
            {
                debug!(
                    "{:?}: Sending DocumentState::Idle to Constellation",
                    self.pipeline_id()
                );
                let event = ScriptMsg::SetDocumentState(DocumentState::Idle);
                self.send_to_constellation(event);
                self.has_sent_idle_message.set(true);
            }
        }

        issued_reflow
    }

    /// If writing a screenshot, synchronously update the layout epoch that it set

Review comment (security):
I could also duplicate this:
```
            let test_wait = html_element.map_or(false, |elem| {
                elem.has_class(&atom!("test-wait"), CaseSensitivity::CaseSensitive)
            });
            ```

Code after:
        // >  - the document has pending stylesheet requests
        // >  - the document has pending layout operations which might cause the user agent to request
        // >    a font, or which depend on recently-loaded fonts
        //
        // Thus, we are queueing promise resolution here. This reflow should have been triggered by
        // a "rendering opportunity" in `ScriptThread::handle_web_font_loaded, which should also
        // make sure a microtask checkpoint happens, triggering the promise callback.
        if !pending_web_fonts && is_ready_state_complete {
            font_face_set.fulfill_ready_promise_if_needed();
        }

        // If writing a screenshot, check if the script has reached a state
        // where it's safe to write the image. This means that:
        // 1) The reflow is for display (otherwise it could be a query)
        // 2) The html element doesn't contain the 'reftest-wait' class
        // 3) The load event has fired.
        // When all these conditions are met, notify the constellation
        // that this pipeline is ready to write the image (from the script thread
        // perspective at least).
        if self.prepare_for_screenshot && for_display {
            // Checks if the html element has reftest-wait attribute present.
            // See http://testthewebforward.org/docs/reftests.html
            // and https://web-platform-tests.org/writing-tests/crashtest.html
            let html_element = document.GetDocumentElement();
            let reftest_wait = html_element.map_or(false, |elem| {
                elem.has_class(&atom!("reftest-wait"), CaseSensitivity::CaseSensitive) ||
                    elem.has_class(&Atom::from("test-wait"), CaseSensitivity::CaseSensitive)
            });

            let has_sent_idle_message = self.has_sent_idle_message.get();
            let pending_images = !self.pending_layout_images.borrow().is_empty();

            if !has_sent_idle_message &&
                is_ready_state_complete &&
                !reftest_wait &&
                !pending_images &&
                !pending_web_fonts
            {
                debug!(
                    "{:?}: Sending DocumentState::Idle to Constellation",
                    self.pipeline_id()
                );
                let event = ScriptMsg::SetDocumentState(DocumentState::Idle);
                self.send_to_constellation(event);
                self.has_sent_idle_message.set(true);
            }
        }

        issued_reflow
    }