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._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;
216 appendText: function(text) {
217 this.nativeNode.data = this.nativeNode.data + text;
220 prependText: function(text) {
221 this.nativeNode.data = text + this.nativeNode.data;
226 var parseXML = function(xml) {
230 var Document = function(xml) {
234 $.extend(Document.prototype, Backbone.Events, {
235 ElementNodeFactory: ElementNode,
236 TextNodeFactory: TextNode,
238 createElementNode: function(from) {
239 if(!(from instanceof HTMLElement)) {
240 from = $('<' + from.tagName + '>');
242 return new this.ElementNodeFactory(from, this);
245 createTextNode: function(nativeNode) {
246 return new this.TextNodeFactory(nativeNode, this);
249 loadXML: function(xml, options) {
250 options = options || {};
251 defineDocumentProperties(this, $(parseXML(xml)));
252 if(!options.silent) {
253 this.trigger('contentSet');
258 return this.root.toXML();
262 var defineDocumentProperties = function(doc, $document) {
263 Object.defineProperty(doc, 'root', {get: function() {
264 return doc.createElementNode($document[0]);
265 }, configurable: true});
266 Object.defineProperty(doc, 'dom', {get: function() {
268 }, configurable: true});
272 documentFromXML: function(xml) {
273 return new Document(parseXML(xml));
276 elementNodeFromXML: function(xml) {
277 return this.documentFromXML(xml).root;
281 DocumentNode: DocumentNode,
282 ElementNode: ElementNode