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(node) {
153 node = node instanceof DocumentNode ? node : this.document.createElementNode(node);
154 this._$.append(node.nativeNode);
157 unwrapContent: function() {
158 var parent = this.parent();
163 var parentContents = parent.contents(),
164 myContents = this.contents(),
165 myIdx = parent.indexOf(this);
167 if(myContents.length === 0) {
168 return this.detach();
171 var moveLeftRange, moveRightRange, leftMerged;
173 if(myIdx > 0 && (parentContents[myIdx-1].nodeType === TEXT_NODE) && (myContents[0].nodeType === TEXT_NODE)) {
174 parentContents[myIdx-1].appendText(myContents[0].getText());
175 myContents[0].detach();
176 moveLeftRange = true;
182 if(!(leftMerged && myContents.length === 1)) {
183 if(myIdx < parentContents.length - 1 && (parentContents[myIdx+1].nodeType === TEXT_NODE) && (myContents[myContents.length-1].nodeType === TEXT_NODE)) {
184 parentContents[myIdx+1].prependText(myContents[myContents.length-1].getText());
185 myContents[myContents.length-1].detach();
186 moveRightRange = true;
190 var childrenLength = this.contents().length;
191 this.contents().forEach(function(child) {
198 element1: parent.contents()[myIdx + (moveLeftRange ? -1 : 0)],
199 element2: parent.contents()[myIdx + childrenLength-1 + (moveRightRange ? 1 : 0)]
203 wrapText: function(params) {
204 return this.document._wrapText(_.extend({inside: this}, params));
208 var wrapper = $('<div>');
209 wrapper.append(this._getXMLDOMToDump());
210 return wrapper.html();
213 _getXMLDOMToDump: function() {
218 var TextNode = function(nativeNode, document) {
219 DocumentNode.call(this, nativeNode, document);
221 TextNode.prototype = Object.create(DocumentNode.prototype);
223 $.extend(TextNode.prototype, {
224 nodeType: Node.TEXT_NODE,
226 getText: function() {
227 return this.nativeNode.data;
230 setText: function(text) {
231 this.nativeNode.data = text;
232 this.triggerTextChangeEvent();
235 appendText: function(text) {
236 this.nativeNode.data = this.nativeNode.data + text;
237 this.triggerTextChangeEvent();
240 prependText: function(text) {
241 this.nativeNode.data = text + this.nativeNode.data;
242 this.triggerTextChangeEvent();
245 wrapWith: function(desc) {
246 if(typeof desc.start === 'number' && typeof desc.end === 'number') {
247 return this.document._wrapText({
248 inside: this.parent(),
249 textNodeIdx: this.parent().indexOf(this),
250 offsetStart: Math.min(desc.start, desc.end),
251 offsetEnd: Math.max(desc.start, desc.end),
252 _with: {tag: desc.tagName, attrs: desc.attrs}
255 return DocumentNode.prototype.wrapWith.call(this, desc);
259 triggerTextChangeEvent: function() {
260 var event = new events.ChangeEvent('nodeTextChange', {node: this});
261 this.document.trigger('change', event);
266 var parseXML = function(xml) {
270 var Document = function(xml) {
274 $.extend(Document.prototype, Backbone.Events, {
275 ElementNodeFactory: ElementNode,
276 TextNodeFactory: TextNode,
278 createElementNode: function(from) {
279 if(!(from instanceof HTMLElement)) {
281 from = document.createTextNode(from.text);
283 var node = $('<' + from.tagName + '>');
285 _.keys(from.attrs || {}).forEach(function(key) {
286 node.attr(key, from.attrs[key]);
292 return new this.ElementNodeFactory(from, this);
295 createTextNode: function(nativeNode) {
296 return new this.TextNodeFactory(nativeNode, this);
299 loadXML: function(xml, options) {
300 options = options || {};
301 defineDocumentProperties(this, $(parseXML(xml)));
302 if(!options.silent) {
303 this.trigger('contentSet');
308 return this.root.toXML();
311 _wrapText: function(params) {
312 params = _.extend({textNodeIdx: 0}, params);
313 if(typeof params.textNodeIdx === 'number') {
314 params.textNodeIdx = [params.textNodeIdx];
317 var contentsInside = params.inside.contents(),
318 idx1 = Math.min.apply(Math, params.textNodeIdx),
319 idx2 = Math.max.apply(Math, params.textNodeIdx),
320 textNode1 = contentsInside[idx1],
321 textNode2 = contentsInside[idx2],
322 sameNode = textNode1.sameNode(textNode2),
323 prefixOutside = textNode1.getText().substr(0, params.offsetStart),
324 prefixInside = textNode1.getText().substr(params.offsetStart),
325 suffixInside = textNode2.getText().substr(0, params.offsetEnd),
326 suffixOutside = textNode2.getText().substr(params.offsetEnd)
329 if(!(textNode1.parent().sameNode(textNode2.parent()))) {
330 throw new Error('Wrapping text in non-sibling text nodes not supported.');
333 var wrapperElement = this.createElementNode({tagName: params._with.tag, attrs: params._with.attrs});
334 textNode1.after(wrapperElement);
337 if(prefixOutside.length > 0) {
338 wrapperElement.before({text:prefixOutside});
341 var core = textNode1.getText().substr(params.offsetStart, params.offsetEnd - params.offsetStart);
342 wrapperElement.append({text: core});
345 if(prefixInside.length > 0) {
346 wrapperElement.append({text: prefixInside});
348 for(var i = idx1 + 1; i < idx2; i++) {
349 wrapperElement.append(contentsInside[i]);
351 if(suffixInside.length > 0) {
352 wrapperElement.append({text: suffixInside});
355 if(suffixOutside.length > 0) {
356 wrapperElement.after({text: suffixOutside});
358 return wrapperElement;
362 var defineDocumentProperties = function(doc, $document) {
363 Object.defineProperty(doc, 'root', {get: function() {
364 return doc.createElementNode($document[0]);
365 }, configurable: true});
366 Object.defineProperty(doc, 'dom', {get: function() {
368 }, configurable: true});
372 documentFromXML: function(xml) {
373 return new Document(parseXML(xml));
376 elementNodeFromXML: function(xml) {
377 return this.documentFromXML(xml).root;
381 DocumentNode: DocumentNode,
382 ElementNode: ElementNode