From a0aba9dc44059952da09f52fc150dc1c659ed027 Mon Sep 17 00:00:00 2001 From: Dominik Schilling Date: Sat, 13 Aug 2016 18:34:12 +0000 Subject: [PATCH] Script Loader: Fix protocol-relative URLs for the `preconnect` relation type. `wp_resource_hints()` parses the URL for the `preconnect` and `dns-prefetch` relation types to ensure correct values for both. While protocol-relative URLs are supported for `dns-prefetch`, the double slash was lost for `preconnect`. Props swissspidy, peterwilsoncc. Props azaozz for review. Fixes #37652. git-svn-id: https://develop.svn.wordpress.org/trunk@38255 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/general-template.php | 7 +- tests/phpunit/tests/general/resourceHints.php | 66 +++++++++++++++++++ 2 files changed, 69 insertions(+), 4 deletions(-) diff --git a/src/wp-includes/general-template.php b/src/wp-includes/general-template.php index 4e049d5714..12ffd4a91e 100644 --- a/src/wp-includes/general-template.php +++ b/src/wp-includes/general-template.php @@ -2839,12 +2839,11 @@ function wp_resource_hints() { continue; } - if ( 'dns-prefetch' === $relation_type ) { - $url = '//' . $parsed['host']; - } else if ( ! empty( $parsed['scheme'] ) ) { + if ( 'preconnect' === $relation_type && ! empty( $parsed['scheme'] ) ) { $url = $parsed['scheme'] . '://' . $parsed['host']; } else { - $url = $parsed['host']; + // Use protocol-relative URLs for dns-prefetch or if scheme is missing. + $url = '//' . $parsed['host']; } } diff --git a/tests/phpunit/tests/general/resourceHints.php b/tests/phpunit/tests/general/resourceHints.php index f6da5574cc..8a95755cd3 100644 --- a/tests/phpunit/tests/general/resourceHints.php +++ b/tests/phpunit/tests/general/resourceHints.php @@ -66,6 +66,37 @@ class Tests_WP_Resource_Hints extends WP_UnitTestCase { return $hints; } + /** + * @ticket 37652 + */ + function test_preconnect() { + $expected = "\n" . + "\n" . + "\n" . + "\n" . + "\n"; + + add_filter( 'wp_resource_hints', array( $this, '_add_preconnect_domains' ), 10, 2 ); + + $actual = get_echo( 'wp_resource_hints' ); + + remove_filter( 'wp_resource_hints', array( $this, '_add_preconnect_domains' ) ); + + $this->assertEquals( $expected, $actual ); + } + + function _add_preconnect_domains( $hints, $method ) { + if ( 'preconnect' === $method ) { + $hints[] = '//wordpress.org'; + $hints[] = 'https://make.wordpress.org'; + $hints[] = 'htps://example.com'; // Invalid URLs should be skipped. + $hints[] = 'http://google.com'; + $hints[] = 'w.org'; + } + + return $hints; + } + function test_prerender() { $expected = "\n" . "\n" . @@ -176,4 +207,39 @@ class Tests_WP_Resource_Hints extends WP_UnitTestCase { $actual = get_echo( 'wp_resource_hints' ); $this->assertEquals( $expected, $actual ); } + + /** + * @ticket 37652 + */ + function test_malformed_urls() { + $expected = "\n"; + + // Errant colon. + add_filter( 'wp_resource_hints', array( $this, '_add_malformed_url_errant_colon' ), 10, 2 ); + $actual = get_echo( 'wp_resource_hints' ); + remove_filter( 'wp_resource_hints', array( $this, '_add_malformed_url_errant_colon' ) ); + $this->assertEquals( $expected, $actual ); + + // Unsupported Scheme. + add_filter( 'wp_resource_hints', array( $this, '_add_malformed_url_unsupported_scheme' ), 10, 2 ); + $actual = get_echo( 'wp_resource_hints' ); + remove_filter( 'wp_resource_hints', array( $this, '_add_malformed_url_unsupported_scheme' ) ); + $this->assertEquals( $expected, $actual ); + } + + function _add_malformed_url_errant_colon( $hints, $method ) { + if ( 'preconnect' === $method ) { + $hints[] = '://core.trac.wordpress.org/ticket/37652'; + } + + return $hints; + } + + function _add_malformed_url_unsupported_scheme( $hints, $method ) { + if ( 'preconnect' === $method ) { + $hints[] = 'git://develop.git.wordpress.org/'; + } + + return $hints; + } }