mirror of
https://github.com/marko-js/marko.git
synced 2025-12-08 19:26:05 +00:00
122 lines
3.3 KiB
JavaScript
122 lines
3.3 KiB
JavaScript
/* jshint newcap:false */
|
|
var specialElHandlers = require('../../morphdom/specialElHandlers');
|
|
|
|
function VNode() {}
|
|
|
|
VNode.prototype = {
|
|
$__VNode: function(finalChildCount) {
|
|
this.$__finalChildCount = finalChildCount;
|
|
this.$__childCount = 0;
|
|
this.$__firstChild = null;
|
|
this.$__lastChild = null;
|
|
this.$__parentNode = null;
|
|
this.$__nextSibling = null;
|
|
},
|
|
|
|
get firstChild() {
|
|
var firstChild = this.$__firstChild;
|
|
|
|
if (firstChild && firstChild.$__DocumentFragment) {
|
|
var nestedFirstChild = firstChild.firstChild;
|
|
// The first child is a DocumentFragment node.
|
|
// If the DocumentFragment node has a first child then we will return that.
|
|
// Otherwise, the DocumentFragment node is not *really* the first child and
|
|
// we need to skip to its next sibling
|
|
return nestedFirstChild || firstChild.nextSibling;
|
|
}
|
|
|
|
return firstChild;
|
|
},
|
|
|
|
get nextSibling() {
|
|
var nextSibling = this.$__nextSibling;
|
|
|
|
if (nextSibling) {
|
|
if (nextSibling.$__DocumentFragment) {
|
|
var firstChild = nextSibling.firstChild;
|
|
return firstChild || nextSibling.nextSibling;
|
|
}
|
|
} else {
|
|
var parentNode = this.$__parentNode;
|
|
if (parentNode && parentNode.$__DocumentFragment) {
|
|
return parentNode.nextSibling;
|
|
}
|
|
}
|
|
|
|
return nextSibling;
|
|
},
|
|
|
|
$__appendChild: function(child) {
|
|
this.$__childCount++;
|
|
|
|
if (this.$__isTextArea) {
|
|
if (child.$__Text) {
|
|
var childValue = child.nodeValue;
|
|
this.$__value = (this.$__value || '') + childValue;
|
|
} else {
|
|
throw TypeError();
|
|
}
|
|
} else {
|
|
var lastChild = this.$__lastChild;
|
|
|
|
child.$__parentNode = this;
|
|
|
|
if (lastChild) {
|
|
lastChild.$__nextSibling = child;
|
|
} else {
|
|
this.$__firstChild = child;
|
|
}
|
|
|
|
this.$__lastChild = child;
|
|
}
|
|
|
|
return child;
|
|
},
|
|
|
|
$__finishChild: function finishChild() {
|
|
if (this.$__childCount == this.$__finalChildCount && this.$__parentNode) {
|
|
return this.$__parentNode.$__finishChild();
|
|
} else {
|
|
return this;
|
|
}
|
|
},
|
|
|
|
actualize: function(doc) {
|
|
var actualNode = this.$__actualize(doc);
|
|
|
|
var curChild = this.firstChild;
|
|
|
|
while(curChild) {
|
|
actualNode.appendChild(curChild.actualize(doc));
|
|
curChild = curChild.nextSibling;
|
|
}
|
|
|
|
if (this.$__nodeType === 1) {
|
|
var elHandler = specialElHandlers[this.$__nodeName];
|
|
if (elHandler !== undefined) {
|
|
elHandler(actualNode, this);
|
|
}
|
|
}
|
|
|
|
return actualNode;
|
|
}
|
|
|
|
// ,toJSON: function() {
|
|
// var clone = Object.assign({
|
|
// nodeType: this.nodeType
|
|
// }, this);
|
|
//
|
|
// for (var k in clone) {
|
|
// if (k.startsWith('_')) {
|
|
// delete clone[k];
|
|
// }
|
|
// }
|
|
// delete clone._nextSibling;
|
|
// delete clone._lastChild;
|
|
// delete clone.parentNode;
|
|
// return clone;
|
|
// }
|
|
};
|
|
|
|
module.exports = VNode;
|