Skip to content

Commit

Permalink
fix #610 ignore properties set to Object.prototype
Browse files Browse the repository at this point in the history
  • Loading branch information
amitguptagwl committed Sep 20, 2023
1 parent 75acbc5 commit 145521e
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 1 deletion.
3 changes: 2 additions & 1 deletion spec/html_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ const {XMLParser, XMLBuilder} = require("../src/fxp");
describe("XMLParser", function() {

it("should parse HTML with basic entities, <pre>, <script>, <br>", function() {
const html = `
Object.prototype.something = 'strange';
const html = `
<html lang="en">
<head>
<script>
Expand Down
1 change: 1 addition & 0 deletions spec/j2x_ordered_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,7 @@ describe("XMLBuilder", function () {
});

it("should build XML when leaf nodes or attributes are parsed to array", function () {
Object.prototype.something = 'strange';
const XMLdata = `<report>
<store>
<region>US</region>
Expand Down
9 changes: 9 additions & 0 deletions spec/j2x_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -624,5 +624,14 @@ describe("XMLBuilder", function() {

expect(result).toEqual(expected);
});
it("should ignore Object level prototype properties or function", function () {
Object.prototype.something = 'strange';
const jObj = { a: 1 }
const builder = new XMLBuilder();
const result = builder.build(jObj);
const expected = `<a>1</a>`;

expect(result).toEqual(expected);
});

});
1 change: 1 addition & 0 deletions src/xmlbuilder/json2xml.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ Builder.prototype.j2x = function(jObj, level) {
let attrStr = '';
let val = '';
for (let key in jObj) {
if(!jObj.hasOwnProperty(key)) continue;
if (typeof jObj[key] === 'undefined') {
// supress undefined node only if it is not an attribute
if (this.isAttribute(key)) {
Expand Down
4 changes: 4 additions & 0 deletions src/xmlbuilder/orderedJs2Xml.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ function arrToStr(arr, options, jPath, indentation) {
for (let i = 0; i < arr.length; i++) {
const tagObj = arr[i];
const tagName = propName(tagObj);
if(tagName === undefined) continue;

let newJPath = "";
if (jPath.length === 0) newJPath = tagName
else newJPath = `${jPath}.${tagName}`;
Expand Down Expand Up @@ -90,6 +92,7 @@ function propName(obj) {
const keys = Object.keys(obj);
for (let i = 0; i < keys.length; i++) {
const key = keys[i];
if(!obj.hasOwnProperty(key)) continue;
if (key !== ":@") return key;
}
}
Expand All @@ -98,6 +101,7 @@ function attr_to_str(attrMap, options) {
let attrStr = "";
if (attrMap && !options.ignoreAttributes) {
for (let attr in attrMap) {
if(!attrMap.hasOwnProperty(attr)) continue;
let attrVal = options.attributeValueProcessor(attr, attrMap[attr]);
attrVal = replaceEntitiesValue(attrVal, options);
if (attrVal === true && options.suppressBooleanAttributes) {
Expand Down

0 comments on commit 145521e

Please sign in to comment.