WordPress.org

Make WordPress Core

Changeset 43565


Ignore:
Timestamp:
08/11/2018 05:41:29 PM (18 months ago)
Author:
SergeyBiryukov
Message:

Script/Style Dependencies: Make sure that inline scripts for handles without a source are printed.

See [36550] for WP_Styles.

Props bpayton.
Fixes #44551.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/class.wp-scripts.php

    r43563 r43565  
    270270        if ( $after_handle ) {
    271271            $after_handle = sprintf( "<script type='text/javascript'>\n%s\n</script>\n", $after_handle );
     272        }
     273
     274        if ( $before_handle || $after_handle ) {
     275            $inline_script_tag = "{$cond_before}{$before_handle}{$after_handle}{$cond_after}";
     276        } else {
     277            $inline_script_tag = '';
    272278        }
    273279
     
    313319
    314320        // A single item may alias a set of items, by having dependencies, but no source.
    315         if ( ! $obj->src ) {
     321        if ( ! $src ) {
     322            if ( $inline_script_tag ) {
     323                if ( $this->do_concat ) {
     324                    $this->print_html .= $inline_script_tag;
     325                } else {
     326                    echo $inline_script_tag;
     327                }
     328            }
     329
    316330            return true;
    317331        }
  • trunk/tests/phpunit/tests/dependencies/scripts.php

    r42636 r43565  
    401401        $expected  = "<script type='text/javascript'>\nconsole.log(\"before\");\n</script>\n";
    402402        $expected .= "<script type='text/javascript' src='http://example.com'></script>\n";
     403        $expected .= "<script type='text/javascript'>\nconsole.log(\"after\");\n</script>\n";
     404
     405        $this->assertEquals( $expected, get_echo( 'wp_print_scripts' ) );
     406    }
     407
     408    /**
     409     * @ticket 44551
     410     */
     411    function test_wp_add_inline_script_before_for_handle_without_source() {
     412        wp_register_script( 'test-example', '' );
     413        wp_enqueue_script( 'test-example' );
     414        wp_add_inline_script( 'test-example', 'console.log("before");', 'before' );
     415
     416        $expected = "<script type='text/javascript'>\nconsole.log(\"before\");\n</script>\n";
     417
     418        $this->assertEquals( $expected, get_echo( 'wp_print_scripts' ) );
     419    }
     420
     421    /**
     422     * @ticket 44551
     423     */
     424    function test_wp_add_inline_script_after_for_handle_without_source() {
     425        wp_register_script( 'test-example', '' );
     426        wp_enqueue_script( 'test-example' );
     427        wp_add_inline_script( 'test-example', 'console.log("after");' );
     428
     429        $expected = "<script type='text/javascript'>\nconsole.log(\"after\");\n</script>\n";
     430
     431        $this->assertEquals( $expected, get_echo( 'wp_print_scripts' ) );
     432    }
     433
     434    /**
     435     * @ticket 44551
     436     */
     437    function test_wp_add_inline_script_before_and_after_for_handle_without_source() {
     438        wp_register_script( 'test-example', '' );
     439        wp_enqueue_script( 'test-example' );
     440        wp_add_inline_script( 'test-example', 'console.log("before");', 'before' );
     441        wp_add_inline_script( 'test-example', 'console.log("after");' );
     442
     443        $expected  = "<script type='text/javascript'>\nconsole.log(\"before\");\n</script>\n";
    403444        $expected .= "<script type='text/javascript'>\nconsole.log(\"after\");\n</script>\n";
    404445
Note: See TracChangeset for help on using the changeset viewer.