UNPKG

playwright-trx-reporter

Version:

TRX reporter for playwright

87 lines (86 loc) 3.52 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.serialize2Xml = void 0; /* eslint-disable @typescript-eslint/no-use-before-define */ const copyFromPlaywrightRepo_1 = require("./copyFromPlaywrightRepo"); const ATTRIBUTE_PREFIX = '$'; // elemnt could be string, in which case it will be transformed to // an element with only text content. // Vut if it has any attribute, then // text content should be '__text' const TEXT_PROPERTY = '__text'; function isAttribute(p) { return p.startsWith(ATTRIBUTE_PREFIX); } function isText(p) { return p === TEXT_PROPERTY; } function isElement(p) { return !isAttribute(p) && !isText(p); } function isEmpty(value) { return value === undefined || value === null || value === '' || (Array.isArray(value) && value.length === 0); } function serializeString2Xml(lines, modelName, model, stripANSIControlSequences) { const startTag = `<${modelName}>`; lines.push(startTag); const rawText = model; if (rawText) { lines.push((0, copyFromPlaywrightRepo_1.escape)(rawText, stripANSIControlSequences, true)); } const endTag = `</${modelName}>`; lines.push(endTag); } function serialize2XmlElementWithChildren(lines, tagName, attributes, elementKeyValuePairs, rawContent, stripANSIControlSequences) { const startTag = `<${tagName}${attributes.length ? ' ' : ''}${attributes.join(' ')}>`; lines.push(startTag); elementKeyValuePairs.forEach(([key, value]) => { if (Array.isArray(value)) { value.forEach((v) => serialize2Xml(lines, key, v, stripANSIControlSequences)); } else { serialize2Xml(lines, key, value, stripANSIControlSequences); } }); if (rawContent) { lines.push((0, copyFromPlaywrightRepo_1.escape)(rawContent, stripANSIControlSequences, true)); } const endTag = `</${tagName}>`; lines.push(endTag); } function serialize2XmlElementWithoutChildren(lines, tagName, attributes) { const selfCloseTag = `<${tagName}${attributes.length ? ' ' : ''}${attributes.join(' ')}/>`; lines.push(selfCloseTag); } function serialize2Xml(lines, modelName, model, stripANSIControlSequences) { if (isElement(modelName) && typeof model === 'string') { serializeString2Xml(lines, modelName, model, stripANSIControlSequences); return; } const properties = Object.keys(model); const attributeProperties = properties.filter(isAttribute); const elementProperties = properties.filter(isElement); const attributeKeyValuePairs = attributeProperties.map((p) => { const key = p.slice(1); // remove '$' prefix const value = model[p]; return [key, value]; }).filter(([, value]) => !isEmpty(value)); const elementKeyValuePairs = elementProperties.map((p) => { const value = model[p]; return [p, value]; }).filter(([, value]) => !isEmpty(value)); const attrs = attributeKeyValuePairs.map(([key, value]) => { const attrValue = (0, copyFromPlaywrightRepo_1.escape)(String(value), stripANSIControlSequences, false); return `${key}="${attrValue}"`; }); if (elementKeyValuePairs.length > 0 || model[TEXT_PROPERTY]) { serialize2XmlElementWithChildren(lines, modelName, attrs, elementKeyValuePairs, model[TEXT_PROPERTY], stripANSIControlSequences); } else { serialize2XmlElementWithoutChildren(lines, modelName, attrs); } } exports.serialize2Xml = serialize2Xml;