[wp-trac] [WordPress Trac] #49360: Scripts: Add polyfill for window.URL

WordPress Trac noreply at wordpress.org
Tue Feb 11 21:32:12 UTC 2020


#49360: Scripts: Add polyfill for window.URL
---------------------------+-------------------------
 Reporter:  aduth          |       Owner:  aduth
     Type:  defect (bug)   |      Status:  reopened
 Priority:  normal         |   Milestone:  5.4
Component:  Script Loader  |     Version:  trunk
 Severity:  normal         |  Resolution:
 Keywords:  has-patch      |     Focuses:  javascript
---------------------------+-------------------------

Comment (by aduth):

 There's another issue in r47238, which is that the test used to check
 whether the polyfill should be included is not accurate.

 Specifically, this line:

 https://github.com/WordPress/wordpress-develop/blob/d6352c54/src/wp-
 includes/script-loader.php#L129

 This instructs the browser to load the polyfill if `window.URL` evaluates
 to a falsey value. However, it was overlooked that while IE11 does not
 support the `URL` constructor specifically, it does have the `URL` global
 and supports some of its static members.

 See: https://developer.mozilla.org/en-
 US/docs/Web/API/URL#Browser_compatibility

 Currently, the polyfill is not being loaded at all in Internet Explorer.
 The impact of this is not much different than it is with the polyfill as
 implemented without the expected throwing behavior. Since the
 [https://github.com/WordPress/gutenberg/blob/59d74630a9d2390f9f88e4a7086b4c51e14b3560/packages/url/src
 /is-url.js#L16-L25 implementation of `isURL`] already wraps itself in a
 `try` / `catch`, the error will be caught. The main difference is that
 rather than always return `true` from this function, it will always return
 `false`. For most practical purposes, this is probably a better outcome
 than if the faulty polyfill implementation were loaded.

 Thus, the test should be updated to one which specifically targets whether
 `URL` can be used as a constructor.

 Some options that might be available include:

 - Try/catch on a test construction
    - `( function() { try { new URL( 'http://w.org' ); return true; } catch
 ( e ) { return false; } } )()`
    - Minified: `!function(){try{new
 URL("http://w.org")}catch(e){return!1}}()`
 - Other detection of ability to use as constructor
    - `URL.prototype`

-- 
Ticket URL: <https://core.trac.wordpress.org/ticket/49360#comment:9>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list