script function

Element script({
  1. String? src,
  2. String? content,
  3. bool defer = false,
  4. bool async = false,
  5. bool module = false,
  6. String? nonce,
  7. String? integrity,
  8. String? crossorigin,
  9. String? id,
  10. String? classes,
  11. Map<String, Object?> attributes = const {},
})

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,
    },
  );
}