sameNode: function(otherNode) {
return this.nativeNode === otherNode.nativeNode;
- }
+ },
+
+ parent: function() {
+ return this.nativeNode.parentNode ? new ElementNode(this.nativeNode.parentNode) : null;
+ },
+
+ before: function(node) {
+ this._$.before(node.nativeNode);
+ },
+
+ wrapWith: function(node) {
+ if(this.parent())
+ this.before(node);
+ node.append(this);
+ },
});
var ElementNode = function(nativeNode) {
return this._$.contents().index(node._$);
},
- parent: function() {
- return new ElementNode(this._$.parent());
- },
-
getAttr: function(name) {
return this._$.attr(name);
},
this._$.append(documentNode.nativeNode);
},
- before: function(node) {
- this._$.before(node.nativeNode);
- },
-
unwrapContent: function() {
var parent = this.parent();
if(!parent)
expect(node1.contents()[0].sameNode(node2)).to.be.true;
});
+ it('wraps element node with another element node', function() {
+ var node = elementNodeFromXML('<div></div>'),
+ wrapper = elementNodeFromXML('<wrapper></wrapper>');
+
+ node.wrapWith(wrapper);
+ expect(node.parent().sameNode(wrapper)).to.be.true;
+ });
+
+ it('wraps text node with element node', function() {
+ var node = elementNodeFromXML('<div>Alice</div>'),
+ textNode = node.contents()[0],
+ wrapper = elementNodeFromXML('<wrapper></wrapper>');
+
+ textNode.wrapWith(wrapper);
+ expect(textNode.parent().sameNode(wrapper)).to.be.true;
+ expect(node.contents()).to.have.length(1);
+ });
+
it('unwraps element node contents', function() {
var node = elementNodeFromXML('<div>Alice <div>has <span>propably</span> a cat</div>!</div>'),
outerDiv = node.contents()[1];