1<!DOCTYPE html> 2<!-- 3Copyright (c) 2015 The Chromium Authors. All rights reserved. 4Use of this source code is governed by a BSD-style license that can be 5found in the LICENSE file. 6--> 7<link rel="import" href="/tracing/base/base.html"> 8<script> 9 10'use strict'; 11 12tr.exportTo('tr.b', function() { 13 function _iterateElementDeeplyImpl(element, cb, thisArg, includeElement) { 14 if (includeElement) { 15 if (cb.call(thisArg, element)) 16 return true; 17 } 18 19 if (element.shadowRoot) { 20 if (_iterateElementDeeplyImpl(element.shadowRoot, cb, thisArg, false)) 21 return true; 22 } 23 for (var i = 0; i < element.children.length; i++) { 24 if (_iterateElementDeeplyImpl(element.children[i], cb, thisArg, true)) 25 return true; 26 } 27 } 28 function iterateElementDeeply(element, cb, thisArg) { 29 _iterateElementDeeplyImpl(element, cb, thisArg, false); 30 } 31 32 function findDeepElementMatchingPredicate(element, predicate) { 33 var foundElement = undefined; 34 function matches(element) { 35 var match = predicate(element); 36 if (!match) 37 return false; 38 foundElement = element; 39 return true; 40 } 41 iterateElementDeeply(element, matches); 42 return foundElement; 43 } 44 45 function findDeepElementsMatchingPredicate(element, predicate) { 46 var foundElements = []; 47 function matches(element) { 48 var match = predicate(element); 49 if (match) { 50 foundElements.push(element); 51 } 52 return false; 53 } 54 iterateElementDeeply(element, matches); 55 return foundElements; 56 } 57 58 function findDeepElementMatching(element, selector) { 59 return findDeepElementMatchingPredicate(element, function(element) { 60 return element.matches(selector); 61 }); 62 } 63 function findDeepElementsMatching(element, selector) { 64 return findDeepElementsMatchingPredicate(element, function(element) { 65 return element.matches(selector); 66 }); 67 } 68 function findDeepElementWithTextContent(element, re) { 69 return findDeepElementMatchingPredicate(element, function(element) { 70 if (element.children.length !== 0) 71 return false; 72 return re.test(element.textContent); 73 }); 74 } 75 return { 76 iterateElementDeeply: iterateElementDeeply, 77 findDeepElementMatching: findDeepElementMatching, 78 findDeepElementsMatching: findDeepElementsMatching, 79 findDeepElementMatchingPredicate: findDeepElementMatchingPredicate, 80 findDeepElementsMatchingPredicate: findDeepElementsMatchingPredicate, 81 findDeepElementWithTextContent: findDeepElementWithTextContent 82 }; 83}); 84</script> 85