aboutsummaryrefslogtreecommitdiffstats
path: root/src/core/DOMEval.js
blob: b0238fd69213a306ca38e4cc2037976263b3083d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import document from "../var/document.js";

var preservedScriptAttributes = {
	type: true,
	src: true,
	nonce: true,
	noModule: true
};

function DOMEval( code, node, doc ) {
	doc = doc || document;

	var i, val,
		script = doc.createElement( "script" );

	script.text = code;
	if ( node ) {
		for ( i in preservedScriptAttributes ) {

			// Support: Firefox <=64 - 66+, Edge <=18+
			// Some browsers don't support the "nonce" property on scripts.
			// On the other hand, just using `getAttribute` is not enough as
			// the `nonce` attribute is reset to an empty string whenever it
			// becomes browsing-context connected.
			// See https://github.com/whatwg/html/issues/2369
			// See https://html.spec.whatwg.org/#nonce-attributes
			// The `node.getAttribute` check was added for the sake of
			// `jQuery.globalEval` so that it can fake a nonce-containing node
			// via an object.
			val = node[ i ] || node.getAttribute && node.getAttribute( i );
			if ( val ) {
				script.setAttribute( i, val );
			}
		}
	}
	doc.head.appendChild( script ).parentNode.removeChild( script );
}

export default DOMEval;