diff --git a/docs/api/puppeteer.page.addstyletag_2.md b/docs/api/puppeteer.page.addstyletag_2.md deleted file mode 100644 index 031fccd96d8..00000000000 --- a/docs/api/puppeteer.page.addstyletag_2.md +++ /dev/null @@ -1,25 +0,0 @@ ---- -sidebar_label: Page.addStyleTag_2 ---- - -# Page.addStyleTag() method - -#### Signature: - -```typescript -class Page { - addStyleTag( - options: FrameAddStyleTagOptions - ): Promise>; -} -``` - -## Parameters - -| Parameter | Type | Description | -| --------- | ----------------------------------------------------------------- | ----------- | -| options | [FrameAddStyleTagOptions](./puppeteer.frameaddstyletagoptions.md) | | - -**Returns:** - -Promise<[ElementHandle](./puppeteer.elementhandle.md)<HTMLStyleElement \| HTMLLinkElement>> diff --git a/docs/api/puppeteer.page.md b/docs/api/puppeteer.page.md index a8fcc26286a..cf3725e7fa3 100644 --- a/docs/api/puppeteer.page.md +++ b/docs/api/puppeteer.page.md @@ -84,7 +84,6 @@ page.off('request', logRequest); | [addScriptTag(options)](./puppeteer.page.addscripttag.md) | | Adds a <script> tag into the page with the desired URL or content. | | [addStyleTag(options)](./puppeteer.page.addstyletag.md) | |

Adds a <link rel="stylesheet"> tag into the page with the desired URL or a <style type="text/css"> tag with the content.

Shortcut for [page.mainFrame().addStyleTag(options)](./puppeteer.frame.addstyletag_1.md).

| | [addStyleTag(options)](./puppeteer.page.addstyletag_1.md) | | | -| [addStyleTag(options)](./puppeteer.page.addstyletag_2.md) | | | | [authenticate(credentials)](./puppeteer.page.authenticate.md) | | Provide credentials for HTTP authentication. | | [bringToFront()](./puppeteer.page.bringtofront.md) | | Brings page to front (activates tab). | | [browser()](./puppeteer.page.browser.md) | | Get the browser the page belongs to. | diff --git a/packages/puppeteer-core/src/api/Frame.ts b/packages/puppeteer-core/src/api/Frame.ts index 03b330100cc..f50c8f87ff2 100644 --- a/packages/puppeteer-core/src/api/Frame.ts +++ b/packages/puppeteer-core/src/api/Frame.ts @@ -25,6 +25,7 @@ import { IsolatedWorldChart, WaitForSelectorOptions, } from '../common/IsolatedWorld.js'; +import {LazyArg} from '../common/LazyArg.js'; import {PuppeteerLifeCycleEvent} from '../common/LifecycleWatcher.js'; import { EvaluateFunc, @@ -33,6 +34,7 @@ import { InnerLazyParams, NodeFor, } from '../common/types.js'; +import {importFSPromises} from '../common/util.js'; import {TaskManager} from '../common/WaitTask.js'; import {KeyboardTypeOptions} from './Input.js'; @@ -780,10 +782,67 @@ export class Frame { async addStyleTag( options: FrameAddStyleTagOptions ): Promise>; - async addStyleTag(): Promise< - ElementHandle - > { - throw new Error('Not implemented'); + async addStyleTag( + options: FrameAddStyleTagOptions + ): Promise> { + let {content = ''} = options; + const {path} = options; + if (+!!options.url + +!!path + +!!content !== 1) { + throw new Error( + 'Exactly one of `url`, `path`, or `content` must be specified.' + ); + } + + if (path) { + const fs = await importFSPromises(); + + content = await fs.readFile(path, 'utf8'); + content += '/*# sourceURL=' + path.replace(/\n/g, '') + '*/'; + options.content = content; + } + + return this.mainRealm().transferHandle( + await this.isolatedRealm().evaluateHandle( + async ({Deferred}, {url, content}) => { + const deferred = Deferred.create(); + let element: HTMLStyleElement | HTMLLinkElement; + if (!url) { + element = document.createElement('style'); + element.appendChild(document.createTextNode(content!)); + } else { + const link = document.createElement('link'); + link.rel = 'stylesheet'; + link.href = url; + element = link; + } + element.addEventListener( + 'load', + () => { + deferred.resolve(); + }, + {once: true} + ); + element.addEventListener( + 'error', + event => { + deferred.reject( + new Error( + (event as ErrorEvent).message ?? 'Could not load style' + ) + ); + }, + {once: true} + ); + document.head.appendChild(element); + await deferred.valueOrThrow(); + return element; + }, + LazyArg.create(context => { + return context.puppeteerUtil; + }), + options + ) + ); } /** diff --git a/packages/puppeteer-core/src/api/Page.ts b/packages/puppeteer-core/src/api/Page.ts index 6223b880bcf..8e1fd58f054 100644 --- a/packages/puppeteer-core/src/api/Page.ts +++ b/packages/puppeteer-core/src/api/Page.ts @@ -1206,11 +1206,8 @@ export class Page extends EventEmitter { ): Promise>; async addStyleTag( options: FrameAddStyleTagOptions - ): Promise>; - async addStyleTag(): Promise< - ElementHandle - > { - throw new Error('Not implemented'); + ): Promise> { + return this.mainFrame().addStyleTag(options); } /** diff --git a/packages/puppeteer-core/src/common/Frame.ts b/packages/puppeteer-core/src/common/Frame.ts index 296f70541b8..0d22302994a 100644 --- a/packages/puppeteer-core/src/common/Frame.ts +++ b/packages/puppeteer-core/src/common/Frame.ts @@ -17,11 +17,7 @@ import {Protocol} from 'devtools-protocol'; import {ElementHandle} from '../api/ElementHandle.js'; -import { - Frame as BaseFrame, - FrameAddScriptTagOptions, - FrameAddStyleTagOptions, -} from '../api/Frame.js'; +import {Frame as BaseFrame, FrameAddScriptTagOptions} from '../api/Frame.js'; import {HTTPResponse} from '../api/HTTPResponse.js'; import {Page, WaitTimeoutOptions} from '../api/Page.js'; import {assert} from '../util/assert.js'; @@ -401,75 +397,6 @@ export class Frame extends BaseFrame { ); } - override async addStyleTag( - options: Omit - ): Promise>; - override async addStyleTag( - options: FrameAddStyleTagOptions - ): Promise>; - override async addStyleTag( - options: FrameAddStyleTagOptions - ): Promise> { - let {content = ''} = options; - const {path} = options; - if (+!!options.url + +!!path + +!!content !== 1) { - throw new Error( - 'Exactly one of `url`, `path`, or `content` must be specified.' - ); - } - - if (path) { - const fs = await importFSPromises(); - - content = await fs.readFile(path, 'utf8'); - content += '/*# sourceURL=' + path.replace(/\n/g, '') + '*/'; - options.content = content; - } - - return this.mainRealm().transferHandle( - await this.isolatedRealm().evaluateHandle( - async ({Deferred}, {url, content}) => { - const deferred = Deferred.create(); - let element: HTMLStyleElement | HTMLLinkElement; - if (!url) { - element = document.createElement('style'); - element.appendChild(document.createTextNode(content!)); - } else { - const link = document.createElement('link'); - link.rel = 'stylesheet'; - link.href = url; - element = link; - } - element.addEventListener( - 'load', - () => { - deferred.resolve(); - }, - {once: true} - ); - element.addEventListener( - 'error', - event => { - deferred.reject( - new Error( - (event as ErrorEvent).message ?? 'Could not load style' - ) - ); - }, - {once: true} - ); - document.head.appendChild(element); - await deferred.valueOrThrow(); - return element; - }, - LazyArg.create(context => { - return context.puppeteerUtil; - }), - options - ) - ); - } - override async title(): Promise { return this.isolatedRealm().title(); } diff --git a/packages/puppeteer-core/src/common/Page.ts b/packages/puppeteer-core/src/common/Page.ts index 6adc2c6c32a..5397143cbfd 100644 --- a/packages/puppeteer-core/src/common/Page.ts +++ b/packages/puppeteer-core/src/common/Page.ts @@ -21,11 +21,7 @@ import {Protocol} from 'devtools-protocol'; import type {Browser} from '../api/Browser.js'; import type {BrowserContext} from '../api/BrowserContext.js'; import {ElementHandle} from '../api/ElementHandle.js'; -import { - Frame, - FrameAddScriptTagOptions, - FrameAddStyleTagOptions, -} from '../api/Frame.js'; +import {Frame, FrameAddScriptTagOptions} from '../api/Frame.js'; import {HTTPRequest} from '../api/HTTPRequest.js'; import {HTTPResponse} from '../api/HTTPResponse.js'; import {JSHandle} from '../api/JSHandle.js'; @@ -589,18 +585,6 @@ export class CDPPage extends Page { return this.mainFrame().addScriptTag(options); } - override async addStyleTag( - options: Omit - ): Promise>; - override async addStyleTag( - options: FrameAddStyleTagOptions - ): Promise>; - override async addStyleTag( - options: FrameAddStyleTagOptions - ): Promise> { - return this.mainFrame().addStyleTag(options); - } - override async exposeFunction( name: string, pptrFunction: Function | {default: Function} diff --git a/test/TestExpectations.json b/test/TestExpectations.json index f8ed847696f..609b2e9f338 100644 --- a/test/TestExpectations.json +++ b/test/TestExpectations.json @@ -173,6 +173,12 @@ "parameters": ["webDriverBiDi"], "expectations": ["FAIL", "TIMEOUT"] }, + { + "testIdPattern": "[page.spec] Page Page.addStyleTag *", + "platforms": ["darwin", "linux", "win32"], + "parameters": ["webDriverBiDi"], + "expectations": ["PASS"] + }, { "testIdPattern": "[page.spec] Page Page.browser *", "platforms": ["darwin", "linux", "win32"], @@ -1124,14 +1130,8 @@ { "testIdPattern": "[page.spec] Page Page.addStyleTag should throw when added with content to the CSP page", "platforms": ["darwin", "linux", "win32"], - "parameters": ["webDriverBiDi"], - "expectations": ["PASS"] - }, - { - "testIdPattern": "[page.spec] Page Page.addStyleTag should throw when added with URL to the CSP page", - "platforms": ["darwin", "linux", "win32"], - "parameters": ["webDriverBiDi"], - "expectations": ["PASS"] + "parameters": ["firefox"], + "expectations": ["SKIP"] }, { "testIdPattern": "[page.spec] Page Page.close should *not* run beforeunload by default", @@ -1763,12 +1763,6 @@ "parameters": ["cdp", "firefox"], "expectations": ["FAIL"] }, - { - "testIdPattern": "[coverage.spec] Coverage specs CSSCoverage should ignore injected stylesheets", - "platforms": ["darwin", "linux", "win32"], - "parameters": ["chrome", "webDriverBiDi"], - "expectations": ["FAIL"] - }, { "testIdPattern": "[coverage.spec] Coverage specs CSSCoverage should work with complicated usecases", "platforms": ["darwin", "linux", "win32"], @@ -3077,18 +3071,6 @@ "parameters": ["cdp", "firefox"], "expectations": ["SKIP"] }, - { - "testIdPattern": "[page.spec] Page Page.addStyleTag should throw an error if loading from url fail", - "platforms": ["darwin", "linux", "win32"], - "parameters": ["firefox", "webDriverBiDi"], - "expectations": ["PASS"] - }, - { - "testIdPattern": "[page.spec] Page Page.addStyleTag should throw when added with content to the CSP page", - "platforms": ["darwin", "linux", "win32"], - "parameters": ["cdp", "firefox"], - "expectations": ["SKIP"] - }, { "testIdPattern": "[page.spec] Page Page.close should not be visible in browser.pages", "platforms": ["darwin", "linux", "win32"],