Skip to content

Commit a77ab27

Browse files
committed
introduce a template function to output a inline script
This handles the output of a potentially available nonce.
1 parent e0aa677 commit a77ab27

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

inc/Ui/Editor.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ public function show()
150150
// start editor html output
151151
if ($wr) {
152152
// sets changed to true when previewed
153-
echo '<script>/*<![CDATA[*/textChanged = ' . ($mod ? 'true' : 'false') . '/*!]]>*/</script>';
153+
tpl_inlineScript('textChanged = ' . ($mod ? 'true' : 'false') . ';');
154154
}
155155

156156
// print intro locale text (edit, rditrev, or read.txt)

inc/template.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -440,6 +440,30 @@ function _tpl_metaheaders_action($data)
440440
}
441441
}
442442

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+
443467
/**
444468
* Print a link
445469
*

0 commit comments

Comments
 (0)