Harvest inline <style> CSS from the live DOM for Baseline feature detection - #330
Open
smsnedden wants to merge 3 commits into
Open
Harvest inline <style> CSS from the live DOM for Baseline feature detection#330smsnedden wants to merge 3 commits into
smsnedden wants to merge 3 commits into
Conversation
…ection Add harvestInlineStyles() which reads inline <style> blocks from a page's live DOM and returns them as CSSSource[], the same shape produced by extractCSSFromHar(). Reading the rendered document rather than parsing the HTML captures <style> blocks injected at runtime by JavaScript, which static parsing would miss. The read is strictly read-only (no DOM mutation, no layout), so a later change can run it after metrics collection without perturbing performance measurements. This is the second slice of the Baseline (--baseline) CSS analysis feature; wiring it into the test run and CSS feature detection over the collected sources follow in later changes. - Add harvestInlineStyles() in baselineCssExtract.ts and update the extractCSSFromHar() doc to reference it - Document CSSSource.file as a provenance label (URL for external stylesheets, page-derived label for inline blocks) and css as the parsed field - Add engine-parameterized tests (chromium/firefox/webkit) covering static and JS-injected <style> blocks, document order, empty-block skipping, and a read-only (no DOM mutation) guarantee
The harvest will run inside the Baseline detection pass (alongside JS API detection), not the performance run, so tie the read-only guarantee to not interfering with co-located instrumentation rather than to metrics safety.
The 'does not modify the DOM' test still described the read-only guarantee in terms of metrics collection; match the reframed harvestInlineStyles JSDoc so the rationale is consistent across source and tests.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Second slice of the
--baselineCSS analysis feature. This PR adds extraction of inline<style>blocks by reading them from a page's live DOM; CSS feature detection (css-tree) and Baseline lookup (web-features) over the collected sources follow in later PRs. (External stylesheets landed in #317.)Like the external extractor,
harvestInlineStylesis intentionally not wired into the CLI orlaunchTestyet. It is designed to be driven by the Baseline detection pass — a live second-pass browser navigation (the same pass that performs JS API detection) — which is productionized in a follow-up PR. Reading inline CSS there keeps all live-DOM detection in one place and one navigation, and runs it once: Baseline analyzes browser-agnostic source CSS, so there is no need to repeat it across the browser matrix. Keeping this slice separate keeps each PR small and reviewable.Why read the live DOM instead of parsing the HTML? So that
<style>blocks a page injects at runtime (e.g. via JavaScript) are captured — static HTML parsing would miss them. The browser is already running in the detection pass, so it is the ground truth for what the page actually applied.Changes
harvestInlineStyles(page): Promise<CSSSource[]>(src/baselineCssExtract.ts)Reads inline CSS from a loaded page's live DOM:
<style>element in document order, returning oneCSSSourceper non-empty block (the same{ css, file }shape asextractCSSFromHar).page.evaluatethat reads<style>text with no DOM mutation and no layout-triggering reads — so it can run inside the shared detection pass without interfering with co-located instrumentation or page state.Out of scope (documented in the JSDoc; may be revisited later): only
<style>element text is read, so CSS with no<style>text — rules added viaCSSStyleSheet.insertRule()or constructableadoptedStyleSheets— is not captured, nor are<style>elements inside shadow roots (querySelectorAlldoes not pierce shadow DOM) or<template>content.Types (
src/types.ts)CSSSource:cssis the field that gets parsed;fileis a provenance label for reporting — a URL for external stylesheets, a page-derived label (e.g.https://example.com/ (inline style #1)) for inline blocks. It is a display string, not a URL to parse.Tests
Extends
packages/telescope/__tests__/baselineCssExtract.test.tswith 6 tests parameterized per rendering engine (chromium/firefox/webkit; Firefox-only in CI), driving a real page viasetContent:<style>block extracted as one source<style>injected by JavaScript after load is captured (the case static parsing misses)<style>blocks → empty array