Skip to content
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

report(html): replace Typed OM getComputedStyle() with CSSOM equivalent #5984

Merged
merged 6 commits into from
Sep 8, 2018
Merged

report(html): replace Typed OM getComputedStyle() with CSSOM equivalent #5984

merged 6 commits into from
Sep 8, 2018

Conversation

midzer
Copy link
Contributor

@midzer midzer commented Sep 7, 2018

Summary

Replace Typed OM's element.getComputedStyle() with CSSOM window.getComputedStyle()

Typed OM is not widely supported yet. Our delivered JS should work properly in all major browsers

https://developers.google.com/web/updates/2018/03/cssom#computed
Related Issues/PRs

fixes #5966

Yeah, this replacement looks a bit hacky. But I've not found a more elegant way

@@ -130,17 +130,21 @@ class ReportUIFeatures {

_setupHeaderAnimation() {
const scoresWrapper = this._dom.find('.lh-scores-wrapper', this._document);
this.headerOverlap = /** @type {number} */
(Number(window.getComputedStyle(scoresWrapper).marginTop.replace('px', '')));
const computedMarginTop = scoresWrapper.style.marginTop ?
Copy link
Member

Choose a reason for hiding this comment

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

computedStyle should get you the value even if an inline style is present so i'm not sure why this is necessary. whatcha thinkin?

const computedMarginTop = scoresWrapper.style.marginTop ?
window.getComputedStyle(scoresWrapper).marginTop : '0px';
this.headerOverlap = computedMarginTop ?
/** @type {number} */ (Number(computedMarginTop.replace('px', ''))) : 0;
Copy link
Member

Choose a reason for hiding this comment

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

I know you're copying what was here but parseFloat(computedMarginTop) should do the same. (unless there's some tsc thing i'm not thinking of).
could even do parseFloat(computedMarginTop|| 0) to drop your ternary?

this.headerHeight = /** @type {number} */
(Number(window.getComputedStyle(this.headerBackground).height.replace('px', '')));
const computedHeight = this.headerBackground.style.height ?
window.getComputedStyle(this.headerBackground).height : '0px';
Copy link
Member

Choose a reason for hiding this comment

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

2 above comments apply here too.

@midzer
Copy link
Contributor Author

midzer commented Sep 7, 2018

I appreciate your feedback @paulirish
Now patch shouldn't look like a workaround any more

Copy link
Member

@paulirish paulirish left a comment

Choose a reason for hiding this comment

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

lg! thx

Copy link
Member

@brendankenny brendankenny left a comment

Choose a reason for hiding this comment

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

Last feedback :)

(scoresWrapper.computedStyleMap().get('margin-top').value);

const computedMarginTop = window.getComputedStyle(scoresWrapper).marginTop;
this.headerOverlap = /** @type {number} */ (parseFloat(computedMarginTop || '0'));
Copy link
Member

Choose a reason for hiding this comment

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

parseFloat returns a number, so no longer need to cast

// @ts-ignore - TODO: move off CSSOM to support other browsers
this.headerHeight = this.headerBackground.computedStyleMap().get('height').value;
const computedHeight = window.getComputedStyle(this.headerBackground).height;
this.headerHeight = /** @type {number} */ (parseFloat(computedHeight || '0'));
Copy link
Member

Choose a reason for hiding this comment

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

Same

Copy link
Member

@brendankenny brendankenny left a comment

Choose a reason for hiding this comment

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

Looks great!

@paulirish paulirish merged commit 942cc02 into GoogleChrome:master Sep 8, 2018
@midzer midzer deleted the replaceTypedOmFunction branch September 8, 2018 02:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Report: "e.computedStyleMap is not a function" error when viewing in Safari
3 participants