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();
105 this.getAttrs().forEach(function(attribute) {
106 node.setAttr(attribute.name, attribute.value, true);
108 node.setData(this.getData());
110 this._setNativeNode(node._$[0]);
111 this.triggerChangeEvent('nodeTagChange', {oldTagName: oldTagName, newTagName: this.getTagName()});
114 getAttr: function(name) {
115 return this._$.attr(name);
118 setAttr: function(name, value, silent) {
119 var oldVal = this.getAttr(name);
120 this._$.attr(name, value);
122 this.triggerChangeEvent('nodeAttrChange', {attr: name, oldVal: oldVal, newVal: value});
126 getAttrs: function() {
128 for(var i = 0; i < this.nativeNode.attributes.length; i++) {
129 toret.push(this.nativeNode.attributes[i]);
134 append: function(documentNode) {
135 this._$.append(documentNode.nativeNode);
138 unwrapContent: function() {
139 var parent = this.parent();
144 var parentContents = parent.contents(),
145 myContents = this.contents(),
146 myIdx = parent.indexOf(this);
148 if(myContents.length === 0) {
149 return this.detach();
152 var moveLeftRange, moveRightRange, leftMerged;
154 if(myIdx > 0 && (parentContents[myIdx-1].nodeType === TEXT_NODE) && (myContents[0].nodeType === TEXT_NODE)) {
155 parentContents[myIdx-1].appendText(myContents[0].getText());
156 myContents[0].detach();
157 moveLeftRange = true;
163 if(!(leftMerged && myContents.length === 1)) {
164 if(myIdx < parentContents.length - 1 && (parentContents[myIdx+1].nodeType === TEXT_NODE) && (myContents[myContents.length-1].nodeType === TEXT_NODE)) {
165 parentContents[myIdx+1].prependText(myContents[myContents.length-1].getText());
166 myContents[myContents.length-1].detach();
167 moveRightRange = true;
171 var childrenLength = this.contents().length;
172 this.contents().forEach(function(child) {
179 element1: parent.contents()[myIdx + (moveLeftRange ? -1 : 0)],
180 element2: parent.contents()[myIdx + childrenLength-1 + (moveRightRange ? 1 : 0)]
185 var wrapper = $('<div>');
186 wrapper.append(this._$);
187 return wrapper.html();
191 var TextNode = function(nativeNode, document) {
192 DocumentNode.call(this, nativeNode, document);
195 $.extend(TextNode.prototype, DocumentNode.prototype, {
196 nodeType: Node.TEXT_NODE,
198 getText: function() {
199 return this.nativeNode.data;
202 appendText: function(text) {
203 this.nativeNode.data = this.nativeNode.data + text;
206 prependText: function(text) {
207 this.nativeNode.data = text + this.nativeNode.data;
212 var parseXML = function(xml) {
216 var Document = function(xml) {
217 var $document = $(parseXML(xml));
220 Object.defineProperty(this, 'root', {get: function() {
221 return doc.createElementNode($document[0]);
223 Object.defineProperty(this, 'dom', {get: function() {
227 $.extend(Document.prototype, Backbone.Events, {
228 ElementNodeFactory: ElementNode,
229 TextNodeFactory: TextNode,
231 createElementNode: function(from) {
232 if(!(from instanceof HTMLElement)) {
233 from = $('<' + from.tagName + '>');
235 return new this.ElementNodeFactory(from, this);
238 createTextNode: function(nativeNode) {
239 return new this.TextNodeFactory(nativeNode, this);
243 return this.root.toXML();
249 documentFromXML: function(xml) {
250 return new Document(parseXML(xml));
253 elementNodeFromXML: function(xml) {
254 return this.documentFromXML(xml).root;
258 DocumentNode: DocumentNode,
259 ElementNode: ElementNode