summaryrefslogtreecommitdiffstats
path: root/dom/html/reftests/toblob-todataurl/blob.js
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-19 00:47:55 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-19 00:47:55 +0000
commit26a029d407be480d791972afb5975cf62c9360a6 (patch)
treef435a8308119effd964b339f76abb83a57c29483 /dom/html/reftests/toblob-todataurl/blob.js
parentInitial commit. (diff)
downloadfirefox-26a029d407be480d791972afb5975cf62c9360a6.tar.xz
firefox-26a029d407be480d791972afb5975cf62c9360a6.zip
Adding upstream version 124.0.1.upstream/124.0.1
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'dom/html/reftests/toblob-todataurl/blob.js')
-rw-r--r--dom/html/reftests/toblob-todataurl/blob.js68
1 files changed, 68 insertions, 0 deletions
diff --git a/dom/html/reftests/toblob-todataurl/blob.js b/dom/html/reftests/toblob-todataurl/blob.js
new file mode 100644
index 0000000000..4ed9fdb372
--- /dev/null
+++ b/dom/html/reftests/toblob-todataurl/blob.js
@@ -0,0 +1,68 @@
+function init() {
+ function end() {
+ document.documentElement.className = '';
+ }
+
+ function next() {
+ compressAndDisplay(original, end);
+ }
+
+ var original = getImageFromDataUrl(sample);
+ setImgLoadListener(original, next);
+}
+
+function compressAndDisplay(image, next) {
+ var canvas = document.createElement('canvas');
+ canvas.width = image.naturalWidth;
+ canvas.height = image.naturalHeight;
+ var ctx = canvas.getContext('2d');
+ ctx.drawImage(image, 0, 0);
+
+ function gotBlob(blob) {
+ var img = getImageFromBlob(blob);
+ setImgLoadListener(img, next);
+ document.body.appendChild(img);
+ }
+
+ // I want to test passing 'undefined' as quality as well
+ if ('quality' in window) {
+ canvas.toBlob(gotBlob, 'image/jpeg', quality);
+ } else {
+ canvas.toBlob(gotBlob, 'image/jpeg');
+ }
+}
+
+function setImgLoadListener(img, func) {
+ if (img.complete) {
+ func.call(img, { target: img});
+ } else {
+ img.addEventListener('load', func);
+ }
+}
+
+function naturalDimensionsHandler(e) {
+ var img = e.target;
+ img.width = img.naturalWidth;
+ img.height = img.naturalHeight;
+}
+
+function getImageFromBlob(blob) {
+ var img = document.createElement('img');
+ img.src = window.URL.createObjectURL(blob);
+ setImgLoadListener(img, naturalDimensionsHandler);
+ setImgLoadListener(img, function(e) {
+ window.URL.revokeObjectURL(e.target.src);
+ });
+
+ return img;
+}
+
+function getImageFromDataUrl(url) {
+ var img = document.createElement('img');
+ img.src = url;
+ setImgLoadListener(img, naturalDimensionsHandler);
+
+ return img;
+}
+
+init();