Skip to content

Commit

Permalink
AX: Spell check attributes for an isolated object's subrange do not m…
Browse files Browse the repository at this point in the history
…atch the spell check attributes of the object's full range.

https://bugs.webkit.org/show_bug.cgi?id=255574
<rdar://problem/108174222>

Reviewed by Chris Fleizach.

We were caching the attributed string for an isolated object range with spell check attributes and using it for any subrange. However, the spell check attributes of the full range may not match the spell check attributes of a subrange. For instance, if the object's range text is "hello world", there is no misspellings in it. But if a subrange with offset 1 and length 9 is requested, this would contains the text "ello worl" which contains two misspelled words. For this reason, it is necessary to perform the spell check upon request of subranges.

Covered by test accessibility/content-editable-as-textarea.html.

* Source/WebCore/accessibility/AccessibilityObject.h:
* Source/WebCore/accessibility/isolatedtree/mac/AXIsolatedObjectMac.mm:
(WebCore::AXIsolatedObject::initializePlatformProperties):
(WebCore::AXIsolatedObject::cachedAttributedStringForTextMarkerRange const):
* Source/WebCore/accessibility/mac/AccessibilityObjectMac.mm:
(WebCore::attributedStringSetSpelling):
* Source/WebCore/accessibility/mac/WebAccessibilityObjectWrapperBase.h:

Canonical link: https://commits.webkit.org/263070@main
  • Loading branch information
AndresGonzalezApple committed Apr 18, 2023
1 parent 3cb14d2 commit 616bcfa
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 9 deletions.
5 changes: 0 additions & 5 deletions Source/WebCore/accessibility/AccessibilityObject.h
Expand Up @@ -40,18 +40,13 @@

#if PLATFORM(COCOA)
#include <wtf/RetainPtr.h>
#endif

#if PLATFORM(COCOA)

OBJC_CLASS NSArray;
OBJC_CLASS NSAttributedString;
OBJC_CLASS NSData;
OBJC_CLASS NSMutableAttributedString;
OBJC_CLASS NSString;
OBJC_CLASS NSValue;
OBJC_CLASS NSView;

#endif

namespace WebCore {
Expand Down
Expand Up @@ -190,12 +190,26 @@
return nil;

NSMutableAttributedString *result = [[NSMutableAttributedString alloc] initWithAttributedString:[attributedText attributedSubstringFromRange:*nsRange]];
if (!result.length)
return result;

auto resultRange = NSMakeRange(0, result.length);
// The AttributedString is cached with spelling info. If the caller does not request spelling info, we have to remove it before returning.
if (spellCheck == SpellCheck::No) {
auto fullRange = NSMakeRange(0, result.length);
[result removeAttribute:NSAccessibilityMisspelledTextAttribute range:fullRange];
[result removeAttribute:NSAccessibilityMarkedMisspelledTextAttribute range:fullRange];
[result removeAttribute:NSAccessibilityMisspelledTextAttribute range:resultRange];
[result removeAttribute:NSAccessibilityMarkedMisspelledTextAttribute range:resultRange];
return result;
}

// For any nsRange different from the full range, remove exissting spell check attribute and spell check the text.
auto fullRange = NSMakeRange(0, [attributedText length]);
if (!NSEqualRanges(*nsRange, fullRange)) {
[result removeAttribute:NSAccessibilityMisspelledTextAttribute range:resultRange];
[result removeAttribute:NSAccessibilityMarkedMisspelledTextAttribute range:resultRange];
// FIXME: pull attributedStringSetSpelling off the main thread.
performFunctionOnMainThread([result = retainPtr(result), &resultRange] (AccessibilityObject* axObject) {
attributedStringSetSpelling(result.get(), axObject->node(), String { [result string] }, resultRange);
});
}

return result;
Expand Down
2 changes: 1 addition & 1 deletion Source/WebCore/accessibility/mac/AccessibilityObjectMac.mm
Expand Up @@ -663,7 +663,7 @@ static void attributedStringSetElement(NSMutableAttributedString *attrString, NS
[attrString addAttribute:attribute value:(__bridge id)axElement.get() range:range];
}

static void attributedStringSetSpelling(NSMutableAttributedString *attrString, Node* node, StringView text, const NSRange& range)
void attributedStringSetSpelling(NSMutableAttributedString *attrString, Node* node, StringView text, const NSRange& range)
{
ASSERT(node);

Expand Down
Expand Up @@ -51,6 +51,7 @@ class VisiblePosition;
bool attributedStringContainsRange(NSAttributedString *, const NSRange&);
void attributedStringSetNumber(NSMutableAttributedString *, NSString *, NSNumber *, const NSRange&);
void attributedStringSetFont(NSMutableAttributedString *, CTFontRef, const NSRange&);
void attributedStringSetSpelling(NSMutableAttributedString *, Node*, StringView, const NSRange&);
RetainPtr<NSAttributedString> attributedStringCreate(Node*, StringView, AXCoreObject::SpellCheck);
}

Expand Down

0 comments on commit 616bcfa

Please sign in to comment.