Project

General

Profile

1
/*
2
 * Copyright 2004 ThoughtWorks, Inc
3
 *
4
 *  Licensed under the Apache License, Version 2.0 (the "License");
5
 *  you may not use this file except in compliance with the License.
6
 *  You may obtain a copy of the License at
7
 *
8
 *      http://www.apache.org/licenses/LICENSE-2.0
9
 *
10
 *  Unless required by applicable law or agreed to in writing, software
11
 *  distributed under the License is distributed on an "AS IS" BASIS,
12
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
 *  See the License for the specific language governing permissions and
14
 *  limitations under the License.
15
 *
16
 */
17

    
18
elementFindMatchingChildren = function(element, selector) {
19
  var matches = [];
20

    
21
  var childCount = element.childNodes.length;
22
  for (var i=0; i<childCount; i++) {
23
    var child = element.childNodes[i];
24
    if (selector(child)) {
25
      matches.push(child);
26
    } else {
27
      childMatches = elementFindMatchingChildren(child, selector);
28
      matches.push(childMatches);
29
    }
30
  }
31

    
32
  return matches.flatten();
33
}
34

    
35
ELEMENT_NODE_TYPE = 1;
36

    
37
elementFindFirstMatchingChild = function(element, selector) {
38

    
39
  var childCount = element.childNodes.length;
40
  for (var i=0; i<childCount; i++) {
41
    var child = element.childNodes[i];
42
    if (child.nodeType == ELEMENT_NODE_TYPE) {
43
      if (selector(child)) {
44
        return child;
45
      }
46
      result = elementFindFirstMatchingChild(child, selector);
47
      if (result) {
48
        return result;
49
      }
50
    }
51
  }
52
  return null;
53
}
54

    
55
elementFindFirstMatchingParent = function(element, selector) {
56
  var current = element.parentNode;
57
  while (current != null) {
58
    if (selector(current)) {
59
      break;
60
    }
61
    current = current.parentNode;
62
  }
63
  return current;
64
}
65

    
66
elementFindMatchingChildById = function(element, id) {
67
  return elementFindFirstMatchingChild(element, function(element){return element.id==id} );
68
}
69

    
(1-1/20)