Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
},
"homepage": "https://testplane.io/",
"engines": {
"node": ">= 18.0.0"
"node": ">= 18.17.0"

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

URL.canParse

},
"keywords": [
"testplane",
Expand Down
7 changes: 6 additions & 1 deletion src/browser/cdp/selectivity/css-selectivity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ export class CSSSelectivity {
return;
}

if (!sourceURL || !sourceMapURL || sourceURL.startsWith("chrome-error://")) {
if (!sourceMapURL || sourceURL.startsWith("chrome-error://")) {
this._stylesSourceMap[styleSheetId] ||= null;
return;
}
Expand All @@ -73,6 +73,11 @@ export class CSSSelectivity {

let sourceMapResolvedUrl = urlResolve(sourceURL, sourceMapURL);

if (!URL.canParse(sourceMapResolvedUrl)) {

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of checking "is there any base url" we see "if it resolved to valid url" (no source url + embedded sourcemap = valid)

this._stylesSourceMap[styleSheetId] ||= null;
return;
}

const mapResult = this._mapSourceMapUrl
? this._mapSourceMapUrl({ type: "css", sourceUrl: sourceURL, sourceMapUrl: sourceMapResolvedUrl })
: true;
Expand Down
56 changes: 56 additions & 0 deletions test/src/browser/cdp/selectivity/css-selectivity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ describe("CDP/Selectivity/CSSSelectivity", () => {
let pathStub: { posix: { join: SinonStub } };
let hasProtocolStub: SinonStub;
let isDataProtocolStub: SinonStub;
let canParseStub: SinonStub;
let debugSelectivityStub: SinonStub;

const CacheType = { Asset: "a", CssSessionCache: "cs" };
Expand Down Expand Up @@ -99,6 +100,9 @@ describe("CDP/Selectivity/CSSSelectivity", () => {
};
hasProtocolStub = sandbox.stub().returns(false);
isDataProtocolStub = sandbox.stub().callsFake((url: string) => url.startsWith("data:"));
// In production "urlResolve" yields an absolute (parseable) url; the stub above returns a relative one,
// so we stub the global "URL.canParse" to mirror the production "parseable" outcome by default.
canParseStub = sandbox.stub(URL, "canParse").returns(true);
debugSelectivityStub = sandbox.stub();

getCachedSelectivityFileStub = sandbox.stub().resolves(JSON.stringify(mockSourceMap));
Expand Down Expand Up @@ -1174,4 +1178,56 @@ describe("CDP/Selectivity/CSSSelectivity", () => {
});
});
});

describe("anonymous stylesheets with embedded source maps", () => {
const embeddedSourceMapUrl = "data:application/json;base64,eyJ2ZXJzaW9uIjozfQ==";
const anonymousStyleSheetEvent = {
header: {
...styleSheetEvent.header,
sourceURL: "", // anonymous stylesheet (e.g. injected <style>)
sourceMapURL: embeddedSourceMapUrl,
},
};

it("should fetch the embedded source map for a stylesheet without a sourceURL", async () => {
patchSourceMapSourcesStub.returns({ sources: ["src/styles.css"], sourceRoot: "/root" });
cdpMock.css.stopRuleUsageTracking.resolves({
ruleUsage: [{ styleSheetId: "stylesheet-123", startOffset: 0, endOffset: 100, used: true }],
});

const cssSelectivity = new CSSSelectivity(cdpMock as any, sessionId, wdSessionId, sourceRoot, null);

await cssSelectivity.start();

const styleSheetAddedHandler = cdpMock.css.on.getCall(0).args[1];
styleSheetAddedHandler(anonymousStyleSheetEvent, sessionId);

const result = await cssSelectivity.stop();

// Embedded (data:) source maps are fetched directly and never offloaded to fs-cache
assert.calledWith(fetchTextWithBrowserFallbackStub, embeddedSourceMapUrl, cdpMock.runtime, sessionId);
assert.neverCalledWith(hasCachedSelectivityFileStub, CacheType.Asset, embeddedSourceMapUrl);
assert.neverCalledWith(setCachedSelectivityFileStub, CacheType.Asset, embeddedSourceMapUrl);
assert.deepEqual(Array.from(result || []).sort(), ["/root/src/styles.css"]);
});

it("should skip the stylesheet when the resolved source map url is not parseable", async () => {
canParseStub.returns(false);
cdpMock.css.stopRuleUsageTracking.resolves({
ruleUsage: [{ styleSheetId: "stylesheet-123", startOffset: 0, endOffset: 100, used: true }],
});

const cssSelectivity = new CSSSelectivity(cdpMock as any, sessionId, wdSessionId, sourceRoot, null);

await cssSelectivity.start();

const styleSheetAddedHandler = cdpMock.css.on.getCall(0).args[1];
styleSheetAddedHandler(styleSheetEvent, sessionId);

const result = await cssSelectivity.stop();

assert.notCalled(fetchTextWithBrowserFallbackStub);
assert.deepEqual(Array.from(result || []), []);
});
});
});
Loading