aboutsummaryrefslogtreecommitdiffstats
path: root/infrastructure/ace/www/magicdom.js
blob: 4bad3d4120ff5151fc6bbccb322a64c3ef91b98f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
/**
 * Copyright 2009 Google Inc.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS-IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */


function makeMagicDom(rootDomNode, contentWindow){
  function nodeToString(node) {
    if (isNodeText(node)) return '"'+node.nodeValue+'"';
    else return '<'+node.tagName+'>';
  }
  
  var doc = rootDomNode.ownerDocument || rootDomNode.document;

  function childIndex(dnode) {
    var idx = 0;
    var n = dnode;
    while (n.previousSibling) {
      idx++;
      n = n.previousSibling;
    }
    return idx;
  }
  
  function ensureNormalized(dnode) {
    function mergePair(text1, text2) {
      var theParent = text1.parentNode;
      var newTextNode = mdom.doc.createTextNode(text1.nodeValue+""+text2.nodeValue);
      theParent.insertBefore(newTextNode, text1);
      theParent.removeChild(text1);
      theParent.removeChild(text2);
      return newTextNode;
    }

    var n = dnode;
    if (!isNodeText(n)) return;
    while (n.previousSibling && isNodeText(n.previousSibling)) {
      n = mergePair(n.previousSibling, n);
    }
    while (n.nextSibling && isNodeText(n.nextSibling)) {
      n = mergePair(n, n.nextSibling);
    }
  }
  
  function nextUniqueId() {
    // returns new unique identifier string;
    // not actually checked for uniqueness, but unique
    // wrt magicdom.
    // is document-unique to allow document.getElementById even
    // in theoretical case of multiple magicdoms per doc
    var doc = mdom.doc;
    var nextId = (getAssoc(doc, "nextId") || 1);
    setAssoc(doc, "nextId", nextId+1);
    return "magicdomid"+nextId;
  }

  var nodeProto = {
    parent: function() {
      return wrapDom(((! this.isRoot) && this.dom.parentNode) || null);
    },
    index: function() {
      return childIndex(this.dom);
    },
    equals: function (otherNode) {
      return otherNode && otherNode.dom && (this.dom == otherNode.dom);
    },
    prev: function() {
      return wrapDom(this.dom.previousSibling || null);
    },
    next: function() {
      return wrapDom(this.dom.nextSibling || null);
    },
    remove: function() {
      if (! this.isRoot) {
	var dnode = this.dom;
	var prevSib = dnode.previousSibling;
	var nextSib = dnode.nextSibling;
	var normalizeNeeded = (prevSib && isNodeText(prevSib) && nextSib && isNodeText(nextSib));
	var theParent = dnode.parentNode;
	theParent.removeChild(dnode);
	if (normalizeNeeded) {
	  ensureNormalized(prevSib);
	}
      }
    },
    addNext: function (newNode) {
      var dnode = this.dom;
      var nextSib = dnode.nextSibling;
      if (nextSib) {
	dnode.parentNode.insertBefore(newNode.dom, nextSib);
      }
      else {
	dnode.parentNode.appendChild(newNode.dom);
      }
      if (newNode.isText) ensureNormalized(newNode.dom);
    },
    addPrev: function (newNode) {
      var dnode = this.dom;
      dnode.parentNode.insertBefore(newNode.dom, dnode);
      if (newNode.isText) ensureNormalized(newNode.dom);
    },
    replaceWith: function (newNodes) { // var-args
      this.replaceWithArray(arguments);
    },
    replaceWithArray: function (newNodes) {
      var addFunc;
      if (this.next()) {
	var next = this.next();
	addFunc = function (n) { next.addPrev(n); };
      }
      else {
	var parent = this.parent();
	addFunc = function (n) { parent.appendChild(n); };
      }
      // when using "this" functions, have to keep text
      // nodes from merging inappropriately
      var tempNode = mdom.newElement("span");
      this.addNext(tempNode);
      this.remove();      
      forEach(newNodes, function (n) {
	addFunc(n);
      });
      tempNode.remove();
    },
    getProp: function (propName) {
      return getAssoc(this.dom, propName);
    },
    setProp: function (propName, value) {
      setAssoc(this.dom, propName, value);
    },
    // not consistent between browsers in how line-breaks are handled
    innerText: function() {
      var dnode = this.dom;
      if ((typeof dnode.innerText) == "string") return dnode.innerText;
      if ((typeof dnode.textContent) == "string") return dnode.textContent;
      if ((typeof dnode.nodeValue) == "string") return dnode.nodeValue;
      return "";
    },
    depth: function() {
      try { // ZZZ
	var d = 0;
	var n = this;
	while (! n.isRoot) {
	  d++;
	  n = n.parent();
	}
	return d;
      }
      catch (e) {
	parent.BAD_NODE = this.dom;
	throw e;
      }
    }
  };
  
  var textNodeProto = extend(object(nodeProto), {
    isText: true,
    text: function() {
      return this.dom.nodeValue;
    },
    eachChild: function() {},
    childCount: function() { return 0; },
    eachDescendant: function() {},
    // precondition: 0 <= start < end <= length
    wrapRange: function(start, end, newNode) {
      var origText = this.text();
      var text1 = null;
      if (start > 0) {
	text1 = mdom.newText(origText.substring(0, start));
      }
      var text2 = mdom.newText(origText.substring(start, end));
      var text3 = null;
      if (end < origText.length) {
	text3 = mdom.newText(origText.substring(end, origText.length));
      }
      newNode.appendChild(text2);
      var nodesToUse = []
      if (text1) nodesToUse.push(text1);
      nodesToUse.push(newNode);
      if (text3) nodesToUse.push(text3);
      this.replaceWithArray(nodesToUse);
      return [text1, newNode, text3];
    }
  });
  
  var elementNodeProto = extend(object(nodeProto), {
    isText: false,
    childCount: function() {
      return this.dom.childNodes.length;
    },
    child: function (i) {
      return wrapDom(this.dom.childNodes.item(i));
    },
    firstChild: function() {
      return ((this.childCount() > 0) && this.child(0)) || null;
    },
    lastChild: function() {
      return ((this.childCount() > 0) && this.child(this.childCount()-1)) || null;
    },
    appendChild: function (newNode) {
      this.dom.appendChild(newNode.dom);
      if (newNode.isText) {
	ensureNormalized(newNode.dom);
      }
    },
    prependChild: function (newNode) {
      if (this.childCount() > 0) {
	this.child(0).addPrev(newNode);
      }
      else {
	this.appendChild(newNode);
      }
    },
    eachChild: function (func) {
      for(var i=0;i<this.childCount();i++) {
	var result = func(this.child(i), i);
	if (result) break;
      }
    },
    eachDescendant: function (func) {
      this.eachChild(function (n) {
	var result = func(n);
	if (! result) n.eachDescendant(func);
      });
    },
    dumpContents: function() {
      var mnode = this, dnode = this.dom;
      if (mnode.childCount() < 1) {
	mnode.remove();
      }
      else {
	var theParent = dnode.parentNode;
	var n;
	while ((n = dnode.firstChild)) {
	  dnode.removeChild(n);
	  theParent.insertBefore(n, dnode);
	  ensureNormalized(n);
	}
	mnode.remove();
      }
    },
    uniqueId: function() {
      // not actually guaranteed to be unique, e.g. if user copy-pastes
      // nodes with ids
      var dnode = this.dom;
      if (dnode.id) return dnode.id;
      dnode.id = nextUniqueId();
      return dnode.id;
    }
  });
  
  function wrapDom(dnode) {
    if (! dnode) return dnode;
    var mnode;
    if (isNodeText(dnode)) {
      mnode = object(textNodeProto);
    }
    else {
      mnode = object(elementNodeProto);
    }
    mnode.isRoot = (dnode == rootDomNode);
    mnode.dom = dnode;
    return mnode;
  }

  var mdom = {};
  mdom.root = wrapDom(rootDomNode);
  mdom.doc = doc;
  mdom.win = contentWindow;
  mdom.byId = function (id) {
    return wrapDom(mdom.doc.getElementById(id));
  }
  mdom.newText = function (txt) {
    return wrapDom(mdom.doc.createTextNode(txt));
  }
  mdom.newElement = function (tagName) {
    return wrapDom(mdom.doc.createElement(tagName));
  }
  mdom.wrapDom = wrapDom;

  return mdom;
}