Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

LibWeb/HTML: Dispatch toggle and beforetoggle events from dialogs #2223

Merged
merged 1 commit into from
Nov 9, 2024

Conversation

AtkinsSJ
Copy link
Member

@AtkinsSJ AtkinsSJ commented Nov 8, 2024

Corresponds to whatwg/html#10091

@shlyakpavel
Copy link
Contributor

Can we make a test? I guess something like

<html>
<body>
    <dialog id="test-dialog">Test Dialog</dialog>
    <script>
        const dialog = document.getElementById('test-dialog');
        let preventNextToggle = false;

        dialog.addEventListener('beforetoggle', (event) => {
            console.log(`beforetoggle event fired: open=${dialog.open}`);
            if (preventNextToggle) {
                event.preventDefault();
                console.log('Toggle action prevented');
            }
        });

        dialog.addEventListener('toggle', () => {
            console.log(`toggle event fired: open=${dialog.open}`);
        });

        async function runTests() {
            console.log('--- Running Tests for Patch ---');

            // Test 1: Prevent toggle from closed to open
            console.log('Test 1: Prevent toggle from closed to open');
            preventNextToggle = true;
            dialog.show();
            await new Promise(r => setTimeout(r, 10)); // Wait for events
            console.log(dialog.open === false ? 'success' : 'fail');

            // Test 2: Prevent toggle from open to closed
            console.log('Test 2: Prevent toggle from open to closed');
            dialog.show();  // Ensure dialog is open
            preventNextToggle = true;
            dialog.close();
            await new Promise(r => setTimeout(r, 10));
            console.log(dialog.open === true ? 'success' : 'fail');

            // Test 3: Ensure toggle event is dispatched on open
            console.log('Test 3: Ensure toggle event is dispatched on open');
            preventNextToggle = false;
            let toggleFired = false;
            dialog.addEventListener('toggle', () => toggleFired = true, { once: true });
            dialog.show();
            await new Promise(r => setTimeout(r, 10));
            console.log(toggleFired ? 'success' : 'fail');

            // Test 4: Ensure toggle event is dispatched on close
            console.log('Test 4: Ensure toggle event is dispatched on close');
            toggleFired = false;
            dialog.addEventListener('toggle', () => toggleFired = true, { once: true });
            dialog.close();
            await new Promise(r => setTimeout(r, 10));
            console.log(toggleFired ? 'success' : 'fail');
        }

        runTests();
    </script>
</body>
</html>

should work

@AtkinsSJ
Copy link
Member Author

AtkinsSJ commented Nov 8, 2024

I avoided adding a test because we still have so many FIXMEs in this code, that I'm not sure it does anything correctly enough to test. I can try importing some WPT tests if needed though.

@awesomekling awesomekling merged commit 36f8dfa into LadybirdBrowser:master Nov 9, 2024
6 checks passed
@AtkinsSJ AtkinsSJ deleted the dialog-toggle-events branch November 11, 2024 12:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants