Skip to content

Commit

Permalink
feat(UI): Remove copyStyleSheets (#5273)
Browse files Browse the repository at this point in the history
Document Picture-in-Picture API owners are [removing `copyStyleSheets` option](WICG/document-picture-in-picture#79) as it can be done in JavaScript.
This PR removes this option and adds code to copy the style sheets of the PiP window.
  • Loading branch information
beaufortfrancois authored Jun 12, 2023
1 parent bb2c06a commit fe43ed3
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
1 change: 0 additions & 1 deletion externs/pictureinpicture.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ HTMLMediaElement.prototype.webkitPresentationMode;
* @typedef {{
* width: (number|undefined),
* height: (number|undefined),
* copyStyleSheets: (boolean|undefined),
* }}
*/
var DocumentPictureInPictureOptions;
Expand Down
31 changes: 30 additions & 1 deletion ui/pip_button.js
Original file line number Diff line number Diff line change
Expand Up @@ -164,9 +164,11 @@ shaka.ui.PipButton = class extends shaka.ui.Element {
const pipWindow = await window.documentPictureInPicture.requestWindow({
width: rectPipPlayer.width,
height: rectPipPlayer.height,
copyStyleSheets: true,
});

// Copy style sheets to the Picture-in-Picture window.
this.copyStyleSheetsToWindow_(pipWindow);

// Add placeholder for the player.
const parentPlayer = pipPlayer.parentNode || document.body;
const placeholder = this.videoContainer_.cloneNode(true);
Expand All @@ -190,6 +192,33 @@ shaka.ui.PipButton = class extends shaka.ui.Element {
});
}

/** @private */
copyStyleSheetsToWindow_(win) {
const styleSheets = /** @type {!Iterable<*>} */(document.styleSheets);
const allCSS = [...styleSheets]
.map((sheet) => {
try {
return [...sheet.cssRules].map((rule) => rule.cssText).join('');
} catch (e) {
const link = /** @type {!HTMLLinkElement} */(
document.createElement('link'));

link.rel = 'stylesheet';
link.type = sheet.type;
link.media = sheet.media;
link.href = sheet.href;
win.document.head.appendChild(link);
}
return '';
})
.filter(Boolean)
.join('\n');
const style = document.createElement('style');

style.textContent = allCSS;
win.document.head.appendChild(style);
}

/** @private */
onEnterPictureInPicture_() {
const LocIds = shaka.ui.Locales.Ids;
Expand Down

0 comments on commit fe43ed3

Please sign in to comment.