diff --git a/crates/oxc_angular_compiler/src/parser/html/parser.rs b/crates/oxc_angular_compiler/src/parser/html/parser.rs index 681615bd6..0a7c9ee62 100644 --- a/crates/oxc_angular_compiler/src/parser/html/parser.rs +++ b/crates/oxc_angular_compiler/src/parser/html/parser.rs @@ -680,11 +680,15 @@ impl<'a> HtmlParser<'a> { self.advance(); // Validate self-closing: only void, custom, and foreign elements can be self-closed - // Foreign elements include those with explicit namespace prefix (e.g., svg:rect) - // or those with implicit namespace prefix (e.g., has implicitNamespacePrefix='svg') + // Foreign elements include those with explicit namespace prefix (e.g., svg:rect), + // with implicit namespace prefix (e.g., has implicitNamespacePrefix='svg'), + // or one taken from the element they are in (`
` in `` is `:svg:div`). + // Angular checks element tags only: a component tag is never reported. let tag_def = get_html_tag_definition(&local_name); - let is_foreign = has_ns_prefix || tag_def.implicit_namespace_prefix.is_some(); - if !(tag_def.can_self_close || is_foreign || tag_def.is_void) { + let is_foreign = has_ns_prefix + || tag_def.implicit_namespace_prefix.is_some() + || self.inherits_namespace(); + if !is_component && !(tag_def.can_self_close || is_foreign || tag_def.is_void) { let err = self.make_error( start, format!( diff --git a/crates/oxc_angular_compiler/src/parser/html/tags.rs b/crates/oxc_angular_compiler/src/parser/html/tags.rs index 704e6cdd6..58332396b 100644 --- a/crates/oxc_angular_compiler/src/parser/html/tags.rs +++ b/crates/oxc_angular_compiler/src/parser/html/tags.rs @@ -272,6 +272,21 @@ pub fn get_html_tag_definition(tag_name: &str) -> HtmlTagDefinition { ..Default::default() }, + // The other elements of Angular's DOM schema (`allKnownElementNames()` without a + // namespace), which `html_tags.ts` defines as `canSelfClose: false`: `
` is an error. + "a" | "abbr" | "address" | "article" | "aside" | "audio" | "b" | "bdi" | "bdo" + | "blockquote" | "body" | "button" | "canvas" | "caption" | "cite" | "code" + | "colgroup" | "content" | "data" | "datalist" | "del" | "details" | "dfn" | "dialog" + | "dir" | "div" | "dl" | "em" | "fieldset" | "figcaption" | "figure" | "font" + | "footer" | "form" | "frame" | "frameset" | "geolocation" | "h1" | "h2" | "h3" | "h4" + | "h5" | "h6" | "head" | "header" | "hgroup" | "html" | "i" | "iframe" | "ins" | "kbd" + | "keygen" | "label" | "legend" | "main" | "map" | "mark" | "marquee" | "media" + | "menu" | "menuitem" | "meter" | "nav" | "noscript" | "object" | "ol" | "output" + | "picture" | "progress" | "q" | "ruby" | "s" | "samp" | "search" | "section" + | "select" | "selectedcontent" | "slot" | "small" | "span" | "strong" | "sub" + | "summary" | "sup" | "table" | "template" | "time" | "u" | "ul" | "unknown" | "var" + | "video" => HtmlTagDefinition::default(), + // Default: unknown tags get canSelfClose: true _ => default_tag_definition(), } @@ -457,4 +472,11 @@ mod tests { let custom = get_html_tag_definition("my-custom-element"); assert!(custom.can_self_close); } + + #[test] + fn test_known_dom_elements_cannot_self_close() { + for name in ["div", "span", "button", "section", "table", "DIV", "video", "slot"] { + assert!(!get_html_tag_definition(name).can_self_close, "{name}"); + } + } } diff --git a/crates/oxc_angular_compiler/tests/hmr_fingerprint_test.rs b/crates/oxc_angular_compiler/tests/hmr_fingerprint_test.rs index 484e78585..ee2f4628c 100644 --- a/crates/oxc_angular_compiler/tests/hmr_fingerprint_test.rs +++ b/crates/oxc_angular_compiler/tests/hmr_fingerprint_test.rs @@ -58,9 +58,9 @@ fn same(a: &str, b: &str, classes: &[&str]) -> bool { // The HMR decision // ----------------------------------------------------------------- -const TWO: &str = "@Component({ selector: 'a', template: '', styles: ['.a{}'] }) +const TWO: &str = "@Component({ selector: 'a', template: '', styles: ['.a{}'] }) export class A { x = 1 } -@Component({ selector: 'b', template: ``, styles: '.b{}' }) +@Component({ selector: 'b', template: ``, styles: '.b{}' }) export class B {} "; @@ -68,13 +68,13 @@ export class B {} fn an_edit_inside_template_or_styles_keeps_the_fingerprint() { let classes = &["A", "B"]; for edited in [ - TWO.replace("", "changed"), + TWO.replace("", "changed"), TWO.replace(".a{}", ".a{color:red}"), - TWO.replace("``", "`${'x'}`"), + TWO.replace("``", "`${'x'}`"), TWO.replace("'.b{}'", "'.b{} .c{}'"), TWO.replace("['.a{}']", "['.a{}', '.more{}']"), TWO.replace("['.a{}']", "[]"), - TWO.replace("", "both").replace(".a{}", ""), + TWO.replace("", "both").replace(".a{}", ""), ] { assert!(same(TWO, &edited, classes), "{edited}"); } @@ -87,11 +87,11 @@ fn an_edit_anywhere_else_changes_the_fingerprint() { TWO.replace("x = 1", "x = 2"), TWO.replace("selector: 'a'", "selector: 'aa'"), TWO.replace("export class B {}", "export class B {}\n// trailing comment"), - TWO.replace("template: ''", "templateUrl: './a.html'"), + TWO.replace("template: ''", "templateUrl: './a.html'"), // The shape of a value is kept: a string and an array are not the same. TWO.replace("['.a{}']", "''"), // The quote style is kept too. - TWO.replace("''", "\"\""), + TWO.replace("''", "\"\""), ] { assert!(!same(TWO, &edited, classes), "{edited}"); } @@ -114,9 +114,9 @@ fn resource_urls_stay_in_the_fingerprint() { #[test] fn only_the_named_classes_are_read() { assert_eq!(components(TWO, &["A"]), ["A"]); - assert!(same(TWO, &TWO.replace("", "x"), &["A"])); - assert!(!same(TWO, &TWO.replace("", "x"), &["A"])); - assert!(!same(TWO, &TWO.replace("", "x"), &[])); + assert!(same(TWO, &TWO.replace("", "x"), &["A"])); + assert!(!same(TWO, &TWO.replace("", "x"), &["A"])); + assert!(!same(TWO, &TWO.replace("", "x"), &[])); assert_eq!(fingerprint(TWO, &["Nope"]).excluded, []); } @@ -124,7 +124,7 @@ fn only_the_named_classes_are_read() { /// file broken (here an unescaped quote inside the template) reloads. #[test] fn a_syntax_error_excludes_nothing() { - let broken = TWO.replace("", "it's"); + let broken = TWO.replace("", "it's"); let result = fingerprint(&broken, &["A", "B"]); assert_eq!(result.excluded, []); assert_eq!(result.components, NONE); @@ -155,7 +155,7 @@ fn transform_reports_the_same_fingerprint() { #[test] fn transform_keeps_a_jit_components_template() { let source = format!( - "{IMPORT}@Component({{ selector: 'a', template: '' }})\nexport class A {{}}\n@Component({{ selector: 'b', template: '', jit: true }})\nexport class B {{}}\n" + "{IMPORT}@Component({{ selector: 'a', template: '' }})\nexport class A {{}}\n@Component({{ selector: 'b', template: '', jit: true }})\nexport class B {{}}\n" ); let allocator = Allocator::default(); let options = TransformOptions { hmr: true, ..TransformOptions::default() }; @@ -165,7 +165,7 @@ fn transform_keeps_a_jit_components_template() { result.hmr_fingerprint.as_deref(), Some(fingerprint_raw(&source, &["A"]).fingerprint.as_str()) ); - let edited_jit = source.replace("", "x"); + let edited_jit = source.replace("", "x"); assert_ne!( result.hmr_fingerprint.as_deref(), Some(fingerprint_raw(&edited_jit, &["A"]).fingerprint.as_str()) @@ -180,11 +180,11 @@ fn transform_keeps_a_jit_components_template() { fn reads_a_nested_component() { let source = format!( "{IMPORT}export function make() {{ - @Component({{ selector: 'a', template: '', styles: ['.a{{}}'] }}) + @Component({{ selector: 'a', template: '', styles: ['.a{{}}'] }}) class A {{ x = 1 }} return A; }} -function one() {{ @Component({{ template: '' }}) class B {{}} return B; }} +function one() {{ @Component({{ template: '' }}) class B {{}} return B; }} function two() {{ @Component({{ template: '' }}) class B {{}} return B; }} " ); @@ -206,10 +206,10 @@ function two() {{ @Component({{ template: '' }}) class B {{}} return B; }} assert_eq!(update.fingerprint.as_deref(), Some(fingerprint.as_str())); let of = |edited: String| fingerprint_raw(&edited, &["A"]).fingerprint; - assert_eq!(of(source.replace("", "x")), fingerprint); + assert_eq!(of(source.replace("", "x")), fingerprint); assert_eq!(of(source.replace(".a{}", ".a{color:red}")), fingerprint); assert_ne!(of(source.replace("x = 1", "x = 2")), fingerprint); - assert_ne!(of(source.replace("", "x")), fingerprint); + assert_ne!(of(source.replace("", "x")), fingerprint); } // ----------------------------------------------------------------- @@ -225,7 +225,7 @@ fn leaves_a_namespace_member_out() { @Component({{ selector: 'n', template: '' }}) export class App {{}} }} -@Component({{ selector: 'app', template: '' }}) +@Component({{ selector: 'app', template: '' }}) export class App {{}} " ); @@ -238,7 +238,7 @@ export class App {{}} assert_eq!(fingerprint_raw(&source, &["App"]).fingerprint, fingerprint); let of = |edited: String| fingerprint_raw(&edited, &["App"]).fingerprint; - assert_eq!(of(source.replace("", "x")), fingerprint); + assert_eq!(of(source.replace("", "x")), fingerprint); assert_ne!(of(source.replace("", "x")), fingerprint); } @@ -623,11 +623,11 @@ fn ignores_decoys_elsewhere_in_the_source() { ] { assert_eq!(fields(body, &["Foo"]), ["''"], "{body}"); } - let body = "@Component({ template: '' }) + let body = "@Component({ template: '' }) class First {} const helper = () => '@Component({...})' -@Component({ template: '' }) +@Component({ template: '' }) class Second {}"; assert_eq!(components(body, &["First", "Second"]), ["First", "Second"]); - assert_eq!(fields(body, &["First", "Second"]), ["''", "''"]); + assert_eq!(fields(body, &["First", "Second"]), ["''", "''"]); } diff --git a/crates/oxc_angular_compiler/tests/html_parser_test.rs b/crates/oxc_angular_compiler/tests/html_parser_test.rs index 65663ae87..70a9e578e 100644 --- a/crates/oxc_angular_compiler/tests/html_parser_test.rs +++ b/crates/oxc_angular_compiler/tests/html_parser_test.rs @@ -2933,10 +2933,56 @@ mod more_error_tests { fn should_report_self_closing_html_element() { // TS: it("should report self closing html element") //

is self-closing but p is not a void element, not a custom element - let _errors = parse_errors("

"); - // Angular reports: 'Only void, custom and foreign elements can be self closed "p"' - // Our parser may or may not report this - // For now, just verify parsing doesn't panic + assert_eq!( + parse_errors("

"), + vec!["Only void, custom and foreign elements can be self closed \"p\"".to_string()] + ); + } + + #[test] + fn should_report_self_closing_known_dom_element() { + // Angular defines every element of its DOM schema as `canSelfClose: false` + // (`html_tags.ts`), not only the ones with a definition of their own. + for (html, name) in [ + ("

", "div"), + ("x", "span"), + ("
", "DIV"), + ("