import { DOMParser , XMLSerializer } from 'xmldom' ; /**
 * Created by martin on 01.05.2017.
 * Some Tool functions for XML Handling.
 */ export class DOMUtilities { param element param tagName return public static getFirstElementByTagName ( element , tagName ) { const matchingElements = element . getElementsByTagName ( tagName ) ; if ( matchingElements && matchingElements . length > 0 ) { return matchingElements . item ( 0 ) ; } else { return null ; } } param element param tagName param id return public static getElementByTagNameAndId ( element , tagName , id ) { const matchingElements = element . getElementsByTagName ( tagName ) ; if ( matchingElements && matchingElements . length > 0 ) { for ( let i = 0 ; i < matchingElements . length ; i ++ ) { const node = matchingElements . item ( i ) ; if ( node . getAttribute ( 'id' ) === id ) { return node ; } } } return null ; } param element public static getElementFollowingSibling ( element ) { if ( ! element ) { return null ; } let e = element . nextSibling ; while ( e ) { if ( e . nodeType === e . ELEMENT_NODE ) { return < Element > e ; } e = e . nextSibling ; } return null ; } param element public static getElementPrecedingSibling ( element ) { if ( ! element ) { return null ; } let e = element . previousSibling ; while ( e ) { if ( e . nodeType === e . ELEMENT_NODE ) { return < Element > e ; } e = e . previousSibling ; } return null ; } param element return public static getXMLContent ( element ) { if ( ! element ) { return null ; } let result = new XMLSerializer ( ) . serializeToString ( element ) ; const tagName = element . nodeName ; const reStartMsg = new RegExp ( '<' + tagName + '[^>]*>' , 'g' ) ; result = result . replace ( reStartMsg , '' ) ; const reEndMsg = new RegExp ( '</' + tagName + '>' , 'g' ) ; result = result . replace ( reEndMsg , '' ) ; return result ; } param element return public static getPCDATA ( element ) { if ( ! element ) { return null ; } let result = '' ; const childNodes = element . childNodes ; for ( let i = 0 ; i < childNodes . length ; i ++ ) { const 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 ; } param element param pcdata public static replaceContentWithXMLContent ( element , pcdata ) { while ( element . firstChild ) { element . removeChild ( element . firstChild ) ; } const pcdataFragment = new DOMParser ( ) . parseFromString ( '<fragment>' + pcdata + '</fragment>' , 'application/xml' ) ; const newChildren = pcdataFragment . getElementsByTagName ( 'fragment' ) . item ( 0 ) . childNodes ; for ( let j = 0 ; j < newChildren . length ; j ++ ) { const newChild = newChildren . item ( j ) ; element . appendChild ( element . ownerDocument . importNode ( newChild , true ) ) ; } } param element return public static getPreviousElementSibling ( element ) { let node = element . previousSibling ; while ( node !== null ) { if ( node . nodeType === node . ELEMENT_NODE ) { return < Element > node ; } node = node . previousSibling ; } return null ; } param elementNameToCreate param previousSibling return public static createFollowingSibling ( elementNameToCreate , previousSibling ) { const newElement = previousSibling . ownerDocument . createElement ( elementNameToCreate ) ; return < Element > DOMUtilities . insertAfter ( newElement , previousSibling ) ; } param newElement param previousSibling public static insertAfter ( newElement , previousSibling ) { if ( previousSibling . nextSibling !== null ) { previousSibling . parentNode . insertBefore ( newElement , previousSibling . nextSibling ) ; } else { previousSibling . parentNode . appendChild ( newElement ) ; } return newElement ; } param newElement param nextSibling public static insertBefore ( newElement , nextSibling ) { nextSibling . parentNode . insertBefore ( newElement , nextSibling ) ; return newElement ; } }