6 ], function($, _, Backbone, events) {
11 var TEXT_NODE = Node.TEXT_NODE;
14 var DocumentNode = function(nativeNode, document) {
16 throw new Error('undefined document for a node');
18 this.document = document;
19 this._setNativeNode(nativeNode);
23 $.extend(DocumentNode.prototype, {
24 _setNativeNode: function(nativeNode) {
25 this.nativeNode = nativeNode;
26 this._$ = $(nativeNode);
29 detach: function() { this._$.detach(); },
31 sameNode: function(otherNode) {
32 return this.nativeNode === otherNode.nativeNode;
36 return this.nativeNode.parentNode ? this.document.createElementNode(this.nativeNode.parentNode) : null;
39 before: function(node) {
40 this._$.before(node.nativeNode);
43 wrapWith: function(node) {
50 triggerChangeEvent: function(type, metaData) {
51 var event = new events.ChangeEvent(type, $.extend({node: this}, metaData || {}));
52 this.document.trigger('change', event);
56 var ElementNode = function(nativeNode, document) {
57 DocumentNode.call(this, nativeNode, document);
60 $.extend(ElementNode.prototype, DocumentNode.prototype, {
61 nodeType: Node.ELEMENT_NODE,
63 setData: function(key, value) {
64 if(value !== undefined) {
65 this._$.data(key, value);
67 this._$.removeData(_.keys(this._$.data()));
72 getData: function(key) {
74 return this._$.data(key);
76 return this._$.data();
79 getTagName: function() {
80 return this.nativeNode.tagName.toLowerCase();
83 contents: function() {
85 document = this.document;
86 this._$.contents().each(function() {
87 if(this.nodeType === Node.ELEMENT_NODE) {
88 toret.push(document.createElementNode(this));
90 else if(this.nodeType === Node.TEXT_NODE) {
91 toret.push(document.createTextNode(this));
97 indexOf: function(node) {
98 return this._$.contents().index(node._$);
101 setTag: function(tagName) {
102 var node = this.document.createElementNode({tagName: tagName}),
103 oldTagName = this.getTagName(),
104 myContents = this._$.contents();
106 this.getAttrs().forEach(function(attribute) {
107 node.setAttr(attribute.name, attribute.value, true);
109 node.setData(this.getData());
111 if(this.sameNode(this.document.root)) {
112 defineDocumentProperties(this.document, node._$);
114 this._$.replaceWith(node._$);
115 this._setNativeNode(node._$[0]);
116 this._$.append(myContents);
117 this.triggerChangeEvent('nodeTagChange', {oldTagName: oldTagName, newTagName: this.getTagName()});
120 getAttr: function(name) {
121 return this._$.attr(name);
124 setAttr: function(name, value, silent) {
125 var oldVal = this.getAttr(name);
126 this._$.attr(name, value);
128 this.triggerChangeEvent('nodeAttrChange', {attr: name, oldVal: oldVal, newVal: value});
132 getAttrs: function() {
134 for(var i = 0; i < this.nativeNode.attributes.length; i++) {
135 toret.push(this.nativeNode.attributes[i]);
140 append: function(documentNode) {
141 this._$.append(documentNode.nativeNode);
144 unwrapContent: function() {
145 var parent = this.parent();
150 var parentContents = parent.contents(),
151 myContents = this.contents(),
152 myIdx = parent.indexOf(this);
154 if(myContents.length === 0) {
155 return this.detach();
158 var moveLeftRange, moveRightRange, leftMerged;
160 if(myIdx > 0 && (parentContents[myIdx-1].nodeType === TEXT_NODE) && (myContents[0].nodeType === TEXT_NODE)) {
161 parentContents[myIdx-1].appendText(myContents[0].getText());
162 myContents[0].detach();
163 moveLeftRange = true;
169 if(!(leftMerged && myContents.length === 1)) {
170 if(myIdx < parentContents.length - 1 && (parentContents[myIdx+1].nodeType === TEXT_NODE) && (myContents[myContents.length-1].nodeType === TEXT_NODE)) {
171 parentContents[myIdx+1].prependText(myContents[myContents.length-1].getText());
172 myContents[myContents.length-1].detach();
173 moveRightRange = true;
177 var childrenLength = this.contents().length;
178 this.contents().forEach(function(child) {
185 element1: parent.contents()[myIdx + (moveLeftRange ? -1 : 0)],
186 element2: parent.contents()[myIdx + childrenLength-1 + (moveRightRange ? 1 : 0)]
191 var wrapper = $('<div>');
192 wrapper.append(this._$);
193 return wrapper.html();
197 var TextNode = function(nativeNode, document) {
198 DocumentNode.call(this, nativeNode, document);
201 $.extend(TextNode.prototype, DocumentNode.prototype, {
202 nodeType: Node.TEXT_NODE,
204 getText: function() {
205 return this.nativeNode.data;
208 appendText: function(text) {
209 this.nativeNode.data = this.nativeNode.data + text;
212 prependText: function(text) {
213 this.nativeNode.data = text + this.nativeNode.data;
218 var parseXML = function(xml) {
222 var Document = function(xml) {
226 $.extend(Document.prototype, Backbone.Events, {
227 ElementNodeFactory: ElementNode,
228 TextNodeFactory: TextNode,
230 createElementNode: function(from) {
231 if(!(from instanceof HTMLElement)) {
232 from = $('<' + from.tagName + '>');
234 return new this.ElementNodeFactory(from, this);
237 createTextNode: function(nativeNode) {
238 return new this.TextNodeFactory(nativeNode, this);
241 loadXML: function(xml) {
242 defineDocumentProperties(this, $(parseXML(xml)));
244 this.trigger('contentSet');
248 return this.root.toXML();
252 var defineDocumentProperties = function(doc, $document) {
253 Object.defineProperty(doc, 'root', {get: function() {
254 return doc.createElementNode($document[0]);
255 }, configurable: true});
256 Object.defineProperty(doc, 'dom', {get: function() {
258 }, configurable: true});
262 documentFromXML: function(xml) {
263 return new Document(parseXML(xml));
266 elementNodeFromXML: function(xml) {
267 return this.documentFromXML(xml).root;
271 DocumentNode: DocumentNode,
272 ElementNode: ElementNode