OLD | NEW |
1 // This is used by several tests to help get images reliably preloaded. | 1 // This is used by several tests to help get images reliably preloaded. |
2 | 2 |
3 // Given a node, loads all urls specified in style declarations | 3 // Given a node, loads all urls specified in style declarations |
4 // attached to the node or it's decendants. | 4 // attached to the node or it's decendants. |
5 // imageCount specifies the number of images we expect to find (to try to add so
me | 5 // imageCount specifies the number of images we expect to find (to try to add so
me |
6 // protection against brittleness due to imperfect url parsing, since other miss
ing a preload | 6 // protection against brittleness due to imperfect url parsing, since other miss
ing a preload |
7 // will typically result in a test that fails only occasionally). | 7 // will typically result in a test that fails only occasionally). |
8 // If failPattern is specified, then any url that matches the regex | 8 // If failPattern is specified, then any url that matches the regex |
9 // will be expected to fail to load. | 9 // will be expected to fail to load. |
10 function preloadImagesFromStyle(rootNode, imageCount, onComplete, failPattern) { | 10 function preloadImagesFromStyle(rootNode, imageCount, onComplete, failPattern) { |
11 var basePath = location.href.substring(0, location.href.lastIndexOf('/') + 1
); | 11 var basePath = location.href.substring(0, location.href.lastIndexOf('/') + 1
); |
12 var nodes = rootNode.querySelectorAll('[style]'); | 12 var nodes = rootNode.querySelectorAll('[style]'); |
13 var imagesToLoad = []; | 13 var imagesToLoad = []; |
14 var seenUrls = {}; | 14 var seenUrls = {}; |
15 for (var i = 0; i < nodes.length; i++) { | 15 for (var i = 0; i < nodes.length; i++) { |
16 var urls = nodes[i].style.cssText.split(/url\w*\(([^)]*)\)/); | 16 var urls = nodes[i].style.cssText.split(/url\w*\("([^)]*)"\)/); |
17 for (var j = 1; j < urls.length; j += 2) { | 17 for (var j = 1; j < urls.length; j += 2) { |
18 // Attempt to convert URL to a relative path in order to have determ
inistic error messages. | 18 // Attempt to convert URL to a relative path in order to have determ
inistic error messages. |
19 var url = urls[j]; | 19 var url = urls[j]; |
20 if (url.indexOf(basePath) == 0) | 20 if (url.indexOf(basePath) == 0) |
21 url = url.substring(basePath.length); | 21 url = url.substring(basePath.length); |
22 | 22 |
23 var error = false; | 23 var error = false; |
24 if (failPattern && failPattern.test(url)) | 24 if (failPattern && failPattern.test(url)) |
25 error = true; | 25 error = true; |
26 if (url in seenUrls) | 26 if (url in seenUrls) |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
65 } | 65 } |
66 | 66 |
67 for (var i = 0; i < imagesToLoad.length; i++) { | 67 for (var i = 0; i < imagesToLoad.length; i++) { |
68 var img = new Image(); | 68 var img = new Image(); |
69 var expectError = imagesToLoad[i].error; | 69 var expectError = imagesToLoad[i].error; |
70 img.addEventListener('load', onImageLoad.bind(undefined, imagesToLoad[i]
.url, !expectError)); | 70 img.addEventListener('load', onImageLoad.bind(undefined, imagesToLoad[i]
.url, !expectError)); |
71 img.addEventListener('error', onImageLoad.bind(undefined, imagesToLoad[i
].url, !!expectError)); | 71 img.addEventListener('error', onImageLoad.bind(undefined, imagesToLoad[i
].url, !!expectError)); |
72 img.src = imagesToLoad[i].url; | 72 img.src = imagesToLoad[i].url; |
73 } | 73 } |
74 } | 74 } |
OLD | NEW |