Comparing version 1.4.2 to 1.5.0
@@ -15,2 +15,3 @@ var common = require('./common'); | ||
common.ensureFlagExists('indentCdata', options); | ||
common.ensureFlagExists('indentAttributes', options); | ||
common.ensureFlagExists('indentInstruction', options); | ||
@@ -39,3 +40,3 @@ common.ensureFlagExists('fullTagEmptyElement', options); | ||
function writeAttributes(attributes, options) { | ||
function writeAttributes(attributes, options, depth) { | ||
if (options.ignoreAttributes) { | ||
@@ -48,13 +49,17 @@ return ''; | ||
attr = '' + attributes[key]; // ensure Number and Boolean are converted to String | ||
result += ' ' + key + '="' + attr.replace(/"/g, '"') + '"'; | ||
result += (options.spaces && options.indentAttributes? writeIndentation(options, depth+1, false) : ' ') | ||
result += key + '="' + attr.replace(/"/g, '"') + '"'; | ||
} | ||
} | ||
if (attributes && Object.keys(attributes).length && options.spaces && options.indentAttributes) { | ||
result += writeIndentation(options, depth, false); | ||
} | ||
return result; | ||
} | ||
function writeDeclaration(declaration, options) { | ||
return options.ignoreDeclaration ? '' : '<?xml' + writeAttributes(declaration[options.attributesKey], options) + '?>'; | ||
function writeDeclaration(declaration, options, depth) { | ||
return options.ignoreDeclaration ? '' : '<?xml' + writeAttributes(declaration[options.attributesKey], options, depth) + '?>'; | ||
} | ||
function writeInstruction(instruction, options) { | ||
function writeInstruction(instruction, options, depth) { | ||
if (options.ignoreInstruction) { | ||
@@ -70,3 +75,3 @@ return ''; | ||
if (typeof instruction[key] === 'object') { | ||
return '<?' + key + writeAttributes(instruction[key][options.attributesKey], options) + '?>'; | ||
return '<?' + key + writeAttributes(instruction[key][options.attributesKey], options, depth) + '?>'; | ||
} else { | ||
@@ -131,3 +136,3 @@ return '<?' + key + (instruction[key] ? ' ' + instruction[key] : '') + '?>'; | ||
if (element[options.attributesKey]) { | ||
xml += writeAttributes(element[options.attributesKey], options); | ||
xml += writeAttributes(element[options.attributesKey], options, depth); | ||
} | ||
@@ -159,3 +164,3 @@ if (options.fullTagEmptyElement || (element[options.elementsKey] && element[options.elementsKey].length) || (element[options.attributesKey] && element[options.attributesKey]['xml:space'] === 'preserve')) { | ||
instruction[element[options.nameKey]] = element[options.attributesKey] ? element : element[options.instructionKey]; | ||
return xml + (options.indentInstruction ? indent : '') + writeInstruction(instruction, options); | ||
return xml + (options.indentInstruction ? indent : '') + writeInstruction(instruction, options, depth); | ||
} | ||
@@ -204,3 +209,3 @@ }, ''); | ||
if (element[options.attributesKey]) { | ||
xml += writeAttributes(element[options.attributesKey], options); | ||
xml += writeAttributes(element[options.attributesKey], options, depth); | ||
} | ||
@@ -228,4 +233,4 @@ if (options.fullTagEmptyElement || hasContentCompact(element, options, true) || element[options.attributesKey] && element[options.attributesKey]['xml:space'] === 'preserve') { | ||
switch (key) { | ||
case options.declarationKey: xml += writeDeclaration(nodes[i], options); break; | ||
case options.instructionKey: xml += (options.indentInstruction ? writeIndentation(options, depth, firstLine) : '') + writeInstruction(nodes[i], options); break; | ||
case options.declarationKey: xml += writeDeclaration(nodes[i], options, depth); break; | ||
case options.instructionKey: xml += (options.indentInstruction ? writeIndentation(options, depth, firstLine) : '') + writeInstruction(nodes[i], options, depth); break; | ||
case options.attributesKey: case options.parentKey: break; // skip | ||
@@ -253,3 +258,3 @@ case options.textKey: xml += (options.indentText ? writeIndentation(options, depth, firstLine) : '') + writeText(nodes[i], options); break; | ||
if (js[options.declarationKey]) { | ||
xml += writeDeclaration(js[options.declarationKey], options); | ||
xml += writeDeclaration(js[options.declarationKey], options, 0); | ||
} | ||
@@ -256,0 +261,0 @@ if (js[options.elementsKey] && js[options.elementsKey].length) { |
@@ -25,2 +25,3 @@ var sax = require('sax'); | ||
common.ensureFlagExists('sanitize', options); | ||
common.ensureFlagExists('instructionHasAttributes', options); | ||
common.ensureFlagExists('captureSpacesBetweenElements', options); | ||
@@ -27,0 +28,0 @@ common.ensureKeyExists('declaration', options); |
{ | ||
"name": "xml-js", | ||
"version": "1.4.2", | ||
"version": "1.5.0", | ||
"description": "A convertor between XML text and Javascript object / JSON text.", | ||
@@ -5,0 +5,0 @@ "repository": { |
@@ -111,3 +111,3 @@ ![XML ⇔ JS/JSON](http://nashwaan.github.io/xml-js/images/logo.svg) | ||
To see the result of this code, see the output above in *Synopsis* section. | ||
To see the result of this code, see the output above in [Synopsis](#synopsis) section. | ||
@@ -164,2 +164,3 @@ Or [run and edit](https://runkit.com/587874e079a2f60013c1f5ac/587874e079a2f60013c1f5ad) this code live in the browser. | ||
| `indentCdata` | `false` | Whether to write CData in a new line and indent it. Will generate `<a>\n <![CDATA[foo]]></a>` instead of `<a><![CDATA[foo]]></a>`. See [discussion](https://github.com/nashwaan/xml-js/issues/14) | | ||
| `indentAttributes` | `false` | Whether to print attributes across multiple lines and indent them (when `spaces` is not `0`). See [example](https://github.com/nashwaan/xml-js/issues/31). | | ||
| `ignoreDeclaration` | `false` | Whether to ignore writing declaration directives of xml. For example, `<?xml?>` will be ignored. | | ||
@@ -166,0 +167,0 @@ | `ignoreInstruction` | `false` | Whether to ignore writing processing instruction of xml. For example, `<?go there?>` will be ignored. | |
@@ -42,5 +42,8 @@ export interface ElementCompact { | ||
declare namespace Options { | ||
interface XML2JSON extends XML2JS { | ||
spaces?: number | string | ||
} | ||
interface XML2JS extends ChangingKeyNames, IgnoreOptions { | ||
compact?: boolean | ||
spaces?: number | string | ||
trim?: boolean | ||
@@ -61,2 +64,3 @@ sanitize?: boolean | ||
indentCdata?: boolean | ||
indentAttributes?: boolean | ||
indentInstruction?: boolean | ||
@@ -93,3 +97,3 @@ fullTagEmptyElement?: boolean | ||
export function json2xml(json: string, options?: Options.JS2XML): string; | ||
export function xml2json(xml: string, options?: Options.XML2JS): string; | ||
export function xml2json(xml: string, options?: Options.XML2JSON): string; | ||
export function xml2js(xml: string, options?: Options.XML2JS): any; |
@@ -47,3 +47,3 @@ import { Element, ElementCompact } from './index' | ||
// xml2js | ||
let jsResult1: any = convert.xml2js(xml, {compact:true, spaces:4}); | ||
let jsResult1: any = convert.xml2js(xml, {compact:true}); | ||
let jsResult2: any = convert.xml2js(xml, {compact:false}); | ||
@@ -50,0 +50,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
67756
914
330