[iOS] ASSERTION FAILURE: !isMissingPostLayoutData in WebKit::EditorState::postLayoutD...
[WebKit-https.git] / Source / WebKit / WebProcess / WebPage / glib / WebPageGLib.cpp
1 /*
2  * Copyright (C) 2019 Igalia S.L.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23  * THE POSSIBILITY OF SUCH DAMAGE.
24  */
25
26 #include "config.h"
27 #include "WebPage.h"
28
29 #include "EditorState.h"
30 #include "InputMethodState.h"
31 #include "UserMessage.h"
32 #include "WebKitExtensionManager.h"
33 #include "WebKitUserMessage.h"
34 #include "WebKitWebExtension.h"
35 #include "WebKitWebPagePrivate.h"
36 #include "WebPageProxyMessages.h"
37 #include <WebCore/Editor.h>
38 #include <WebCore/Frame.h>
39 #include <WebCore/FrameView.h>
40 #include <WebCore/HTMLInputElement.h>
41 #include <WebCore/HTMLTextAreaElement.h>
42 #include <WebCore/TextIterator.h>
43 #include <WebCore/VisiblePosition.h>
44 #include <WebCore/VisibleUnits.h>
45
46 namespace WebKit {
47 using namespace WebCore;
48
49 void WebPage::sendMessageToWebExtensionWithReply(UserMessage&& message, CompletionHandler<void(UserMessage&&)>&& completionHandler)
50 {
51     auto* extension = WebKitExtensionManager::singleton().extension();
52     if (!extension) {
53         completionHandler(UserMessage(message.name, WEBKIT_USER_MESSAGE_UNHANDLED_MESSAGE));
54         return;
55     }
56
57     auto* page = webkit_web_extension_get_page(extension, m_identifier.toUInt64());
58     if (!page) {
59         completionHandler(UserMessage(message.name, WEBKIT_USER_MESSAGE_UNHANDLED_MESSAGE));
60         return;
61     }
62
63     webkitWebPageDidReceiveUserMessage(page, WTFMove(message), WTFMove(completionHandler));
64 }
65
66 void WebPage::sendMessageToWebExtension(UserMessage&& message)
67 {
68     sendMessageToWebExtensionWithReply(WTFMove(message), [](UserMessage&&) { });
69 }
70
71 void WebPage::getPlatformEditorState(Frame& frame, EditorState& result) const
72 {
73     if (result.isMissingPostLayoutData || !frame.view() || frame.view()->needsLayout())
74         return;
75
76     auto& postLayoutData = result.postLayoutData();
77     postLayoutData.caretRectAtStart = frame.selection().absoluteCaretBounds();
78
79     const VisibleSelection& selection = frame.selection().selection();
80     if (selection.isNone())
81         return;
82
83 #if PLATFORM(GTK)
84     const Editor& editor = frame.editor();
85     if (selection.isRange()) {
86         if (editor.selectionHasStyle(CSSPropertyFontWeight, "bold") == TrueTriState)
87             postLayoutData.typingAttributes |= AttributeBold;
88         if (editor.selectionHasStyle(CSSPropertyFontStyle, "italic") == TrueTriState)
89             postLayoutData.typingAttributes |= AttributeItalics;
90         if (editor.selectionHasStyle(CSSPropertyWebkitTextDecorationsInEffect, "underline") == TrueTriState)
91             postLayoutData.typingAttributes |= AttributeUnderline;
92         if (editor.selectionHasStyle(CSSPropertyWebkitTextDecorationsInEffect, "line-through") == TrueTriState)
93             postLayoutData.typingAttributes |= AttributeStrikeThrough;
94     } else if (selection.isCaret()) {
95         if (editor.selectionStartHasStyle(CSSPropertyFontWeight, "bold"))
96             postLayoutData.typingAttributes |= AttributeBold;
97         if (editor.selectionStartHasStyle(CSSPropertyFontStyle, "italic"))
98             postLayoutData.typingAttributes |= AttributeItalics;
99         if (editor.selectionStartHasStyle(CSSPropertyWebkitTextDecorationsInEffect, "underline"))
100             postLayoutData.typingAttributes |= AttributeUnderline;
101         if (editor.selectionStartHasStyle(CSSPropertyWebkitTextDecorationsInEffect, "line-through"))
102             postLayoutData.typingAttributes |= AttributeStrikeThrough;
103     }
104 #endif
105
106     if (selection.isContentEditable()) {
107         auto selectionStart = selection.visibleStart();
108         auto surroundingStart = startOfEditableContent(selectionStart);
109         auto surroundingEnd = endOfEditableContent(selectionStart);
110         auto surroundingRange = makeRange(surroundingStart, surroundingEnd);
111         auto compositionRange = frame.editor().compositionRange();
112         if (compositionRange && surroundingRange->contains(*compositionRange)) {
113             auto clonedRange = surroundingRange->cloneRange();
114             surroundingRange->setEnd(compositionRange->startPosition());
115             clonedRange->setStart(compositionRange->endPosition());
116             postLayoutData.surroundingContext = plainText(surroundingRange.get()) + plainText(clonedRange.ptr());
117             postLayoutData.surroundingContextCursorPosition = characterCount(*surroundingRange);
118             postLayoutData.surroundingContextSelectionPosition = postLayoutData.surroundingContextCursorPosition;
119         } else {
120             postLayoutData.surroundingContext = plainText(surroundingRange.get());
121             if (surroundingStart.isNull() || selectionStart.isNull())
122                 postLayoutData.surroundingContextCursorPosition = 0;
123             else
124                 postLayoutData.surroundingContextCursorPosition = characterCount(*makeRange(surroundingStart, selectionStart));
125             if (surroundingStart.isNull() || selection.visibleEnd().isNull())
126                 postLayoutData.surroundingContextSelectionPosition = 0;
127             else
128                 postLayoutData.surroundingContextSelectionPosition = characterCount(*makeRange(surroundingStart, selection.visibleEnd()));
129         }
130     }
131 }
132
133 static Optional<InputMethodState> inputMethodSateForElement(Element* element)
134 {
135     if (!element || !element->shouldUseInputMethod())
136         return WTF::nullopt;
137
138     InputMethodState state;
139     if (is<HTMLInputElement>(*element)) {
140         auto& inputElement = downcast<HTMLInputElement>(*element);
141         state.setPurposeForInputElement(inputElement);
142         state.addHintsForAutocapitalizeType(inputElement.autocapitalizeType());
143     } else if (is<HTMLTextAreaElement>(*element) || (element->hasEditableStyle() && is<HTMLElement>(*element))) {
144         auto& htmlElement = downcast<HTMLElement>(*element);
145         state.setPurposeOrHintForInputMode(htmlElement.canonicalInputMode());
146         state.addHintsForAutocapitalizeType(htmlElement.autocapitalizeType());
147     }
148
149     if (element->isSpellCheckingEnabled())
150         state.hints.add(InputMethodState::Hint::Spellcheck);
151
152     return state;
153 }
154
155 void WebPage::setInputMethodState(Element* element)
156 {
157     auto state = inputMethodSateForElement(element);
158     if (m_inputMethodState == state)
159         return;
160
161     m_inputMethodState = state;
162     send(Messages::WebPageProxy::SetInputMethodState(state));
163 }
164
165 } // namespace WebKit