From 33f3a0c23219a3b2e46b762b2a223899d9533de4 Mon Sep 17 00:00:00 2001 From: rigazilla Date: Wed, 9 Apr 2025 11:56:43 +0200 Subject: [PATCH] Configurable summary field (#250) --- README.md | 3 + action.js | 3 +- action.yml | 4 + dist/index.js | 941 +++++++++++++++++++++++++------------------------- 4 files changed, 480 insertions(+), 471 deletions(-) diff --git a/README.md b/README.md index 7535456b..8fdb74c4 100644 --- a/README.md +++ b/README.md @@ -53,6 +53,9 @@ Optional. Set to `true` to get the file name from the stack trace. The default i Optional: If you use GitHub Enterprise, you can set this to the URL of your server (e.g. https://github.myorg.com/api/v3). +### `summary` + +Optional: Optional custom summary text to display in the workflow run output. Useful when generating reports in a separate workflow and linking back to the original test execution. Markdown is supported. ## Example usage diff --git a/action.js b/action.js index 20d6a039..7da8766e 100644 --- a/action.js +++ b/action.js @@ -19,6 +19,7 @@ const action = async () => { const skipPublishing = core.getInput('skip_publishing') === 'true'; const isFilenameInStackTrace = core.getInput('file_name_in_stack_trace') === 'true'; const githubBaseUrl = core.getInput('github_base_url'); + const summary = core.getInput('summary'); let { count, skipped, annotations } = await parseTestReports(reportPaths, isFilenameInStackTrace, ignoreFlakyTests); const foundResults = count > 0 || skipped > 0; @@ -59,7 +60,7 @@ const action = async () => { conclusion, output: { title, - summary: '', + summary: summary, annotations: annotations.slice(0, 50) } }; diff --git a/action.yml b/action.yml index 01c1da67..8cff8df9 100644 --- a/action.yml +++ b/action.yml @@ -47,6 +47,10 @@ inputs: description: 'if you use Github Enterprise, you can set this to the URL of your server (e.g. https://github.myorg.com/api/v3)' required: false default: "" + summary: + description: 'Optional custom summary text to display in the workflow run output' + required: false + default: "" outputs: outcome: diff --git a/dist/index.js b/dist/index.js index 43f43861..64849e26 100644 --- a/dist/index.js +++ b/dist/index.js @@ -25,6 +25,7 @@ const action = async () => { const skipPublishing = core.getInput('skip_publishing') === 'true'; const isFilenameInStackTrace = core.getInput('file_name_in_stack_trace') === 'true'; const githubBaseUrl = core.getInput('github_base_url'); + const summary = core.getInput('summary'); let { count, skipped, annotations } = await parseTestReports(reportPaths, isFilenameInStackTrace, ignoreFlakyTests); const foundResults = count > 0 || skipped > 0; @@ -65,7 +66,7 @@ const action = async () => { conclusion, output: { title, - summary: '', + summary: summary, annotations: annotations.slice(0, 50) } }; @@ -36394,17 +36395,17 @@ function wrappy (fn, cb) { /***/ 1709: /***/ ((module) => { -module.exports = { - - isArray: function(value) { - if (Array.isArray) { - return Array.isArray(value); - } - // fallback for older browsers like IE 8 - return Object.prototype.toString.call( value ) === '[object Array]'; - } - -}; +module.exports = { + + isArray: function(value) { + if (Array.isArray) { + return Array.isArray(value); + } + // fallback for older browsers like IE 8 + return Object.prototype.toString.call( value ) === '[object Array]'; + } + +}; /***/ }), @@ -36412,19 +36413,19 @@ module.exports = { /***/ 8821: /***/ ((module, __unused_webpack_exports, __nccwpck_require__) => { -/*jslint node:true */ - -var xml2js = __nccwpck_require__(2903); -var xml2json = __nccwpck_require__(8919); -var js2xml = __nccwpck_require__(4453); -var json2xml = __nccwpck_require__(1900); - -module.exports = { - xml2js: xml2js, - xml2json: xml2json, - js2xml: js2xml, - json2xml: json2xml -}; +/*jslint node:true */ + +var xml2js = __nccwpck_require__(2903); +var xml2json = __nccwpck_require__(8919); +var js2xml = __nccwpck_require__(4453); +var json2xml = __nccwpck_require__(1900); + +module.exports = { + xml2js: xml2js, + xml2json: xml2json, + js2xml: js2xml, + json2xml: json2xml +}; /***/ }), @@ -36759,24 +36760,24 @@ module.exports = function (js, options) { /***/ 1900: /***/ ((module, __unused_webpack_exports, __nccwpck_require__) => { -var js2xml = __nccwpck_require__(4453); - -module.exports = function (json, options) { - if (json instanceof Buffer) { - json = json.toString(); - } - var js = null; - if (typeof (json) === 'string') { - try { - js = JSON.parse(json); - } catch (e) { - throw new Error('The JSON structure is invalid'); - } - } else { - js = json; - } - return js2xml(js, options); -}; +var js2xml = __nccwpck_require__(4453); + +module.exports = function (json, options) { + if (json instanceof Buffer) { + json = json.toString(); + } + var js = null; + if (typeof (json) === 'string') { + try { + js = JSON.parse(json); + } catch (e) { + throw new Error('The JSON structure is invalid'); + } + } else { + js = json; + } + return js2xml(js, options); +}; /***/ }), @@ -36784,49 +36785,49 @@ module.exports = function (json, options) { /***/ 6717: /***/ ((module, __unused_webpack_exports, __nccwpck_require__) => { -var isArray = (__nccwpck_require__(1709).isArray); - -module.exports = { - - copyOptions: function (options) { - var key, copy = {}; - for (key in options) { - if (options.hasOwnProperty(key)) { - copy[key] = options[key]; - } - } - return copy; - }, - - ensureFlagExists: function (item, options) { - if (!(item in options) || typeof options[item] !== 'boolean') { - options[item] = false; - } - }, - - ensureSpacesExists: function (options) { - if (!('spaces' in options) || (typeof options.spaces !== 'number' && typeof options.spaces !== 'string')) { - options.spaces = 0; - } - }, - - ensureAlwaysArrayExists: function (options) { - if (!('alwaysArray' in options) || (typeof options.alwaysArray !== 'boolean' && !isArray(options.alwaysArray))) { - options.alwaysArray = false; - } - }, - - ensureKeyExists: function (key, options) { - if (!(key + 'Key' in options) || typeof options[key + 'Key'] !== 'string') { - options[key + 'Key'] = options.compact ? '_' + key : key; - } - }, - - checkFnExists: function (key, options) { - return key + 'Fn' in options; - } - -}; +var isArray = (__nccwpck_require__(1709).isArray); + +module.exports = { + + copyOptions: function (options) { + var key, copy = {}; + for (key in options) { + if (options.hasOwnProperty(key)) { + copy[key] = options[key]; + } + } + return copy; + }, + + ensureFlagExists: function (item, options) { + if (!(item in options) || typeof options[item] !== 'boolean') { + options[item] = false; + } + }, + + ensureSpacesExists: function (options) { + if (!('spaces' in options) || (typeof options.spaces !== 'number' && typeof options.spaces !== 'string')) { + options.spaces = 0; + } + }, + + ensureAlwaysArrayExists: function (options) { + if (!('alwaysArray' in options) || (typeof options.alwaysArray !== 'boolean' && !isArray(options.alwaysArray))) { + options.alwaysArray = false; + } + }, + + ensureKeyExists: function (key, options) { + if (!(key + 'Key' in options) || typeof options[key + 'Key'] !== 'string') { + options[key + 'Key'] = options.compact ? '_' + key : key; + } + }, + + checkFnExists: function (key, options) { + return key + 'Fn' in options; + } + +}; /***/ }), @@ -36834,368 +36835,368 @@ module.exports = { /***/ 2903: /***/ ((module, __unused_webpack_exports, __nccwpck_require__) => { -var sax = __nccwpck_require__(2043); -var expat /*= require('node-expat');*/ = { on: function () { }, parse: function () { } }; -var helper = __nccwpck_require__(6717); -var isArray = (__nccwpck_require__(1709).isArray); - -var options; -var pureJsParser = true; -var currentElement; - -function validateOptions(userOptions) { - options = helper.copyOptions(userOptions); - helper.ensureFlagExists('ignoreDeclaration', options); - helper.ensureFlagExists('ignoreInstruction', options); - helper.ensureFlagExists('ignoreAttributes', options); - helper.ensureFlagExists('ignoreText', options); - helper.ensureFlagExists('ignoreComment', options); - helper.ensureFlagExists('ignoreCdata', options); - helper.ensureFlagExists('ignoreDoctype', options); - helper.ensureFlagExists('compact', options); - helper.ensureFlagExists('alwaysChildren', options); - helper.ensureFlagExists('addParent', options); - helper.ensureFlagExists('trim', options); - helper.ensureFlagExists('nativeType', options); - helper.ensureFlagExists('nativeTypeAttributes', options); - helper.ensureFlagExists('sanitize', options); - helper.ensureFlagExists('instructionHasAttributes', options); - helper.ensureFlagExists('captureSpacesBetweenElements', options); - helper.ensureAlwaysArrayExists(options); - helper.ensureKeyExists('declaration', options); - helper.ensureKeyExists('instruction', options); - helper.ensureKeyExists('attributes', options); - helper.ensureKeyExists('text', options); - helper.ensureKeyExists('comment', options); - helper.ensureKeyExists('cdata', options); - helper.ensureKeyExists('doctype', options); - helper.ensureKeyExists('type', options); - helper.ensureKeyExists('name', options); - helper.ensureKeyExists('elements', options); - helper.ensureKeyExists('parent', options); - helper.checkFnExists('doctype', options); - helper.checkFnExists('instruction', options); - helper.checkFnExists('cdata', options); - helper.checkFnExists('comment', options); - helper.checkFnExists('text', options); - helper.checkFnExists('instructionName', options); - helper.checkFnExists('elementName', options); - helper.checkFnExists('attributeName', options); - helper.checkFnExists('attributeValue', options); - helper.checkFnExists('attributes', options); - return options; -} - -function nativeType(value) { - var nValue = Number(value); - if (!isNaN(nValue)) { - return nValue; - } - var bValue = value.toLowerCase(); - if (bValue === 'true') { - return true; - } else if (bValue === 'false') { - return false; - } - return value; -} - -function addField(type, value) { - var key; - if (options.compact) { - if ( - !currentElement[options[type + 'Key']] && - (isArray(options.alwaysArray) ? options.alwaysArray.indexOf(options[type + 'Key']) !== -1 : options.alwaysArray) - ) { - currentElement[options[type + 'Key']] = []; - } - if (currentElement[options[type + 'Key']] && !isArray(currentElement[options[type + 'Key']])) { - currentElement[options[type + 'Key']] = [currentElement[options[type + 'Key']]]; - } - if (type + 'Fn' in options && typeof value === 'string') { - value = options[type + 'Fn'](value, currentElement); - } - if (type === 'instruction' && ('instructionFn' in options || 'instructionNameFn' in options)) { - for (key in value) { - if (value.hasOwnProperty(key)) { - if ('instructionFn' in options) { - value[key] = options.instructionFn(value[key], key, currentElement); - } else { - var temp = value[key]; - delete value[key]; - value[options.instructionNameFn(key, temp, currentElement)] = temp; - } - } - } - } - if (isArray(currentElement[options[type + 'Key']])) { - currentElement[options[type + 'Key']].push(value); - } else { - currentElement[options[type + 'Key']] = value; - } - } else { - if (!currentElement[options.elementsKey]) { - currentElement[options.elementsKey] = []; - } - var element = {}; - element[options.typeKey] = type; - if (type === 'instruction') { - for (key in value) { - if (value.hasOwnProperty(key)) { - break; - } - } - element[options.nameKey] = 'instructionNameFn' in options ? options.instructionNameFn(key, value, currentElement) : key; - if (options.instructionHasAttributes) { - element[options.attributesKey] = value[key][options.attributesKey]; - if ('instructionFn' in options) { - element[options.attributesKey] = options.instructionFn(element[options.attributesKey], key, currentElement); - } - } else { - if ('instructionFn' in options) { - value[key] = options.instructionFn(value[key], key, currentElement); - } - element[options.instructionKey] = value[key]; - } - } else { - if (type + 'Fn' in options) { - value = options[type + 'Fn'](value, currentElement); - } - element[options[type + 'Key']] = value; - } - if (options.addParent) { - element[options.parentKey] = currentElement; - } - currentElement[options.elementsKey].push(element); - } -} - -function manipulateAttributes(attributes) { - if ('attributesFn' in options && attributes) { - attributes = options.attributesFn(attributes, currentElement); - } - if ((options.trim || 'attributeValueFn' in options || 'attributeNameFn' in options || options.nativeTypeAttributes) && attributes) { - var key; - for (key in attributes) { - if (attributes.hasOwnProperty(key)) { - if (options.trim) attributes[key] = attributes[key].trim(); - if (options.nativeTypeAttributes) { - attributes[key] = nativeType(attributes[key]); - } - if ('attributeValueFn' in options) attributes[key] = options.attributeValueFn(attributes[key], key, currentElement); - if ('attributeNameFn' in options) { - var temp = attributes[key]; - delete attributes[key]; - attributes[options.attributeNameFn(key, attributes[key], currentElement)] = temp; - } - } - } - } - return attributes; -} - -function onInstruction(instruction) { - var attributes = {}; - if (instruction.body && (instruction.name.toLowerCase() === 'xml' || options.instructionHasAttributes)) { - var attrsRegExp = /([\w:-]+)\s*=\s*(?:"([^"]*)"|'([^']*)'|(\w+))\s*/g; - var match; - while ((match = attrsRegExp.exec(instruction.body)) !== null) { - attributes[match[1]] = match[2] || match[3] || match[4]; - } - attributes = manipulateAttributes(attributes); - } - 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) { - currentElement[options.declarationKey][options.parentKey] = currentElement; - } - } else { - if (options.ignoreInstruction) { - return; - } - if (options.trim) { - instruction.body = instruction.body.trim(); - } - var value = {}; - 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); - } -} - -function onStartElement(name, attributes) { - var element; - if (typeof name === 'object') { - attributes = name.attributes; - name = name.name; - } - attributes = manipulateAttributes(attributes); - if ('elementNameFn' in options) { - name = options.elementNameFn(name, currentElement); - } - if (options.compact) { - element = {}; - if (!options.ignoreAttributes && attributes && Object.keys(attributes).length) { - element[options.attributesKey] = {}; - var key; - for (key in attributes) { - if (attributes.hasOwnProperty(key)) { - element[options.attributesKey][key] = attributes[key]; - } - } - } - if ( - !(name in currentElement) && - (isArray(options.alwaysArray) ? options.alwaysArray.indexOf(name) !== -1 : options.alwaysArray) - ) { - currentElement[name] = []; - } - if (currentElement[name] && !isArray(currentElement[name])) { - currentElement[name] = [currentElement[name]]; - } - if (isArray(currentElement[name])) { - currentElement[name].push(element); - } else { - currentElement[name] = element; - } - } else { - if (!currentElement[options.elementsKey]) { - currentElement[options.elementsKey] = []; - } - element = {}; - element[options.typeKey] = 'element'; - element[options.nameKey] = name; - if (!options.ignoreAttributes && attributes && Object.keys(attributes).length) { - element[options.attributesKey] = attributes; - } - if (options.alwaysChildren) { - element[options.elementsKey] = []; - } - currentElement[options.elementsKey].push(element); - } - element[options.parentKey] = currentElement; // will be deleted in onEndElement() if !options.addParent - currentElement = element; -} - -function onText(text) { - if (options.ignoreText) { - return; - } - if (!text.trim() && !options.captureSpacesBetweenElements) { - return; - } - if (options.trim) { - text = text.trim(); - } - if (options.nativeType) { - text = nativeType(text); - } - if (options.sanitize) { - text = text.replace(/&/g, '&').replace(//g, '>'); - } - addField('text', text); -} - -function onComment(comment) { - if (options.ignoreComment) { - return; - } - if (options.trim) { - comment = comment.trim(); - } - addField('comment', comment); -} - -function onEndElement(name) { - var parentElement = currentElement[options.parentKey]; - if (!options.addParent) { - delete currentElement[options.parentKey]; - } - currentElement = parentElement; -} - -function onCdata(cdata) { - if (options.ignoreCdata) { - return; - } - if (options.trim) { - cdata = cdata.trim(); - } - addField('cdata', cdata); -} - -function onDoctype(doctype) { - if (options.ignoreDoctype) { - return; - } - doctype = doctype.replace(/^ /, ''); - if (options.trim) { - doctype = doctype.trim(); - } - addField('doctype', doctype); -} - -function onError(error) { - error.note = error; //console.error(error); -} - -module.exports = function (xml, userOptions) { - - var parser = pureJsParser ? sax.parser(true, {}) : parser = new expat.Parser('UTF-8'); - var result = {}; - currentElement = result; - - options = validateOptions(userOptions); - - if (pureJsParser) { - parser.opt = {strictEntities: true}; - parser.onopentag = onStartElement; - parser.ontext = onText; - parser.oncomment = onComment; - parser.onclosetag = onEndElement; - parser.onerror = onError; - parser.oncdata = onCdata; - parser.ondoctype = onDoctype; - parser.onprocessinginstruction = onInstruction; - } else { - parser.on('startElement', onStartElement); - parser.on('text', onText); - parser.on('comment', onComment); - parser.on('endElement', onEndElement); - parser.on('error', onError); - //parser.on('startCdata', onStartCdata); - //parser.on('endCdata', onEndCdata); - //parser.on('entityDecl', onEntityDecl); - } - - if (pureJsParser) { - parser.write(xml).close(); - } else { - if (!parser.parse(xml)) { - throw new Error('XML parsing error: ' + parser.getError()); - } - } - - if (result[options.elementsKey]) { - var temp = result[options.elementsKey]; - delete result[options.elementsKey]; - result[options.elementsKey] = temp; - delete result.text; - } - - return result; - -}; +var sax = __nccwpck_require__(2043); +var expat /*= require('node-expat');*/ = { on: function () { }, parse: function () { } }; +var helper = __nccwpck_require__(6717); +var isArray = (__nccwpck_require__(1709).isArray); + +var options; +var pureJsParser = true; +var currentElement; + +function validateOptions(userOptions) { + options = helper.copyOptions(userOptions); + helper.ensureFlagExists('ignoreDeclaration', options); + helper.ensureFlagExists('ignoreInstruction', options); + helper.ensureFlagExists('ignoreAttributes', options); + helper.ensureFlagExists('ignoreText', options); + helper.ensureFlagExists('ignoreComment', options); + helper.ensureFlagExists('ignoreCdata', options); + helper.ensureFlagExists('ignoreDoctype', options); + helper.ensureFlagExists('compact', options); + helper.ensureFlagExists('alwaysChildren', options); + helper.ensureFlagExists('addParent', options); + helper.ensureFlagExists('trim', options); + helper.ensureFlagExists('nativeType', options); + helper.ensureFlagExists('nativeTypeAttributes', options); + helper.ensureFlagExists('sanitize', options); + helper.ensureFlagExists('instructionHasAttributes', options); + helper.ensureFlagExists('captureSpacesBetweenElements', options); + helper.ensureAlwaysArrayExists(options); + helper.ensureKeyExists('declaration', options); + helper.ensureKeyExists('instruction', options); + helper.ensureKeyExists('attributes', options); + helper.ensureKeyExists('text', options); + helper.ensureKeyExists('comment', options); + helper.ensureKeyExists('cdata', options); + helper.ensureKeyExists('doctype', options); + helper.ensureKeyExists('type', options); + helper.ensureKeyExists('name', options); + helper.ensureKeyExists('elements', options); + helper.ensureKeyExists('parent', options); + helper.checkFnExists('doctype', options); + helper.checkFnExists('instruction', options); + helper.checkFnExists('cdata', options); + helper.checkFnExists('comment', options); + helper.checkFnExists('text', options); + helper.checkFnExists('instructionName', options); + helper.checkFnExists('elementName', options); + helper.checkFnExists('attributeName', options); + helper.checkFnExists('attributeValue', options); + helper.checkFnExists('attributes', options); + return options; +} + +function nativeType(value) { + var nValue = Number(value); + if (!isNaN(nValue)) { + return nValue; + } + var bValue = value.toLowerCase(); + if (bValue === 'true') { + return true; + } else if (bValue === 'false') { + return false; + } + return value; +} + +function addField(type, value) { + var key; + if (options.compact) { + if ( + !currentElement[options[type + 'Key']] && + (isArray(options.alwaysArray) ? options.alwaysArray.indexOf(options[type + 'Key']) !== -1 : options.alwaysArray) + ) { + currentElement[options[type + 'Key']] = []; + } + if (currentElement[options[type + 'Key']] && !isArray(currentElement[options[type + 'Key']])) { + currentElement[options[type + 'Key']] = [currentElement[options[type + 'Key']]]; + } + if (type + 'Fn' in options && typeof value === 'string') { + value = options[type + 'Fn'](value, currentElement); + } + if (type === 'instruction' && ('instructionFn' in options || 'instructionNameFn' in options)) { + for (key in value) { + if (value.hasOwnProperty(key)) { + if ('instructionFn' in options) { + value[key] = options.instructionFn(value[key], key, currentElement); + } else { + var temp = value[key]; + delete value[key]; + value[options.instructionNameFn(key, temp, currentElement)] = temp; + } + } + } + } + if (isArray(currentElement[options[type + 'Key']])) { + currentElement[options[type + 'Key']].push(value); + } else { + currentElement[options[type + 'Key']] = value; + } + } else { + if (!currentElement[options.elementsKey]) { + currentElement[options.elementsKey] = []; + } + var element = {}; + element[options.typeKey] = type; + if (type === 'instruction') { + for (key in value) { + if (value.hasOwnProperty(key)) { + break; + } + } + element[options.nameKey] = 'instructionNameFn' in options ? options.instructionNameFn(key, value, currentElement) : key; + if (options.instructionHasAttributes) { + element[options.attributesKey] = value[key][options.attributesKey]; + if ('instructionFn' in options) { + element[options.attributesKey] = options.instructionFn(element[options.attributesKey], key, currentElement); + } + } else { + if ('instructionFn' in options) { + value[key] = options.instructionFn(value[key], key, currentElement); + } + element[options.instructionKey] = value[key]; + } + } else { + if (type + 'Fn' in options) { + value = options[type + 'Fn'](value, currentElement); + } + element[options[type + 'Key']] = value; + } + if (options.addParent) { + element[options.parentKey] = currentElement; + } + currentElement[options.elementsKey].push(element); + } +} + +function manipulateAttributes(attributes) { + if ('attributesFn' in options && attributes) { + attributes = options.attributesFn(attributes, currentElement); + } + if ((options.trim || 'attributeValueFn' in options || 'attributeNameFn' in options || options.nativeTypeAttributes) && attributes) { + var key; + for (key in attributes) { + if (attributes.hasOwnProperty(key)) { + if (options.trim) attributes[key] = attributes[key].trim(); + if (options.nativeTypeAttributes) { + attributes[key] = nativeType(attributes[key]); + } + if ('attributeValueFn' in options) attributes[key] = options.attributeValueFn(attributes[key], key, currentElement); + if ('attributeNameFn' in options) { + var temp = attributes[key]; + delete attributes[key]; + attributes[options.attributeNameFn(key, attributes[key], currentElement)] = temp; + } + } + } + } + return attributes; +} + +function onInstruction(instruction) { + var attributes = {}; + if (instruction.body && (instruction.name.toLowerCase() === 'xml' || options.instructionHasAttributes)) { + var attrsRegExp = /([\w:-]+)\s*=\s*(?:"([^"]*)"|'([^']*)'|(\w+))\s*/g; + var match; + while ((match = attrsRegExp.exec(instruction.body)) !== null) { + attributes[match[1]] = match[2] || match[3] || match[4]; + } + attributes = manipulateAttributes(attributes); + } + 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) { + currentElement[options.declarationKey][options.parentKey] = currentElement; + } + } else { + if (options.ignoreInstruction) { + return; + } + if (options.trim) { + instruction.body = instruction.body.trim(); + } + var value = {}; + 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); + } +} + +function onStartElement(name, attributes) { + var element; + if (typeof name === 'object') { + attributes = name.attributes; + name = name.name; + } + attributes = manipulateAttributes(attributes); + if ('elementNameFn' in options) { + name = options.elementNameFn(name, currentElement); + } + if (options.compact) { + element = {}; + if (!options.ignoreAttributes && attributes && Object.keys(attributes).length) { + element[options.attributesKey] = {}; + var key; + for (key in attributes) { + if (attributes.hasOwnProperty(key)) { + element[options.attributesKey][key] = attributes[key]; + } + } + } + if ( + !(name in currentElement) && + (isArray(options.alwaysArray) ? options.alwaysArray.indexOf(name) !== -1 : options.alwaysArray) + ) { + currentElement[name] = []; + } + if (currentElement[name] && !isArray(currentElement[name])) { + currentElement[name] = [currentElement[name]]; + } + if (isArray(currentElement[name])) { + currentElement[name].push(element); + } else { + currentElement[name] = element; + } + } else { + if (!currentElement[options.elementsKey]) { + currentElement[options.elementsKey] = []; + } + element = {}; + element[options.typeKey] = 'element'; + element[options.nameKey] = name; + if (!options.ignoreAttributes && attributes && Object.keys(attributes).length) { + element[options.attributesKey] = attributes; + } + if (options.alwaysChildren) { + element[options.elementsKey] = []; + } + currentElement[options.elementsKey].push(element); + } + element[options.parentKey] = currentElement; // will be deleted in onEndElement() if !options.addParent + currentElement = element; +} + +function onText(text) { + if (options.ignoreText) { + return; + } + if (!text.trim() && !options.captureSpacesBetweenElements) { + return; + } + if (options.trim) { + text = text.trim(); + } + if (options.nativeType) { + text = nativeType(text); + } + if (options.sanitize) { + text = text.replace(/&/g, '&').replace(//g, '>'); + } + addField('text', text); +} + +function onComment(comment) { + if (options.ignoreComment) { + return; + } + if (options.trim) { + comment = comment.trim(); + } + addField('comment', comment); +} + +function onEndElement(name) { + var parentElement = currentElement[options.parentKey]; + if (!options.addParent) { + delete currentElement[options.parentKey]; + } + currentElement = parentElement; +} + +function onCdata(cdata) { + if (options.ignoreCdata) { + return; + } + if (options.trim) { + cdata = cdata.trim(); + } + addField('cdata', cdata); +} + +function onDoctype(doctype) { + if (options.ignoreDoctype) { + return; + } + doctype = doctype.replace(/^ /, ''); + if (options.trim) { + doctype = doctype.trim(); + } + addField('doctype', doctype); +} + +function onError(error) { + error.note = error; //console.error(error); +} + +module.exports = function (xml, userOptions) { + + var parser = pureJsParser ? sax.parser(true, {}) : parser = new expat.Parser('UTF-8'); + var result = {}; + currentElement = result; + + options = validateOptions(userOptions); + + if (pureJsParser) { + parser.opt = {strictEntities: true}; + parser.onopentag = onStartElement; + parser.ontext = onText; + parser.oncomment = onComment; + parser.onclosetag = onEndElement; + parser.onerror = onError; + parser.oncdata = onCdata; + parser.ondoctype = onDoctype; + parser.onprocessinginstruction = onInstruction; + } else { + parser.on('startElement', onStartElement); + parser.on('text', onText); + parser.on('comment', onComment); + parser.on('endElement', onEndElement); + parser.on('error', onError); + //parser.on('startCdata', onStartCdata); + //parser.on('endCdata', onEndCdata); + //parser.on('entityDecl', onEntityDecl); + } + + if (pureJsParser) { + parser.write(xml).close(); + } else { + if (!parser.parse(xml)) { + throw new Error('XML parsing error: ' + parser.getError()); + } + } + + if (result[options.elementsKey]) { + var temp = result[options.elementsKey]; + delete result[options.elementsKey]; + result[options.elementsKey] = temp; + delete result.text; + } + + return result; + +}; /***/ }), @@ -37203,28 +37204,28 @@ module.exports = function (xml, userOptions) { /***/ 8919: /***/ ((module, __unused_webpack_exports, __nccwpck_require__) => { -var helper = __nccwpck_require__(6717); -var xml2js = __nccwpck_require__(2903); - -function validateOptions (userOptions) { - var options = helper.copyOptions(userOptions); - helper.ensureSpacesExists(options); - return options; -} - -module.exports = function(xml, userOptions) { - var options, js, json, parentKey; - options = validateOptions(userOptions); - js = xml2js(xml, options); - parentKey = 'compact' in options && options.compact ? '_parent' : 'parent'; - // parentKey = ptions.compact ? '_parent' : 'parent'; // consider this - if ('addParent' in options && options.addParent) { - json = JSON.stringify(js, function (k, v) { return k === parentKey? '_' : v; }, options.spaces); - } else { - json = JSON.stringify(js, null, options.spaces); - } - return json.replace(/\u2028/g, '\\u2028').replace(/\u2029/g, '\\u2029'); -}; +var helper = __nccwpck_require__(6717); +var xml2js = __nccwpck_require__(2903); + +function validateOptions (userOptions) { + var options = helper.copyOptions(userOptions); + helper.ensureSpacesExists(options); + return options; +} + +module.exports = function(xml, userOptions) { + var options, js, json, parentKey; + options = validateOptions(userOptions); + js = xml2js(xml, options); + parentKey = 'compact' in options && options.compact ? '_parent' : 'parent'; + // parentKey = ptions.compact ? '_parent' : 'parent'; // consider this + if ('addParent' in options && options.addParent) { + json = JSON.stringify(js, function (k, v) { return k === parentKey? '_' : v; }, options.spaces); + } else { + json = JSON.stringify(js, null, options.spaces); + } + return json.replace(/\u2028/g, '\\u2028').replace(/\u2029/g, '\\u2029'); +}; /***/ }),