queryXPath('//script'); static::addNonceAttributes($scriptElems, static::$placeholder); // Apply to styles $styleElems = $doc->queryXPath('//style'); static::addNonceAttributes($styleElems, static::$placeholder); return $doc->getBodyInnerHtml(); } /** * Apply the give nonce value to the given prepared HTML. */ public static function apply(string $html, string $nonce): string { return str_replace(static::$placeholder, $nonce, $html); } protected static function addNonceAttributes(DOMNodeList $nodes, string $attrValue): void { /** @var DOMElement $node */ foreach ($nodes as $node) { $node->setAttribute('nonce', $attrValue); } } }