使WordPress成为核心

变更集56687


忽略:
时间戳:
2023年9月25日下午09:03:19(12个月(之前)
作者:
威斯顿鲁特
消息:

脚本加载器:使用wp_get_script_tag()wp_get_inline_script_tag()/wp_print_inline_script_tag()helper函数在前端和登录屏幕上输出脚本。

使用脚本标记助手函数允许插件使用wp脚本属性wp_inline脚本属性过滤器注入现时属性应用内容安全策略(例如严格CSP)。辅助函数的使用还简化了中的逻辑WP_脚本.

  • 更新wp_get_inline_script_tag()在不使用HTML5时,将内联脚本包装在CDATA块中,以实现XHTML兼容性。
  • 确保类型属性首先在中打印wp_get_inline_script_tag()用于背压。
  • 包装现有内容<脚本>输出缓冲区中的标记以保留IDE支持。
  • wp_get_inline_script_tag(),将换行符附加到$javascript在它被传递到wp_inline脚本属性筛选以正确计算CSP哈希。
  • 块模板skip_link(),选择将内联脚本排队,而不是打印它。
  • 添加外部马力作曲家.json在下面建议与之前一样,它是运行PHPUnit测试的未声明依赖项。
  • 更新要依赖的测试DOM文档要比较脚本标记,请规范化非语义差异。

Props westonruter、spacedmonkey、flixos90、10upsimon、dmsnell、mukesh27、joemgill、swissspidy、azaozz。
修复#58664.
请参见#39941.

位置:
大旅行箱
文件夹:
17已编辑

图例:

未修改
补充
远离的
  • 主干/作曲家.json

    56421兰特 56687兰特  
    1212“要求”:{
    1313“php”:“>=7.0”
     14    },
     15“建议”:{
     16“ext-dom”:“*”
    1417    },
    1518“require-dev”:{
  • trunk/src/wp-includes/class-wp-customize-manager.php

    56549兰特 56687兰特  
    465465“错误”=>$ajax_message,
    466466            );
     467$消息。=对象获取清理();
     468对象开始();
    467469            ?>
    468470<脚本>
     
    473475</script>
    474476            <?php(电话)
    475 $消息=对象获取清理();
     477$消息=wp_get_inline_script_tag(str_replace(数组('<script>','</script>'),'',ob_get_clean()));
    476478        }
    477479
     
    20842086回报;
    20852087        }
     2088对象开始();
    20862089        ?>
    20872090<脚本>
     
    21072110</script>
    21082111        <?php(电话)
     2112wp_print_inline_script_tag(str_replace(数组('<script>','</script>'),'',ob_get_clean()));
    21092113    }
    21102114
     
    22022206        }
    22032207
     2208对象开始();
    22042209        ?>
    2205 <脚本type=“text/javascript”>
     2210<脚本>
    22062211var_wpCustomizeSettings=<?php-echo wp_json_encode($settings);?>;
    22072212_wpCustomizeSettings.values={};
     
    22262231</script>
    22272232        <?php(电话)
     2233wp_print_inline_script_tag(str_replace(数组('<script>','</script>'),'',ob_get_clean()));
    22282234    }
    22292235
     
    49774983        }
    49784984
     4985对象开始();
    49794986        ?>
    4980 <脚本type=“text/javascript”>
     4987<脚本>
    49814988var_wpCustomizeSettings=<?php-echo wp_json_encode($settings);?>;
    49824989_wpCustomizeSettings.initialClientTimestamp=_.now();
     
    50135020</script>
    50145021        <?php(电话)
     5022wp_print_inline_script_tag(str_replace(数组('<script>','</script>'),'',ob_get_clean()));
    50155023    }
    50165024
  • trunk/src/wp-includes/class-wp-customize-nav-menus.php

    55990兰特 56687兰特  
    15601560“navMenuInstanceArgs”=>$this->preview_nav_menu_instance_args,
    15611561        );
    1562        printf('<script>var_wpCustomizePreviewNavMenusExports=%s;</script>',wp_json_encode($exports) );
     1562       wp_print_inline_script_tag(sprintf('var_wpCustomizePreviewNavMenusExports=%s;',wp_json_encode($exports)) );
    15631563    }
    15641564
  • trunk/src/wp-includes/class-wp-customize-widgets.php

    56559兰特 56687兰特  
    13111311取消设置($registered_widget['callback']);//可能不是JSON可序列化的。
    13121312        }
    1313 
    1314         ?>
    1315 <script type=“text/javascript”>
    1316 var_wpWidgetCustomizerPreviewSettings=<?php-echo wp_json_encode($settings);?>;
    1317 </script>
    1318         <?php(电话)
     1313wp_print_inline_script_tag(打印行脚本标记)(
     1314sprintf('var_wpWidgetCustomizerPreviewSettings=%s;',wp_json_encode($settings))
     1315        );
    13191316    }
    13201317
  • trunk/src/wp-includes/class-wp-scripts.php

    r56273型 56687兰特  
    124124
    125125    /**
    126 *包含脚本标记的类型属性的字符串。
    127      *
    128 *如果活动主题未声明HTML5支持“script”,
    129 *然后初始化为“type='text/javascript'”。
    130      *
    131 *@自5.3.0起
    132 *@var字符串
    133      */
    134 私有$type_attr=“”;
    135 
    136     /**
    137126*保存给定脚本句柄的从属项映射(作为句柄)。
    138127*用于优化递归依赖关系树检查。
     
    168157     */
    169158公共函数init(){
    170 如果(
    171 函数存在('is_admin')&&!is_admin()
    172         &&
    173 function_exists('current_theme_supports')&&!current_theme_supports('html5','script')
    174         ) {
    175 $this->type_attr=“type='text/javascript'”;
    176         }
    177 
    178159        /**
    179160*在初始化WP_Scripts实例时激发。
     
    246227        }
    247228
    248 printf(“<script%s id='%s-js-extra'>\n”,$this->type_attr,esc_attr($handle));
    249 
    250 //HTML 5不需要CDATA。
    251 if($this->typeatr){
    252 echo“/*<![CDATA[*/\n”;
    253         }
    254 
    255 echo“$output\n”;
    256 
    257 if($this->typeatr){
    258 echo“/*]]>*/\n”;
    259         }
    260 
    261 echo“</script>\n”;
     229wp_print_inline_script_tag($output,数组('id'=>“{$handle}-js-extra”));
    262230
    263231返回true;
     
    336304$translations=$this->print_translations($handle,false);
    337305if($翻译){
    338 $个翻译=sprintf(“<script%s id='%s-js-translations'>\n%s\n</script>\n”,$this->type_attr,esc_attr($handle),$translations);
     306$个翻译=wp_get_inline_script_tag($translations,数组('id'=>“{$handle}-js-translations”));
    339307        }
    340308
     
    404372
    405373/**此筛选器记录在wp-includs/class-wp-scripts.php中*/
    406 $src=esc_url(apply_filters('script_loader_src',$src,$handle));
     374$src=esc_url_未加工的(apply_filters('script_loader_src',$src,$handle));
    407375
    408376if(!$src){
     
    410378        }
    411379
     380$attr=数组(
     381'src'=>$src,
     382'id'=>“{$handle}-js”,
     383        );
     384if($strategy){
     385$attr[$strategy]=true;
     386        }
     387if($intended_strategy){
     388$attr['data-wp-strategy']=$intended_strategy;
     389        }
    412390$tag=$个翻译$之前的条件$before_script;
    413 $标签=冲刺(
    414 “<script%s src='%s'id='%s-js'%s%s></script>\n”,
    415 $this->type_attr,
    416 $src,//值在上面转义。
    417 esc_attr($handle),
    418 美元战略?“{$strategy}”:“”,
    419 $intended_strategy?“data-wp-strategy='{$intended_strategy}'”:“”
    420         );
     391$标签=wp_get_script_tag($attr);
    421392$标签=$后脚本$cond_after;
    422393
     
    721692
    722693if($display){
    723            printf(“<script%s id='%s-js-translations'>\n%s\n</script>\n”,$this->type_attr,esc_attr($handle),$output);
     694           wp_print_inline_script_tag($output,数组('id'=>“{$handle}-js-translations”));
    724695        }
    725696
  • trunk/src/wp-includes/comment-template.php

    56635兰特 56687兰特  
    13671367if(current_user_can('unfiltered_html')){
    13681368wp_nonce_field('unscreed-html-comment_'.$post_id,'_wp_unscreed-html_comment_disabled',false);
    1369        echo“<script>(function(){if(window===window.parent){document.getElementById('_wp_unfiltered_html_comment_disabled').name='_wp_unfitered_html _comment';}})();</script>\n”;
     1369       wp_print_inline_script_tag(“(function(){if(window===window.parent){document.getElementById('_wp_unfiltered_html_comment_disabled').name='_wp_unfixed_html_coment';}})();”);
    13701370    }
    13711371}
  • trunk/src/wp-includes/customize/class-wp-customize-selective-refresh.php

    55161兰特 56687兰特  
    194194
    195195//将数据导出到JS。
    196        printf('<script>var_customizePartialRefreshExports=%s;</script>',wp_json_encode($exports) );
     196       wp_print_inline_script_tag(sprintf('var_customizePartialRefreshExports=%s;',wp_json_encode($exports)) );
    197197    }
    198198
  • trunk/src/wp-includes/functions.php

    56662兰特 56687兰特  
    76567656$name=“wp-preview-”。(int)$post->ID;
    76577657
     7658对象开始();
    76587659    ?>
    76597660<脚本>
     
    76717672</script>
    76727673    <?php(电话)
     7674wp_print_inline_script_tag(str_replace(数组('<script>','</script>'),'',ob_get_clean()));
    76737675}
    76747676
  • trunk/src/wp-includes/script-loader.php

    56646兰特 56687兰特  
    27882788函数wp_get_script_tag($attributes){
    27892789if(!isset($attributes['type'])&&!is_admin()&&!current_theme_supports(“html5”,“script”){
    2790 $attributes['type']='text/javascript';
     2790//出于传统原因,将type属性保留为第一个属性(在核心中一直是这样)。
     2791$attributes=数组_合并(
     2792数组(“type”=>“text/javascript”),
     2793$个属性
     2794        );
    27912795    }
    27922796    /**
     
    28312835 */
    28322836函数wp_get_inline_script_tag($javascript,$attributes=array()){
    2833 if(!isset($attributes['type'])&&!is_admin()&&!current_theme_supports(“html5”,“script”){
    2834 $attributes['type']='text/javascript';
    2835     }
     2837$is_html5=current_theme_supports('html5','script')|| is_admin();
     2838if(!isset($attributes['type'])&&$是html5){
     2839//出于传统原因,将type属性保留为第一个属性(在核心中一直是这样)。
     2840$attributes=数组_合并(
     2841数组(“type”=>“text/javascript”),
     2842$个属性
     2843        );
     2844    }
     2845
     2846//如果不是HTML5,请确保标记与XHTML兼容。
     2847如果(!$is_html5){
     2848$javascript=str_replace(']]>',']]]><![CDATA[>',$javascript);//退出任何现有的CDATA部分。
     2849$javascript=sprintf(“/*<![CDATA[*/\n%s\n/*]]>*/”,$javascript);
     2850    }
     2851
     2852$javascript=“\n”。trim($javascript,“\n\r”)。“\n”;
     2853
    28362854    /**
    28372855*筛选要添加到脚本标记的属性。
     
    28452863     */
    28462864$attributes=应用过滤器('wp_inline_script_attributes',$attritributes,$javascript);
    2847 
    2848 $javascript=“\n”。trim($javascript,“\n\r”)。“\n”;
    28492865
    28502866return sprintf(“<script%s>%s</script>\n”,wp_sanitize_script_attributes($attributes),$javascript);
  • trunk/src/wp-includes/theme-templates.php

    56682兰特 56687兰特  
    161161
    162162    /**
    163      *打印跳过链接脚本。
     163     *排队skip-link脚本。
    164164     */
     165对象开始();
    165166    ?>
    166167<脚本>
     
    205206</script>
    206207    <?php(电话)
     208$skip_link_script=str_replace(数组('<script>','</script>'),'',ob_get_clean());
     209$script_handle='wp-block-template-skip-link';
     210wp_register_script($script_handle,false);
     211wp_add_inline_script($script_handle,$skip_link_script);
     212wp_enqueue_script($script_handle);
    207213}
    208214
  • trunk/src/wp-includes/theme.php

    56686兰特 56687兰特  
    37843784$home_origin=解析url(home_url());
    37853785$cross_domain=(strtollower($admin_origin['host'])!==strtolower($home_origin['host']);
    3786    $type_attr=当前主题支持('html5','script')?'':'type=“text/javascript”';
     3786   对象开始();
    37873787    ?>
    3788 <脚本<?php echo$type_attr;?>>
     3788<脚本>
    37893789(函数(){
    37903790var请求,b=document.body,c='className',cs='customize-support',rcs=new RegExp('(^|\\s+)(no-)?'+cs+“(\\s+|$)”);
     
    38023802</script>
    38033803    <?php(电话)
     3804wp_print_inline_script_tag(str_replace(数组('<script>','</script>'),'',ob_get_clean()));
    38043805}
    38053806
  • trunk/src/wp-includes/widgets/class-wp-widget-archives.php

    54062兰特 56687卢比  
    101101断裂;
    102102            }
    103 
    104 $type_attr=当前主题支持('html5','script')?'':'type=“text/javascript”';
    105103            ?>
    106104
     
    110108</选择>
    111109
    112 <脚本<?php echo$type_attr;?>>
    113 /* <![CDATA[*/
     110            <?php对象启动()?>
     111<脚本>
    114112(函数(){
    115113var dropdown=文档.getElementById(“<?php echo esc_js($dropdown_id);?>”);
     
    121119dropdown.onchange=onSelectChange;
    122120})();
    123 /* ]]> */
    124121</script>
    125122            <?php(电话)
     123wp_print_inline_script_tag(str_replace(数组('<script>','</script>'),'',ob_get_clean()));
    126124}其他{
    127125$format=current_theme_supports('html5','navigation-widgets')?'html5':'xhtml';
  • trunk/src/wp-includes/widgets/class-wp-widget-categories.php

    56547兰特 56687卢比  
    9393echo“</form>”;
    9494
    95            $type_attr=当前主题支持('html5','script')?'':'type=“text/javascript”';
     95           对象开始();
    9696            ?>
    9797
    98 <脚本<?php-echo$type_attr;?>>
    99 /* <![CDATA[*/
     98<脚本>
    10099(函数(){
    101100var dropdown=文档.getElementById(“<?php echo esc_js($dropdown_id);?>”);
     
    107106dropdown.onchange=onCatChange;
    108107})();
    109 /* ]]> */
    110108</script>
    111109
    112110            <?php(电话)
     111wp_print_inline_script_tag(str_replace(数组('<script>','</script>'),'',ob_get_clean()));
    113112}其他{
    114113$format=current_theme_supports('html5','navigation widgets')?'html5':'xhtml';
  • 主干/src/wp-login.php

    56654兰特 56687兰特  
    102102     */
    103103if('loggedout'===$wp_error->get_error_code()){
     104对象开始();
    104105        ?>
    105106<script>if(窗口中的“sessionStorage”){try{for(sessionStorage中的var key){if(key.indexOf(“wp-autosave-”)!=-1){sessionStorage.removeItem(key)}}}捕获(e){}}</脚本>
    106107        <?php(电话)
     108wp_print_inline_script_tag(str_replace(数组('<script>','</script>'),'',ob_get_clean()));
    107109    }
    108110
     
    194196</头>
    195197<body class=“login no js<?php echo esc_attr(内爆('',$classes));?>”>
    196 <script type=“text/javascript”>
    197 document.body.className=文档.body.className.replace('no-js','js');
    198 </script>
     198    <?php(电话)
     199wp_print_inline_script_tag(“document.body.className=文档.body.className.replace('no-js','js');”);
     200    ?>
     201
    199202    <?php(电话)
    200203    /**
     
    415418
    416419if(!空($input_id)){
     420对象开始();
    417421        ?>
    418 <脚本type=“text/javascript”>
     422<脚本>
    419423尝试{document.getElementById('<?php-echo$input_id;?>').focus();}catch(e){}
    420424if(wpOnload类型===“函数”)wpOn加载();
    421425</script>
    422426        <?php(电话)
     427wp_print_inline_script_tag(str_replace(数组('<script>','</script>'),'',ob_get_clean()));
    423428    }
    424429
     
    442447 */
    443448函数wp_shake_js(){
    444     ?>
    445 <script type=“text/javascript”>
    446 document.querySelector('form').classList.add('shake');
    447 </script>
    448     <?php(电话)
     449wp_print_inline_script_tag(“document.querySelector('form').classList.add('shake');”);
    449450}
    450451
     
    13581359
    13591360if($customize_login){
     1361对象开始();
    13601362                    ?>
    1361 <脚本type=“text/javascript”>setTimeout(function(){new-wp.customize.Messenger({url:'<?php-echo-wp_customize_url();?>',通道:'login'}).send('login')},1000)</脚本>
     1363<脚本>setTimeout(function(){new-wp.customize.Messenger({url:'<?php-echo-wp_customize_url();?>',通道:'login'}).send('login')},1000)</脚本>
    13621364                    <?php(电话)
     1365wp_print_inline_script_tag(str_replace(数组('<script>','</script>'),'',ob_get_clean()));
    13631366                }
    13641367
     
    16061609$login_script.=“if(wpOnload的类型===‘函数’){wpOn加载()}”;
    16071610
    1608         ?>
    1609 <script type=“text/javascript”>
    1610             <?php echo$login_script;?>
    1611 </script>
    1612         <?php(电话)
     1611wp_print_inline_script_tag($login_script);
    16131612
    16141613if($interim_login){
     1614对象开始();
    16151615            ?>
    1616 <脚本type=“text/javascript”>
     1616<脚本>
    16171617(函数(){
    16181618尝试{
     
    16281628</script>
    16291629            <?php(电话)
     1630wp_print_inline_script_tag(str_replace(数组('<script>','</script>'),'',ob_get_clean()));
    16301631        }
    16311632
  • trunk/tests/phpunit/tests/customize/manager.php

    56547兰特 56687卢比  
    31373137$manager->remove_frameles_preview_messenger_channel();
    31383138$output=ob_get_clean();
    3139 $this->assertStringContainsString('<script>',$output);
     3139$this->assertStringContainsString('<script',$output);
    31403140    }
    31413141
  • trunk/tests/phpunit/tests/dependencies/scripts.php

    56559兰特 56687兰特  
    4343$this->wp_scripts_print_translations_output=<<JS
    4444<script type='text/javascript'id='__HANDLE__-js-translations'>
     45/* <![数据]*/
    4546(功能(域、翻译){
    4647var localeData=translations.locale_data[domain]| | translations.locale_data.messages;
     
    4849wp.i18n.setLocaleData(本地数据,域);
    4950})(“__DOMAIN__”,__JSON_TRANSLATIONS__);
     51/* ]]> */
    5052</script>
    5153JS;
     
    7880应为$。=“<script type='text/javascript'src='http://example.com'id='empty-deps-null-version-js'></script>\n“;
    7981
    80 $this->断言相同(需要$,get_echo('wp_print_scripts'));
     82$this->断言相等标记(需要$,get_echo('wp_print_scripts'));
    8183
    8284//没有要打印的脚本。
     
    119121$expected=“<script type='text/javascript'src='http://example.org/ms-isa-1js'id='ms-isa-1-js'data-wp-strategy='{$strategy}'></script>\n“;
    120122应为$。=wp_get_inline脚本标记(
    121            “console.log(“在一个之后”);\n”,
     123           '控制台.log(“一个之后”);',
    122124阵列(
    123125“id”=>“ms-isa-1-js-after”,
    124126            )
    125127        );
    126 $this->断言相同($expected,$output,'附加到延迟主脚本的“after”位置的内联脚本无法打印/执行');
     128$this->断言等于标记($expected,$output,'附加到延迟主脚本的“after”位置的内联脚本无法打印/执行。');
    127129    }
    128130
     
    147149$expected=“<script type='text/javascript'src='http://example.org/ms-insa-3.js'id='ms-insa-3-js'></script>\n“;
    148150应为$。=wp_get_inline脚本标记(
    149            “console.log(“在一个之后”);\n”,
     151           '控制台.log(“一个之后”);',
    150152阵列(
    151153'id'=>'ms-insa-3-js-after'之后,
     
    153155        );
    154156
    155 $this->断言相同($expected,$output,'附加到阻塞主脚本的“after”位置的内联脚本无法打印/执行');
     157$this->断言等于标记($expected,$output,'附加到阻塞主脚本的“after”位置的内联脚本无法打印/执行');
    156158    }
    157159
     
    181183
    182184$预期值=wp_get_inline_script_tag(
    183            “console.log(\”before first\“);\n”,
     185           '控制台.log(“before first”);',
    184186阵列(
    185187“id”=>“ds-i1-1-js-before”,
     
    190192应为$。=“<script type='text/javascript'src='http://example.org/ds-i1-3.js'id='ds-i13-js'$strategy数据wp strategy='{$strategy}'></script>\n“;
    191193应为$。=wp_get_inline脚本标记(
    192            “console.log(\”before last“);\n”,
     194           'console.log(“before last”);',
    193195阵列(
    194196“id”=>“ms-i1-1-js-before”,
     
    198200应为$。=“<script type='text/javascript'src='http://example.org/ms-i1-1.js'id='ms-i1-1-js'{$strategy}data-wp-strategy='{$stratigy}'></script>\n“;
    199201
    200 $this->断言相同($expected,$output,'附加到延迟主脚本的“before”位置的内联脚本无法打印/执行');
     202$this->断言等于标记($expected,$output,'附加到延迟主脚本的“before”位置的内联脚本无法打印/执行');
    201203    }
    202204
     
    216218$output=get_echo('wp_print_scripts');
    217219$expected=“<script type='text/javascript'src='/main-script-a1.js'id='main-script-a1-js'异步数据wp策略='async'></script>\n”;
    218 $this->断言相同($expected,$output,“使用异步加载策略排队的脚本在打印时无法将异步属性应用于脚本句柄”);
     220$this->断言等于标记($expected,$output,“使用异步加载策略排队的脚本在打印时无法将异步属性应用于脚本句柄”);
    219221    }
    220222
     
    236238wp_enqueue_script('dependency-script-a2','/dependency-script-a2.js',array(),null);
    237239wp_enqueue_script('main-script-a2','/main-script-a2.js',数组('dependency-script-a2'),null,压缩('strategy'));
    238 $output=get_echo('wp_print_scripts');
    239 $expected=“<script type='text/javascript'src='/main-script-a2.js'id='main-script-a2-js'{$strategy}data-wp-strategy='{$stratigy}'></script>”;
    240 $this->assertStringContainsString($expected,$output,‘阻塞依赖项的依赖项可以自由使用任何策略’);
     240$output=get_echo('wp_print_scripts');
     241$预期=“<script id='dependency-script-a2-js'src='/dependency-script-a2.js'></script>\n”;
     242应为$。=“<script type='text/javascript'src='/main-script-a2.js'id='main-script-a2-js'{$strategy}data-wp-strategy='{$stratigy}'></script>”;
     243$this->assertEqualMarkup($expected,$output,‘阻塞依赖项的依赖项可以自由使用任何策略’);
    241244    }
    242245
     
    258261wp_enqueue_script('dependent-script-a3','/dependent-script-a3.js',数组('main-script-a3'),null);
    259262$output=get_echo('wp_print_scripts');
    260 应为$=“<script type='text/javascript'src='/main-script-a3.js'id='main-script-a3-js'data-wp-strategy='{$strategy}'></script>”;
     263应为$=str_replace(“'”,“”,“<script type='text/javascript'src='/main-script-a3.js'id='main-script-a3-js'data-wp-strategy='{$strategy}'></script>”);
    261264$this->assertStringContainsString($expected,$output,'阻塞依赖项必须强制延迟的依赖项成为阻塞项。');
    262265    }
     
    276279     */
    277280公共函数test_delayed_dependent_with_blocking_dependency_not_enqueued($strategy){
     281$this->add_html5_script_theme_support();
    278282wp_enqueue_script('main-script-a4','/main-script-a4.js',array(),null,compact('strategy'));
    279283//此依赖项已注册但未排队,因此不应将其纳入合格的加载策略。
    280284wp_register_script('dependent-script-a4','/dependent-script-a4.js',数组('main-script-a4]),null);
    281285$output=get_echo('wp_print_scripts');
    282 应为$=“<script type='text/javascript'src='/main-script-a4.js'id='main-script-a4-js'{$strategy}data-wp-strategy='{$stratigy}'></script>”;
     286应为$=str_replace(“'”,“”,“<script src='/main-script-a4.js'id='main-script-a4-js'{$strategy}data-wp-strategy='{$stratigy}'></script>”);
    283287$this->assertStringContainsString($expected,$output,'只有排队的依赖项才应该影响合格的策略');
    284288    }
     
    965969     */
    966970公共函数test_loading_strategy_with_defer_having_no_dependents_nor_dependencies(){
     971$this->add_html5_script_theme_support();
    967972wp_enqueue_script('main-script-d1','http://example.com/main-script-d1.js',array(),null,array('strategy'=>'defer'));
    968973$output=get_echo('wp_print_scripts');
    969 应为$=“<script type='text/javascript'src='http://example.com/main-script-d1.js'id='main-script-d1-js'延迟data-wp-strategy='defer'></script>\n“;
     974应为$=str_replace(“'”,“”,“<script src='http://example.com/main-script-d1.js'id='main-script-d1-js'延迟data-wp-strategy='defer'></script>\n“);
    970975$this->assertStringContainsString($expected,$output,'expected defer,as there no dependent or dependency');
    971976    }
     
    981986     */
    982987公共函数test_loading_strategy_with_defer_dependent_and_varied_dependenties(){
     988$this->add_html5_script_theme_support();
    983989wp_enqueue_script('依赖脚本-d2-1','http://example.com/dependency-script-d2-1.js',array(),null,数组('strategy'=>'defer');
    984990wp_enqueue_script('依赖脚本-d2-2','http://example.com/dependency-script-d2-2.js',array(),null);
     
    986992wp_enqueue_script('main-script-d2','http://example.com/main-script-d2.js',数组('dependency-script-d2-1','dependenty-script-d2-3'),null,数组('策略'=>'defer');
    987993$output=get_echo('wp_print_scripts');
    988 应为$=“<script type='text/javascript'src='http://example.com/main-script-d2.js'id='main-script-d2-js'延迟data-wp-strategy='defer'></script>\n“;
     994应为$='<script src=“http://example.com/main-script-d2.js“id=”main-script-d2-js“defer data-wp-strategy=”defer“></script>';
    989995$this->assertStringContainsString($expected,$output,'expected defer,as all dependencies are deferred or blocking');
    990996    }
     
    10001006     */
    10011007公共函数test_loading_strategy_with_all_defer_dependencies(){
     1008$this->add_html5_script_theme_support();
    10021009wp_enqueue_script('main-script-d3','http://example.com/main-script-d3.js',array(),null,数组('strategy'=>'defer');
    10031010wp_enqueue_script('依赖脚本-d3-1','http://example.com/dependent-script-d3-1.js',数组('main-script-d3'),null,数组('策略'=>'defer'));
     
    10051012wp_enqueue_script('依赖脚本-d3-3','http://example.com/dependent-script-d3-3.js',数组('dependent-script-d3-2'),null,数组('策略'=>'defer');
    10061013$output=get_echo('wp_print_scripts');
    1007 应为$=“<script type='text/javascript'src='http://example.com/main-script-d3.js'id='main-script-d3-js'延迟data-wp-strategy='defer'></script>\n“;
     1014应为$='<script src=“http://example.com/main-script-d3.js“id=”main-script-d3-js“defer data-wp-strategy=”defer“></script>';
    10081015$this->assertStringContainsString($expected,$output,'expected defer,as all dependent have defer loading strateg');
    10091016    }
     
    10301037应为$。=“<script type='text/javascript'src='/dependent-script-d4-3.js'id='dependent-script-d4-3-js'defer data-wp-strategy='defer'></script>\n”;
    10311038
    1032 $this->断言相同($expected,$output,“注册为defer但具有异步依赖项的脚本预计会延迟所述依赖项”);
     1039$this->断言等于标记($expected,$output,'注册为defer但具有异步从属项的脚本应具有延迟的所述从属项。');
    10331040    }
    10341041
     
    10501057wp_enqueue_script('dependent-script-d4-3','/dependent-script-d4-3.js',数组('dedependment-script-d4-2'),null,数组('策略'=>'defer'));
    10511058$output=get_echo('wp_print_scripts');
    1052 应为$=“<script type='text/javascript'src='/main-script-d4.js'id='main-script-d4-js'data-wp-strategy='defer'></script>\n”;
     1059应为$=str_replace(“'”,“”,“<script type='text/javascript'src='/main-script-d4.js'id='main-script-d4-js'data-wp-strategy='defer'></script>\n”);
    10531060$this->assertStringContainsString($expected,$output,'注册为defer但所有依赖项都没有策略的脚本应该成为阻塞的(没有策略)。');
    10541061    }
     
    10681075$output=get_echo('wp_print_scripts');
    10691076$预期=“<script type='text/javascript'src='/main-script-b1.js'id='main-script-b1-js'></script>\n”;
     1077$expected=str_replace(“”,“”,$expected);
    10701078$this->assertSame($expected,$output,'使用“阻塞”策略注册的脚本,如果没有依赖项,则不应打印加载策略属性');
    10711079
     
    10741082$output=get_echo('wp_print_scripts');
    10751083$预期=“<script type='text/javascript'src='/main-script-b2.js'id='main-script-b2-js'></script>\n”;
     1084$expected=str_replace(“”,“”,$expected);
    10761085$this->assertSame($expected,$output,'脚本注册时没有分配策略,并且没有依赖项,应该不会打印加载策略属性。');
    10771086    }
     
    11001109$expected_header.=“<script type='text/javascript'src='/enqueue-header-new.js'id='enqueue-hheader-new-js'></script>\n”;
    11011110
    1102 $this->断言相同($expected_header、$actual_header,'使用旧的$in_footer参数或新的$args参数注册/排队的脚本应该具有相同的结果。');
     1111$this->断言等于标记($expected_header、$actual_header,'使用旧的$in_footer参数或新的$args参数注册/排队的脚本应该具有相同的结果。');
    11031112$this->assertEmpty($actual_footer,'由于所有脚本都是针对head的,所以页脚应该是空的。');
    11041113    }
     
    11281137
    11291138$this->assertEmpty($actual_header,'由于所有脚本都指向页脚,因此页眉应为空。');
    1130 $this->断言相同($expected_footer,$actual_footer'使用旧的$in_footer参数或新的$args参数注册/排队的脚本应该具有相同的结果。');
     1139$this->断言等于标记($expected_footer,$actual_footer'使用旧的$in_footer参数或新的$args参数注册/排队的脚本应该具有相同的结果。');
    11311140    }
    11321141
     
    12471256wp_enqueue_script(“无效策略”);
    12481257
    1249 $this->断言相同(
     1258$this->断言等于标记(
    12501259“<script type='text/javascript'src='/defaults.js'id='invalid-strategy-js'></script>\n”,
    12511260get_echo('wp_print_scripts')
     
    12721281wp_enqueue_script(“无效策略”);
    12731282
    1274 $this->断言相同(
     1283$this->断言等于标记(
    12751284“<script type='text/javascript'src='/defaults.js'id='invalid-strategy-js'></script>\n”,
    12761285get_echo('wp_print_scripts')
     
    12931302wp_enqueue_script('invalid-strategy','/defaults.js',array(),null,数组('strategy'=>'random-stratey'));
    12941303
    1295 $this->断言相同(
     1304$this->断言等于标记(
    12961305“<script type='text/javascript'src='/defaults.js'id='invalid-strategy-js'></script>\n”,
    12971306get_echo('wp_print_scripts')
     
    13311340应为$。=“<script type='text/javascript'src='/main-script.js'id='main-defer-script-js'延迟data-wp-strategy='defer'></script>\n”;
    13321341
    1333 $this->断言相同($expected,$print_scripts,'当使用“defer”加载策略注册主脚本时,脚本连接不正确。延迟的脚本不应是脚本连接加载查询的一部分。');
     1342$this->断言相等标记($expected,$print_scripts,'当使用“defer”加载策略注册主脚本时,脚本连接不正确。延迟的脚本不应是脚本连接加载查询的一部分。');
    13341343    }
    13351344
     
    13661375应为$。=“<script type='text/javascript'src='/main-script.js'id='main-async-script-1-js'异步data-wp-strategy='async'>\n”;
    13671376
    1368 $this->断言相同($expected,$print_scripts,'当使用“异步”加载策略注册主脚本时,脚本连接不正确。异步脚本不应是脚本连接加载查询的一部分。');
     1377$this->断言相等标记($expected,$print_scripts,'当使用“异步”加载策略注册主脚本时,脚本连接不正确。异步脚本不应是脚本连接加载查询的一部分。');
    13691378    }
    13701379
     
    14051414应为$。=“<script type='text/javascript'src='/main-script.js'id='deferred-script-2-js'defer data-wp-strategy='defer'></script>\n”;
    14061415
    1407 $this->断言相同($expected,$print_scripts,'在注册其他阻塞脚本后,如果将主脚本注册为延迟脚本,则脚本连接不正确。延迟脚本不应是脚本连接加载器查询字符串的一部分。');
     1416$this->断言等于标记($expected,$print_scripts,'在注册其他阻塞脚本后,如果将主脚本注册为延迟脚本,则脚本连接不正确。延迟脚本不应是脚本连接加载器查询字符串的一部分。');
    14081417    }
    14091418
     
    14131422公共函数test_wp_enqueue_script_with_html5_support_does_not_contain_type_attribute(){
    14141423全局$wp_version;
    1415 add_theme_support(“html5”,数组(“script”));
    14161424
    14171425$GLOBALS['wp_scripts']=新wp_scripts();
     
    14221430$expected=“<script src='http://example.com?版本={$wp_version}'id='empty-deps-no-version-js'></script>\n“;
    14231431
    1424 $this->断言相同(需要$,get_echo('wp_print_scripts'));
     1432$this->断言等于标记(需要$,get_echo('wp_print_scripts'));
    14251433    }
    14261434
     
    14611469
    14621470//加油!
    1463 $this->断言相同(需要$,get_echo('wp_print_scripts'));
     1471$this->断言等于标记(需要$,get_echo('wp_print_scripts'));
    14641472
    14651473//没有可打印的脚本。
     
    15041512
    15051513//加油!
    1506 $this->断言相同(需要$,get_echo('wp_print_scripts'));
     1514$this->断言等于标记(需要$,get_echo('wp_print_scripts'));
    15071515
    15081516//没有要打印的脚本。
     
    15221530
    15231531//加油!
    1524 $this->断言相同(需要$,get_echo('wp_print_scripts'));
     1532$this->断言等于标记(需要$,get_echo('wp_print_scripts'));
    15251533
    15261534//没有要打印的脚本。
     
    15401548$预期=“<!--[if lt IE 9]>\n<script type='text/javascript'id='test-conditional-with-data-js-extra'>\n/*<![CDATA[*/\ntesting\n/*]]>*/\n</script>\n<![endif]-->\n”;
    15411549应为$。=“<!--[if lt IE 9]>\n<script type='text/javascript'src='http://example.com'id='test-conditional-with-data-js'></script>\n<![endif]-->\n“;
     1550$expected=str_replace(“'”,“”,$expected);
    15421551
    15431552//加油!
    1544 $this->断言相同(需要$,get_echo('wp_print_scripts'));
     1553$this->断言等于标记(需要$,get_echo('wp_print_scripts'));
    15451554
    15461555//没有要打印的脚本。
     
    15601569
    15611570//加油!
    1562 $this->断言相同(需要$,get_echo('wp_print_scripts'));
     1571$this->断言等于标记(需要$,get_echo('wp_print_scripts'));
    15631572
    15641573//没有要打印的脚本。
    1565 $this->断言相同(“”,get_echo(“wp_print_scripts”);
     1574$this->断言等于标记(“”,get_echo(“wp_print_scripts”);
    15661575    }
    15671576
     
    15891598wp_enqueue_script('handle-three');
    15901599
    1591 $this->断言相同(需要$,get_echo('wp_print_scripts'));
     1600$this->断言等于标记(需要$,get_echo('wp_print_scripts'));
    15921601    }
    15931602
     
    16771686$expected_footer=“<script type='text/javascript'src='/parent.js'id='parent-js'></script>\n”;
    16781687
    1679 $this->断言相同($expected_header,$header,'预期标头标记相同。');
    1680 $this->断言相同($expected_footer、$footer,'需要相同的页脚标记。');
     1688$this->断言等于标记($expected_header,$header,'预期标头标记相同。');
     1689$this->断言等于标记($expected_footer、$footer,'需要相同的页脚标记。');
    16811690    }
    16821691
     
    16981707$expected_footer.=“<script type='text/javascript'src='/parent.js'id='parent-js'></script>\n”;
    16991708
    1700 $this->断言相同($expected_header,$header,'预期标头标记相同。');
    1701 $this->断言相同($expected_footer、$footer,'需要相同的页脚标记。');
     1709$this->断言等于标记($expected_header,$header,'预期标头标记相同。');
     1710$this->断言等于标记($expected_footer、$footer,'需要相同的页脚标记。');
    17021711    }
    17031712
     
    17291738$expected_footer.=“<script type='text/javascript'src='/parent-footer.js'id='parent-footer-js'></script>\n”;
    17301739
    1731 $this->断言相同($expected_header,$header,'预期标头标记相同。');
    1732 $this->断言相同($expected_footer、$footer,'需要相同的页脚标记。');
     1740$this->断言相等标记($expected_header,$header,'预期标头标记相同。');
     1741$this->断言等于标记($expected_footer、$footer,'需要相同的页脚标记。');
    17331742    }
    17341743
     
    19531962$expected_localized.=“<script type='text/javascript'id='test-example-js-extra'>\n/*<![CDATA[*/\nvar testExample={\”foo\“:\”bar\“};\n/*]]>*/\n</script>\n”;
    19541963$expected_localized.=“<![endif]-->\n”;
     1964$expected_localized=str_replace(“”,“”,$expected _localize);
    19551965
    19561966$expected=“<!--[if gte IE 9]>\n”;
     
    19591969应为$。=“<script type='text/javascript'id='test-example-js-after'>\nconsole.log(\”after\“);\n</script>\n”;
    19601970应为$。=“<![endif]->\n”;
     1971$expected=str_replace(“”,“”,$expected);
    19611972
    19621973wp_enqueue_script(“test-example”,“example.com”,array(),null);
     
    21252136$print_scripts=$this->getActualOutput();
    21262137
    2127 $tail=substr($print_scripts,strrpos($print _ scripts,“<script type='text/javascript'src='/customize-dependency.js'id='customize-deependency-js'>”);
     2138$tail=substr($print_scripts,strrpos($print _ scripts,'<script type=“text/javascript”src=“/customize-dependency.js”id=“customize-deependency-js”>');
     2139
    21282140$this->assertEqualMarkup($expected_tail,$tail);
    21292141    }
     
    23052317应为$。=“<script type='text/javascript'src='/wp-includes/js/script.js'id='test-example-js'></script>\n”;
    23062318
    2307 $this->断言SameIgnoreEOL公司(需要$,get_echo('wp_print_scripts'));
     2319$this->断言等于标记(需要$,get_echo('wp_print_scripts'));
    23082320    }
    23092321
     
    23322344应为$。=“<script type='text/javascript'src='/wp-content/plugins/my plugin/js/script.js'id='plugin-example-js'></script>\n”;
    23332345
    2334 $this->断言SameIgnoreEOL公司(需要$,get_echo('wp_print_scripts'));
     2346$this->断言等于标记(需要$,get_echo('wp_print_scripts'));
    23352347    }
    23362348
     
    23592371应为$。=“<script type='text/javascript'src='/wp-content/themes/my-theme/js/script.js'id='eme-example-js'></script>\n”;
    23602372
    2361 $this->断言SameIgnoreEOL公司(需要$,get_echo('wp_print_scripts'));
     2373$this->断言等于标记(需要$,get_echo('wp_print_scripts'));
    23622374    }
    23632375
     
    23862398应为$。=“<script type='text/javascript'src='/wp-admin/js/script.js'id='script-handle-js'></script>\n”;
    23872399
    2388 $this->断言SameIgnoreEOL公司(需要$,get_echo('wp_print_scripts'));
     2400$this->断言等于标记(应为$,get_echo('wp_print_scripts'));
    23892401    }
    23902402
     
    24162428应为$。=“<script type='text/javascript'src='/wp-admin/js/script.js'id='test-example-js'></script>\n”;
    24172429
    2418 $this->断言SameIgnoreEOL公司(需要$,get_echo('wp_print_scripts'));
     2430$this->断言等于标记(需要$,get_echo('wp_print_scripts'));
    24192431    }
    24202432
     
    24452457应为$。=“<script type='text/javascript'src='/wp-includes/js/script.js'id='test-example-js'></script>\n”;
    24462458
    2447 $this->断言SameIgnoreEOL公司(需要$,get_echo('wp_print_scripts'));
     2459$this->断言等于标记(需要$,get_echo('wp_print_scripts'));
    24482460    }
    24492461
     
    24752487应为$。=“<script type='text/javascript'src='/wp-includes/js/script2.js'id='test-example-js'></script>\n”;
    24762488
    2477 $this->断言SameIgnoreEOL公司(需要$,get_echo('wp_print_scripts'));
     2489$this->断言等于标记(需要$,get_echo('wp_print_scripts'));
    24782490    }
    24792491
     
    28642876应为$。=“<script type='text/javascript'src='http://example.com'id='test-example-js'></script>\n“;
    28652877
    2866 $this->断言相同(应为$,get_echo('wp_print_scripts'));
     2878$this->断言等于标记(需要$,get_echo('wp_print_scripts'));
    28672879    }
    28682880
     
    29292941应为$。=“<script type='text/javascript'src='/default/common.js'id='common-js'></script>\n”;
    29302942
    2931 $this->断言相同(预期为$,$print_scripts);
     2943$this->断言等于标记(预期为$,$print_scripts);
    29322944    }
    29332945
     
    29682980     *
    29692981*@param string$markup标记。
    2970 *@return DOM元素主体元素包裹供应ed标记片段。
     2982*@返回DOM文档包含规范的文档ed标记片段。
    29712983     */
    29722984受保护的函数parse_markup_fragment($markup){
     
    29862998        }
    29872999
    2988 返回$身体;
    2989     }
    2990 
    2991     /**
    2992 *断言标记相等.
     3000返回$dom公司;
     3001    }
     3002
     3003    /**
     3004*断言标记相等规范化脚本标记后.
    29933005     *
    29943006*@param string$应为预期标记。
     
    29973009     */
    29983010受保护的函数assertEqualMarkup($expected,$actual,$message=“”){
     3011$expected_dom=$this->parse_markup_fragment($expected);
     3012$actual_dom=$this->parse_markup_fragment($actual);
     3013foreach(数组($expected_dom,$actual_dom)作为$dom){
     3014$xpath=新DOMXPath($dom);
     3015/**@var DOMElement$脚本*/
     3016
     3017//规格化类型属性。缺少时,默认为text/javascript。
     3018foreach($xpath->query(“//script[not(@type)]”)作为$script){
     3019$script->setAttribute('type','text/javascript');
     3020            }
     3021
     3022//规范化脚本内容以删除CDATA包装。
     3023foreach($xpath->query('//script[contains(text(),“<![CDATA[”)]')作为$script){
     3024$script->textContent=str_replace(
     3025阵列(
     3026“/*<![CDATA[*/\n”,
     3027“\n/*]]>*/”,
     3028                    ),
     3029                    '',
     3030$script->textContent
     3031                );
     3032            }
     3033
     3034//将兼容XHTML的布尔属性规范化为HTML5属性。
     3035foreach(数组(“async”,“defer”)作为$属性){
     3036foreach(迭代器_to_array($xpath->query(“//script[@{$attribute}='{$attribute}']”))作为$script){
     3037$script->removeAttribute($attribute);
     3038$script->setAttributeNode($dom->createAttribute($attribute));
     3039                }
     3040            }
     3041        }
     3042
    29993043$this->资产等于(
    3000             $此->parse_markup_fragment(需要$),
    3001             $此->parse_markup_fragment($actual),
     3044            $expected_dom->getElementsByTagName('body')->项(0),
     3045            $actual_dom->getElementsByTagName(“body”)->项(0),
    30023046$消息
    30033047        );
    30043048    }
     3049
     3050    /**
     3051*添加html5脚本主题支持。
     3052     */
     3053受保护函数add_html5_script_theme_support(){
     3054add_theme_support('html5',数组('script'));
     3055    }
    30053056}
  • trunk/tests/phpunit/tests/dependencies/wpInlineScriptTag.php

    51657兰特 56687兰特  
    120120        );
    121121    }
     122
     123    /**
     124*测试CDATA包装复制是否得到处理。
     125     *
     126*@门票58664
     127     */
     128公共函数test_get_inline_script_tag_with_duplicated_cdata_wrappers(){
     129删除主题支持('html5');
     130
     131$this->资产相同(
     132“<script type=\”text/javascript\“>\n/*<![CDATA[*/\n/*<0![CDATA[*/console.log('Hello World!');/*]]]><![CATA[>*/\n/*]]>*/\n</script>\n”,
     133wp_get_inline_script_tag(“/*<![CDATA[*/console.log('你好!');/*]]>*/”)
     134        );
     135    }
    122136}
注:请参见TracChangeset(跟踪变更集)获取有关使用变更集查看器的帮助。