haikuwebkit/LayoutTests/resources/image-preload-helper.js

75 lines
3.0 KiB
JavaScript
Raw Permalink Normal View History

// This is used by several tests to help get images reliably preloaded.
// Given a node, loads all urls specified in style declarations
// attached to the node or it's decendants.
// imageCount specifies the number of images we expect to find (to try to add some
// protection against brittleness due to imperfect url parsing, since other missing a preload
// will typically result in a test that fails only occasionally).
// If failPattern is specified, then any url that matches the regex
// will be expected to fail to load.
function preloadImagesFromStyle(rootNode, imageCount, onComplete, failPattern) {
var basePath = location.href.substring(0, location.href.lastIndexOf('/') + 1);
var nodes = rootNode.querySelectorAll('[style]');
var imagesToLoad = [];
var seenUrls = {};
for (var i = 0; i < nodes.length; i++) {
Serialize all URLs with double-quotes per CSSOM spec https://bugs.webkit.org/show_bug.cgi?id=184935 Patch by Chris Nardi <cnardi@chromium.org> on 2018-05-04 Reviewed by Antti Koivisto. LayoutTests/imported/w3c: Update expectations for using double quotes when serializing URLs. * web-platform-tests/cssom/serialize-values-expected.txt: * web-platform-tests/web-animations/animation-model/animation-types/accumulation-per-property-expected.txt: * web-platform-tests/web-animations/animation-model/animation-types/addition-per-property-expected.txt: * web-platform-tests/web-animations/animation-model/animation-types/interpolation-per-property-expected.txt: Source/WebCore: According to https://drafts.csswg.org/cssom/#serialize-a-url, all URLs should be serialized as strings, which means they should have double quotes around the text of the URL. Update our implementation to match this (and Firefox/Chrome). Also remove isCSSTokenizerURL() as this method is no longer needed. Tests: Many LayoutTests updated to use double quotes. * css/CSSMarkup.cpp: (WebCore::serializeString): Remove FIXME as this was already fixed in a previous patch. (WebCore::serializeURL): Remove FIXME and update implementation. LayoutTests: Update tests to use double quotes when serializing URLs, and fixes for using double quotes in the test. * css3/calc/cross-fade-calc.html: * css3/filters/backdrop/backdropfilter-property-computed-style-expected.txt: * css3/filters/backdrop/backdropfilter-property-computed-style.html: * css3/filters/backdrop/backdropfilter-property-parsing-expected.txt: * css3/filters/backdrop/backdropfilter-property-parsing.html: * css3/filters/filter-property-computed-style-expected.txt: * css3/filters/filter-property-computed-style.html: * css3/filters/filter-property-expected.txt: * css3/filters/filter-property-parsing-expected.txt: * css3/filters/filter-property-parsing.html: * css3/filters/filter-property.html: * css3/filters/unprefixed-expected.txt: * css3/filters/unprefixed.html: * fast/backgrounds/background-shorthand-after-set-backgroundSize-expected.txt: * fast/backgrounds/background-shorthand-after-set-backgroundSize.html: * fast/backgrounds/background-shorthand-with-backgroundSize-style-expected.txt: * fast/backgrounds/background-shorthand-with-backgroundSize-style.html: * fast/backgrounds/multiple-backgrounds-computed-style-expected.txt: * fast/backgrounds/multiple-backgrounds-computed-style.html: * fast/css/background-position-serialize-expected.txt: * fast/css/cursor-parsing-expected.txt: * fast/css/cursor-parsing-image-set-expected.txt: * fast/css/cursor-parsing-image-set.html: * fast/css/cursor-parsing.html: * fast/css/getComputedStyle/computed-style-border-image-expected.txt: * fast/css/getComputedStyle/computed-style-border-image.html: * fast/css/getComputedStyle/computed-style-cross-fade-expected.txt: * fast/css/getComputedStyle/computed-style-cross-fade.html: * fast/css/getComputedStyle/computed-style-properties-expected.txt: * fast/css/getComputedStyle/computed-style-properties.html: * fast/css/getComputedStyle/getComputedStyle-background-shorthand-expected.txt: * fast/css/getComputedStyle/getComputedStyle-background-shorthand.html: * fast/css/getComputedStyle/getComputedStyle-list-style-shorthand-expected.txt: * fast/css/getComputedStyle/getComputedStyle-list-style-shorthand.html: * fast/css/image-set-parsing.html: * fast/css/image-set-setting-expected.txt: * fast/css/image-set-setting.html: * fast/css/image-set-unprefixed-expected.txt: * fast/css/image-set-unprefixed.html: * fast/css/invalid-cursor-property-crash.html: * fast/css/parse-border-image-repeat-null-crash-expected.txt: * fast/css/uri-token-parsing-expected.txt: * fast/css/uri-token-parsing.html: * fast/css/url-with-multi-byte-unicode-escape-expected.txt: * fast/css/url-with-multi-byte-unicode-escape.html: * fast/filter-image/parse-filter-image-expected.txt: * fast/filter-image/parse-filter-image.html: * fast/innerHTML/innerHTML-uri-resolution.html: * fast/inspector-support/cssURLQuotes-expected.txt: * fast/inspector-support/cssURLQuotes.html: * fast/masking/parsing-clip-path-iri-expected.txt: * fast/masking/parsing-clip-path-iri.html: * fast/masking/parsing-mask-expected.txt: * fast/masking/parsing-mask.html: * fast/shapes/parsing/parsing-shape-outside-expected.txt: * fast/shapes/parsing/parsing-shape-outside.html: * fast/shapes/parsing/parsing-test-utils.js: (removeBaseURL): * platform/gtk/imported/w3c/web-platform-tests/web-animations/animation-model/animation-types/accumulation-per-property-expected.txt: * platform/gtk/imported/w3c/web-platform-tests/web-animations/animation-model/animation-types/addition-per-property-expected.txt: * platform/gtk/imported/w3c/web-platform-tests/web-animations/animation-model/animation-types/interpolation-per-property-expected.txt: * platform/ios/fast/css/image-set-unprefixed-expected.txt: Removed. * platform/mac-sierra/imported/w3c/web-platform-tests/web-animations/animation-model/animation-types/accumulation-per-property-expected.txt: * platform/mac-sierra/imported/w3c/web-platform-tests/web-animations/animation-model/animation-types/addition-per-property-expected.txt: * platform/mac-sierra/imported/w3c/web-platform-tests/web-animations/animation-model/animation-types/interpolation-per-property-expected.txt: * platform/win/fast/css/parse-border-image-repeat-null-crash-expected.txt: * platform/wpe/imported/w3c/web-platform-tests/web-animations/animation-model/animation-types/accumulation-per-property-expected.txt: * platform/wpe/imported/w3c/web-platform-tests/web-animations/animation-model/animation-types/addition-per-property-expected.txt: * platform/wpe/imported/w3c/web-platform-tests/web-animations/animation-model/animation-types/interpolation-per-property-expected.txt: * resources/image-preload-helper.js: (preloadImagesFromStyle): * svg/css/svg-attribute-parser-mode-expected.txt: * svg/css/svg-attribute-parser-mode.html: * svg/custom/marker-getPropertyValue-expected.txt: * svg/custom/marker-getPropertyValue.svg: Canonical link: https://commits.webkit.org/200792@main git-svn-id: https://svn.webkit.org/repository/webkit/trunk@231359 268f45cc-cd09-0410-ab3c-d52691b4dbfc
2018-05-04 17:32:48 +00:00
var urls = nodes[i].style.cssText.split(/url\w*\("([^)]*)"\)/);
for (var j = 1; j < urls.length; j += 2) {
// Attempt to convert URL to a relative path in order to have deterministic error messages.
var url = urls[j];
if (url.indexOf(basePath) == 0)
url = url.substring(basePath.length);
var error = false;
if (failPattern && failPattern.test(url))
error = true;
if (url in seenUrls)
continue;
seenUrls[url] = true;
imagesToLoad.push({url: url, error: error});
}
}
if (imageCount != imagesToLoad.length) {
var msg = 'Found the following ' + imagesToLoad.length + ' images, when expecting ' + imageCount + ': ';
for (var i = 0; i < imagesToLoad.length; i++) {
msg += '\n' + imagesToLoad[i].url;
}
testFailed(msg);
}
loadImages(imagesToLoad, onComplete);
}
// For each object in the given array, attempt to load the image specified by the
// url property. If the error property is specified and true, then the load is
// expected to fail. Once all loads have completed or failed, onComplete is invoked.
function loadImages(imagesToLoad, onComplete) {
var imagesLeftToLoad = imagesToLoad.length;
function onImageLoad(url, success, e) {
// This debug output order is non-deterministic - only show when not running in DRT
if (!window.testRunner)
debug( 'Event "' + e.type + '": ' + url);
if (!success)
testFailed('Got unexpected \'' + e.type + '\' event for image: ' + url);
imagesLeftToLoad--;
if (imagesLeftToLoad == 0) {
onComplete();
}
if (imagesLeftToLoad < 0)
testFailed('Got more load/error callbacks than expected.');
}
for (var i = 0; i < imagesToLoad.length; i++) {
var img = new Image();
var expectError = imagesToLoad[i].error;
img.addEventListener('load', onImageLoad.bind(undefined, imagesToLoad[i].url, !expectError));
img.addEventListener('error', onImageLoad.bind(undefined, imagesToLoad[i].url, !!expectError));
img.src = imagesToLoad[i].url;
}
}