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 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._getXMLDOMToDump());
193 return wrapper.html();
196 _getXMLDOMToDump: function() {
201 var TextNode = function(nativeNode, document) {
202 DocumentNode.call(this, nativeNode, document);
205 $.extend(TextNode.prototype, DocumentNode.prototype, {
206 nodeType: Node.TEXT_NODE,
208 getText: function() {
209 return this.nativeNode.data;
212 setText: function(text) {
213 this.nativeNode.data = text;
214 this.triggerTextChangeEvent();
217 appendText: function(text) {
218 this.nativeNode.data = this.nativeNode.data + text;
219 this.triggerTextChangeEvent();
222 prependText: function(text) {
223 this.nativeNode.data = text + this.nativeNode.data;
224 this.triggerTextChangeEvent();
227 triggerTextChangeEvent: function() {
228 var event = new events.ChangeEvent('nodeTextChange', {node: this});
229 this.document.trigger('change', event);
234 var parseXML = function(xml) {
238 var Document = function(xml) {
242 $.extend(Document.prototype, Backbone.Events, {
243 ElementNodeFactory: ElementNode,
244 TextNodeFactory: TextNode,
246 createElementNode: function(from) {
247 if(!(from instanceof HTMLElement)) {
248 from = $('<' + from.tagName + '>');
250 return new this.ElementNodeFactory(from, this);
253 createTextNode: function(nativeNode) {
254 return new this.TextNodeFactory(nativeNode, this);
257 loadXML: function(xml, options) {
258 options = options || {};
259 defineDocumentProperties(this, $(parseXML(xml)));
260 if(!options.silent) {
261 this.trigger('contentSet');
266 return this.root.toXML();
270 var defineDocumentProperties = function(doc, $document) {
271 Object.defineProperty(doc, 'root', {get: function() {
272 return doc.createElementNode($document[0]);
273 }, configurable: true});
274 Object.defineProperty(doc, 'dom', {get: function() {
276 }, configurable: true});
280 documentFromXML: function(xml) {
281 return new Document(parseXML(xml));
284 elementNodeFromXML: function(xml) {
285 return this.documentFromXML(xml).root;
289 DocumentNode: DocumentNode,
290 ElementNode: ElementNode