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

Provide DOMRect implementation for IE11 #6666

Closed
wants to merge 8 commits into from

Conversation

brandonpayton
Copy link
Member

@brandonpayton brandonpayton commented May 9, 2018

Description

This PR creates a space for maintaining our own polyfills and uses it to provide a DOMRect polyfill. In the spirit of open source, this same polyfill has been submitted to the Financial Times' polyfill repository, but we need to maintain a local copy until it is merged and can be included as a vendor script.

Fixes #7036.

How has this been tested?

  • Ran unit tests.
  • Loaded Gutenberg in IE11 and noted that there is no longer a DOMRect-related error when clicking in a paragraph block to focus.
  • Loaded Gutenberg in Chrome, clicked in a paragraph block to focus, noted no error, and observed what appeared to be normal behavior.
  • Ran the build in Windows and macOS and confirmed polyfill copying to build/ works.

Checklist:

  • My code is tested.
  • My code follows the WordPress code style.
  • My code follows the accessibility standards.
  • My code has proper inline documentation.

@aduth
Copy link
Member

aduth commented May 9, 2018

This may no longer be necessary with #6467 which removes the DOMRect instantiation.

@brandonpayton
Copy link
Member Author

Thanks for letting me know. I'll hold onto this until that is merged.

@SymbolicallyMe
Copy link

SymbolicallyMe commented Jun 1, 2018

TinyMCE provides methods that contain various tools for rect/position calculation.

Reference: https://www.tinymce.com/docs/api/tinymce.geom/tinymce.geom.rect/

Example:

import tinymce from 'tinymce';

// Create a new DOMRect
return tinymce.geom.Rect.create( x, y, width, height );

@brandonpayton
Copy link
Member Author

brandonpayton commented Jun 1, 2018

Hi @SymbolicallyMe, I didn't know that, and it's good to know. For this PR, I just want a simple fix to stop an error in IE11. I was hoping that TinyMCE's Rect had the same interface as DOMRect, but that doesn't appear to be the case.

@aduth, despite our recent conversation about maintaining these sorts of things, what do you think of adding this as a stopgap while we await #6467? It's an implementation detail that could be removed later and seems better than IE11 throwing an error on new DOMRect. EDIT: Actually, it could be worse with a faulty DOMRect, but I think this implementation is good.

@aduth
Copy link
Member

aduth commented Jun 6, 2018

I'd be open to it, but do you think we could do it in a pattern more like what was used in #7033 to polyfill Node#contains ?

@brandonpayton
Copy link
Member Author

I'd prefer that but haven't found anything that is consumable à la carte via our polyfill mechanism. Actually, the only one I've found is also incorrect because it doesn't properly handle negative widths and heights. :) It's possible I'm not great at searching, but I have tried.

Honestly, this is simple enough, and I am frustrated by the absence of such a polyfill. Maybe I should just take a little time and submit it to FT's polyfill-service. We don't really want to own it, and it's easy to consume their polyfills via unpkg.

@aduth
Copy link
Member

aduth commented Jun 11, 2018

If there are no other options and the implementation is simple enough (like the one included already in these changes), it seems fine to maintain ourselves, at least near-term, in a manner for inclusion aligned with what we're doing with other polyfills (i.e. separate script enqueue).

Maybe I should just take a little time and submit it to FT's polyfill-service.

This would be reasonable too, also in the spirit of open source!

@brandonpayton
Copy link
Member Author

I submitted a PR to add DOMRect to the Financial Times' polyfill-service:
polyfillpolyfill/polyfill-service#1732

It took longer than I expected, but I am now more comfortable with their approach and will be able to contribute more easily in the future.

@danielbachhuber danielbachhuber added the [Status] Blocked Used to indicate that a current effort isn't able to move forward label Jun 27, 2018
@danielbachhuber
Copy link
Member

Flagging as [Status] Blocked per waiting on upstream PR merge.

@brandonpayton brandonpayton removed the [Status] Blocked Used to indicate that a current effort isn't able to move forward label Jul 13, 2018
@brandonpayton brandonpayton self-assigned this Jul 13, 2018
@brandonpayton
Copy link
Member Author

