{"version":3,"file":"dom.mjs","sources":["../../../../../../../../src/providers/s3/utils/client/runtime/xmlParser/dom.ts"],"sourcesContent":["// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.\n// SPDX-License-Identifier: Apache-2.0\n/**\n * Drop-in replacement for fast-xml-parser's XmlParser class used in the AWS SDK S3 client XML deserializer. This\n * implementation is not tested against the full xml conformance test suite. It is only tested against the XML responses\n * from S3. This implementation requires the `DOMParser` class in the runtime.\n */\nexport const parser = {\n    parse: (xmlStr) => {\n        const domParser = new DOMParser();\n        const xml = domParser.parseFromString(xmlStr, 'text/xml');\n        const parsedObj = parseXmlNode(xml);\n        const rootKey = Object.keys(parsedObj)[0];\n        return parsedObj[rootKey];\n    },\n};\nconst parseXmlNode = (node) => {\n    if (isDocumentNode(node)) {\n        return {\n            [node.documentElement.nodeName]: parseXmlNode(node.documentElement),\n        };\n    }\n    if (node.nodeType === Node.TEXT_NODE) {\n        return node.nodeValue?.trim();\n    }\n    if (isElementNode(node)) {\n        // Node like <Location>foo</Location> will be converted to { Location: 'foo' }\n        // instead of { Location: { '#text': 'foo' } }.\n        if (isTextOnlyElementNode(node)) {\n            return node.childNodes[0].nodeValue;\n        }\n        const nodeValue = {};\n        // convert attributes\n        for (const attr of node.attributes) {\n            if (!isNamespaceAttributeName(attr.nodeName)) {\n                nodeValue[attr.nodeName] = attr.nodeValue;\n            }\n        }\n        // convert child nodes\n        if (node.children.length > 0) {\n            for (const child of node.children) {\n                const childValue = parseXmlNode(child);\n                if (childValue === undefined) {\n                    continue;\n                }\n                const childName = child.nodeName;\n                if (nodeValue[childName] === undefined) {\n                    nodeValue[childName] = childValue;\n                }\n                else if (Array.isArray(nodeValue[childName])) {\n                    nodeValue[childName].push(childValue);\n                }\n                else {\n                    nodeValue[childName] = [nodeValue[childName], childValue];\n                }\n            }\n        }\n        // Return empty element node as empty string instead of `{}`, which is the default behavior of fast-xml-parser.\n        return Object.keys(nodeValue).length === 0 ? '' : nodeValue;\n    }\n};\nconst isElementNode = (node) => node.nodeType === Node.ELEMENT_NODE;\nconst isDocumentNode = (node) => node.nodeType === Node.DOCUMENT_NODE;\nconst isTextOnlyElementNode = (node) => hasOnlyNamespaceAttributes(node) &&\n    node.children.length === 0 &&\n    node.firstChild?.nodeType === Node.TEXT_NODE;\nconst hasOnlyNamespaceAttributes = (node) => {\n    for (const attr of node.attributes) {\n        if (!isNamespaceAttributeName(attr.nodeName)) {\n            return false;\n        }\n    }\n    return true;\n};\nconst isNamespaceAttributeName = (name) => name === 'xmlns' || name.startsWith('xmlns:');\n"],"names":[],"mappings":"AAAA;AACA;AACA;AACA;AACA;AACA;AACA;AACY,MAAC,MAAM,GAAG;AACtB,IAAI,KAAK,EAAE,CAAC,MAAM,KAAK;AACvB,QAAQ,MAAM,SAAS,GAAG,IAAI,SAAS,EAAE;AACzC,QAAQ,MAAM,GAAG,GAAG,SAAS,CAAC,eAAe,CAAC,MAAM,EAAE,UAAU,CAAC;AACjE,QAAQ,MAAM,SAAS,GAAG,YAAY,CAAC,GAAG,CAAC;AAC3C,QAAQ,MAAM,OAAO,GAAG,MAAM,CAAC,IAAI,CAAC,SAAS,CAAC,CAAC,CAAC,CAAC;AACjD,QAAQ,OAAO,SAAS,CAAC,OAAO,CAAC;AACjC,IAAI,CAAC;AACL;AACA,MAAM,YAAY,GAAG,CAAC,IAAI,KAAK;AAC/B,IAAI,IAAI,cAAc,CAAC,IAAI,CAAC,EAAE;AAC9B,QAAQ,OAAO;AACf,YAAY,CAAC,IAAI,CAAC,eAAe,CAAC,QAAQ,GAAG,YAAY,CAAC,IAAI,CAAC,eAAe,CAAC;AAC/E,SAAS;AACT,IAAI;AACJ,IAAI,IAAI,IAAI,CAAC,QAAQ,KAAK,IAAI,CAAC,SAAS,EAAE;AAC1C,QAAQ,OAAO,IAAI,CAAC,SAAS,EAAE,IAAI,EAAE;AACrC,IAAI;AACJ,IAAI,IAAI,aAAa,CAAC,IAAI,CAAC,EAAE;AAC7B;AACA;AACA,QAAQ,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