Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: properly parse closing where the last attribute has no value #485

Merged
merged 4 commits into from Mar 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions lib/sax.js
Expand Up @@ -313,7 +313,9 @@ function parseElementStartPart(source, start, el, currentNSMap, entityReplacer,
el.closed = true;
case S_ATTR_NOQUOT_VALUE:
case S_ATTR:
break;
case S_ATTR_SPACE:
el.closed = true;
break;
//case S_EQ:
default:
Expand Down
18 changes: 18 additions & 0 deletions test/parse/__snapshots__/parse-element.test.js.snap
@@ -1,5 +1,23 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`XML Node Parse closing tag with unquoted value ending with // 1`] = `
Object {
"warning": Array [
"[xmldom warning] attribute \\"lazy/\\" missed quot(\\")!
@#[line:3,col:3]",
],
}
`;

exports[`XML Node Parse closing tag with unquoted value including / followed by space / 1`] = `
Object {
"warning": Array [
"[xmldom warning] attribute \\"lazy/\\" missed quot(\\")!!
@#[line:3,col:3]",
],
}
`;

exports[`XML Node Parse namespaced attributes unclosed root tag will be closed 1`] = `
Object {
"actual": "<xml xmlns=\\"1\\" xmlns:a=\\"2\\" a:test=\\"3/\\"/>",
Expand Down
111 changes: 111 additions & 0 deletions test/parse/parse-element.test.js
Expand Up @@ -39,6 +39,117 @@ describe('XML Node Parse', () => {
expect(actual).toBe(`<xml><book/><title>Harry Potter</title></xml>`);
});

it('closing tag without attribute value', () => {
const actual = new DOMParser()
.parseFromString(
`<template>
<view>
<image lazy />
<image></image>
</view>
</template>`,
'text/xml'
)
.toString();
expect(actual).toBe(
`<template>
<view>
<image lazy="lazy"/>
<image/>
</view>
</template>`
);
});
it('closing tag with unquoted value following /', () => {
const actual = new DOMParser()
.parseFromString(
`<template>
<view>
<image lazy=lazy/>
<image></image>
</view>
</template>`,
'text/xml'
)
.toString();
expect(actual).toBe(
`<template>
<view>
<image lazy="lazy"/>
<image/>
</view>
</template>`
);
});
it('closing tag with unquoted value following space and /', () => {
const actual = new DOMParser()
.parseFromString(
`<template>
<view>
<image lazy=lazy />
<image></image>
</view>
</template>`,
'text/xml'
)
.toString();
expect(actual).toBe(
`<template>
<view>
<image lazy="lazy"/>
<image/>
</view>
</template>`
);
});
it('closing tag with unquoted value including / followed by space /', () => {
const { errors, parser } = getTestParser();
const actual = parser
.parseFromString(
`<template>
<view>
<image lazy=lazy/ />
<image></image>
</view>
</template>`,
'text/xml'
)
.toString();
expect(errors).toMatchSnapshot();
expect(actual).toBe(
`<template>
<view>
<image lazy="lazy/"/>
<image/>
</view>
</template>`
);
});
it('closing tag with unquoted value ending with //', () => {
const { errors, parser } = getTestParser();

const actual = parser
.parseFromString(
`<template>
<view>
<image lazy=lazy//>
<image></image>
</view>
</template>`,
'text/xml'
)
.toString();
expect(errors).toMatchSnapshot();
expect(actual).toBe(
`<template>
<view>
<image lazy="lazy/"/>
<image/>
</view>
</template>`
);
});

describe('simple attributes', () => {
describe('nothing special', () => {
it.each(['<xml a="1" b="2"></xml>', '<xml a="1" b="2" ></xml>', '<xml a="1" b="2" />'])('%s', (input) => {
Expand Down