From aa793d5a4aa4185a0f9ed0e89551510e4b545d8d Mon Sep 17 00:00:00 2001 From: Adrien Castex Date: Fri, 9 Jun 2017 16:13:14 +0200 Subject: [PATCH] Fixed the issue of not applying to the name of XML elements the default namespace --- lib/helper/XML.d.ts | 1 + lib/helper/XML.js | 34 ++++++++++++++++++++++++---------- src/helper/XML.ts | 34 ++++++++++++++++++++++++---------- 3 files changed, 49 insertions(+), 20 deletions(-) diff --git a/lib/helper/XML.d.ts b/lib/helper/XML.d.ts index 4fd19846..ab5b0660 100644 --- a/lib/helper/XML.d.ts +++ b/lib/helper/XML.d.ts @@ -3,6 +3,7 @@ export interface XMLElement { attributes?: any; elements: XMLElement[]; name?: string; + findIndex(name: string): number; find(name: string): XMLElement; findMany(name: string): XMLElement[]; } diff --git a/lib/helper/XML.js b/lib/helper/XML.js index 7229ef11..a4a62bc8 100644 --- a/lib/helper/XML.js +++ b/lib/helper/XML.js @@ -20,27 +20,41 @@ function seekForNS(node, parentNS) { return ns; } function mutateNodeNS(node, parentNS) { - if (parentNS === void 0) { parentNS = { _default: 'DAV' }; } + if (parentNS === void 0) { parentNS = { _default: 'DAV:' }; } var nss = seekForNS(node, parentNS); if (node.name) { for (var ns in nss) { - if (ns === '_default') - continue; - if (node.name.indexOf(ns + ':') === 0) + if (ns === '_default' && node.name.indexOf(':') === -1) { + node.name = nss[ns] + node.name; + break; + } + else if (node.name.indexOf(ns + ':') === 0) { node.name = nss[ns] + node.name.substring((ns + ':').length); + break; + } } } - node.find = function (name) { - for (var index in node.elements) + node.findIndex = function (name) { + for (var index = 0; index < node.elements.length; ++index) if (node.elements[index].name && node.elements[index].name === name) - return node.elements[index]; + return index; + return -1; + }; + node.find = function (name) { + for (var _i = 0, _a = node.elements; _i < _a.length; _i++) { + var element = _a[_i]; + if (element.name && element.name === name) + return element; + } throw Errors_1.Errors.XMLNotFound; }; node.findMany = function (name) { var elements = []; - for (var index in node.elements) - if (node.elements[index].name && node.elements[index].name === name) - elements.push(node.elements[index]); + for (var _i = 0, _a = node.elements; _i < _a.length; _i++) { + var element = _a[_i]; + if (element.name && element.name === name) + elements.push(element); + } return elements; }; if (node.elements) diff --git a/src/helper/XML.ts b/src/helper/XML.ts index 42734b2b..f970d0de 100644 --- a/src/helper/XML.ts +++ b/src/helper/XML.ts @@ -24,7 +24,7 @@ function seekForNS(node : any, parentNS : any) : any return ns; } -function mutateNodeNS(node : any, parentNS = { _default: 'DAV' }) +function mutateNodeNS(node : any, parentNS = { _default: 'DAV:' }) { const nss = seekForNS(node, parentNS); @@ -32,27 +32,40 @@ function mutateNodeNS(node : any, parentNS = { _default: 'DAV' }) { for(const ns in nss) { - if(ns === '_default') - continue; - if(node.name.indexOf(ns + ':') === 0) + if(ns === '_default' && node.name.indexOf(':') === -1) + { + node.name = nss[ns] + node.name; + break; + } + else if(node.name.indexOf(ns + ':') === 0) + { node.name = nss[ns] + node.name.substring((ns + ':').length); + break; + } } } - node.find = function(name : string) : XMLElement + node.findIndex = function(name : string) : number { - for(const index in node.elements) + for(let index = 0; index < node.elements.length; ++index) if(node.elements[index].name && node.elements[index].name === name) - return node.elements[index]; + return index; + return -1; + } + node.find = function(name : string) : XMLElement + { + for(const element of node.elements) + if(element.name && element.name === name) + return element; throw Errors.XMLNotFound; } node.findMany = function(name : string) : XMLElement[] { const elements : XMLElement[] = []; - for(const index in node.elements) - if(node.elements[index].name && node.elements[index].name === name) - elements.push(node.elements[index]); + for(const element of node.elements) + if(element.name && element.name === name) + elements.push(element); return elements; } @@ -70,6 +83,7 @@ export interface XMLElement elements : XMLElement[] name ?: string + findIndex(name : string) : number find(name : string) : XMLElement findMany(name : string) : XMLElement[] }