UNPKG

ngx-i18nsupport-lib

Version:

A Typescript library to work with Angular generated i18n files (xliff, xmb)

184 lines 6.32 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var xmldom_1 = require("xmldom"); /** * Created by martin on 01.05.2017. * Some Tool functions for XML Handling. */ var DOMUtilities = /** @class */ (function () { function DOMUtilities() { } /** * return the first subelement with the given tag. * @param element * @param tagName * @return {Element} subelement or null, if not existing. */ DOMUtilities.getFirstElementByTagName = function (element, tagName) { var matchingElements = element.getElementsByTagName(tagName); if (matchingElements && matchingElements.length > 0) { return matchingElements.item(0); } else { return null; } }; /** * return an element with the given tag and id attribute. * @param element * @param tagName * @param id * @return {Element} subelement or null, if not existing. */ DOMUtilities.getElementByTagNameAndId = function (element, tagName, id) { var matchingElements = element.getElementsByTagName(tagName); if (matchingElements && matchingElements.length > 0) { for (var i = 0; i < matchingElements.length; i++) { var node = matchingElements.item(i); if (node.getAttribute('id') === id) { return node; } } } return null; }; /** * Get next sibling, that is an element. * @param element */ DOMUtilities.getElementFollowingSibling = function (element) { if (!element) { return null; } var e = element.nextSibling; while (e) { if (e.nodeType === e.ELEMENT_NODE) { return e; } e = e.nextSibling; } return null; }; /** * Get previous sibling, that is an element. * @param element */ DOMUtilities.getElementPrecedingSibling = function (element) { if (!element) { return null; } var e = element.previousSibling; while (e) { if (e.nodeType === e.ELEMENT_NODE) { return e; } e = e.previousSibling; } return null; }; /** * return content of element as string, including all markup. * @param element * @return {string} */ DOMUtilities.getXMLContent = function (element) { if (!element) { return null; } var result = new xmldom_1.XMLSerializer().serializeToString(element); var tagName = element.nodeName; var reStartMsg = new RegExp('<' + tagName + '[^>]*>', 'g'); result = result.replace(reStartMsg, ''); var reEndMsg = new RegExp('</' + tagName + '>', 'g'); result = result.replace(reEndMsg, ''); return result; }; /** * return PCDATA content of element. * @param element * @return {string} */ DOMUtilities.getPCDATA = function (element) { if (!element) { return null; } var result = ''; var childNodes = element.childNodes; for (var i = 0; i < childNodes.length; i++) { var child = childNodes.item(i); if (child.nodeType === child.TEXT_NODE || child.nodeType === child.CDATA_SECTION_NODE) { result = result + child.nodeValue; } } return result.length === 0 ? null : result; }; /** * replace PCDATA content with a new one. * @param element * @param pcdata */ DOMUtilities.replaceContentWithXMLContent = function (element, pcdata) { // remove all children while (element.firstChild) { element.removeChild(element.firstChild); } // parseICUMessage pcdata var pcdataFragment = new xmldom_1.DOMParser().parseFromString('<fragment>' + pcdata + '</fragment>', 'application/xml'); var newChildren = pcdataFragment.getElementsByTagName('fragment').item(0).childNodes; for (var j = 0; j < newChildren.length; j++) { var newChild = newChildren.item(j); element.appendChild(element.ownerDocument.importNode(newChild, true)); } }; /** * find the previous sibling that is an element. * @param {Node} element * @return {Element} */ DOMUtilities.getPreviousElementSibling = function (element) { var node = element.previousSibling; while (node !== null) { if (node.nodeType === node.ELEMENT_NODE) { return node; } node = node.previousSibling; } return null; }; /** * Create an Element Node that is the next sibling of a given node. * @param elementNameToCreate * @param {Node} previousSibling * @return {Element} */ DOMUtilities.createFollowingSibling = function (elementNameToCreate, previousSibling) { var newElement = previousSibling.ownerDocument.createElement(elementNameToCreate); return DOMUtilities.insertAfter(newElement, previousSibling); }; /** * Insert newElement directly after previousSibling. * @param newElement * @param previousSibling */ DOMUtilities.insertAfter = function (newElement, previousSibling) { if (previousSibling.nextSibling !== null) { previousSibling.parentNode.insertBefore(newElement, previousSibling.nextSibling); } else { previousSibling.parentNode.appendChild(newElement); } return newElement; }; /** * Insert newElement directly before nextSibling. * @param newElement * @param nextSibling */ DOMUtilities.insertBefore = function (newElement, nextSibling) { nextSibling.parentNode.insertBefore(newElement, nextSibling); return newElement; }; return DOMUtilities; }()); exports.DOMUtilities = DOMUtilities; //# sourceMappingURL=S:/experimente/ngx-i18nsupport-lib/src/impl/dom-utilities.js.map