Skip to content
Permalink
Browse files
Simplify element removal in ConfigParser
  • Loading branch information
raphinesse authored and dpogue committed May 22, 2018
1 parent 3e9a4ef commit 23a9bef3f47482606b04fcdd14a6e254ce5b0aee
Showing 1 changed file with 10 additions and 19 deletions.
@@ -86,6 +86,11 @@ function findElementAttributeValue (attributeName, elems) {
return value || '';
}

function removeChildren (el, selector) {
const matches = el.findall(selector);
matches.forEach(child => el.remove(child));
}

ConfigParser.prototype = {
getAttribute: function (attr) {
return this.doc.getroot().attrib[attr];
@@ -428,17 +433,10 @@ ConfigParser.prototype = {
* @param id name of the plugin
*/
removePlugin: function (id) {
if (id) {
var plugins = this.doc.findall('./plugin/[@name="' + id + '"]')
.concat(this.doc.findall('./feature/param[@name="id"][@value="' + id + '"]/..'));
var children = this.doc.getroot().getchildren();
plugins.forEach(function (plugin) {
var idx = children.indexOf(plugin);
if (idx > -1) {
children.splice(idx, 1);
}
});
}
if (!id) return;
const root = this.doc.getroot();
removeChildren(root, `./plugin/[@name="${id}"]`);
removeChildren(root, `./feature/param[@name="id"][@value="${id}"]/..`);
},

// Add any element to the root
@@ -469,14 +467,7 @@ ConfigParser.prototype = {
* @param {String} name the engine name.
*/
removeEngine: function (name) {
var engines = this.doc.findall('./engine/[@name="' + name + '"]');
for (var i = 0; i < engines.length; i++) {
var children = this.doc.getroot().getchildren();
var idx = children.indexOf(engines[i]);
if (idx > -1) {
children.splice(idx, 1);
}
}
removeChildren(this.doc.getroot(), `./engine/[@name="${name}"]`);
},
getEngines: function () {
var engines = this.doc.findall('./engine');

0 comments on commit 23a9bef

Please sign in to comment.