diff --git a/lib/helper/XML.d.ts b/lib/helper/XML.d.ts index b1a87eeb..51c9825a 100644 --- a/lib/helper/XML.d.ts +++ b/lib/helper/XML.d.ts @@ -3,9 +3,12 @@ export interface XMLElement { attributes?: any; elements: XMLElement[]; name?: string; + type?: string; findIndex(name: string): number; find(name: string): XMLElement; findMany(name: string): XMLElement[]; + findText(): string; + findTexts(): string[]; } export declare abstract class XML { static parse(xml: string | Int8Array): XMLElement; @@ -13,6 +16,7 @@ export declare abstract class XML { static parseXML(xml: string | Int8Array): XMLElement; static toJSON(xml: string): string; static toXML(xml: XMLElement | any, includeDeclaration?: boolean): string; + private static explodeName(name, attributes); static createElement(name: string, attributes?: any, text?: string): { type: string; name: string; diff --git a/lib/helper/XML.js b/lib/helper/XML.js index b75a2aa1..f594af44 100644 --- a/lib/helper/XML.js +++ b/lib/helper/XML.js @@ -59,6 +59,23 @@ function mutateNodeNS(node, parentNS) { } return elements; }; + node.findText = function () { + for (var _i = 0, _a = node.elements; _i < _a.length; _i++) { + var element = _a[_i]; + if (element && element.type === 'text') + return element.text; + } + return ''; + }; + node.findTexts = function () { + var texts = []; + for (var _i = 0, _a = node.elements; _i < _a.length; _i++) { + var element = _a[_i]; + if (element && element.type === 'text') + texts.push(element.text); + } + return texts; + }; if (node.elements) node.elements.forEach(function (n) { return mutateNodeNS(n, nss); }); else @@ -117,9 +134,7 @@ var XML = (function () { compact: false }); }; - XML.createElement = function (name, attributes, text) { - if (!attributes) - attributes = {}; + XML.explodeName = function (name, attributes) { var li1 = name.lastIndexOf(':'); var li2 = name.indexOf(':'); var lindex = Math.max(li1 === li2 && name.indexOf('DAV:') !== 0 ? -1 : li1, name.lastIndexOf('/')) + 1; @@ -136,6 +151,12 @@ var XML = (function () { attributes['xmlns:' + kname] = value; name = kname + ':' + name.substring(lindex); } + return name; + }; + XML.createElement = function (name, attributes, text) { + if (!attributes) + attributes = {}; + name = XML.explodeName(name, attributes); var result = { type: 'element', name: name, @@ -158,11 +179,20 @@ var XML = (function () { if (element.type === 'element') { if (!element.attributes) element.attributes = {}; - var lindex_1 = element.name.lastIndexOf('/'); - if (lindex_1 !== -1) { - ++lindex_1; - element.attributes['xmlns:x'] = element.name.substring(0, lindex_1); - element.name = 'x:' + element.name.substring(lindex_1); + element.name = XML.explodeName(element.name, element.attributes); + if (element.elements) { + var list_1 = []; + element.elements.forEach(function (e) { return list_1.push(e); }); + while (list_1.length > 0) { + var current = list_1.shift(); + if (current.type !== 'element') + continue; + if (current.elements) + current.elements.forEach(function (e) { return list_1.push(e); }); + if (!current.attributes) + current.attributes = {}; + current.name = XML.explodeName(current.name, current.attributes); + } } } if (element.constructor === Array) diff --git a/src/helper/XML.ts b/src/helper/XML.ts index 7181a139..c67c5031 100644 --- a/src/helper/XML.ts +++ b/src/helper/XML.ts @@ -72,6 +72,23 @@ function mutateNodeNS(node : any, parentNS = { _default: 'DAV:' }) return elements; } + node.findText = function() : string + { + for(const element of node.elements) + if(element && element.type === 'text') + return element.text; + return ''; + } + node.findTexts = function() : string[] + { + const texts = []; + + for(const element of node.elements) + if(element && element.type === 'text') + texts.push(element.text); + + return texts; + } if(node.elements) node.elements.forEach(n => mutateNodeNS(n, nss)) @@ -85,10 +102,13 @@ export interface XMLElement attributes ?: any elements : XMLElement[] name ?: string + type ?: string findIndex(name : string) : number find(name : string) : XMLElement findMany(name : string) : XMLElement[] + findText() : string + findTexts() : string[] } export abstract class XML @@ -159,11 +179,8 @@ export abstract class XML }); } - static createElement(name : string, attributes ?: any, text ?: string) + private static explodeName(name, attributes) { - if(!attributes) - attributes = {}; - const li1 = name.lastIndexOf(':'); const li2 = name.indexOf(':'); const lindex = Math.max(li1 === li2 && name.indexOf('DAV:') !== 0 ? -1 : li1, name.lastIndexOf('/')) + 1; @@ -183,6 +200,16 @@ export abstract class XML name = kname + ':' + name.substring(lindex); } + return name; + } + + static createElement(name : string, attributes ?: any, text ?: string) + { + if(!attributes) + attributes = {}; + + name = XML.explodeName(name, attributes); + const result = { type: 'element', name, @@ -209,13 +236,27 @@ export abstract class XML { if(!element.attributes) element.attributes = { }; - - let lindex = element.name.lastIndexOf('/'); - if(lindex !== -1) + + element.name = XML.explodeName(element.name, element.attributes); + + if(element.elements) { - ++lindex; - element.attributes['xmlns:x'] = element.name.substring(0, lindex); - element.name = 'x:' + element.name.substring(lindex); + const list = []; + element.elements.forEach((e) => list.push(e)); + + while(list.length > 0) + { + const current = list.shift(); + if(current.type !== 'element') + continue; + + if(current.elements) + current.elements.forEach((e) => list.push(e)); + + if(!current.attributes) + current.attributes = {}; + current.name = XML.explodeName(current.name, current.attributes); + } } }