I updated this PR to use a local version of the polyfill submitted to the FT polyfill service. Since we don't control whether or when contributions are accepted and published, it makes sense that we need a place to maintain polyfills locally, and this PR adds one.

@aduth, are you up for taking another look at this?

Copy link
Member

@aduth aduth left a comment

Choose a reason for hiding this comment

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

I'm very understanding of the busy-ness factor, but it's not very inspiring to have a lack of even simple acknowledgement from the maintainers in over a month. I agree we should implement here. If we continue to have issues, we could consider publishing as a module proper as well.

Noted a few changes to consider.

@@ -186,6 +193,10 @@ function gutenberg_register_scripts_and_styles() {
'wp-dom',
gutenberg_get_script_polyfill( array(
'document.contains' => 'wp-polyfill-node-contains',
'"DOMRect" in this && ( function( DOMRect ) {' .
'try { return new DOMRect(); }' .
Copy link
Member

Choose a reason for hiding this comment

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

This relies on gutenberg_get_script_polyfill interpreting the result of the test as truthy, not explicitly true, since this will return an instance of DOMRect. While it may work as-is, I don't think it's a very strong guarantee. I might suggest simply changing this to new DOMRect(); return true;

@@ -186,6 +193,10 @@ function gutenberg_register_scripts_and_styles() {
'wp-dom',
gutenberg_get_script_polyfill( array(
'document.contains' => 'wp-polyfill-node-contains',
'"DOMRect" in this && ( function( DOMRect ) {' .
'try { return new DOMRect(); }' .
'catch (e) { return false; }' .
Copy link
Member

Choose a reason for hiding this comment

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

Nit: Coding Guidelines: Needs whitespace between parentheses.

https://make.wordpress.org/core/handbook/best-practices/coding-standards/javascript/#spacing

'"DOMRect" in this && ( function( DOMRect ) {' .
'try { return new DOMRect(); }' .
'catch (e) { return false; }' .
'}(this.DOMRect))' => 'wp-polyfill-domrect',
Copy link
Member

Choose a reason for hiding this comment

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

Nit: Coding Guidelines: Needs whitespace between parentheses.

https://make.wordpress.org/core/handbook/best-practices/coding-standards/javascript/#spacing

'wp-polyfill-domrect',
gutenberg_url( 'build/polyfills/DOMRect.js' ),
array(),
filemtime( gutenberg_dir_path() . 'build/polyfills/DOMRect.js' ),
Copy link
Member

Choose a reason for hiding this comment

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

For consistency and to avoid potential issues with OS-specific treatment of capitalization in file names, I might suggest we call this file dom-rect.js instead.

@brandonpayton
Copy link
Member Author

Thanks for taking a look. I made the updates you suggested.

Yeah, the response time has not been great on that PR. It may be because DOMRect is not an exciting addition, but some response would be good.

Copy link
Member

@aduth aduth left a comment

Choose a reason for hiding this comment

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

Is this something we'd consider to publish as package(s)?

'document.contains' => 'wp-polyfill-node-contains',
'document.contains' => 'wp-polyfill-node-contains',
'"DOMRect" in this && ( function( DOMRect ) {' .
'try { return new DOMRect(); return true; }' .
Copy link
Member

Choose a reason for hiding this comment

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

return true; is unreachable here. We don't want the return on the new DOMRect(); statement.

Copy link
Member Author

Choose a reason for hiding this comment

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

🤦‍♂️ fixing...

@brandonpayton
Copy link
Member Author

Is this something we'd consider to publish as package(s)?

Given the current lack of response to the FT polyfill-service DOMRect PR, providing our own polyfill packages seems like a reasonable idea.

For now though, it looks like we no longer use the DOMRect constructor with new DOMRect(), so this PR is unnecessary.

I'm closing this, but if you think there's value in keeping it going, let me know. I'm up for converting to a polyfill package. Thanks for your time and attention on this.

@brandonpayton brandonpayton deleted the fix/ie11-missing-DOMRect branch July 24, 2018 15:18
@brandonpayton
Copy link
Member Author

Deleted our branch for this but keeping a reference with my fork:
https://github.com/brandonpayton/gutenberg/tree/fix/ie11-missing-DOMRect

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.

4 participants