efb6797f9531ab43e4a2e8bc1967f99a0cbc9fd1
[WebKit-https.git] / LayoutTests / editing / pasteboard / img-srcset-copy-paste-canonicalization.html
1 <!DOCTYPE html>
2 <html>
3 <body>
4 <script src="../../resources/js-test-pre.js"></script>
5 <div id="destination" contenteditable="true"></div>
6 <iframe id="iframe" src="resources/img-srcset-copy-paste-canonicalization-iframe.html"></iframe>
7 <script>
8     window.jsTestIsAsync = true;
9
10     description("The following test does a copy and a paste of an image with the srcset attribute. The test is successful if the value of the srcset attribute has been canonicalized. To run the test manually, copy the green square into the contentediable area above the iframe.");
11     
12     function canonicalize(href, relativeURL) {
13         var hrefSegments = href.split("/");
14
15         // Remove the filename from the href
16         hrefSegments.splice(hrefSegments.length - 1, 1);
17
18         var relativeURLSegments = relativeURL.split("/");
19         while (relativeURLSegments.length > 0 && relativeURLSegments[0] == "..") {
20             hrefSegments.splice(hrefSegments.length - 1, 1);
21             relativeURLSegments.splice(0, 1);
22         }
23         return hrefSegments.concat(relativeURLSegments).join("/");
24     }
25
26     var iframeHref;
27     var sourceImageSegments;
28     var destinationImageSegments;
29
30     function runTests(href) {
31         iframeHref = href;
32
33         var iframe = document.getElementById("iframe");
34         var iframeDocument = iframe.contentDocument;
35
36         var source = iframeDocument.getElementById("source");
37         var destination = document.getElementById("destination");
38
39         source.focus();
40         iframeDocument.execCommand("SelectAll");
41         iframeDocument.execCommand("Copy");
42         destination.focus();
43         document.execCommand("SelectAll");
44         document.execCommand("Paste");
45
46         var sourceImage = iframeDocument.getElementById("image");
47         var destinationImage = document.getElementById("image");
48         if (!destinationImage) {
49             testFailed("Image was not pasted");
50             finishJSTest();
51             return;
52         }
53
54         destinationImage.onerror = function() {
55             testFailed("Image should not fail to load");
56             finishJSTest();
57         }
58         image.onload = function() {
59             finishJSTest();
60         }
61
62         sourceImageSegments = sourceImage.getAttribute("srcset").split(" ");
63         destinationImageSegments = destinationImage.getAttribute("srcset").split(" ");
64         shouldBe("canonicalize(iframeHref, sourceImageSegments[0])", "destinationImageSegments[0]");
65         shouldBe("canonicalize(iframeHref, sourceImageSegments[2])", "destinationImageSegments[2]");
66         shouldBe("sourceImageSegments.length", "4");
67         shouldBe("destinationImageSegments.length", "4");
68         shouldBe("sourceImageSegments[1]", "\"2x,\"");
69         shouldBe("destinationImageSegments[1]", "\"2x,\"");
70         shouldBe("sourceImageSegments[3]", "\"1x\"");
71         shouldBe("destinationImageSegments[3]", "\"1x\"");
72     }
73 </script>
74 <script src="../../resources/js-test-post.js"></script>
75 </body>
76 </html>