File: adom.js

package info (click to toggle)
emacspeak 29.0-9
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 12,904 kB
  • sloc: xml: 55,354; lisp: 48,335; cpp: 2,321; tcl: 1,500; makefile: 936; python: 836; sh: 785; perl: 459; ansic: 241
file content (508 lines) | stat: -rw-r--r-- 13,364 bytes parent folder | download | duplicates (2)
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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
//$Id: adom.js 5695 2008-07-20 17:11:43Z tv.raman.tv $
// <Class ADom

/*
 * ADOM: Holds a proxy to a DOM
 * Provides convenience methods for obtaining custom views
 * Constructor takes  the   document to view as argument
 */

ADom = function  (document) {
    this.document_ = document;
    document.adom = this;
    this.root_ = document.documentElement;
    this.current_ = document.documentElement;
    this.view_ = null;
};

// >
// < Navigators:

/*
 * Reset view.
 * Resets current to point at the root.
 * @return {node} current node.
 */
ADom.prototype.reset = function () {
    this.root_ = this.document_.documentElement;
    return this.current_ = this.root_;
};

/*
 * next: Move to next sibling.
 * @return {node} current node.
 */
ADom.prototype.next = function () {
    return this.current_ = this.current_.nextSibling;
};

/*
 * previous: Move to previous sibling.
 * @return {node} current node.
 */
ADom.prototype.previous = function() {
    return this.current_ = this.current_.previousSibling;
};

/*
 * up: Move to parent.
 * @return {node} current node.
 */
ADom.prototype.up = function () {
    return this.current_ = this.parentNode;
};

/*
 * down: Move to first child
 * @return {node} current node.
 */
ADom.prototype.down = function () {
    return this.current_ = this.current_.firstChild;
};

/*
 * first: Move to first sibling
 * @return {node} current node.
 */
ADom.prototype.first = function () {
    return this.current_ = this.current_.parentNode.firstChild;
};

/*
 * last: Move to last sibling.
 * @return {node} current node.
 */
ADom.prototype.last = function () {
    return this.current_  = this.current_.parentNode.lastChild;
};

/*
 * Move to  document body
 * @return {node} current node.
 */
ADom.prototype.body = function () {
    return this.current_ =  this.document_.body;
};


/*
 * Move to  element identified by id 
 * @return {node} current node.
 */
ADom.prototype.selectId = function (elementId) {
    return this.current_ =  this.document_.getElementById(elementId);
};



// >
// <Summarizers:

/*
 * base: Return appropriately encoded <base ../>
 * @return: {String} HTML base element.
 */
ADom.prototype.base = function () {
    return '<base href=\"' + this.document_.baseURI +  '\"/>\n';
};

/*
 * Return HTML for current node.
 * Produces a <base ../> if optional boolean flag gen_base is true.
 *@Return {string}; HTML
 */
ADom.prototype.html = function (gen_base) {
    var html ="";
    if (gen_base) {
        html += this.base();
    }
    html +='<' + this.current_.tagName;
    if (this.current_.value !== undefined) {
      html += ' value' + '=';
      html += '\"' +this.current_.value + '\"\n';
    }
    var map = this.current_.attributes;
    if (map  instanceof NamedNodeMap) {
        for (var i = 0; i < map.length; i++) {
            html += ' ' + map[i].name + '=';
            html += '\"' +map[i].value + '\"\n';
        }
    }
    if (this.current_.childNodes.length === 0) {
        return html += '/>\n';
    } else {
        html += '>\n' + this.current_.innerHTML;
        html += '</' + this.current_.tagName +'>\n';
        return html;
    }
};

/*
 * summarize: Summarize current node.
 * @Return {string};
 */
ADom.prototype.summarize = function () {
    var summary = this.current_.tagName +' ';
    summary += 'has ' + this.current_.childNodes.length + 'children ';
    summary += ' with ' + this.current_.innerHTML.length + ' bytes of content.';
    return summary;
};

/*
 * title: return document title
 * @Return  {string}
 */
ADom.prototype.title = function () {
    return this.document_.title;
};

/*
 * url: Return base URL of document.
 * @return {String} url
 */
