Skip to content

Commit 145521e

Browse files
committed
fix #610 ignore properties set to Object.prototype
1 parent 75acbc5 commit 145521e

File tree

5 files changed

+17
-1
lines changed

5 files changed

+17
-1
lines changed

Diff for: spec/html_spec.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ const {XMLParser, XMLBuilder} = require("../src/fxp");
44
describe("XMLParser", function() {
55

66
it("should parse HTML with basic entities, <pre>, <script>, <br>", function() {
7-
const html = `
7+
Object.prototype.something = 'strange';
8+
const html = `
89
<html lang="en">
910
<head>
1011
<script>

Diff for: spec/j2x_ordered_spec.js

+1
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,7 @@ describe("XMLBuilder", function () {
247247
});
248248

249249
it("should build XML when leaf nodes or attributes are parsed to array", function () {
250+
Object.prototype.something = 'strange';
250251
const XMLdata = `<report>
251252
<store>
252253
<region>US</region>

Diff for: spec/j2x_spec.js

+9
Original file line numberDiff line numberDiff line change
@@ -624,5 +624,14 @@ describe("XMLBuilder", function() {
624624

625625
expect(result).toEqual(expected);
626626
});
627+
it("should ignore Object level prototype properties or function", function () {
628+
Object.prototype.something = 'strange';
629+
const jObj = { a: 1 }
630+
const builder = new XMLBuilder();
631+
const result = builder.build(jObj);
632+
const expected = `<a>1</a>`;
633+
634+
expect(result).toEqual(expected);
635+
});
627636

628637
});

Diff for: src/xmlbuilder/json2xml.js

+1
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ Builder.prototype.j2x = function(jObj, level) {
7979
let attrStr = '';
8080
let val = '';
8181
for (let key in jObj) {
82+
if(!jObj.hasOwnProperty(key)) continue;
8283
if (typeof jObj[key] === 'undefined') {
8384
// supress undefined node only if it is not an attribute
8485
if (this.isAttribute(key)) {

Diff for: src/xmlbuilder/orderedJs2Xml.js

+4
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ function arrToStr(arr, options, jPath, indentation) {
2121
for (let i = 0; i < arr.length; i++) {
2222
const tagObj = arr[i];
2323
const tagName = propName(tagObj);
24+
if(tagName === undefined) continue;
25+
2426
let newJPath = "";
2527
if (jPath.length === 0) newJPath = tagName
2628
else newJPath = `${jPath}.${tagName}`;
@@ -90,6 +92,7 @@ function propName(obj) {
9092
const keys = Object.keys(obj);
9193
for (let i = 0; i < keys.length; i++) {
9294
const key = keys[i];
95+
if(!obj.hasOwnProperty(key)) continue;
9396
if (key !== ":@") return key;
9497
}
9598
}
@@ -98,6 +101,7 @@ function attr_to_str(attrMap, options) {
98101
let attrStr = "";
99102
if (attrMap && !options.ignoreAttributes) {
100103
for (let attr in attrMap) {
104+
if(!attrMap.hasOwnProperty(attr)) continue;
101105
let attrVal = options.attributeValueProcessor(attr, attrMap[attr]);
102106
attrVal = replaceEntitiesValue(attrVal, options);
103107
if (attrVal === true && options.suppressBooleanAttributes) {

0 commit comments

Comments
 (0)