Skip to content

Commit

Permalink
Fixed the issue of not applying to the name of XML elements the defau…
Browse files Browse the repository at this point in the history
…lt namespace
  • Loading branch information
AdrienCastex committed Jun 9, 2017
1 parent efa5449 commit aa793d5
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 20 deletions.
1 change: 1 addition & 0 deletions lib/helper/XML.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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[];
}
Expand Down
34 changes: 24 additions & 10 deletions lib/helper/XML.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
34 changes: 24 additions & 10 deletions src/helper/XML.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,35 +24,48 @@ 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);

if(node.name)
{
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;
}
Expand All @@ -70,6 +83,7 @@ export interface XMLElement
elements : XMLElement[]
name ?: string

findIndex(name : string) : number
find(name : string) : XMLElement
findMany(name : string) : XMLElement[]
}
Expand Down

0 comments on commit aa793d5

Please sign in to comment.