@@ -379,6 +379,7 @@ function tpl_metaheaders($alt = true)
379379 }
380380 jsinfo ();
381381 $ script .= 'var JSINFO = ' . json_encode ($ JSINFO , JSON_THROW_ON_ERROR ) . '; ' ;
382+ $ script .= '(function(H){H.className=H.className.replace(/\bno-js\b/, \'js \')})(document.documentElement); ' ;
382383 $ head ['script ' ][] = ['_data ' => $ script ];
383384
384385 // load jquery
@@ -411,39 +412,58 @@ function tpl_metaheaders($alt = true)
411412 * For tags having a body attribute specify the body data in the special
412413 * attribute '_data'. This field will NOT BE ESCAPED automatically.
413414 *
415+ * Inline scripts will use any nonce provided in the environment variable 'NONCE'.
416+ *
414417 * @param array $data
415418 *
416419 * @author Andreas Gohr <[email protected] > 417420 */
418421function _tpl_metaheaders_action ($ data )
419422{
423+ $ nonce = getenv ('NONCE ' );
420424 foreach ($ data as $ tag => $ inst ) {
421- if ($ tag == 'script ' ) {
422- echo "<!--[if gte IE 9]><!--> \n" ; // no scripts for old IE
423- }
424425 foreach ($ inst as $ attr ) {
425426 if (empty ($ attr )) {
426427 continue ;
427428 }
429+ if ($ nonce && $ tag == 'script ' && !empty ($ attr ['_data ' ])) {
430+ $ attr ['nonce ' ] = $ nonce ; // add nonce to inline script tags
431+ }
428432 echo '< ' , $ tag , ' ' , buildAttributes ($ attr );
429433 if (isset ($ attr ['_data ' ]) || $ tag == 'script ' ) {
430- if ($ tag == 'script ' && isset ($ attr ['_data ' ]))
431- $ attr ['_data ' ] = "/*<![CDATA[*/ " .
432- $ attr ['_data ' ] .
433- "\n/*!]]>*/ " ;
434-
435434 echo '> ' , $ attr ['_data ' ] ?? '' , '</ ' , $ tag , '> ' ;
436435 } else {
437436 echo '/> ' ;
438437 }
439438 echo "\n" ;
440439 }
441- if ($ tag == 'script ' ) {
442- echo "<!--<![endif]--> \n" ;
443- }
444440 }
445441}
446442
443+ /**
444+ * Output the given script as inline script tag
445+ *
446+ * This function will add the nonce attribute if a nonce is available.
447+ *
448+ * The script is NOT automatically escaped!
449+ *
450+ * @param string $script
451+ * @param bool $return Return or print directly?
452+ * @return string|void
453+ */
454+ function tpl_inlineScript ($ script , $ return = false )
455+ {
456+ $ nonce = getenv ('NONCE ' );
457+ if ($ nonce ) {
458+ $ script = '<script nonce=" ' . $ nonce . '"> ' . $ script . '</script> ' ;
459+ } else {
460+ $ script = '<script> ' . $ script . '</script> ' ;
461+ }
462+
463+ if ($ return ) return $ script ;
464+ echo $ script ;
465+ }
466+
447467/**
448468 * Print a link
449469 *
0 commit comments