script function
A <script>: an external one (script(src: '/app.js', defer: true))
or an inline one (script(content: 'start()')), not both.
Inline content is written as JavaScript, not escaped, so it must be
trusted code; a </script or <!-- in it is defused (see
RawTextNode) so it cannot close the element. To hand data to a
script, use jsonScript rather than interpolating it into content.
module writes type="module"; nonce is the request's
Content-Security-Policy nonce, for a page whose script-src lists one.
Implementation
Element script({
String? src,
String? content,
bool defer = false,
bool async = false,
bool module = false,
String? nonce,
String? integrity,
String? crossorigin,
String? id,
String? classes,
Map<String, Object?> attributes = const {},
}) {
if (src != null && content != null) {
throw ArgumentError(
'A <script> has either a src or inline content, not both.',
);
}
return _tag(
'script',
[if (content != null) RawTextNode(content, tag: 'script')],
id,
classes,
{
'src': src,
'type': module ? 'module' : null,
'defer': defer,
'async': async,
'nonce': nonce,
'integrity': integrity,
'crossorigin': crossorigin,
...attributes,
},
);
}