Skip to content

Commit

Permalink
Resolves #27 - error for XML documents with comments (#28)
Browse files Browse the repository at this point in the history
  • Loading branch information
justinwilaby committed Jun 6, 2020
1 parent 46186fb commit fdfc6bc
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 3 deletions.
2 changes: 0 additions & 2 deletions lib/sax-wasm.js

This file was deleted.

Binary file modified lib/sax-wasm.wasm
Binary file not shown.
54 changes: 54 additions & 0 deletions src/js/__test__/comment.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import { Attribute, SaxEventType, SAXParser } from '../saxWasm'
import { readFileSync } from 'fs';
import { resolve } from 'path';
import { deepStrictEqual } from 'assert';

const saxWasm = readFileSync(resolve(__dirname, '../../../lib/sax-wasm.wasm'));

describe('SaxWasm', () => {
let parser: SAXParser;
let _event: SaxEventType;
let _data: Attribute[];

before(async () => {
parser = new SAXParser(SaxEventType.Comment | SaxEventType.Attribute | SaxEventType.OpenTag);

parser.eventHandler = function (event: SaxEventType, data: Attribute) {
_event = event;
_data.push(data as Attribute);
};
return parser.prepareWasm(saxWasm);
});

beforeEach(() => {
_data = [];
});

afterEach(() => {
parser.end();
});

it('should correctly recognize elements after reporting a comment', () => {
parser.write(Buffer.from(`<?xml version="1.0" encoding="UTF-8"?>
<plugin name="test 1 attr">
<name name="test 2 attr">the plugin name</name>
<!-- name="test 3 attr" some comment -->
<keywords name="test 4 attr">some,key,words</keywords>
</plugin>`));
const names = [
"name",
"plugin",
"name",
"name",
undefined,
"name",
"keywords"
];
deepStrictEqual(_data.length, 7);
_data.forEach((data, index) => deepStrictEqual(data.name, names[index]));
});
});
2 changes: 1 addition & 1 deletion src/sax/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,7 @@ impl SAXParser {
self.comment.push_str("--");
self.comment.push_str(grapheme);
}
self.state = State::Comment;
self.state = State::BeginWhitespace;
} else {
self.new_text();
}
Expand Down

0 comments on commit fdfc6bc

Please sign in to comment.