office-addin-manifest
Version:
Read and modify Office Add-in manifest files.
179 lines • 6.83 kB
JavaScript
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.
Object.defineProperty(exports, "__esModule", { value: true });
exports.setXmlElementValue = exports.setXmlElementAttributeValue = exports.getXmlElementValue = exports.getXmlElementsValue = exports.getXmlElementsAttributeValue = exports.getXmlElements = exports.getXmlElementAttributeValue = exports.getXmlElement = exports.getXmlAttributeValue = void 0;
/**
* Given an xml element, returns the value of the attribute with the specified name.
* @param xml Xml object
* @param name Attribute name
* @returns The attribute value or undefined
* @example Given the the following xml, the attribute name "DefaultValue" will return the value "abc".
* <First DefaultValue="abc">1</First>
*/
function getXmlAttributeValue(xml, name) {
try {
return xml.$[name];
}
catch (_a) {
// reading xml values is resilient to errors but you can uncomment the next line for debugging if attributes are missing
// console.error(`Unable to get xml attribute value "${name}". ${err}`);
}
}
exports.getXmlAttributeValue = getXmlAttributeValue;
/**
* Given an xml object, returns the first inner element with the specified name, or undefined.
* @param xml Xml object
* @param name Element name
* @returns Xml object or undefined
* @example Given the the following xml, the name "Second" will return the xml object for <Second>...</Second>.
* <Current>
* <First>1</First>
* <Second>2</Second>
* </Current>
*/
function getXmlElement(xml, name) {
try {
const element = xml[name];
if (element instanceof Array) {
return element[0];
}
}
catch (_a) {
// reading xml values is resilient to errors but you can uncomment the next line for debugging if elements are missing
// console.error(`Unable to get xml element "${name}". ${err}`);
}
}
exports.getXmlElement = getXmlElement;
/**
* Given an xml object, returns the attribute value for the first inner element with the specified name, or undefined.
* @param xml Xml object
* @param elementName Element name
* @param attributeName Attribute name
* @example Given the the following xml, the element name "First" and attribute name "DefaultValue" will return the value "abc".
* <Current>
* <First DefaultValue="abc">1</First>
* </Current>
*/
function getXmlElementAttributeValue(xml, elementName, attributeName = "DefaultValue") {
const element = getXmlElement(xml, elementName);
if (element) {
return getXmlAttributeValue(element, attributeName);
}
}
exports.getXmlElementAttributeValue = getXmlElementAttributeValue;
/**
* Given an xml object, returns an array with the inner elements with the specified name.
* @param xml Xml object
* @param name Element name
* @returns Array of xml objects;
* @example Given the the following xml, the name "Item" will return an array with the two items.
* <Items>
* <Item>1</Item>
* <Item>2</Item>
* </Items>
*/
function getXmlElements(xml, name) {
try {
const elements = xml[name];
return elements instanceof Array ? elements : [];
}
catch (_a) {
return [];
}
}
exports.getXmlElements = getXmlElements;
/**
* Given an xml object, for the specified element, returns the values of the inner elements with the specified item element name.
* @param xml The xml object.
* @param name The name of the inner xml element.
* @example Given the the following xml, the container name "Items" and item name "Item" will return ["One", "Two"].
* If the attributeName is "AnotherValue", then it will return ["First", "Second"].
* <Items>
* <Item DefaultValue="One" AnotherValue="First">1</Item>
* <Item DefaultValue="Two" AnotherValue="Second">2</Item>
* </Current>
*/
function getXmlElementsAttributeValue(xml, name, itemElementName, attributeName = "DefaultValue") {
const values = [];
try {
const xmlElements = xml[name][0][itemElementName];
xmlElements.forEach((xmlElement) => {
const elementValue = getXmlAttributeValue(xmlElement, attributeName);
if (elementValue !== undefined) {
values.push(elementValue);
}
});
}
catch (_a) {
// do nothing
}
return values;
}
exports.getXmlElementsAttributeValue = getXmlElementsAttributeValue;
/**
* Given an xml object, for the specified element, returns the values of the inner elements with the specified item element name.
* @param xml The xml object.
* @param name The name of the inner xml element.
* @example Given the the following xml, the container name "Items" and item name "Item" will return ["1", "2"].
* <Items>
* <Item>1</Item>
* <Item>2</Item>
* </Current>
*/
function getXmlElementsValue(xml, name, itemElementName) {
const values = [];
getXmlElements(xml, name).forEach((xmlElement) => {
const elementValue = getXmlElementValue(xmlElement, itemElementName);
if (elementValue !== undefined) {
values.push(elementValue);
}
});
return values;
}
exports.getXmlElementsValue = getXmlElementsValue;
/**
* Returns the value of the first inner xml element with the specified name.
* @param xml The xml object.
* @param name The name of the inner xml element.
* @example Given the the following xml, the name "Second" will return the value "2".
* <Current>
* <First>1</First>
* <Second>2</Second>
* </Current>
*/
function getXmlElementValue(xml, name) {
try {
const element = xml[name];
if (element instanceof Array) {
return element[0];
}
}
catch (_a) {
// reading xml values is resilient to errors but you can uncomment the next line for debugging if elements are missing
// console.error(`Unable to get xml element value "${name}". ${err}`);
}
}
exports.getXmlElementValue = getXmlElementValue;
/**
* Given an xml object, set the attribute value for the specified element name.
* @param xml Xml object
* @param elementName Element name
* @param attributeValue Attribute value
* @param attributeName Attribute name
*/
function setXmlElementAttributeValue(xml, elementName, attributeValue, attributeName = "DefaultValue") {
xml[elementName][0].$[attributeName] = attributeValue;
}
exports.setXmlElementAttributeValue = setXmlElementAttributeValue;
/**
* Given an xml object, set the inner xml element
* @param xml Xml object
* @param elementName Element name
* @param elementValue Element value
*/
function setXmlElementValue(xml, elementName, elementValue) {
xml[elementName] = elementValue;
}
exports.setXmlElementValue = setXmlElementValue;
//# sourceMappingURL=xml.js.map
;