Skip to content

Commit

Permalink
fix: properly parse closing where the last attribute has no value (#485)
Browse files Browse the repository at this point in the history
Closes #486
  • Loading branch information
Bulandent authored and karfau committed Mar 31, 2023
1 parent 238b1ea commit eae888a
Show file tree
Hide file tree
Showing 5 changed files with 169 additions and 20 deletions.
18 changes: 18 additions & 0 deletions CHANGELOG.md
Expand Up @@ -4,6 +4,24 @@ All notable changes to this project will be documented in this file.

This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [0.8.7](https://github.com/xmldom/xmldom/compare/0.8.6...0.8.7)

### Fixed

- properly parse closing where the last attribute has no value [`#485`](https://github.com/xmldom/xmldom/pull/485) / [`#486`](https://github.com/xmldom/xmldom/issues/486)

Thank you, [@bulandent](https://github.com/bulandent), for your contributions


## [0.7.10](https://github.com/xmldom/xmldom/compare/0.7.9...0.7.10)

### Fixed

- properly parse closing where the last attribute has no value [`#485`](https://github.com/xmldom/xmldom/pull/485) / [`#486`](https://github.com/xmldom/xmldom/issues/486)

Thank you, [@bulandent](https://github.com/bulandent), for your contributions


## [0.8.6](https://github.com/xmldom/xmldom/compare/0.8.5...0.8.6)

### Fixed
Expand Down
4 changes: 3 additions & 1 deletion lib/sax.js
Expand Up @@ -303,7 +303,9 @@ function parseElementStartPart(source,start,el,currentNSMap,entityReplacer,error
el.closed = true;
case S_ATTR_NOQUOT_VALUE:
case S_ATTR:
case S_ATTR_SPACE:
break;
case S_ATTR_SPACE:
el.closed = true;
break;
//case S_EQ:
default:
Expand Down
38 changes: 19 additions & 19 deletions test/dom/document.test.js
Expand Up @@ -165,31 +165,31 @@ describe('Document.prototype', () => {
})
describe('replaceChild', () => {
it('should remove the only element and add the new one', () => {
const doc = new DOMImplementation().createDocument('', 'xml');
const initialFirstChild = doc.firstChild;
const replacement = doc.createElement('replaced');
const doc = new DOMImplementation().createDocument('', 'xml')
const initialFirstChild = doc.firstChild
const replacement = doc.createElement('replaced')

doc.replaceChild(replacement, doc.firstChild);
doc.replaceChild(replacement, doc.firstChild)

expect(doc.childNodes).toHaveLength(1);
expect(initialFirstChild.parentNode).toBeNull();
expect(doc.documentElement.name).toBe(replacement.name);
});
});
expect(doc.childNodes).toHaveLength(1)
expect(initialFirstChild.parentNode).toBeNull()
expect(doc.documentElement.name).toBe(replacement.name)
})
})
describe('removeChild', () => {
it('should remove all connections to node', () => {
const doc = new DOMImplementation().createDocument('', 'xml');
doc.insertBefore(doc.createComment('just a comment'), doc.firstChild);
expect(doc.childNodes).toHaveLength(2);
const initialElement = doc.firstChild;
const doc = new DOMImplementation().createDocument('', 'xml')
doc.insertBefore(doc.createComment('just a comment'), doc.firstChild)
expect(doc.childNodes).toHaveLength(2)
const initialElement = doc.firstChild

doc.removeChild(initialElement);
doc.removeChild(initialElement)

// expect(doc.documentElement).toBeNull();
expect(initialElement.parentNode).toBeNull();
expect(initialElement.nextSibling).toBeNull();
expect(initialElement.parentNode).toBeNull()
expect(initialElement.nextSibling).toBeNull()
// expect(initialElement.previousSibling).toBeNull();
expect(doc.childNodes).toHaveLength(1);
});
});
expect(doc.childNodes).toHaveLength(1)
})
})
})
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 @@ -49,6 +49,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([
Expand Down

0 comments on commit eae888a

Please sign in to comment.