Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: make manually inserted script tag, spec compliant. #749

Merged
merged 5 commits into from
Mar 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion site/content/assets.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ Classic script assets processed by Trunk must follow these three rules:
- Must have the attribute `data-trunk`.
- Must have the attribute `src`, pointing to a script file

This will typically look like: `<script data-trunk src="{path}" ..other options here.. />`. All `<script data-trunk .../>` HTML elements will be replaced with the output HTML of the associated pipeline.
This will typically look like: `<script data-trunk src="{path}" ..other options here..></script>`. All `<script data-trunk ...></script>` HTML elements will be replaced with the output HTML of the associated pipeline.

Trunk will copy script files found in the source HTML without content modification. This content is hashed for cache control. The `src` attribute must be included in the script pointing to the script file to be processed.

Expand Down
6 changes: 3 additions & 3 deletions src/pipelines/js.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ impl Js {
// Build the path to the target asset.
let src_attr = attrs
.get(ATTR_SRC)
.context(r#"required attr `src` missing for <script data-trunk .../> element"#)?;
.context(r#"required attr `src` missing for <script data-trunk ...> element"#)?;
let mut path = PathBuf::new();
path.extend(src_attr.split('/'));
let asset = AssetFile::new(&html_dir, path).await?;
Expand Down Expand Up @@ -134,13 +134,13 @@ pub struct JsOutput {

impl JsOutput {
pub async fn finalize(self, dom: &mut Document) -> Result<()> {
let mut attrs = self.attrs.clone();
let mut attrs = self.attrs;
self.integrity.insert_into(&mut attrs);

dom.replace_with_html(
&super::trunk_script_id_selector(self.id),
&format!(
r#"<script src="{base}{file}"{attrs}/>"#,
r#"<script src="{base}{file}"{attrs}></script>"#,
attrs = AttrWriter::new(&attrs, AttrWriter::EXCLUDE_SCRIPT),
base = &self.cfg.public_url,
file = self.file
Expand Down
1 change: 1 addition & 0 deletions src/pipelines/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -450,6 +450,7 @@ impl Document {
Ok(())
}

/// Will silently fail when attempting to append to [Void Element](https://developer.mozilla.org/en-US/docs/Glossary/Void_element).
fn append_html(&mut self, selector: &str, html: &str) -> Result<()> {
self.select_mut(selector, |el| {
el.append(html, lol_html::html_content::ContentType::Html);
Expand Down