/testing/selenium-core/scripts/find_matching_child.js

http://datanucleus-appengine.googlecode.com/ · JavaScript · 69 lines · 44 code · 9 blank · 16 comment · 11 complexity · daec880a72f303043d9925901fbcfcea MD5 · raw file

  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. elementFindMatchingChildren = function(element, selector) {
  18. var matches = [];
  19. var childCount = element.childNodes.length;
  20. for (var i=0; i<childCount; i++) {
  21. var child = element.childNodes[i];
  22. if (selector(child)) {
  23. matches.push(child);
  24. } else {
  25. childMatches = elementFindMatchingChildren(child, selector);
  26. matches.push(childMatches);
  27. }
  28. }
  29. return matches.flatten();
  30. }
  31. ELEMENT_NODE_TYPE = 1;
  32. elementFindFirstMatchingChild = function(element, selector) {
  33. var childCount = element.childNodes.length;
  34. for (var i=0; i<childCount; i++) {
  35. var child = element.childNodes[i];
  36. if (child.nodeType == ELEMENT_NODE_TYPE) {
  37. if (selector(child)) {
  38. return child;
  39. }
  40. result = elementFindFirstMatchingChild(child, selector);
  41. if (result) {
  42. return result;
  43. }
  44. }
  45. }
  46. return null;
  47. }
  48. elementFindFirstMatchingParent = function(element, selector) {
  49. var current = element.parentNode;
  50. while (current != null) {
  51. if (selector(current)) {
  52. break;
  53. }
  54. current = current.parentNode;
  55. }
  56. return current;
  57. }
  58. elementFindMatchingChildById = function(element, id) {
  59. return elementFindFirstMatchingChild(element, function(element){return element.id==id} );
  60. }