8000 WebDriver: [macOS] [Actions] [Key] Shift modifier not applying to typ… · WebKit/WebKit@7bee9f5 · GitHub
[go: up one dir, main page]

Skip to content

Commit 7bee9f5

Browse files
committed
WebDriver: [macOS] [Actions] [Key] Shift modifier not applying to typed text
https://bugs.webkit.org/show_bug.cgi?id=248770 rdar://100588610 Reviewed by BJ Burg. AppKit expects the `characters` for an NSEvent to be pre-transformed to the actual character that will be typed, which means for a capital letter, sending the key code for a letter with the shift modifier held should result in the capital form of that letter being the `characters`. This also add support for composed characters, like Shift+Option+K resulting in the Apple logo, or Shift+Option+8 resulting in the degree symbol. The clearest way to obtain the OS-defined mapping for a letter and its modifiers is to create a temporary NSEvent and call `-[NSEvent characterTransformingModifiers:]`, which applies the provided modifiers to an existing event's `characters`, which we can then use to create the actual NSEvents we will dispatch to the system. Alternatively, we could hardcode WebDriver's list of "shifted" characters, but that only covers keys on a US-English keyboard, and does not provide any allowance for macOS's various Option/Shift modified key chords to produce special characters. This progresses the following WPT test cases: webdriver/tests/perform_actions/key_modifiers.py::test_shift_modifier_generates_capital_letters[\ue008] webdriver/tests/perform_actions/key_modifiers.py::test_shift_modifier_generates_capital_letters[\ue050] * Source/WebKit/UIProcess/Automation/mac/WebAutomationSessionMac.mm: (WebKit::WebAutomationSession::platformSimulateKeyboardInteraction): Canonical link: https://commits.webkit.org/259039@main
1 parent dab7ccb commit 7bee9f5

File tree

1 file changed

+19
-4
lines changed

1 file changed

+19
-4
lines changed

Source/WebKit/UIProcess/Automation/mac/WebAutomationSessionMac.mm

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -675,24 +675,40 @@ static NSEventModifierFlags eventModifierFlagsForVirtualKey(VirtualKey key)
675675
NSString *characters = charCode ? [NSString stringWithCharacters:&charCode.value() length:1] : nil;
676676
NSString *unmodifiedCharacters = charCodeIgnoringModifiers ? [NSString stringWithCharacters:&charCodeIgnoringModifiers.value() length:1] : nil;
677677

678-
auto eventsToBeSent = adoptNS([[NSMutableArray alloc] init]);
678+
switch (interaction) {
679+
case KeyboardInteraction::KeyPress:
680+
case KeyboardInteraction::InsertByKey:
681+
m_currentModifiers |= changedModifiers;
682+
break;
683+
case KeyboardInteraction::KeyRelease:
684+
m_currentModifiers &= ~changedModifiers;
685+
break;
686+
}
679687

680688
// FIXME: this timestamp is not even close to matching native events. Find out how to get closer.
681689
NSTimeInterval timestamp = [NSDate timeIntervalSinceReferenceDate];
682690
NSWindow *window = page.platformWindow();
683691
NSInteger windowNumber = window.windowNumber;
684692
NSPoint eventPosition = NSMakePoint(0, window.frame.size.height);
685693

694+
static constexpr auto characterTransformingModifiers = NSEventModifierFlagShift | NSEventModifierFlagOption;
695+
if (characters && (m_currentModifiers & characterTransformingModifiers)) {
696+
// `characters` will not automatically include the result of modifier keys when creating an NSEvent; AppKit
697+
// expects them to be pre-transformed. Event type is unimportant here as we are more interested in the events
698+
// ability to apply modifiers to its characters.
699+
characters = [[NSEvent keyEventWithType:NSEventTypeKeyDown location:eventPosition modifierFlags:0 timestamp:timestamp windowNumber:windowNumber context:nil characters:characters charactersIgnoringModifiers:unmodifiedCharacters isARepeat:NO keyCode:keyCode] charactersByApplyingModifiers:m_currentModifiers & characterTransformingModifiers];
700+
}
701+
702+
auto eventsToBeSent = adoptNS([[NSMutableArray alloc] init]);
703+
686704
switch (interaction) {
687705
case KeyboardInteraction::KeyPress: {
688706
NSEventType eventType = isStickyModifier ? NSEventTypeFlagsChanged : NSEventTypeKeyDown;
689-
m_currentModifiers |= changedModifiers;
690707
[eventsToBeSent addObject:[NSEvent keyEventWithType:eventType location:eventPosition modifierFlags:m_currentModifiers timestamp:timestamp windowNumber:windowNumber context:nil characters:characters charactersIgnoringModifiers:unmodifiedCharacters isARepeat:NO keyCode:keyCode]];
691708
break;
692709
}
693710
case KeyboardInteraction::KeyRelease: {
694711
NSEventType eventType = isStickyModifier ? NSEventTypeFlagsChanged : NSEventTypeKeyUp;
695-
m_currentModifiers &= ~changedModifiers;
696712

697713
// When using a physical keyboard, if command is held down, releasing a non-modifier key doesn't send a KeyUp event.
698714
bool commandKeyHeldDown = m_currentModifiers & NSEventModifierFlagCommand;
@@ -708,7 +724,6 @@ static NSEventModifierFlags eventModifierFlagsForVirtualKey(VirtualKey key)
708724
if (isStickyModifier)
709725
return;
710726

711-
m_currentModifiers |= changedModifiers;
712727
[eventsToBeSent addObject:[NSEvent keyEventWithType:NSEventTypeKeyDown location:eventPosition modifierFlags:m_currentModifiers timestamp:timestamp windowNumber:windowNumber context:nil characters:characters charactersIgnoringModifiers:unmodifiedCharacters isARepeat:NO keyCode:keyCode]];
713728
[eventsToBeSent addObject:[NSEvent keyEventWithType:NSEventTypeKeyUp location:eventPosition modifierFlags:m_currentModifiers timestamp:timestamp windowNumber:windowNumber context:nil characters:characters charactersIgnoringModifiers:unmodifiedCharacters isARepeat:NO keyCode:keyCode]];
714729
break;

0 commit comments

Comments
 (0)
0