IE11 compatibility fixes (#1948)

This commit is contained in:
Vincent 2023-11-02 23:40:33 +01:00 committed by GitHub
parent 3912e3c2c2
commit 8ddd079b90
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 14 additions and 3 deletions

View File

@ -5,6 +5,11 @@
var attrPrefix = 'hx-target-';
// IE11 doesn't support string.startsWith
function startsWith(str, prefix) {
return str.substring(0, prefix.length) === prefix
}
/**
* @param {HTMLElement} elt
* @param {number} respCode
@ -38,7 +43,7 @@
'***',
'xxx',
];
if (respCode.startsWith('4') || respCode.startsWith('5')) {
if (startsWith(respCode, '4') || startsWith(respCode, '5')) {
attrPossibilities.push('error');
}

View File

@ -139,7 +139,8 @@ describe("hx-on attribute", function() {
it("can handle event types with dots", function () {
var btn = make("<button hx-on='my.custom.event: window.foo = true'>Foo</button>");
btn.dispatchEvent(new CustomEvent('my.custom.event'));
// IE11 doesn't support `new CustomEvent()` so call htmx' internal utility function
btn.dispatchEvent(htmx._("makeEvent")('my.custom.event'));
window.foo.should.equal(true);
delete window.foo;
});

View File

@ -29,6 +29,11 @@ describe('Core htmx extension tests', function() {
onEvent: function(name, evt) {
if (name === 'htmx:beforeRequest') {
evt.preventDefault();
if (IsIE11()) {
// IE11 doesn't set defaultPrevented to true on custom events it seems, so use a
// return false instead to cancel the event
return false
}
}
}
});

View File

@ -168,7 +168,7 @@
<script class="mocha-exec">
document.addEventListener("DOMContentLoaded", function () {
mocha.setup({globals: ['$0', '$1', '$2', '$3', '$4', 'performance', 'requestAnimationFrame', 'cancelAnimationFrame']}); <!-- IE11 -->
mocha.setup({globals: ['$0', '$1', '$2', '$3', '$4', 'performance', 'requestAnimationFrame', 'cancelAnimationFrame', 'confirm']}); <!-- IE11 -->
mocha.run();
})
</script>