ADom.prototype.url = function () {
    return this.document_.baseURI;
};

/*
 * Return document being viewed.
 */
ADom.prototype.document = function () {
    return this.document_;
};


/*
 * Return root of document being viewed.
 */
ADom.prototype.root = function () {
    return this.root_;
};


/*
 * Return the current node being viewed.
 */
ADom.prototype.current = function () {
    return this.current_;
};

// >
// <RingBuffer:

/*
 *  Implements iteration.
 */
var RingBuffer = function (list) {
    this.list_ = list;
    this.index_ = -1;
    this.len_ = list.length;
};

/*
 * item: Return item at specified index.
 * @return: node.
 */
RingBuffer.prototype.item = function (index) {
    return this.list_.item(this.index);
};


RingBuffer.prototype.next = function () {
    if (this.index_ == this.len_ -1) {
        this.index_ = -1;
    }
    this.index_++;
    return this.list_.item(this.index_);
};
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     
RingBuffer.prototype.previous = function () {
    if (this.index_ === -1 || this.index_ === 0) {
        this.index_ = this.len_;
    }
    this.index_--;
    return this.list_.item(this.index_);
};

// >
// <XPathRingBuffer:

/*
 *  Implements RingBuffer.
 */
var XPathRingBuffer = function (nodes) {
    this.list_ = nodes;
    this.index_ = -1;
    this.len_ = nodes.snapshotLength;
};
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           

/*
 * item: Return item at specified index.
 * @return: node.
 */
XPathRingBuffer.prototype.item = function (index) {
    return this.list_.snapshotItem(this.index);
};


XPathRingBuffer.prototype.next = function () {
    if (this.index_ == this.len_ -1) {
        this.index_ = -1;
    }
    this.index_++;
    return this.list_.snapshotItem(this.index_);
};
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                
XPathRingBuffer.prototype.previous = function () {
    if (this.index_ === -1 || this.index_ === 0) {
        this.index_ = this.len_;
    }
    this.index_--;
    return this.list_.snapshotItem(this.index_);
};

// >
// <XPath:

/*
 * filter: Apply XPath selector to create a filtered view.
 * @return {RingBuffer} of selected nodes suitable for use by visit()
 */

ADom.prototype.filter = function (xpath) {
    var start = this.current_ || this.root_;
    try {
      var snap   =
          this.document_.evaluate(xpath,
                                  start,
                                  null, // no namespace resolver
                                  XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE,
                                  null); // no previous results
    return this.view_ = new XPathRingBuffer(snap);
    } catch (err) {
      repl.print("Error evaluating XPath '" +  xpath +"': " +err);
      return null;
    }
};

// >
// <Viewers And Visitors:

/*
 * traverse: Traverse nodes that match test and apply action.
 * Arguments:
 * node: Node where we start traversing.
 * test: Predicate
 * Action: Visit action
 * @return: void
 */

ADom.prototype.traverse = function (node, test, action) {
  if(node.nodeType == document.ELEMENT_NODE) {
    if(test(node)) action(node);
    var child = node.firstChild;
    while(child) this.traverse(child, test, action);
  }
};

/*
 * Set view to forms array
 * Return forms array.
 */
ADom.prototype.forms = function () {
    this.view_ = new RingBuffer(this.document_.forms);
    return this.view_;
};
/*
 * locate: set view_ to RingBuffer of elements found by name
 */
ADom.prototype.locate = function (tagName) {
    var start = this.current_ || this.root_;
    return this.view_ = new RingBuffer(start.getElementsByTagName(tagName));
};

/*
 * visit: visit each node in view_ in turn.
 * Optional argument dir if specified visits in the reverse direction.
 */
ADom.prototype.visit = function (dir) {
    if (dir) {
        this.current_ = this.view_.previous();
    } else  {
        this.current_ = this.view_.next();
    }
    // skip empties
    if (this.current_.childNodes.length === 0 && this.current_.attributes.length  === 0) {
        return this.visit(dir);
    } else {
        return this.current_;
    }
};


/*
 * view: Return HTML for all nodes in view_ array
 * @return: {String} HTML
 */
