Skip to content

Commit 2897845

Browse files
committed
[AG] fix validator: invalidate XML with no closing tag
1 parent ff0b250 commit 2897845

3 files changed

Lines changed: 26 additions & 7 deletions

File tree

spec/attr_spec.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ describe("XMLParser", function() {
214214
const expected = {
215215
"err": {
216216
"code": "InvalidTag",
217-
"msg": "closing tag issue can't have attributes or invalid starting."
217+
"msg": "closing tag \"issue\" can't have attributes or invalid starting."
218218
}
219219
};
220220
const result = validator.validate(xmlData);
@@ -253,7 +253,7 @@ describe("XMLParser", function() {
253253
const expected = {
254254
"err": {
255255
"code": "InvalidAttr",
256-
"msg": "Attributes for rootNode have open quote"
256+
"msg": "Attributes for \"rootNode\" have open quote."
257257
}
258258
};
259259
const result = validator.validate(xmlData);

spec/validator_spec.js

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ describe("XMLParser", function() {
8989
expect(result).toEqual(expected);
9090

9191
xmlData = "<rootNode></rootnode 123>";
92-
expected = {code: "InvalidTag", msg: "closing tag rootnode can't have attributes or invalid starting."};
92+
expected = {code: "InvalidTag", msg: "closing tag \"rootnode\" can't have attributes or invalid starting."};
9393
result = validator.validate(xmlData).err;
9494
//console.log(JSON.stringify(result,null,4));
9595
expect(result).toEqual(expected);
@@ -411,4 +411,17 @@ describe("XMLParser", function() {
411411
var result = validator.validate(xmlData);
412412
expect(result).toEqual(true);
413413
});
414+
415+
it('should validate xml not properly closed', () => {
416+
const xmlData = `
417+
<name
418+
attribute1="attribute1"
419+
attribute2="attribute2"
420+
></name
421+
`;
422+
423+
const expected = {code: "InvalidTag", msg: "closing tag \"name\" don't have proper closing."};
424+
var result = validator.validate(xmlData).err;
425+
expect(result).toEqual(expected);
426+
});
414427
});

src/validator.js

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ exports.validate = function(xmlData, options) {
7474

7575
const result = readAttributeStr(xmlData, i);
7676
if (result === false) {
77-
return {err: {code: 'InvalidAttr', msg: 'Attributes for ' + tagName + ' have open quote'}};
77+
return {err: {code: 'InvalidAttr', msg: 'Attributes for "' + tagName + '" have open quote.'}};
7878
}
7979
let attrStr = result.value;
8080
i = result.index;
@@ -90,9 +90,13 @@ exports.validate = function(xmlData, options) {
9090
return isValid;
9191
}
9292
} else if (closingTag) {
93-
if (attrStr.trim().length > 0) {
93+
if(!result.tagClosed){
9494
return {
95-
err: {code: 'InvalidTag', msg: 'closing tag ' + tagName + " can't have attributes or invalid starting."},
95+
err: {code: 'InvalidTag', msg: 'closing tag "' + tagName + "\" don't have proper closing."},
96+
};
97+
}else if (attrStr.trim().length > 0) {
98+
return {
99+
err: {code: 'InvalidTag', msg: 'closing tag "' + tagName + "\" can't have attributes or invalid starting."},
96100
};
97101
} else {
98102
const otg = tags.pop();
@@ -235,6 +239,7 @@ var singleQuote = "'";
235239
function readAttributeStr(xmlData, i) {
236240
let attrStr = '';
237241
let startChar = '';
242+
let tagClosed = false;
238243
for (; i < xmlData.length; i++) {
239244
if (xmlData[i] === doubleQuote || xmlData[i] === singleQuote) {
240245
if (startChar === '') {
@@ -247,6 +252,7 @@ function readAttributeStr(xmlData, i) {
247252
}
248253
} else if (xmlData[i] === '>') {
249254
if (startChar === '') {
255+
tagClosed = true;
250256
break;
251257
}
252258
}
@@ -256,7 +262,7 @@ function readAttributeStr(xmlData, i) {
256262
return false;
257263
}
258264

259-
return {value: attrStr, index: i};
265+
return {value: attrStr, index: i, tagClosed: tagClosed};
260266
}
261267

262268
/**

0 commit comments

Comments
 (0)