Skip to content

Commit 9d6cd65

Browse files
committed
handle status code 0
1 parent 48d1bb1 commit 9d6cd65

2 files changed

Lines changed: 97 additions & 1 deletion

File tree

spec/dom-to-image-more.spec.js

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1012,6 +1012,102 @@
10121012
.then(done)
10131013
.catch(done);
10141014
});
1015+
1016+
it('should handle HTTP status 0 (network error) with placeholder', function (done) {
1017+
const placeholder = 'data:image/png;base64,PLACEHOLDER';
1018+
const originalPlaceholder = domtoimage.impl.options.imagePlaceholder;
1019+
domtoimage.impl.options.imagePlaceholder = placeholder;
1020+
1021+
// Mock XMLHttpRequest to simulate status 0
1022+
const originalXHR = global.XMLHttpRequest;
1023+
global.XMLHttpRequest = function() {
1024+
const mockXHR = {
1025+
readyState: 0,
1026+
status: 0,
1027+
response: null,
1028+
onreadystatechange: null,
1029+
ontimeout: null,
1030+
responseType: '',
1031+
timeout: 0,
1032+
withCredentials: false,
1033+
open: function() {},
1034+
send: function() {
1035+
// Simulate the request completing with status 0
1036+
setTimeout(() => {
1037+
mockXHR.readyState = 4;
1038+
mockXHR.status = 0;
1039+
if (mockXHR.onreadystatechange) {
1040+
mockXHR.onreadystatechange();
1041+
}
1042+
}, 10);
1043+
},
1044+
setRequestHeader: function() {}
1045+
};
1046+
return mockXHR;
1047+
};
1048+
1049+
domtoimage.impl.util
1050+
.getAndEncode('http://example.com/test-image-with-placeholder.png')
1051+
.then(function (resource) {
1052+
const placeholderData = placeholder.split(/,/)[1];
1053+
assert.equal(resource, placeholderData);
1054+
})
1055+
.then(function() {
1056+
// Restore original XMLHttpRequest and placeholder
1057+
global.XMLHttpRequest = originalXHR;
1058+
domtoimage.impl.options.imagePlaceholder = originalPlaceholder;
1059+
})
1060+
.then(done)
1061+
.catch(done);
1062+
});
1063+
1064+
it('should handle HTTP status 0 (network error) without placeholder', function (done) {
1065+
const originalPlaceholder = domtoimage.impl.options.imagePlaceholder;
1066+
domtoimage.impl.options.imagePlaceholder = undefined;
1067+
1068+
// Mock XMLHttpRequest to simulate status 0
1069+
const originalXHR = global.XMLHttpRequest;
1070+
global.XMLHttpRequest = function() {
1071+
const mockXHR = {
1072+
readyState: 0,
1073+
status: 0,
1074+
response: null,
1075+
onreadystatechange: null,
1076+
ontimeout: null,
1077+
responseType: '',
1078+
timeout: 0,
1079+
withCredentials: false,
1080+
open: function() {},
1081+
send: function() {
1082+
// Simulate the request completing with status 0
1083+
setTimeout(() => {
1084+
mockXHR.readyState = 4;
1085+
mockXHR.status = 0;
1086+
if (mockXHR.onreadystatechange) {
1087+
mockXHR.onreadystatechange();
1088+
}
1089+
}, 10);
1090+
},
1091+
setRequestHeader: function() {}
1092+
};
1093+
return mockXHR;
1094+
};
1095+
1096+
domtoimage.impl.util
1097+
.getAndEncode('http://example.com/test-image-without-placeholder.png')
1098+
.then(function (resource) {
1099+
// Should return empty string when status is 0 and no placeholder
1100+
assert.equal(resource, '');
1101+
})
1102+
.then(function() {
1103+
// Restore original XMLHttpRequest and placeholder
1104+
global.XMLHttpRequest = originalXHR;
1105+
domtoimage.impl.options.imagePlaceholder = originalPlaceholder;
1106+
})
1107+
.then(done)
1108+
.catch(done);
1109+
});
1110+
10151111
});
10161112

10171113
describe('styles', function () {

src/dom-to-image-more.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -927,7 +927,7 @@
927927
return;
928928
}
929929

930-
if (request.status >= 300) {
930+
if (request.status >= 300 || request.status === 0) {
931931
if (placeholder) {
932932
resolve(placeholder);
933933
} else {

0 commit comments

Comments
 (0)