Skip to content

Commit e6fe231

Browse files
committed
Fix serializing template contents
Fixes #10155
1 parent 99ada2e commit e6fe231

File tree

3 files changed

+41
-6
lines changed

3 files changed

+41
-6
lines changed

crates/core/src/environment.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use bitflags::bitflags;
55
use browserslist::Distrib;
66
use serde::{Deserialize, Serialize};
77

8-
#[derive(Clone, Debug, Serialize, Deserialize)]
8+
#[derive(Clone, Debug, Default, Serialize, Deserialize)]
99
#[serde(rename_all = "camelCase")]
1010
pub struct Environment {
1111
pub context: EnvironmentContext,
@@ -421,7 +421,7 @@ impl Engines {
421421
}
422422

423423
bitflags! {
424-
#[derive(Clone, Copy, Hash, Debug, PartialEq, Eq)]
424+
#[derive(Clone, Copy, Hash, Debug, PartialEq, Eq, Default)]
425425
pub struct EnvironmentFlags: u8 {
426426
const IS_LIBRARY = 1 << 0;
427427
const SHOULD_OPTIMIZE = 1 << 1;
@@ -431,9 +431,10 @@ bitflags! {
431431

432432
impl_bitflags_serde!(EnvironmentFlags);
433433

434-
#[derive(PartialEq, Eq, Clone, Copy, Debug, Hash, Serialize, Deserialize)]
434+
#[derive(PartialEq, Eq, Clone, Copy, Debug, Hash, Default, Serialize, Deserialize)]
435435
#[serde(rename_all = "kebab-case")]
436436
pub enum EnvironmentContext {
437+
#[default]
437438
Browser,
438439
WebWorker,
439440
ServiceWorker,
@@ -470,16 +471,18 @@ impl EnvironmentContext {
470471
}
471472
}
472473

473-
#[derive(PartialEq, Eq, Clone, Copy, Debug, Hash, Serialize, Deserialize)]
474+
#[derive(PartialEq, Eq, Clone, Copy, Debug, Hash, Default, Serialize, Deserialize)]
474475
#[serde(rename_all = "lowercase")]
475476
pub enum SourceType {
477+
#[default]
476478
Module,
477479
Script,
478480
}
479481

480-
#[derive(PartialEq, Eq, Clone, Copy, Debug, Hash, Serialize, Deserialize)]
482+
#[derive(PartialEq, Eq, Clone, Copy, Debug, Hash, Default, Serialize, Deserialize)]
481483
#[serde(rename_all = "lowercase")]
482484
pub enum OutputFormat {
485+
#[default]
483486
Global,
484487
Commonjs,
485488
Esmodule,

crates/html/src/arena.rs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -626,6 +626,7 @@ impl<'a, 'arena> Serialize for SerializableHandle<'a, 'arena> {
626626
NodeData::Element {
627627
ref name,
628628
ref attrs,
629+
ref template_contents,
629630
..
630631
} => {
631632
serializer.start_elem(
@@ -640,6 +641,10 @@ impl<'a, 'arena> Serialize for SerializableHandle<'a, 'arena> {
640641
ops.push_front(SerializeOp::Open(c));
641642
child = c.previous_sibling.get();
642643
}
644+
645+
if let Some(template) = template_contents {
646+
ops.push_front(SerializeOp::Open(template));
647+
}
643648
}
644649

645650
NodeData::Doctype { ref name, .. } => serializer.write_doctype(name)?,
@@ -653,7 +658,13 @@ impl<'a, 'arena> Serialize for SerializableHandle<'a, 'arena> {
653658
ref contents,
654659
} => serializer.write_processing_instruction(target, &*contents.borrow())?,
655660

656-
NodeData::Document => panic!("Can't serialize Document node itself"),
661+
NodeData::Document => {
662+
let mut child = handle.last_child.get();
663+
while let Some(c) = child {
664+
ops.push_front(SerializeOp::Open(c));
665+
child = c.previous_sibling.get();
666+
}
667+
}
657668
},
658669

659670
SerializeOp::Close(name) => {

crates/html/src/lib.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -308,3 +308,24 @@ pub fn svg_react(mut options: SvgReactOptions) -> Result<PackageResult, ()> {
308308
})
309309
})
310310
}
311+
312+
#[cfg(test)]
313+
mod tests {
314+
use crate::transform_html;
315+
316+
#[test]
317+
fn test_transform() {
318+
let res = transform_html(crate::TransformOptions {
319+
code: "<html><body><template><div>test</div><span>hi</span></template></body></html>".into(),
320+
file_path: "foo.html".into(),
321+
xml: false,
322+
env: Default::default(),
323+
hmr: false,
324+
});
325+
326+
assert_eq!(
327+
std::str::from_utf8(&res.code).unwrap(),
328+
"<html><head></head><body><template><div>test</div><span>hi</span></template></body></html>"
329+
);
330+
}
331+
}

0 commit comments

Comments
 (0)