html_sys/scripting/
script.rs

1/// The HTML `<script>` element
2///
3/// [MDN Documentation](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/script)
4#[doc(alias = "script")]
5#[non_exhaustive]
6#[derive(Debug, Clone, PartialEq, Default)]
7pub struct Script {
8    pub data_map: crate::DataMap,
9    global_attrs: crate::GlobalAttributes,
10    /// Address of the resource
11    pub src: std::option::Option<std::borrow::Cow<'static, str>>,
12    /// Type of script
13    pub type_: std::option::Option<std::borrow::Cow<'static, str>>,
14    /// Prevents execution in user agents that support module scripts
15    pub nomodule: std::option::Option<std::borrow::Cow<'static, str>>,
16    /// Execute script when available, without blocking while fetching
17    pub async_: std::option::Option<std::borrow::Cow<'static, str>>,
18    /// Defer script execution
19    pub defer: std::option::Option<std::borrow::Cow<'static, str>>,
20    /// How the element handles crossorigin requests
21    pub crossorigin: std::option::Option<std::borrow::Cow<'static, str>>,
22    /// Integrity metadata used in Subresource Integrity checks [SRI]
23    pub integrity: std::option::Option<std::borrow::Cow<'static, str>>,
24    /// Referrer policy for fetches initiated by the element
25    pub referrerpolicy: std::option::Option<std::borrow::Cow<'static, str>>,
26    /// Whether the element is potentially render-blocking
27    pub blocking: std::option::Option<std::borrow::Cow<'static, str>>,
28    /// Sets the priority for fetches initiated by the element
29    pub fetchpriority: std::option::Option<std::borrow::Cow<'static, str>>,
30}
31impl crate::RenderElement for Script {
32    fn write_opening_tag<W: std::fmt::Write>(&self, writer: &mut W) -> std::fmt::Result {
33        write!(writer, "<script")?;
34        if let Some(field) = self.src.as_ref() {
35            write!(writer, r#" src="{field}""#)?;
36        }
37        if let Some(field) = self.type_.as_ref() {
38            write!(writer, r#" type="{field}""#)?;
39        }
40        if let Some(field) = self.nomodule.as_ref() {
41            write!(writer, r#" nomodule="{field}""#)?;
42        }
43        if let Some(field) = self.async_.as_ref() {
44            write!(writer, r#" async="{field}""#)?;
45        }
46        if let Some(field) = self.defer.as_ref() {
47            write!(writer, r#" defer="{field}""#)?;
48        }
49        if let Some(field) = self.crossorigin.as_ref() {
50            write!(writer, r#" crossorigin="{field}""#)?;
51        }
52        if let Some(field) = self.integrity.as_ref() {
53            write!(writer, r#" integrity="{field}""#)?;
54        }
55        if let Some(field) = self.referrerpolicy.as_ref() {
56            write!(writer, r#" referrerpolicy="{field}""#)?;
57        }
58        if let Some(field) = self.blocking.as_ref() {
59            write!(writer, r#" blocking="{field}""#)?;
60        }
61        if let Some(field) = self.fetchpriority.as_ref() {
62            write!(writer, r#" fetchpriority="{field}""#)?;
63        }
64        write!(writer, "{}", self.global_attrs)?;
65        write!(writer, "{}", self.data_map)?;
66        write!(writer, ">")?;
67        Ok(())
68    }
69    #[allow(unused_variables)]
70    fn write_closing_tag<W: std::fmt::Write>(&self, writer: &mut W) -> std::fmt::Result {
71        write!(writer, "</script>")?;
72        Ok(())
73    }
74}
75impl std::fmt::Display for Script {
76    fn fmt(&self, writer: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
77        use crate::RenderElement;
78        self.write_opening_tag(writer)?;
79        self.write_closing_tag(writer)?;
80        Ok(())
81    }
82}
83impl std::ops::Deref for Script {
84    type Target = crate::GlobalAttributes;
85    fn deref(&self) -> &Self::Target {
86        &self.global_attrs
87    }
88}
89impl std::ops::DerefMut for Script {
90    fn deref_mut(&mut self) -> &mut Self::Target {
91        &mut self.global_attrs
92    }
93}