This repository was archived by the owner on Feb 25, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6k
Ability to disable browser context menu #38682
Merged
Merged
Changes from all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
b543526
Proof of concept: Always disable the context menu
justinmc 8d38b6b
Ability to toggle the context menu app-wide using platform channel me…
justinmc 733ba64
Fix license, and take a guess at adding the listener on the host elem…
justinmc 3503fd4
Respond so the framework can awake invokeMethod
justinmc 1bce49f
Avoid doing duplicate work
justinmc 98cdb32
Send success boolean
justinmc f25fbca
Tests
justinmc 4fd56bd
Handle context menu on embedding strategy
justinmc acbc3be
Tests for embedding strategies
justinmc 4ace58d
Merge branch 'main' into disablable-context-menu
justinmc 4bc3f04
Trying to fix the problem with corrupted message in the tests
justinmc a2c5a88
Test via defaultPrevented and manually dispatching events
justinmc a0e294b
Revert "Trying to fix the problem with corrupted message in the tests"
justinmc ad8bc4e
Fix tests by using proper codec
justinmc f626594
Code clean up
justinmc edb57c3
Merge branch 'main' into disablable-context-menu
justinmc 781148d
Operate on domWindow instead of body
justinmc 85b6d88
Test dispatching on different dom elements
justinmc 167babb
Merge branch 'main' into disablable-context-menu
justinmc File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -121,4 +121,60 @@ void doTests() { | |
reason: 'Should be injected `nextTo` the passed element.'); | ||
}); | ||
}); | ||
|
||
group('context menu', () { | ||
setUp(() { | ||
target = createDomElement('this-is-the-target'); | ||
domDocument.body!.append(target); | ||
strategy = CustomElementEmbeddingStrategy(target); | ||
strategy.initialize(); | ||
}); | ||
|
||
tearDown(() { | ||
target.remove(); | ||
}); | ||
|
||
test('disableContextMenu and enableContextMenu can toggle the context menu', () { | ||
// When the app starts, contextmenu events are not prevented. | ||
DomEvent event = createDomEvent('Event', 'contextmenu'); | ||
expect(event.defaultPrevented, isFalse); | ||
target.dispatchEvent(event); | ||
expect(event.defaultPrevented, isFalse); | ||
|
||
// Disabling the context menu causes contextmenu events to be prevented. | ||
strategy.disableContextMenu(); | ||
event = createDomEvent('Event', 'contextmenu'); | ||
expect(event.defaultPrevented, isFalse); | ||
target.dispatchEvent(event); | ||
expect(event.defaultPrevented, isTrue); | ||
Comment on lines
+144
to
+149
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Another important test is to dispatch an event outside of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Great idea, thanks! |
||
|
||
// Disabling again has no effect. | ||
strategy.disableContextMenu(); | ||
event = createDomEvent('Event', 'contextmenu'); | ||
expect(event.defaultPrevented, isFalse); | ||
target.dispatchEvent(event); | ||
expect(event.defaultPrevented, isTrue); | ||
|
||
// Dispatching on a DOM element outside of target's subtree has no effect. | ||
event = createDomEvent('Event', 'contextmenu'); | ||
expect(event.defaultPrevented, isFalse); | ||
domDocument.body!.dispatchEvent(event); | ||
expect(event.defaultPrevented, isFalse); | ||
|
||
// Enabling the context menu means that contextmenu events are back to not | ||
// being prevented. | ||
strategy.enableContextMenu(); | ||
event = createDomEvent('Event', 'contextmenu'); | ||
expect(event.defaultPrevented, isFalse); | ||
target.dispatchEvent(event); | ||
expect(event.defaultPrevented, isFalse); | ||
|
||
// Enabling again has no effect. | ||
strategy.enableContextMenu(); | ||
event = createDomEvent('Event', 'contextmenu'); | ||
expect(event.defaultPrevented, isFalse); | ||
target.dispatchEvent(event); | ||
expect(event.defaultPrevented, isFalse); | ||
}); | ||
}); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've omitted any web or browser namespacing (not "flutter/webcontextmenu") because other methods don't seem to include platform names like that. And maybe in the future this could be used on other platforms.
In the framework PR I'll make the API specific (BrowserContextMenu.disable or something like that).