ADom.prototype.view = function () {
    if (this.view_ === null) {
        return this.current_.html(true);
    }
    var html =this.base();
    var len = this.view_.len_;
    for (var i = 0; i < len; i++) {
        this.visit();
        html += this.html();
        html += '<br/>';
    }
    return html;
};

// >
// < Eventing:

/*
 * target: Return a suitable target for sending keypresses
 * We need to know where the focus is,
 * for now, we depend on Fire Vox  doing the work for us.
 * Uses Fire Vox global CLC_SR_CurrentAtomicObject
 * that gets set by Fire Vox whenever a focus event occurs.
 * If that variable is 0 i.e. unset,
 * we return document.body
 * @Return: Node
 */

ADom.prototype.target = function () {
  if (!CLC_SR_CurrentAtomicObject){
      CLC_SR_CurrentAtomicObject =
CLC_GetFirstAtomicObject(CLC_Window().document.body);
      }
  return CLC_SR_CurrentAtomicObject  || this.document_.body;
};

/**
 * Dispatches a left click event on the element that is the targetNode.
 * @param {Node} targetNode The target node of this operation.
 * @return {Null}
 */
ADom.prototype.click = function(targetNode){
  var evt = document.createEvent('MouseEvents');
  evt.initMouseEvent('click',true,true,document.defaultView,
                     1,0,0,0,0,false,false,false,false,0,null);
  targetNode.dispatchEvent(evt);
};

  /*
   * send a key
   */

ADom.prototype.keyPress = function(targetNode,
                                   theKey,
                                   holdCtrl, holdAlt, holdShift){
  var keyCode = 0;
  var charCode = 0;
  if (theKey == "ENTER"){
    keyCode = 13;
  } else   if (theKey == "TAB"){
    keyCode = 9;
  } else if (theKey.length == 1){
    charCode = theKey.charCodeAt(0);
    keyCode = charCode;
  }
  var evt = document.createEvent('KeyboardEvent');
  evt.initKeyEvent('keypress',true,true,
  this.document_.defaultView,holdCtrl,
                   holdAlt,holdShift,false,keyCode,charCode);
  targetNode.dispatchEvent(evt);
};


  /*
   * send a key down 
   */

ADom.prototype.keyDown = function(targetNode,
                                   theKey,
                                   holdCtrl, holdAlt, holdShift){
  var keyCode = 0;
  var charCode = 0;
  if (theKey == "ENTER"){
    keyCode = 13;
  } else   if (theKey == "TAB"){
    keyCode = 9;
  } else if (theKey.length == 1){
    charCode = theKey.charCodeAt(0);
  }
  var evt = document.createEvent('KeyboardEvent');
  evt.initKeyEvent('keydown',true,true,null,holdCtrl,
                   holdAlt,holdShift,false,keyCode,charCode);
  targetNode.dispatchEvent(evt);
};



  /*
   * send a key up 
   */

ADom.prototype.keyUp = function(targetNode,
                                   theKey,
                                   holdCtrl, holdAlt, holdShift){
  var keyCode = 0;
  var charCode = 0;
  if (theKey == "ENTER"){
    keyCode = 13;
  } else   if (theKey == "TAB"){
    keyCode = 9;
  } else if (theKey.length == 1){
    charCode = theKey.charCodeAt(0);
  }
  var evt = document.createEvent('KeyboardEvent');
  evt.initKeyEvent('keyup',true,true,null,holdCtrl,
                   holdAlt,holdShift,false,keyCode,charCode);
  targetNode.dispatchEvent(evt);
};

// >
// < A11y Reflection:

// >
// <WebSearch:

/*
 * Perform a webSearch:
 */
ADom.prototype.webSearch = function(q) {
  var ub = document.getElementById('urlbar');
  ub.value = q;
handleURLBarCommand();
};

// >
// <repl hookup

/*
 * Update adom pointer in repl to point to current document.
 * @return {ADom}
 */
repl.updateADom = function ()  {
    if (content.document.adom == undefined) {
        // constructor caches adom in content.document
        repl.adom = new ADom(content.document);
    } else {
      repl.adom = content.document.adom;
    }
    return repl.adom;
};

// >
// <end of file



// local variables:
// folded-file: t
// end:

// >