Skip to content

Commit

Permalink
Fix/102 self closing tags (#103)
Browse files Browse the repository at this point in the history
* 3.0.2

* fixes #102 - Close event not fired for selfClosing tags
  • Loading branch information
justinwilaby authored Jan 27, 2025
1 parent c4ed2f8 commit 0e75377
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 6 deletions.
Binary file modified lib/sax-wasm.wasm
Binary file not shown.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "sax-wasm",
"version": "3.0.1",
"version": "3.0.2",
"repository": {
"type": "git",
"url": "https://github.com/justinwilaby/sax-wasm.git"
Expand Down
4 changes: 2 additions & 2 deletions src/js/__test__/jsx.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ describe('When parsing JSX, the SaxWasm', () => {
</Component>`));

deepStrictEqual(_event,SaxEventType.CloseTag);
deepStrictEqual(_data[0].name,'SignIn');
deepStrictEqual(_data[1].name,'User');
deepStrictEqual(_data[0].name,'User');
deepStrictEqual(_data[1].name,'SignIn');
deepStrictEqual(_data[2].name,'Component');
});

Expand Down
19 changes: 19 additions & 0 deletions src/js/__test__/tag.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,25 @@ describe('SaxWasm', () => {
deepEqual(g.selfClosing, false);
});

it('should report selfClosing tags correctly when there is a space after the slash', () => {
parser.events = SaxEventType.CloseTag;

parser.write(Buffer.from(`<Div>
<Div type="JS" viewName="myapp.view.Home" />
<Div type="JSON" viewName="myapp.view.Home" />
<Div type="HTML" viewName="myapp.view.Home" />
<Div type="Template" viewName="myapp.view.Home" />
<AnotherSelfClosingDiv type="Template" viewName="myapp.view.Home" />
</Div>`));
const [, div, div2, div3, div4, div5] = _data;
deepEqual(div.selfClosing, true);
deepEqual(div2.selfClosing, true);
deepEqual(div3.selfClosing, true);
deepEqual(div4.selfClosing, true);
deepEqual(div5.selfClosing, false);
});

it('should handle the BOM', () => {
parser.events = SaxEventType.OpenTag;
parser.write(Buffer.from('\uFEFF<div></div>'));
Expand Down
43 changes: 42 additions & 1 deletion src/sax/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1009,7 +1009,17 @@ impl<'a> SAXParser<'a> {
self.event_handler.handle_event(Event::OpenTag, Entity::Tag(&tag_box));
self.dispatched.push(Dispatched::Tag(tag_box));
}
self.tags.push(tag);

if self.events[Event::CloseTag] && self_closing {
tag.hydrate(self.source_ptr);
let tag_box = Box::new(tag.clone());
self.event_handler.handle_event(Event::CloseTag, Entity::Tag(&tag_box));
self.dispatched.push(Dispatched::Tag(tag_box));
}

if !self_closing {
self.tags.push(tag);
}

self.state = State::BeginWhitespace;
}
Expand Down Expand Up @@ -1565,4 +1575,35 @@ the plugin

Ok(())
}
#[test]
fn test_self_closing_tag() -> Result<()> {
let event_handler = TextEventHandler::new();
let mut sax = SAXParser::new(&event_handler);
let mut events = [false; 10];
events[Event::CloseTag] = true;
sax.events = events;
let str = r#"
<Div>
<Div type="JS" viewName="myapp.view.Home" />
<Div type="JSON" viewName="myapp.view.Home" />
<Div type="HTML" viewName="myapp.view.Home" />
<Div type="Template" viewName="myapp.view.Home" />
<!-- This one will be correctly "closed" -->
<AnotherSelfClosingDiv type="Template" viewName="myapp.view.Home" />
</Div>"#;

sax.write(str.as_bytes());
sax.identity();

let tags = event_handler.tags.borrow();
assert_eq!(tags.len(), 6);
assert_eq!(tags[0].self_closing, true);
assert_eq!(tags[1].self_closing, true);
assert_eq!(tags[2].self_closing, true);
assert_eq!(tags[3].self_closing, true);
assert_eq!(tags[4].self_closing, true);
assert_eq!(tags[5].self_closing, false);
Ok(())
}
}

0 comments on commit 0e75377

Please sign in to comment.