Skip to content

Commit 1770c1b

Browse files
committed
fix: handle whitespace correctly in attribute parsing and support reference attributes in angular templates
1 parent b94056c commit 1770c1b

File tree

3 files changed

+9
-5
lines changed

3 files changed

+9
-5
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "xml-trueformat",
3-
"version": "0.9.3",
3+
"version": "0.9.4",
44
"description": "XML parser that 100% retains all formatting for creating identical XML on roundtrips",
55
"main": "./dist/cjs/index.js",
66
"module": "./dist/esm/index.mjs",

xmlParser.spec.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ describe('XmlParser', () => {
110110
expect(nodes).toEqual([new XmlText('Hello, '), new XmlElement('my', [], [], ' ', true), new XmlText(' World!')]);
111111
});
112112
it('should parse angular template', () => {
113-
const fragment = `<div someDirective></div>
113+
const fragment = `<div someDirective #someRef></div>
114114
<app-radio-group [formControl]="control">
115115
<app-radio-button *ngFor="let item of dialogData.items" [value]="item">
116116
{{ displayLabel(item) }}
@@ -119,8 +119,10 @@ describe('XmlParser', () => {
119119
<button type="button" (click)="confirmChoice()" i18n="@@APPLY_BUTTON">OK</button>
120120
`;
121121
const nodes = XmlParser.parseFragment(fragment);
122+
123+
expect(nodes.map(n => n.toString()).join('')).toEqual(fragment);
122124
expect(nodes).toEqual([
123-
new XmlElement('div', [new XmlAttribute('someDirective', '', ' ', '', '', '"', false)], [], ''),
125+
new XmlElement('div', [new XmlAttribute('someDirective', '', ' ', '', '', '"', false), new XmlAttribute('#someRef', '', ' ', '', '', '"', false)], [], ''),
124126
new XmlText('\n '),
125127
new XmlElement(
126128
'app-radio-group',

xmlParser.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ export class XmlParser {
140140
if (attrName === '') {
141141
throw new Error(`Expected attribute name at position ${this.pos}`);
142142
}
143-
const wsBeforeEqual = this.readWhitespace();
143+
let wsBeforeEqual = this.readWhitespace();
144144
let wsAfterEqual = '';
145145
let quote = '"';
146146
let value = '';
@@ -163,6 +163,8 @@ export class XmlParser {
163163
}
164164
} else {
165165
hasValue = false;
166+
this.pos -= wsBeforeEqual.length; // Rewind to before the whitespace
167+
wsBeforeEqual = '';
166168
}
167169
attributes.push(
168170
new XmlAttribute(
@@ -229,7 +231,7 @@ export class XmlParser {
229231
// Reads a name (letters, digits, underscore, hyphen, colon, period).
230232
readName(): string {
231233
const start = this.pos;
232-
while (this.pos < this.input.length && /[A-Za-z0-9_\-.:\[\]*()]/.test(this.input[this.pos])) {
234+
while (this.pos < this.input.length && /[A-Za-z0-9_\-.:\[\]*()#]/.test(this.input[this.pos])) {
233235
this.pos++;
234236
}
235237
return this.input.substring(start, this.pos);

0 commit comments

Comments
 (0)