Comparing version 1.3.0 to 1.3.1
@@ -28,2 +28,3 @@ #!/usr/bin/env node | ||
{arg: 'always-children', type: 'flag', option:'alwaysChildren', desc: 'Every element will always contain sub-elements (applicable if --compact is not set).'}, | ||
{arg: 'instruction-attr', type: 'flag', option:'instructionHasAttributes', desc: 'Whether to parse contents of processing instruction as attributes.'}, | ||
{arg: 'full-tag', type: 'flag', option:'fullTagEmptyElement', desc: 'XML elements will always be in <a></a> form.'}, | ||
@@ -30,0 +31,0 @@ {arg: 'no-decl', type: 'flag', option:'ignoreDeclaration', desc: 'Declaration instruction <?xml?> will be ignored.'}, |
@@ -1,1 +0,1 @@ | ||
module.exports = require('./lib'); | ||
module.exports = require('./lib'); |
@@ -38,3 +38,6 @@ var common = require('./common'); | ||
function writeAttributes(attributes) { | ||
function writeAttributes(attributes, options) { | ||
if (options.ignoreAttributes) { | ||
return ''; | ||
} | ||
var key, result = ''; | ||
@@ -50,6 +53,9 @@ for (key in attributes) { | ||
function writeDeclaration(declaration, options) { | ||
return '<?xml' + writeAttributes(declaration[options.attributesKey]) + '?>'; | ||
return options.ignoreDeclaration ? '' : '<?xml' + writeAttributes(declaration[options.attributesKey], options) + '?>'; | ||
} | ||
function writeInstruction(instruction, options) { | ||
if (options.ignoreInstruction) { | ||
return ''; | ||
} | ||
var key; | ||
@@ -116,3 +122,3 @@ for (key in instruction) { | ||
if (element[options.attributesKey]) { | ||
xml += writeAttributes(element[options.attributesKey]); | ||
xml += writeAttributes(element[options.attributesKey], options); | ||
} | ||
@@ -174,3 +180,2 @@ if (options.fullTagEmptyElement || (element[options.elementsKey] && element[options.elementsKey].length) || (element[options.attributesKey] && element[options.attributesKey]['xml:space'] === 'preserve')) { | ||
case options.commentKey: | ||
case options.instructionKey: | ||
return true; | ||
@@ -190,3 +195,3 @@ default: | ||
if (element[options.attributesKey]) { | ||
xml += writeAttributes(element[options.attributesKey]); | ||
xml += writeAttributes(element[options.attributesKey], options); | ||
} | ||
@@ -193,0 +198,0 @@ if (options.fullTagEmptyElement || hasContentCompact(element, options, true) || element[options.attributesKey] && element[options.attributesKey]['xml:space'] === 'preserve') { |
@@ -72,10 +72,14 @@ var sax = require('sax'); | ||
element[options.typeKey] = type; | ||
if (typeof value === 'object') { | ||
if (type === 'instruction' && typeof value === 'object') { | ||
for (key in value) { | ||
if (value.hasOwnProperty(key)) { | ||
element[options.nameKey] = key; | ||
element[options[type + 'Key']] = value[key]; | ||
break; | ||
} | ||
} | ||
element[options.nameKey] = key; | ||
if (options.instructionHasAttributes) { | ||
element[options.attributesKey] = value[key][options.attributesKey]; | ||
} else { | ||
element[options[type + 'Key']] = value[key]; | ||
} | ||
} else { | ||
@@ -92,7 +96,4 @@ element[options[type + 'Key']] = value; | ||
function onInstruction(instruction) { | ||
if (instruction.name.toLowerCase() === 'xml') { | ||
if (options.ignoreDeclaration) { | ||
return; | ||
} | ||
currentElement[options.declarationKey] = {}; | ||
var attributes = {}; | ||
if (instruction.body && (instruction.name.toLowerCase() === 'xml' || options.instructionHasAttributes)) { | ||
while (instruction.body) { | ||
@@ -103,8 +104,14 @@ var attribute = instruction.body.match(/([\w:-]+)\s*=\s*"([^"]*)"|'([^']*)'|(\w+)\s*/); | ||
} | ||
if (!currentElement[options.declarationKey][options.attributesKey]) { | ||
currentElement[options.declarationKey][options.attributesKey] = {}; | ||
} | ||
currentElement[options.declarationKey][options.attributesKey][attribute[1]] = attribute[2]; | ||
attributes[attribute[1]] = attribute[2]; | ||
instruction.body = instruction.body.slice(attribute[0].length); // advance the string | ||
} | ||
} | ||
if (instruction.name.toLowerCase() === 'xml') { | ||
if (options.ignoreDeclaration) { | ||
return; | ||
} | ||
currentElement[options.declarationKey] = {}; | ||
if (Object.keys(attributes).length) { | ||
currentElement[options.declarationKey][options.attributesKey] = attributes; | ||
} | ||
if (options.addParent) { | ||
@@ -124,3 +131,8 @@ currentElement[options.declarationKey][options.parentKey] = currentElement; | ||
var value = {}; | ||
value[instruction.name] = instruction.body; | ||
if (options.instructionHasAttributes && Object.keys(attributes).length) { | ||
value[instruction.name] = {}; | ||
value[instruction.name][options.attributesKey] = attributes; | ||
} else { | ||
value[instruction.name] = instruction.body; | ||
} | ||
addField('instruction', value, options); | ||
@@ -127,0 +139,0 @@ } |
{ | ||
"name": "xml-js", | ||
"version": "1.3.0", | ||
"version": "1.3.1", | ||
"description": "A convertor between XML text and Javascript object / JSON text.", | ||
@@ -9,3 +9,3 @@ "repository": { | ||
}, | ||
"author": "Yousuf Almarzooqi", | ||
"author": "Yousuf Almarzooqi <ysf953@gmail.com>", | ||
"license": "MIT", | ||
@@ -25,3 +25,3 @@ "bugs": { | ||
"doctype", | ||
"DOCTYPE", | ||
"processing instruction", | ||
"Javascript", | ||
@@ -78,8 +78,8 @@ "js2xml", | ||
"biased-opener": "^0.2.8", | ||
"browser-sync": "^2.18.6", | ||
"browser-sync": "^2.18.12", | ||
"cash-cat": "^0.2.0", | ||
"codacy-coverage": "^2.0.2", | ||
"codeclimate-test-reporter": "^0.4.0", | ||
"coveralls": "^2.13.0", | ||
"cross-env": "^4.0.0", | ||
"coveralls": "^2.13.1", | ||
"cross-env": "^5.0.0", | ||
"globify": "^2.0.0", | ||
@@ -90,5 +90,5 @@ "istanbul": "^0.4.5", | ||
"npm-run-all": "^4.0.1", | ||
"typescript": "^2.2.2", | ||
"typescript": "^2.3.3", | ||
"watch": "^1.0.1" | ||
} | ||
} |
@@ -196,9 +196,10 @@ ![XML ⇔ JS/JSON](http://nashwaan.github.io/xml-js/images/logo.svg) | ||
| `alwaysChildren` | `false` | Whether to always generate `elements` property even when there are no actual sub elements. `<a></a>` will be `{"elements":[{"type":"element","name":"a","elements":[]}]}` rather than `{"elements":[{"type":"element","name":"a"}]}` (applicable for non-compact output). | | ||
| `ignoreDeclaration` | `false` | Whether to ignore writing declaration property. That is, no `declaration` property will be generated. | | ||
| `ignoreInstruction` | `false` | Whether to ignore writing processing instruction property. That is, no `instruction` property will be generated. | | ||
| `ignoreAttributes` | `false` | Whether to ignore writing attributes of elements.That is, no `attributes` property will be generated. | | ||
| `ignoreComment` | `false` | Whether to ignore writing comments of the elements. That is, no `comment` will be generated. | | ||
| `ignoreCdata` | `false` | Whether to ignore writing CData of the elements. That is, no `cdata` will be generated. | | ||
| `ignoreDoctype` | `false` | Whether to ignore writing Doctype of the elements. That is, no `doctype` will be generated. | | ||
| `ignoreText` | `false` | Whether to ignore writing texts of the elements. That is, no `text` will be generated. | | ||
| `instructionHasAttributes` | `false` | Whether to parse contents of Processing Instruction as attributes or not. `<?go to="there"?>` will be `{"_instruction":{"go":{"_attributes":{"to":"there"}}}}` rather than `{"_instruction":{"go":"to=\"there\""}}`. See [discussion](https://github.com/nashwaan/xml-js/issues/17). | | ||
| `ignoreDeclaration` | `false` | Whether to ignore parsing declaration property. That is, no `declaration` property will be generated. | | ||
| `ignoreInstruction` | `false` | Whether to ignore parsing processing instruction property. That is, no `instruction` property will be generated. | | ||
| `ignoreAttributes` | `false` | Whether to ignore parsing attributes of elements.That is, no `attributes` property will be generated. | | ||
| `ignoreComment` | `false` | Whether to ignore parsing comments of the elements. That is, no `comment` will be generated. | | ||
| `ignoreCdata` | `false` | Whether to ignore parsing CData of the elements. That is, no `cdata` will be generated. | | ||
| `ignoreDoctype` | `false` | Whether to ignore parsing Doctype of the elements. That is, no `doctype` will be generated. | | ||
| `ignoreText` | `false` | Whether to ignore parsing texts of the elements. That is, no `text` will be generated. | | ||
@@ -205,0 +206,0 @@ The below option is applicable only for `xml2json()` function. |
@@ -51,2 +51,3 @@ export interface ElementCompact { | ||
alwaysChildren?: boolean | ||
instructionHasAttributes?: boolean | ||
} | ||
@@ -53,0 +54,0 @@ |
Sorry, the diff of this file is not supported yet
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
66446
24
897
330