/* globals Node */
+var privateKey = '_smartxml';
+
var DocumentNode = function(nativeNode, document) {
if(!document) {
throw new Error('undefined document for a node');
clone.find('*').addBack().each(function() {
var el = this,
clonedData = $(this).data();
-
+ $(el).removeData(privateKey);
_.pairs(clonedData).forEach(function(pair) {
var key = pair[0],
value = pair[1];
var ElementNode = function(nativeNode, document) {
DocumentNode.call(this, nativeNode, document);
+ $(nativeNode).data(privateKey, {node: this});
};
ElementNode.prototype = Object.create(DocumentNode.prototype);
if(key) {
return this._$.data(key);
}
- return this._$.data();
+ var toret = _.clone(this._$.data());
+ delete toret[privateKey];
+ return toret;
},
getTagName: function() {
TextNodeFactory: TextNode,
createDocumentNode: function(from) {
- if(!(from instanceof Node)) {
+ var cached;
+
+ if(from instanceof Node) {
+ cached = ($(from).data(privateKey) || {}).node;
+ if(cached instanceof DocumentNode) {
+ return cached;
+ }
+ } else {
if(typeof from === 'string') {
from = parseXML(from);
this.normalizeXML(from);
});
it('removes parent-describing sibling nodes of unwrapped node', function() {
- var doc = getDocumentFromXML('<root><div><a></a><x></x><a></a></div></root>'),
- div = doc.root.contents()[0],
- x = div.contents()[1];
+ var doc = getDocumentFromXML('<root><div><a></a><x></x><a></a></div></root>');
doc.registerExtension({documentNode: {methods: {
object: {
}
}}});
+ var div = doc.root.contents()[0],
+ x = div.contents()[1];
+
div.unwrapContent();
expect(doc.root.contents().length).to.equal(2);
expect(x.isInDocument()).to.be.false;
});
it('keeps parent-describing nodes in place', function() {
- var doc = getDocumentFromXML('<root>Alice <x></x> probably <y></y> has a cat</root>'),
- root = doc.root,
- x = root.contents()[1],
- y = root.contents()[3];
+ var doc = getDocumentFromXML('<root>Alice <x></x> probably <y></y> has a cat</root>');
doc.registerExtension({documentNode: {methods: {
object: {
}
}}});
+ var root = doc.root,
+ x = root.contents()[1],
+ y = root.contents()[3];
+
root.wrapText({
_with: {tagName: 'span', attrs: {'attr1': 'value1'}},
offsetStart: 1,
beforeEach(function() {
doc = getDocumentFromXML('<section>Alice<div class="test_class"></div></section>');
- elementNode = doc.root;
- textNode = doc.root.contents()[0];
- extension = {};
-
- expect(elementNode.testTransformation).to.be.undefined;
- expect(textNode.testTransformation).to.be.undefined;
- expect(doc.testTransformation).to.be.undefined;
-
- expect(doc.testMethod).to.be.undefined;
- expect(elementNode.testMethod).to.be.undefined;
- expect(textNode.testMethod).to.be.undefined;
- expect(elementNode.elementTestMethod).to.be.undefined;
- expect(textNode.textTestMethod).to.be.undefined;
});
it('allows adding method to a document', function() {
doc.registerExtension(extension);
- /* refresh */
elementNode = doc.root;
textNode = doc.root.contents()[0];
doc.registerExtension(extension);
- /* refresh */
elementNode = doc.root;
textNode = doc.root.contents()[0];