-
Notifications
You must be signed in to change notification settings - Fork 7.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: add a safe getComputedStyle to videojs. #3664
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
/** | ||
* A safe getComputedStyle with an IE8 fallback. | ||
* | ||
* This is because in Firefox, if the player is loaded in an iframe with `display:none`, | ||
* then `getComputedStyle` returns `null`, so, we do a null-check to make sure | ||
* that the player doesn't break in these cases. | ||
* See https://bugzilla.mozilla.org/show_bug.cgi?id=548397 for more details. | ||
* | ||
* @function getComputedStyle | ||
* @param el the element you want the computed style of | ||
* @param prop the property name you want | ||
*/ | ||
export default function getComputedStyle(el, prop) { | ||
if (typeof getComputedStyle === 'function') { | ||
const cs = getComputedStyle(el); | ||
|
||
return cs ? cs[prop] : ''; | ||
} | ||
|
||
return el.currentStyle[prop]; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,6 +25,7 @@ import log from './utils/log.js'; | |
import * as Dom from './utils/dom.js'; | ||
import * as browser from './utils/browser.js'; | ||
import * as Url from './utils/url.js'; | ||
import computedStyle from './utils/get-computed-style.js'; | ||
import extendFn from './extend.js'; | ||
import merge from 'lodash-compat/object/merge'; | ||
import xhr from 'xhr'; | ||
|
@@ -719,6 +720,20 @@ videojs.appendContent = Dom.appendContent; | |
*/ | ||
videojs.insertContent = Dom.insertContent; | ||
|
||
/** | ||
* A safe getComputedStyle with an IE8 fallback. | ||
* | ||
* This is because in Firefox, if the player is loaded in an iframe with `display:none`, | ||
* then `getComputedStyle` returns `null`, so, we do a null-check to make sure | ||
* that the player doesn't break in these cases. | ||
* See https://bugzilla.mozilla.org/show_bug.cgi?id=548397 for more details. | ||
* | ||
* @function getComputedStyle | ||
* @param el the element you want the computed style of | ||
* @param prop the property name you want | ||
*/ | ||
videojs.getComputedStyle = computedStyle; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. do we want to call it There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it should be There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'll do that. |
||
|
||
/* | ||
* Custom Universal Module Definition (UMD) | ||
* | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
seems like we may want to check for el or prop being null here and return empty sting if that happens.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also, shouldn't this be
window.getComputedStyle
?I think this'll detect the module-level function instead of the global one.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, it'll be safer to do
window.
.I guess checking that
el
andprop
are available is good. I'll refrain from doing other type checking, though.