Skip to content

Commit

Permalink
Rework p5.XML implementation to use native API
Browse files Browse the repository at this point in the history
  • Loading branch information
limzykenneth committed Sep 16, 2018
1 parent 9b17460 commit db90f55
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 96 deletions.
25 changes: 1 addition & 24 deletions src/io/files.js
Original file line number Diff line number Diff line change
Expand Up @@ -600,29 +600,6 @@ function makeObject(row, headers) {
return ret;
}

function parseXML(two) {
var one = new p5.XML();
var children = two.childNodes;
if (children && children.length) {
for (var i = 0; i < children.length; i++) {
var node = parseXML(children[i]);
one.addChild(node);
}
one.setName(two.nodeName);
one._setCont(two.textContent);
one._setAttributes(two);
for (var j = 0; j < one.children.length; j++) {
one.children[j].parent = one;
}
return one;
} else {
one.setName(two.nodeName);
one._setCont(two.textContent);
one._setAttributes(two);
return one;
}
}

/**
* Reads the contents of a file and creates an XML object with its values.
* If the name of the file is used as the parameter, as in the above example,
Expand Down Expand Up @@ -1177,7 +1154,7 @@ p5.prototype.httpDo = function() {
return res.text().then(function(text) {
var parser = new DOMParser();
var xml = parser.parseFromString(text, 'text/xml');
return parseXML(xml.documentElement);
return new p5.XML(xml.documentElement);
});
default:
return res.text();
Expand Down
147 changes: 75 additions & 72 deletions src/io/p5.XML.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,13 @@ var p5 = require('../core/main');
* no image displayed
*
*/
p5.XML = function() {
this.name = null; //done
this.attributes = {}; //done
this.children = [];
this.parent = null;
this.content = null; //done
p5.XML = function(DOM) {
if (!DOM) {
var xmlDoc = document.implementation.createDocument(null, 'doc');
this.DOM = xmlDoc.createElement('root');
} else {
this.DOM = DOM;
}
};

/**
Expand Down Expand Up @@ -96,7 +97,7 @@ p5.XML = function() {
* </code></div>
*/
p5.XML.prototype.getParent = function() {
return this.parent;
return new p5.XML(this.DOM.parentElement);
};

/**
Expand Down Expand Up @@ -131,7 +132,7 @@ p5.XML.prototype.getParent = function() {
* </code></div>
*/
p5.XML.prototype.getName = function() {
return this.name;
return this.DOM.tagName;
};

/**
Expand Down Expand Up @@ -169,7 +170,15 @@ p5.XML.prototype.getName = function() {
* </code></div>
*/
p5.XML.prototype.setName = function(name) {
this.name = name;
var content = this.DOM.innerHTML;
var attributes = this.DOM.attributes;
var xmlDoc = document.implementation.createDocument(null, 'default');
var newDOM = xmlDoc.createElement(name);
newDOM.innerHTML = content;
for (var i = 0; i < attributes.length; i++) {
newDOM.setAttribute(attributes[i].nodeName, attributes.nodeValue);
}
this.DOM = newDOM;
};

/**
Expand Down Expand Up @@ -205,7 +214,7 @@ p5.XML.prototype.setName = function(name) {
* </code></div>
*/
p5.XML.prototype.hasChildren = function() {
return this.children.length > 0;
return this.DOM.children.length > 0;
};

/**
Expand Down Expand Up @@ -242,9 +251,11 @@ p5.XML.prototype.hasChildren = function() {
* </code></div>
*/
p5.XML.prototype.listChildren = function() {
return this.children.map(function(c) {
return c.name;
});
var arr = [];
for (var i = 0; i < this.DOM.childNodes.length; i++) {
arr.push(this.DOM.childNodes[i].nodeName);
}
return arr;
};

/**
Expand Down Expand Up @@ -289,14 +300,20 @@ p5.XML.prototype.listChildren = function() {
*/
p5.XML.prototype.getChildren = function(param) {
if (param) {
return this.children.filter(function(c) {
return c.name === param;
});
return elementsToP5XML(this.DOM.getElementsByTagName(param));
} else {
return this.children;
return elementsToP5XML(this.DOM.children);
}
};

function elementsToP5XML(elements) {
var arr = [];
for (var i = 0; i < elements.length; i++) {
arr.push(new p5.XML(elements[i]));
}
return arr;
}

/**
* Returns the first of the element's children that matches the name parameter
* or the child of the given index.It returns undefined if no matching
Expand Down Expand Up @@ -349,12 +366,12 @@ p5.XML.prototype.getChildren = function(param) {
*/
p5.XML.prototype.getChild = function(param) {
if (typeof param === 'string') {
for (var i = 0; i < this.children.length; i++) {
var child = this.children[i];
if (child.name === param) return child;
for (var i = 0; i < this.DOM.children.length; i++) {
var child = this.DOM.children[i];
if (child.tagName === param) return new p5.XML(child);
}
} else {
return this.children[param];
return new p5.XML(this.DOM.children[param]);
}
};

Expand Down Expand Up @@ -386,6 +403,7 @@ p5.XML.prototype.getChild = function(param) {
*
* function setup() {
* var child = new p5.XML();
* child.setName('animal');
* child.setAttribute('id', '3');
* child.setAttribute('species', 'Ornithorhynchus anatinus');
* child.setContent('Platypus');
Expand All @@ -403,7 +421,7 @@ p5.XML.prototype.getChild = function(param) {
*/
p5.XML.prototype.addChild = function(node) {
if (node instanceof p5.XML) {
this.children.push(node);
this.DOM.appendChild(node.DOM);
} else {
// PEND
}
Expand Down Expand Up @@ -467,8 +485,8 @@ p5.XML.prototype.addChild = function(node) {
p5.XML.prototype.removeChild = function(param) {
var ind = -1;
if (typeof param === 'string') {
for (var i = 0; i < this.children.length; i++) {
if (this.children[i].name === param) {
for (var i = 0; i < this.DOM.children.length; i++) {
if (this.DOM.children[i].tagName === param) {
ind = i;
break;
}
Expand All @@ -477,7 +495,7 @@ p5.XML.prototype.removeChild = function(param) {
ind = param;
}
if (ind !== -1) {
this.children.splice(ind, 1);
this.DOM.removeChild(this.DOM.children[ind]);
}
};

Expand Down Expand Up @@ -514,7 +532,7 @@ p5.XML.prototype.removeChild = function(param) {
* </code></div>
*/
p5.XML.prototype.getAttributeCount = function() {
return Object.keys(this.attributes).length;
return this.DOM.attributes.length;
};

/**
Expand Down Expand Up @@ -551,7 +569,12 @@ p5.XML.prototype.getAttributeCount = function() {
* </code></div>
*/
p5.XML.prototype.listAttributes = function() {
return Object.keys(this.attributes);
var arr = [];
for (var i = 0; i < this.DOM.attributes.length; i++) {
var attribute = this.DOM.attributes[i];
arr.push(attribute.nodeName);
}
return arr;
};

/**
Expand Down Expand Up @@ -590,7 +613,12 @@ p5.XML.prototype.listAttributes = function() {
* </code></div>
*/
p5.XML.prototype.hasAttribute = function(name) {
return this.attributes[name] ? true : false;
var obj = {};
for (var i = 0; i < this.DOM.attributes.length; i++) {
var attribute = this.DOM.attributes[i];
obj[attribute.nodeName] = attribute.nodeValue;
}
return obj[name] ? true : false;
};

/**
Expand Down Expand Up @@ -631,7 +659,12 @@ p5.XML.prototype.hasAttribute = function(name) {
* </code></div>
*/
p5.XML.prototype.getNum = function(name, defaultValue) {
return Number(this.attributes[name]) || defaultValue || 0;
var obj = {};
for (var i = 0; i < this.DOM.attributes.length; i++) {
var attribute = this.DOM.attributes[i];
obj[attribute.nodeName] = attribute.nodeValue;
}
return Number(obj[name]) || defaultValue || 0;
};

/**
Expand Down Expand Up @@ -672,7 +705,12 @@ p5.XML.prototype.getNum = function(name, defaultValue) {
* </code></div>
*/
p5.XML.prototype.getString = function(name, defaultValue) {
return String(this.attributes[name]) || defaultValue || null;
var obj = {};
for (var i = 0; i < this.DOM.attributes.length; i++) {
var attribute = this.DOM.attributes[i];
obj[attribute.nodeName] = attribute.nodeValue;
}
return obj[name] ? String(obj[name]) : defaultValue || null;
};

/**
Expand Down Expand Up @@ -713,9 +751,7 @@ p5.XML.prototype.getString = function(name, defaultValue) {
* </code></div>
*/
p5.XML.prototype.setAttribute = function(name, value) {
if (this.attributes[name]) {
this.attributes[name] = value;
}
this.DOM.setAttribute(name, value);
};

/**
Expand Down Expand Up @@ -753,7 +789,10 @@ p5.XML.prototype.setAttribute = function(name, value) {
* </code></div>
*/
p5.XML.prototype.getContent = function(defaultValue) {
return this.content || defaultValue || null;
var str;
str = this.DOM.textContent;
str = str.replace(/\s\s+/g, ',');
return str || defaultValue || null;
};

/**
Expand Down Expand Up @@ -792,45 +831,9 @@ p5.XML.prototype.getContent = function(defaultValue) {
* </code></div>
*/
p5.XML.prototype.setContent = function(content) {
if (!this.children.length) {
this.content = content;
}
};

/* HELPERS */
/**
* This method is called while the parsing of XML (when loadXML() is
* called). The difference between this method and the setContent()
* method defined later is that this one is used to set the content
* when the node in question has more nodes under it and so on and
* not directly text content. While in the other one is used when
* the node in question directly has text inside it.
*
*/
p5.XML.prototype._setCont = function(content) {
var str;
str = content;
str = str.replace(/\s\s+/g, ',');
//str = str.split(',');
this.content = str;
};

/**
* This method is called while the parsing of XML (when loadXML() is
* called). The XML node is passed and its attributes are stored in the
* <a href="#/p5.XML">p5.XML</a>'s attribute Object.
*
*/
p5.XML.prototype._setAttributes = function(node) {
var att = {};
var attributes = node.attributes;
if (attributes) {
for (var i = 0; i < attributes.length; i++) {
var attribute = attributes[i];
att[attribute.nodeName] = attribute.nodeValue;
}
if (!this.DOM.children.length) {
this.DOM.textContent = content;
}
this.attributes = att;
};

module.exports = p5;

0 comments on commit db90f55

Please sign in to comment.