Skip to content

Commit bc10e6b

Browse files
committed
Script Loader: Add sourceURL to inline scripts and styles.
Improve the source locations referenced by developer tooling in supporting browsers. Inline source locations are named like `inline:handle-js-after` and appear in the developer tools "sources" panel. Developed in WordPress#9628. Props jonsurrell, swissspidy, alshakero, westonruter. Fixes #63887. git-svn-id: https://develop.svn.wordpress.org/trunk@60685 602fd350-edb4-49c9-b593-d223f7449a82
1 parent 6c21850 commit bc10e6b

File tree

6 files changed

+171
-52
lines changed

6 files changed

+171
-52
lines changed

src/wp-includes/class-wp-scripts.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,11 @@ public function print_extra_script( $handle, $display = true ) {
222222
return;
223223
}
224224

225+
$output .= sprintf(
226+
"\n//# sourceURL=inline:%s",
227+
rawurlencode( "{$handle}-js-extra" )
228+
);
229+
225230
if ( ! $display ) {
226231
return $output;
227232
}
@@ -521,6 +526,11 @@ public function get_inline_script_data( $handle, $position = 'after' ) {
521526
return '';
522527
}
523528

529+
$data[] = sprintf(
530+
'//# sourceURL=inline:%s',
531+
rawurlencode( "{$handle}-js-{$position}" )
532+
);
533+
524534
return trim( implode( "\n", $data ), "\n" );
525535
}
526536

src/wp-includes/class-wp-styles.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,11 @@ public function print_inline_style( $handle, $display = true ) {
337337
return false;
338338
}
339339

340+
$output[] = sprintf(
341+
'/*# sourceURL=inline:%s */',
342+
rawurlencode( "{$handle}-inline-css" )
343+
);
344+
340345
$output = implode( "\n", $output );
341346

342347
if ( ! $display ) {

tests/phpunit/tests/blocks/editor.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -762,6 +762,7 @@ public function test_ensure_preload_data_script_tag_closes() {
762762
<script src="{$baseurl}/wp-includes/js/dist/api-fetch.min.js?ver=test" id="wp-api-fetch-js"></script>
763763
<script id="wp-api-fetch-js-after">
764764
wp.apiFetch.use( wp.apiFetch.createPreloadingMiddleware( {"/test/v0/test-62797":{"body":["Unclosed comment and a script open tag \\u003C!--\\u003Cscript\\u003E"],"headers":{"Allow":"GET"}}} ) );
765+
//# sourceURL=inline:wp-api-fetch-js-after
765766
</script>
766767
767768
HTML;

0 commit comments

Comments
 (0)