Commit 5998748c authored by Thimo Kraemer's avatar Thimo Kraemer

Revised actions for dom manipulations

parent 71c4ec39
/*
* KSS-RPC v0.13-beta
* KSS-RPC v0.14-beta
*
* Copyright (c) 2017, joonis new media
* Author: Thimo Kraemer <thimo.kraemer@joonis.de>
......@@ -61,7 +61,7 @@
******************************************************************************************/
var kss = {
version: '0.13-beta',
version: '0.14-beta',
_initialized: false,
_ruleSheets: {},
_eventBinders: {},
......@@ -1752,68 +1752,70 @@ kss.cssQuery = function(selector, context, dropContext) {
catch (err) { return result; } // IE8
};
kss._execScripts = function(node) {
var scripts = [node];
if (node.tagName.toLowerCase() != 'script')
scripts = node.getElementsByTagName('script');
kss.each(scripts, function(script) {
var cloned = document.createElement('script');
cloned.text = script.text;
kss.each(script.attributes, function(attr) {
cloned.setAttribute(attr.name, attr.value);
kss.parseHTML = function(html, runScripts) {
var div = document.createElement('div');
div.innerHTML = html;
if (runScripts) {
var scripts = div.getElementsByTagName('script');
kss.each(scripts, function(script) {
var cloned = document.createElement('script');
cloned.text = script.text;
kss.each(script.attributes, function(attr) {
cloned.setAttribute(attr.name, attr.value);
});
script.parentNode.replaceChild(cloned, script);
});
script.parentNode.replaceChild(cloned, script);
}
var nodes = [];
kss.each(div.childNodes, function(node) {
nodes.push(node);
});
return nodes;
};
kss.replaceInnerHTML = function(node, html) {
node.innerHTML = html;
kss._execScripts(node);
kss.applyRules(node);
kss._insertHTMLBefore = function(parent, node, html, runScripts) {
node = node || null;
var nodes = kss.parseHTML(html, runScripts);
kss.each(nodes, function(child) {
// Appends to parent if node is null
parent.insertBefore(child, node);
});
kss.applyRules(parent);
};
kss.replaceHTML = function(node, html) {
if (document.all && !window.atob
&& node.tagName.toLowerCase() == 'body') {
// IE<=9 does not allow to set body.outerHTML
// The body tag itself will be removed from innerHTML.
node.innerHTML = html;
}
else {
node.outerHTML = html;
}
kss._execScripts(node);
kss.applyRules(node.parentNode);
kss.replaceInnerHTML = function(node, html, runScripts) {
node.innerHTML = '';
kss._insertHTMLBefore(node, null, html, runScripts);
};
kss.replaceHTML = function(node, html, runScripts) {
var parent = node.parentNode;
var nextSibling = node.nextElementSibling;
parent.removeChild(node);
kss._insertHTMLBefore(parent, nextSibling, html, runScripts);
};
kss.appendHTML = function(node, html) {
node.insertAdjacentHTML('beforeend', html);
kss._execScripts(node.lastElementChild);
kss.applyRules(node);
kss.appendHTML = function(node, html, runScripts) {
kss._insertHTMLBefore(node, null, html, runScripts);
};
kss.prependHTML = function(node, html) {
node.insertAdjacentHTML('afterbegin', html);
kss._execScripts(node.firstElementChild);
kss.applyRules(node);
kss.prependHTML = function(node, html, runScripts) {
kss._insertHTMLBefore(
node, node.firstElementChild, html, runScripts);
};
kss.insertHTMLAfter = function(node, html) {
node.insertAdjacentHTML('afterend', html);
kss._execScripts(node.nextElementSibling);
kss.applyRules(node.parentNode);
kss.insertHTMLAfter = function(node, html, runScripts) {
kss._insertHTMLBefore(
node.parentNode, node.nextElementSibling, html, runScripts);
};
kss.insertHTMLBefore = function(node, html) {
node.insertAdjacentHTML('beforebegin', html);
kss._execScripts(node.previousElementSibling);
kss.applyRules(node.parentNode);
kss.insertHTMLBefore = function(node, html, runScripts) {
kss._insertHTMLBefore(node.parentNode, node, html, runScripts);
};
kss.deleteNode = function(node) {
node = node.parentNode.removeChild(node);
node.parentNode.removeChild(node);
kss._cleanUp();
return node;
};
kss.getNodeAttr = function(node, name, recursive) {
......@@ -2245,22 +2247,22 @@ kss.getCookie = function(name) {
kss.registerActionProvider({
// Actions to insert content
replaceInnerHTML: function(node, params) {
kss.replaceInnerHTML(node, params.html);
kss.replaceInnerHTML(node, params.html, params.runScripts);
},
replaceHTML: function(node, params) {
kss.replaceHTML(node, params.html);
kss.replaceHTML(node, params.html, params.runScripts);
},
appendHTML: function(node, params) {
kss.appendHTML(node, params.html);
kss.appendHTML(node, params.html, params.runScripts);
},
prependHTML: function(node, params) {
kss.prependHTML(node, params.html);
kss.prependHTML(node, params.html, params.runScripts);
},
insertHTMLAfter: function(node, params) {
kss.insertHTMLAfter(node, params.html);
kss.insertHTMLAfter(node, params.html, params.runScripts);
},
insertHTMLBefore: function(node, params) {
kss.insertHTMLBefore(node, params.html);
kss.insertHTMLBefore(node, params.html, params.runScripts);
},
// Actions to delete content
deleteNode: function(node, params) {
......
This diff is collapsed.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment