Skip to content

Commit

Permalink
Improved the XML 'name to namespace' feature + Added the 'XMLElement'…
Browse files Browse the repository at this point in the history
… methods 'findText() : string' and 'findTexts() : string[]'
  • Loading branch information
AdrienCastex committed Jun 19, 2017
1 parent 585b172 commit 164022b
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 18 deletions.
4 changes: 4 additions & 0 deletions lib/helper/XML.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,20 @@ 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;
static parseJSON(xml: string | Int8Array, compact?: boolean): XMLElement;
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;
Expand Down
46 changes: 38 additions & 8 deletions lib/helper/XML.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand All @@ -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,
Expand All @@ -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)
Expand Down
61 changes: 51 additions & 10 deletions src/helper/XML.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -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
Expand Down Expand Up @@ -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;
Expand All @@ -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,
Expand All @@ -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);
}
}
}

Expand Down

0 comments on commit 164022b

Please sign in to comment.