Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

XMLBuilder: introduce initial indentation level option #566

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions docs/v4/3.XMLBuilder.md
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,43 @@ Don't consider attributes while building XML. Other attributes related propertie
## indentBy
Applicable only if `format:true` is set.

## initialIndentationLevel
Applicable only if `format:true` is set.

Allows you to specify initial indentation level for the builder, with default value being `0`.

Example of setting `initialIndentationLevel` option to custom value and building XML:

```
const data = {
['trans-unit']: {
source: 'source string',
target: 'target string',
},
};

const options = {
format: true,
initialIndentationLevel: 1,
};

const builder = new XMLBuilder(options);
const result = builder.build(data);

console.log(result);
```

Output:

```
<trans-unit>
<source>source string</source>
<target>target string</target>
</trans-unit>
```

Giving us prefixed xml document by one indentation level.

## preserveOrder
When you parse a XML using XMLParser with `preserveOrder: true`, the result JS object has different structure. So parse that structure in original XML, you should set the same option while building the XML from that js object.

Expand Down
49 changes: 49 additions & 0 deletions spec/j2x_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -483,4 +483,53 @@ describe("XMLBuilder", function() {
expect(result).toEqual(expected);
});

it('should start indentation from 0 level by default', function() {
const data = {
['trans-unit']: {
source: 'source string',
target: 'target string',
},
};

const options = {
format: true,
};

const builder = new XMLBuilder(options);
const result = builder.build(data);

const expected =
'<trans-unit>\n' +
' <source>source string</source>\n' +
' <target>target string</target>\n' +
'</trans-unit>\n';

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

it('should start indentation from specified level', function() {
const data = {
['trans-unit']: {
source: 'source string',
target: 'target string',
},
};

const options = {
format: true,
initialIndentationLevel: 1,
};

const builder = new XMLBuilder(options);
const result = builder.build(data);

const expected =
' <trans-unit>\n' +
' <source>source string</source>\n' +
' <target>target string</target>\n' +
' </trans-unit>\n';

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

});
1 change: 1 addition & 0 deletions src/fxp.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ type XmlBuilderOptions = {
commentPropName: false | string;
format: boolean;
indentBy: string;
initialIndentationLevel: number;
arrayNodeName: string;
suppressEmptyNode: boolean;
suppressUnpairedNode: boolean;
Expand Down
5 changes: 3 additions & 2 deletions src/xmlbuilder/json2xml.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const defaultOptions = {
cdataPropName: false,
format: false,
indentBy: ' ',
initialIndentationLevel: 0,
suppressEmptyNode: false,
suppressUnpairedNode: true,
suppressBooleanAttributes: true,
Expand All @@ -33,7 +34,7 @@ const defaultOptions = {
stopNodes: [],
// transformTagName: false,
// transformAttributeName: false,
oneListGroup: false
oneListGroup: false,
};

function Builder(options) {
Expand Down Expand Up @@ -71,7 +72,7 @@ Builder.prototype.build = function(jObj) {
[this.options.arrayNodeName] : jObj
}
}
return this.j2x(jObj, 0).val;
return this.j2x(jObj, this.options.initialIndentationLevel).val;
}
};

Expand Down