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 otherNode && this.nativeNode === otherNode.nativeNode;
36 return this.nativeNode.parentNode ? this.document.createElementNode(this.nativeNode.parentNode) : null;
39 after: function(node) {
40 node = node instanceof ElementNode ? node : this.document.createElementNode(node);
41 this._$.after(node.nativeNode);
45 before: function(node) {
46 node = node instanceof ElementNode ? node : this.document.createElementNode(node);
47 this._$.before(node.nativeNode);
51 wrapWith: function(node) {
52 node = node instanceof ElementNode ? node : this.document.createElementNode(node);
61 triggerChangeEvent: function(type, metaData) {
62 var event = new events.ChangeEvent(type, $.extend({node: this}, metaData || {}));
63 this.document.trigger('change', event);
67 var ElementNode = function(nativeNode, document) {
68 DocumentNode.call(this, nativeNode, document);
70 ElementNode.prototype = Object.create(DocumentNode.prototype);
72 $.extend(ElementNode.prototype, {
73 nodeType: Node.ELEMENT_NODE,
75 setData: function(key, value) {
76 if(value !== undefined) {
77 this._$.data(key, value);
79 this._$.removeData(_.keys(this._$.data()));
84 getData: function(key) {
86 return this._$.data(key);
88 return this._$.data();
91 getTagName: function() {
92 return this.nativeNode.tagName.toLowerCase();
95 contents: function() {
97 document = this.document;
98 this._$.contents().each(function() {
99 if(this.nodeType === Node.ELEMENT_NODE) {
100 toret.push(document.createElementNode(this));
102 else if(this.nodeType === Node.TEXT_NODE) {
103 toret.push(document.createTextNode(this));
109 indexOf: function(node) {
110 return this._$.contents().index(node._$);
113 setTag: function(tagName) {
114 var node = this.document.createElementNode({tagName: tagName}),
115 oldTagName = this.getTagName(),
116 myContents = this._$.contents();
118 this.getAttrs().forEach(function(attribute) {
119 node.setAttr(attribute.name, attribute.value, true);
121 node.setData(this.getData());
123 if(this.sameNode(this.document.root)) {
124 defineDocumentProperties(this.document, node._$);
126 this._$.replaceWith(node._$);
127 this._setNativeNode(node._$[0]);
128 this._$.append(myContents);
129 this.triggerChangeEvent('nodeTagChange', {oldTagName: oldTagName, newTagName: this.getTagName()});
132 getAttr: function(name) {
133 return this._$.attr(name);
136 setAttr: function(name, value, silent) {
137 var oldVal = this.getAttr(name);
138 this._$.attr(name, value);
140 this.triggerChangeEvent('nodeAttrChange', {attr: name, oldVal: oldVal, newVal: value});
144 getAttrs: function() {
146 for(var i = 0; i < this.nativeNode.attributes.length; i++) {
147 toret.push(this.nativeNode.attributes[i]);
152 append: function(documentNode) {
153 this._$.append(documentNode.nativeNode);
156 unwrapContent: function() {
157 var parent = this.parent();
162 var parentContents = parent.contents(),
163 myContents = this.contents(),
164 myIdx = parent.indexOf(this);
166 if(myContents.length === 0) {
167 return this.detach();
170 var moveLeftRange, moveRightRange, leftMerged;
172 if(myIdx > 0 && (parentContents[myIdx-1].nodeType === TEXT_NODE) && (myContents[0].nodeType === TEXT_NODE)) {
173 parentContents[myIdx-1].appendText(myContents[0].getText());
174 myContents[0].detach();
175 moveLeftRange = true;
181 if(!(leftMerged && myContents.length === 1)) {
182 if(myIdx < parentContents.length - 1 && (parentContents[myIdx+1].nodeType === TEXT_NODE) && (myContents[myContents.length-1].nodeType === TEXT_NODE)) {
183 parentContents[myIdx+1].prependText(myContents[myContents.length-1].getText());
184 myContents[myContents.length-1].detach();
185 moveRightRange = true;
189 var childrenLength = this.contents().length;
190 this.contents().forEach(function(child) {
197 element1: parent.contents()[myIdx + (moveLeftRange ? -1 : 0)],
198 element2: parent.contents()[myIdx + childrenLength-1 + (moveRightRange ? 1 : 0)]
203 var wrapper = $('<div>');
204 wrapper.append(this._getXMLDOMToDump());
205 return wrapper.html();
208 _getXMLDOMToDump: function() {
213 var TextNode = function(nativeNode, document) {
214 DocumentNode.call(this, nativeNode, document);
216 TextNode.prototype = Object.create(DocumentNode.prototype);
218 $.extend(TextNode.prototype, {
219 nodeType: Node.TEXT_NODE,
221 getText: function() {
222 return this.nativeNode.data;
225 setText: function(text) {
226 this.nativeNode.data = text;
227 this.triggerTextChangeEvent();
230 appendText: function(text) {
231 this.nativeNode.data = this.nativeNode.data + text;
232 this.triggerTextChangeEvent();
235 prependText: function(text) {
236 this.nativeNode.data = text + this.nativeNode.data;
237 this.triggerTextChangeEvent();
240 triggerTextChangeEvent: function() {
241 var event = new events.ChangeEvent('nodeTextChange', {node: this});
242 this.document.trigger('change', event);
247 var parseXML = function(xml) {
251 var Document = function(xml) {
255 $.extend(Document.prototype, Backbone.Events, {
256 ElementNodeFactory: ElementNode,
257 TextNodeFactory: TextNode,
259 createElementNode: function(from) {
260 if(!(from instanceof HTMLElement)) {
262 from = document.createTextNode(from.text);
264 from = $('<' + from.tagName + '>')[0];
267 return new this.ElementNodeFactory(from, this);
270 createTextNode: function(nativeNode) {
271 return new this.TextNodeFactory(nativeNode, this);
274 loadXML: function(xml, options) {
275 options = options || {};
276 defineDocumentProperties(this, $(parseXML(xml)));
277 if(!options.silent) {
278 this.trigger('contentSet');
283 return this.root.toXML();
287 var defineDocumentProperties = function(doc, $document) {
288 Object.defineProperty(doc, 'root', {get: function() {
289 return doc.createElementNode($document[0]);
290 }, configurable: true});
291 Object.defineProperty(doc, 'dom', {get: function() {
293 }, configurable: true});
297 documentFromXML: function(xml) {
298 return new Document(parseXML(xml));
301 elementNodeFromXML: function(xml) {
302 return this.documentFromXML(xml).root;
306 DocumentNode: DocumentNode,
307 ElementNode: ElementNode