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);
71 $.extend(ElementNode.prototype, DocumentNode.prototype, {
72 nodeType: Node.ELEMENT_NODE,
74 setData: function(key, value) {
75 if(value !== undefined) {
76 this._$.data(key, value);
78 this._$.removeData(_.keys(this._$.data()));
83 getData: function(key) {
85 return this._$.data(key);
87 return this._$.data();
90 getTagName: function() {
91 return this.nativeNode.tagName.toLowerCase();
94 contents: function() {
96 document = this.document;
97 this._$.contents().each(function() {
98 if(this.nodeType === Node.ELEMENT_NODE) {
99 toret.push(document.createElementNode(this));
101 else if(this.nodeType === Node.TEXT_NODE) {
102 toret.push(document.createTextNode(this));
108 indexOf: function(node) {
109 return this._$.contents().index(node._$);
112 setTag: function(tagName) {
113 var node = this.document.createElementNode({tagName: tagName}),
114 oldTagName = this.getTagName(),
115 myContents = this._$.contents();
117 this.getAttrs().forEach(function(attribute) {
118 node.setAttr(attribute.name, attribute.value, true);
120 node.setData(this.getData());
122 if(this.sameNode(this.document.root)) {
123 defineDocumentProperties(this.document, node._$);
125 this._$.replaceWith(node._$);
126 this._setNativeNode(node._$[0]);
127 this._$.append(myContents);
128 this.triggerChangeEvent('nodeTagChange', {oldTagName: oldTagName, newTagName: this.getTagName()});
131 getAttr: function(name) {
132 return this._$.attr(name);
135 setAttr: function(name, value, silent) {
136 var oldVal = this.getAttr(name);
137 this._$.attr(name, value);
139 this.triggerChangeEvent('nodeAttrChange', {attr: name, oldVal: oldVal, newVal: value});
143 getAttrs: function() {
145 for(var i = 0; i < this.nativeNode.attributes.length; i++) {
146 toret.push(this.nativeNode.attributes[i]);
151 append: function(documentNode) {
152 this._$.append(documentNode.nativeNode);
155 unwrapContent: function() {
156 var parent = this.parent();
161 var parentContents = parent.contents(),
162 myContents = this.contents(),
163 myIdx = parent.indexOf(this);
165 if(myContents.length === 0) {
166 return this.detach();
169 var moveLeftRange, moveRightRange, leftMerged;
171 if(myIdx > 0 && (parentContents[myIdx-1].nodeType === TEXT_NODE) && (myContents[0].nodeType === TEXT_NODE)) {
172 parentContents[myIdx-1].appendText(myContents[0].getText());
173 myContents[0].detach();
174 moveLeftRange = true;
180 if(!(leftMerged && myContents.length === 1)) {
181 if(myIdx < parentContents.length - 1 && (parentContents[myIdx+1].nodeType === TEXT_NODE) && (myContents[myContents.length-1].nodeType === TEXT_NODE)) {
182 parentContents[myIdx+1].prependText(myContents[myContents.length-1].getText());
183 myContents[myContents.length-1].detach();
184 moveRightRange = true;
188 var childrenLength = this.contents().length;
189 this.contents().forEach(function(child) {
196 element1: parent.contents()[myIdx + (moveLeftRange ? -1 : 0)],
197 element2: parent.contents()[myIdx + childrenLength-1 + (moveRightRange ? 1 : 0)]
202 var wrapper = $('<div>');
203 wrapper.append(this._getXMLDOMToDump());
204 return wrapper.html();
207 _getXMLDOMToDump: function() {
212 var TextNode = function(nativeNode, document) {
213 DocumentNode.call(this, nativeNode, document);
216 $.extend(TextNode.prototype, DocumentNode.prototype, {
217 nodeType: Node.TEXT_NODE,
219 getText: function() {
220 return this.nativeNode.data;
223 setText: function(text) {
224 this.nativeNode.data = text;
225 this.triggerTextChangeEvent();
228 appendText: function(text) {
229 this.nativeNode.data = this.nativeNode.data + text;
230 this.triggerTextChangeEvent();
233 prependText: function(text) {
234 this.nativeNode.data = text + this.nativeNode.data;
235 this.triggerTextChangeEvent();
238 triggerTextChangeEvent: function() {
239 var event = new events.ChangeEvent('nodeTextChange', {node: this});
240 this.document.trigger('change', event);
245 var parseXML = function(xml) {
249 var Document = function(xml) {
253 $.extend(Document.prototype, Backbone.Events, {
254 ElementNodeFactory: ElementNode,
255 TextNodeFactory: TextNode,
257 createElementNode: function(from) {
258 if(!(from instanceof HTMLElement)) {
260 from = document.createTextNode(from.text);
262 from = $('<' + from.tagName + '>')[0];
265 return new this.ElementNodeFactory(from, this);
268 createTextNode: function(nativeNode) {
269 return new this.TextNodeFactory(nativeNode, this);
272 loadXML: function(xml, options) {
273 options = options || {};
274 defineDocumentProperties(this, $(parseXML(xml)));
275 if(!options.silent) {
276 this.trigger('contentSet');
281 return this.root.toXML();
285 var defineDocumentProperties = function(doc, $document) {
286 Object.defineProperty(doc, 'root', {get: function() {
287 return doc.createElementNode($document[0]);
288 }, configurable: true});
289 Object.defineProperty(doc, 'dom', {get: function() {
291 }, configurable: true});
295 documentFromXML: function(xml) {
296 return new Document(parseXML(xml));
299 elementNodeFromXML: function(xml) {
300 return this.documentFromXML(xml).root;
304 DocumentNode: DocumentNode,
305 ElementNode: ElementNode