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 927392f commit 8624000
Show file tree
Hide file tree
Showing 4 changed files with 158 additions and 18 deletions.
9 changes: 9 additions & 0 deletions CHANGELOG.md
Expand Up @@ -4,6 +4,15 @@ 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.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.7.9](https://github.com/xmldom/xmldom/compare/0.7.8...0.7.9)

### Fixed
Expand Down
38 changes: 20 additions & 18 deletions lib/sax.js
Expand Up @@ -12,7 +12,7 @@ var tagNamePattern = new RegExp('^'+nameStartChar.source+nameChar.source+'*(?:\:
//S_TAG, S_ATTR, S_EQ, S_ATTR_NOQUOT_VALUE
//S_ATTR_SPACE, S_ATTR_END, S_TAG_SPACE, S_TAG_CLOSE
var S_TAG = 0;//tag name offerring
var S_ATTR = 1;//attr name offerring
var S_ATTR = 1;//attr name offerring
var S_ATTR_SPACE=2;//attr name end and space offer
var S_EQ = 3;//=space?
var S_ATTR_NOQUOT_VALUE = 4;//attr value(no quot value only)
Expand All @@ -36,7 +36,7 @@ ParseError.prototype = new Error();
ParseError.prototype.name = ParseError.name

function XMLReader(){

}

XMLReader.prototype = {
Expand Down Expand Up @@ -66,7 +66,7 @@ function parse(source,defaultNSMapCopy,entityMap,domBuilder,errorHandler){
function entityReplacer(a){
var k = a.slice(1,-1);
if(k in entityMap){
return entityMap[k];
return entityMap[k];
}else if(k.charAt(0) === '#'){
return fixedFromCharCode(parseInt(k.substr(1).replace('x','0x')))
}else{
Expand Down Expand Up @@ -95,7 +95,7 @@ function parse(source,defaultNSMapCopy,entityMap,domBuilder,errorHandler){
var lineEnd = 0;
var linePattern = /.*(?:\r\n?|\n)|.*$/g
var locator = domBuilder.locator;

var parseStack = [{currentNSMap:defaultNSMapCopy}]
var closeMap = {};
var start = 0;
Expand All @@ -120,7 +120,7 @@ function parse(source,defaultNSMapCopy,entityMap,domBuilder,errorHandler){
var tagName = source.substring(tagStart + 2, end).replace(/[ \t\n\r]+$/g, '');
var config = parseStack.pop();
if(end<0){

tagName = source.substring(tagStart+2).replace(/[\s<].*/,'');
errorHandler.error("end tag name: "+tagName+' is not complete:'+config.tagName);
end = tagStart+1+tagName.length;
Expand All @@ -147,7 +147,7 @@ function parse(source,defaultNSMapCopy,entityMap,domBuilder,errorHandler){
}else{
parseStack.push(config)
}

end++;
break;
// end elment
Expand All @@ -166,8 +166,8 @@ function parse(source,defaultNSMapCopy,entityMap,domBuilder,errorHandler){
//elStartEnd
var end = parseElementStartPart(source,tagStart,el,currentNSMap,entityReplacer,errorHandler);
var len = el.length;


if(!el.closed && fixSelfClosed(source,end,el.tagName,closeMap)){
el.closed = true;
if(!entityMap.nbsp){
Expand Down Expand Up @@ -297,7 +297,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 Expand Up @@ -431,7 +433,7 @@ function appendElement(el,domBuilder,currentNSMap){
}
//can not set prefix,because prefix !== ''
a.localName = localName ;
//prefix == null for no ns prefix attribute
//prefix == null for no ns prefix attribute
if(nsPrefix !== false){//hack!!
if(localNSMap == null){
localNSMap = {}
Expand All @@ -441,7 +443,7 @@ function appendElement(el,domBuilder,currentNSMap){
}
currentNSMap[nsPrefix] = localNSMap[nsPrefix] = value;
a.uri = NAMESPACE.XMLNS
domBuilder.startPrefixMapping(nsPrefix, value)
domBuilder.startPrefixMapping(nsPrefix, value)
}
}
var i = el.length;
Expand All @@ -453,7 +455,7 @@ function appendElement(el,domBuilder,currentNSMap){
a.uri = NAMESPACE.XML;
}if(prefix !== 'xmlns'){
a.uri = currentNSMap[prefix || '']

//{console.log('###'+a.qName,domBuilder.locator.systemId+'',currentNSMap,a.uri)}
}
}
Expand Down Expand Up @@ -504,7 +506,7 @@ function parseHtmlSpecialContent(source,elStartEnd,tagName,entityReplacer,domBui
domBuilder.characters(text,0,text.length);
return elEndStart;
//}

}
}
return elStartEnd+1;
Expand All @@ -521,7 +523,7 @@ function fixSelfClosed(source,elStartEnd,tagName,closeMap){
closeMap[tagName] =pos
}
return pos<elStartEnd;
//}
//}
}

function _copy (source, target) {
Expand Down Expand Up @@ -555,11 +557,11 @@ function parseDCC(source,start,domBuilder,errorHandler){//sure start with '<!'
var end = source.indexOf(']]>',start+9);
domBuilder.startCDATA();
domBuilder.characters(source,start+9,end-start-9);
domBuilder.endCDATA()
domBuilder.endCDATA()
return end+3;
}
//<!DOCTYPE
//startDTD(java.lang.String name, java.lang.String publicId, java.lang.String systemId)
//startDTD(java.lang.String name, java.lang.String publicId, java.lang.String systemId)
var matchs = split(source,start);
var len = matchs.length;
if(len>1 && /!doctype/i.test(matchs[0][0])){
Expand All @@ -577,7 +579,7 @@ function parseDCC(source,start,domBuilder,errorHandler){//sure start with '<!'
var lastMatch = matchs[len-1]
domBuilder.startDTD(name, pubid, sysid);
domBuilder.endDTD();

return lastMatch.index+lastMatch[0].length
}
}
Expand Down Expand Up @@ -626,7 +628,7 @@ ElementAttributes.prototype = {
getValue:function(i){return this[i].value}
// ,getIndex:function(uri, localName)){
// if(localName){
//
//
// }else{
// var qName = uri
// }
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 @@ -43,6 +43,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 8624000

Please sign in to comment.