Marko v3: Added Node.prototype.replaceWith(newNode)

This commit is contained in:
Patrick Steele-Idem 2016-02-04 11:51:16 -07:00
parent ee91cf2ec6
commit 2ce5493c9a
5 changed files with 29 additions and 0 deletions

View File

@ -58,6 +58,12 @@ class Node {
wrapperNode.appendChild(this);
}
replaceWith(newNode) {
ok(this.container, 'Node does not belong to a container: ' + this);
var replaced = this.container.replaceChild(newNode, this);
ok(replaced, 'Invalid state. Child does not belong to the container');
}
/**
* Converts the provided `array` into a `ArrayContainer`. If the provided `array` is already an instance of a `Container` then it is simply returned.
* @param {[type]} array [description]

View File

@ -0,0 +1,12 @@
function create(__helpers) {
var str = __helpers.s,
empty = __helpers.e,
notEmpty = __helpers.ne,
escapeXml = __helpers.x;
return function render(data, out) {
out.w("<div replaced=\"test-replaceWith\"></div>");
};
}
(module.exports = require("marko").c(__filename)).c(create);

View File

@ -0,0 +1 @@
<test-replaceWith/>

View File

@ -0,0 +1,3 @@
{
"transformer": "./transformer.js"
}

View File

@ -0,0 +1,7 @@
module.exports = function transform(el, context) {
var div = context.builder.htmlElement('div', {
replaced: context.builder.literal('test-replaceWith')
});
el.replaceWith(div);
};