rniwa@webkit.org | e9da7a8 | 2019-10-25 00:08:13 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2019 Apple Inc. All rights reserved. |
| 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 "TextManipulationController.h" |
| 28 | |
wenson_hsieh@apple.com | e2c4adf | 2020-04-27 21:12:52 +0000 | [diff] [blame] | 29 | #include "AccessibilityObject.h" |
rniwa@webkit.org | 7afdb0b | 2019-10-25 20:41:15 +0000 | [diff] [blame] | 30 | #include "CharacterData.h" |
rniwa@webkit.org | e9da7a8 | 2019-10-25 00:08:13 +0000 | [diff] [blame] | 31 | #include "Editing.h" |
rniwa@webkit.org | 7afdb0b | 2019-10-25 20:41:15 +0000 | [diff] [blame] | 32 | #include "ElementAncestorIterator.h" |
rniwa@webkit.org | c46d21d | 2019-12-16 22:36:55 +0000 | [diff] [blame] | 33 | #include "EventLoop.h" |
sihui_liu@apple.com | 5a2c1d3 | 2020-04-23 17:23:54 +0000 | [diff] [blame] | 34 | #include "HTMLBRElement.h" |
don.olmstead@sony.com | f5e4de7 | 2020-03-12 03:19:09 +0000 | [diff] [blame] | 35 | #include "HTMLElement.h" |
wenson_hsieh@apple.com | 5f24d54 | 2020-05-02 02:26:23 +0000 | [diff] [blame] | 36 | #include "HTMLInputElement.h" |
rniwa@webkit.org | 640b073 | 2020-03-08 03:52:06 +0000 | [diff] [blame] | 37 | #include "HTMLNames.h" |
sihui_liu@apple.com | 5a2c1d3 | 2020-04-23 17:23:54 +0000 | [diff] [blame] | 38 | #include "HTMLParserIdioms.h" |
wenson_hsieh@apple.com | f0ad7db | 2020-06-03 21:08:21 +0000 | [diff] [blame] | 39 | #include "InputTypeNames.h" |
wenson_hsieh@apple.com | f2cc5ed1 | 2020-06-25 20:37:34 +0000 | [diff] [blame^] | 40 | #include "NodeRenderStyle.h" |
rniwa@webkit.org | 900f560 | 2019-12-21 02:09:50 +0000 | [diff] [blame] | 41 | #include "NodeTraversal.h" |
| 42 | #include "PseudoElement.h" |
| 43 | #include "Range.h" |
rniwa@webkit.org | e9da7a8 | 2019-10-25 00:08:13 +0000 | [diff] [blame] | 44 | #include "ScriptDisallowedScope.h" |
rniwa@webkit.org | 900f560 | 2019-12-21 02:09:50 +0000 | [diff] [blame] | 45 | #include "Text.h" |
rniwa@webkit.org | e9da7a8 | 2019-10-25 00:08:13 +0000 | [diff] [blame] | 46 | #include "TextIterator.h" |
| 47 | #include "VisibleUnits.h" |
| 48 | |
| 49 | namespace WebCore { |
| 50 | |
rniwa@webkit.org | 7afdb0b | 2019-10-25 20:41:15 +0000 | [diff] [blame] | 51 | inline bool TextManipulationController::ExclusionRule::match(const Element& element) const |
| 52 | { |
| 53 | return switchOn(rule, [&element] (ElementRule rule) { |
| 54 | return rule.localName == element.localName(); |
| 55 | }, [&element] (AttributeRule rule) { |
| 56 | return equalIgnoringASCIICase(element.getAttribute(rule.name), rule.value); |
commit-queue@webkit.org | d873075 | 2019-12-04 18:36:15 +0000 | [diff] [blame] | 57 | }, [&element] (ClassRule rule) { |
| 58 | return element.hasClass() && element.classNames().contains(rule.className); |
rniwa@webkit.org | 7afdb0b | 2019-10-25 20:41:15 +0000 | [diff] [blame] | 59 | }); |
| 60 | } |
| 61 | |
| 62 | class ExclusionRuleMatcher { |
| 63 | public: |
| 64 | using ExclusionRule = TextManipulationController::ExclusionRule; |
| 65 | using Type = TextManipulationController::ExclusionRule::Type; |
| 66 | |
| 67 | ExclusionRuleMatcher(const Vector<ExclusionRule>& rules) |
| 68 | : m_rules(rules) |
| 69 | { } |
| 70 | |
| 71 | bool isExcluded(Node* node) |
| 72 | { |
| 73 | if (!node) |
| 74 | return false; |
| 75 | |
| 76 | RefPtr<Element> startingElement = is<Element>(*node) ? downcast<Element>(node) : node->parentElement(); |
| 77 | if (!startingElement) |
| 78 | return false; |
| 79 | |
| 80 | Type type = Type::Include; |
| 81 | RefPtr<Element> matchingElement; |
darin@apple.com | 25c0c84 | 2020-02-23 04:56:03 +0000 | [diff] [blame] | 82 | for (auto& element : lineageOfType<Element>(*startingElement)) { |
rniwa@webkit.org | 7afdb0b | 2019-10-25 20:41:15 +0000 | [diff] [blame] | 83 | if (auto typeOrNullopt = typeForElement(element)) { |
| 84 | type = *typeOrNullopt; |
| 85 | matchingElement = &element; |
| 86 | break; |
| 87 | } |
| 88 | } |
| 89 | |
darin@apple.com | 25c0c84 | 2020-02-23 04:56:03 +0000 | [diff] [blame] | 90 | for (auto& element : lineageOfType<Element>(*startingElement)) { |
rniwa@webkit.org | 7afdb0b | 2019-10-25 20:41:15 +0000 | [diff] [blame] | 91 | m_cache.set(element, type); |
| 92 | if (&element == matchingElement) |
| 93 | break; |
| 94 | } |
| 95 | |
| 96 | return type == Type::Exclude; |
| 97 | } |
| 98 | |
| 99 | Optional<Type> typeForElement(Element& element) |
| 100 | { |
| 101 | auto it = m_cache.find(element); |
| 102 | if (it != m_cache.end()) |
| 103 | return it->value; |
| 104 | |
| 105 | for (auto& rule : m_rules) { |
| 106 | if (rule.match(element)) |
| 107 | return rule.type; |
| 108 | } |
| 109 | |
| 110 | return WTF::nullopt; |
| 111 | } |
| 112 | |
| 113 | private: |
| 114 | const Vector<ExclusionRule>& m_rules; |
| 115 | HashMap<Ref<Element>, ExclusionRule::Type> m_cache; |
| 116 | }; |
| 117 | |
rniwa@webkit.org | e9da7a8 | 2019-10-25 00:08:13 +0000 | [diff] [blame] | 118 | TextManipulationController::TextManipulationController(Document& document) |
| 119 | : m_document(makeWeakPtr(document)) |
| 120 | { |
| 121 | } |
| 122 | |
rniwa@webkit.org | 7afdb0b | 2019-10-25 20:41:15 +0000 | [diff] [blame] | 123 | void TextManipulationController::startObservingParagraphs(ManipulationItemCallback&& callback, Vector<ExclusionRule>&& exclusionRules) |
rniwa@webkit.org | e9da7a8 | 2019-10-25 00:08:13 +0000 | [diff] [blame] | 124 | { |
| 125 | auto document = makeRefPtr(m_document.get()); |
| 126 | if (!document) |
| 127 | return; |
| 128 | |
| 129 | m_callback = WTFMove(callback); |
rniwa@webkit.org | 7afdb0b | 2019-10-25 20:41:15 +0000 | [diff] [blame] | 130 | m_exclusionRules = WTFMove(exclusionRules); |
rniwa@webkit.org | e9da7a8 | 2019-10-25 00:08:13 +0000 | [diff] [blame] | 131 | |
rniwa@webkit.org | 640b073 | 2020-03-08 03:52:06 +0000 | [diff] [blame] | 132 | observeParagraphs(firstPositionInNode(m_document.get()), lastPositionInNode(m_document.get())); |
rniwa@webkit.org | e44b9135 | 2020-03-04 05:22:14 +0000 | [diff] [blame] | 133 | flushPendingItemsForCallback(); |
rniwa@webkit.org | c46d21d | 2019-12-16 22:36:55 +0000 | [diff] [blame] | 134 | } |
| 135 | |
sihui_liu@apple.com | 0e39ec0 | 2020-06-09 06:54:20 +0000 | [diff] [blame] | 136 | static bool isInPrivateUseArea(UChar character) |
| 137 | { |
| 138 | return 0xE000 <= character && character <= 0xF8FF; |
| 139 | } |
| 140 | |
| 141 | static bool isTokenDelimiter(UChar character) |
| 142 | { |
| 143 | return isHTMLLineBreak(character) || isInPrivateUseArea(character); |
| 144 | } |
| 145 | |
rniwa@webkit.org | cffb789 | 2020-03-07 01:14:23 +0000 | [diff] [blame] | 146 | class ParagraphContentIterator { |
| 147 | public: |
| 148 | ParagraphContentIterator(const Position& start, const Position& end) |
wenson_hsieh@apple.com | d0ed2bd | 2020-04-24 00:07:25 +0000 | [diff] [blame] | 149 | : m_iterator({ *makeBoundaryPoint(start), *makeBoundaryPoint(end) }, TextIteratorIgnoresStyleVisibility) |
darin@apple.com | af46d1b | 2020-03-11 04:07:19 +0000 | [diff] [blame] | 150 | , m_iteratorNode(m_iterator.atEnd() ? nullptr : createLiveRange(m_iterator.range())->firstNode()) |
sihui_liu@apple.com | 12ab7a7 | 2020-06-01 23:54:34 +0000 | [diff] [blame] | 151 | , m_node(start.firstNode()) |
rniwa@webkit.org | cffb789 | 2020-03-07 01:14:23 +0000 | [diff] [blame] | 152 | , m_pastEndNode(end.firstNode()) |
| 153 | { |
sihui_liu@apple.com | 12ab7a7 | 2020-06-01 23:54:34 +0000 | [diff] [blame] | 154 | if (shouldAdvanceIteratorPastCurrentNode()) |
| 155 | advanceIteratorNodeAndUpdateText(); |
rniwa@webkit.org | cffb789 | 2020-03-07 01:14:23 +0000 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | void advance() |
| 159 | { |
sihui_liu@apple.com | 12ab7a7 | 2020-06-01 23:54:34 +0000 | [diff] [blame] | 160 | m_text = WTF::nullopt; |
| 161 | advanceNode(); |
rniwa@webkit.org | cffb789 | 2020-03-07 01:14:23 +0000 | [diff] [blame] | 162 | |
sihui_liu@apple.com | 12ab7a7 | 2020-06-01 23:54:34 +0000 | [diff] [blame] | 163 | if (shouldAdvanceIteratorPastCurrentNode()) |
| 164 | advanceIteratorNodeAndUpdateText(); |
rniwa@webkit.org | cffb789 | 2020-03-07 01:14:23 +0000 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | struct CurrentContent { |
| 168 | RefPtr<Node> node; |
sihui_liu@apple.com | 0e39ec0 | 2020-06-09 06:54:20 +0000 | [diff] [blame] | 169 | Vector<String> text; |
rniwa@webkit.org | cffb789 | 2020-03-07 01:14:23 +0000 | [diff] [blame] | 170 | bool isTextContent { false }; |
| 171 | bool isReplacedContent { false }; |
| 172 | }; |
| 173 | |
| 174 | CurrentContent currentContent() |
| 175 | { |
sihui_liu@apple.com | 0e39ec0 | 2020-06-09 06:54:20 +0000 | [diff] [blame] | 176 | CurrentContent content = { m_node.copyRef(), m_text ? m_text.value() : Vector<String> { }, !!m_text }; |
rniwa@webkit.org | cffb789 | 2020-03-07 01:14:23 +0000 | [diff] [blame] | 177 | if (content.node) { |
| 178 | if (auto* renderer = content.node->renderer()) { |
| 179 | if (renderer->isRenderReplaced()) { |
| 180 | content.isTextContent = false; |
| 181 | content.isReplacedContent = true; |
| 182 | } |
| 183 | } |
| 184 | } |
| 185 | return content; |
| 186 | } |
| 187 | |
sihui_liu@apple.com | 12ab7a7 | 2020-06-01 23:54:34 +0000 | [diff] [blame] | 188 | bool atEnd() const { return !m_text && m_iterator.atEnd() && m_node == m_pastEndNode; } |
rniwa@webkit.org | cffb789 | 2020-03-07 01:14:23 +0000 | [diff] [blame] | 189 | |
| 190 | private: |
sihui_liu@apple.com | 12ab7a7 | 2020-06-01 23:54:34 +0000 | [diff] [blame] | 191 | bool shouldAdvanceIteratorPastCurrentNode() const { return !m_iterator.atEnd() && m_iteratorNode == m_node; } |
| 192 | |
| 193 | void advanceNode() |
rniwa@webkit.org | cffb789 | 2020-03-07 01:14:23 +0000 | [diff] [blame] | 194 | { |
sihui_liu@apple.com | 12ab7a7 | 2020-06-01 23:54:34 +0000 | [diff] [blame] | 195 | if (m_node == m_pastEndNode) |
sihui_liu@apple.com | 5a2c1d3 | 2020-04-23 17:23:54 +0000 | [diff] [blame] | 196 | return; |
| 197 | |
sihui_liu@apple.com | 12ab7a7 | 2020-06-01 23:54:34 +0000 | [diff] [blame] | 198 | m_node = NodeTraversal::next(*m_node); |
| 199 | if (!m_node) |
| 200 | m_node = m_pastEndNode; |
| 201 | } |
| 202 | |
sihui_liu@apple.com | 0e39ec0 | 2020-06-09 06:54:20 +0000 | [diff] [blame] | 203 | void appendToText(Vector<String>& text, StringBuilder& stringBuilder) |
| 204 | { |
| 205 | if (!stringBuilder.isEmpty()) { |
| 206 | text.append(stringBuilder.toString()); |
| 207 | stringBuilder.clear(); |
| 208 | } |
| 209 | } |
| 210 | |
sihui_liu@apple.com | 12ab7a7 | 2020-06-01 23:54:34 +0000 | [diff] [blame] | 211 | void advanceIteratorNodeAndUpdateText() |
| 212 | { |
| 213 | ASSERT(shouldAdvanceIteratorPastCurrentNode()); |
| 214 | |
| 215 | StringBuilder stringBuilder; |
sihui_liu@apple.com | 0e39ec0 | 2020-06-09 06:54:20 +0000 | [diff] [blame] | 216 | Vector<String> text; |
sihui_liu@apple.com | 12ab7a7 | 2020-06-01 23:54:34 +0000 | [diff] [blame] | 217 | while (shouldAdvanceIteratorPastCurrentNode()) { |
sihui_liu@apple.com | 0e39ec0 | 2020-06-09 06:54:20 +0000 | [diff] [blame] | 218 | if (!m_iterator.node()) { |
| 219 | auto iteratorText = m_iterator.text(); |
| 220 | bool containsDelimiter = false; |
| 221 | for (unsigned index = 0; index < iteratorText.length() && !containsDelimiter; ++index) |
| 222 | containsDelimiter = isTokenDelimiter(iteratorText[index]); |
| 223 | |
| 224 | if (containsDelimiter) { |
| 225 | appendToText(text, stringBuilder); |
| 226 | text.append({ }); |
| 227 | } |
| 228 | } else |
| 229 | stringBuilder.append(m_iterator.text()); |
| 230 | |
sihui_liu@apple.com | 12ab7a7 | 2020-06-01 23:54:34 +0000 | [diff] [blame] | 231 | m_iterator.advance(); |
| 232 | m_iteratorNode = m_iterator.atEnd() ? nullptr : createLiveRange(m_iterator.range())->firstNode(); |
| 233 | } |
sihui_liu@apple.com | 0e39ec0 | 2020-06-09 06:54:20 +0000 | [diff] [blame] | 234 | appendToText(text, stringBuilder); |
| 235 | m_text = text; |
rniwa@webkit.org | cffb789 | 2020-03-07 01:14:23 +0000 | [diff] [blame] | 236 | } |
| 237 | |
| 238 | TextIterator m_iterator; |
| 239 | RefPtr<Node> m_iteratorNode; |
sihui_liu@apple.com | 12ab7a7 | 2020-06-01 23:54:34 +0000 | [diff] [blame] | 240 | RefPtr<Node> m_node; |
rniwa@webkit.org | cffb789 | 2020-03-07 01:14:23 +0000 | [diff] [blame] | 241 | RefPtr<Node> m_pastEndNode; |
sihui_liu@apple.com | 0e39ec0 | 2020-06-09 06:54:20 +0000 | [diff] [blame] | 242 | Optional<Vector<String>> m_text; |
rniwa@webkit.org | cffb789 | 2020-03-07 01:14:23 +0000 | [diff] [blame] | 243 | }; |
| 244 | |
wenson_hsieh@apple.com | f0ad7db | 2020-06-03 21:08:21 +0000 | [diff] [blame] | 245 | static bool shouldExtractValueForTextManipulation(const HTMLInputElement& input) |
| 246 | { |
aperez@igalia.com | cb5f4671 | 2020-06-05 15:47:41 +0000 | [diff] [blame] | 247 | if (input.isSearchField() || equalIgnoringASCIICase(input.attributeWithoutSynchronization(HTMLNames::typeAttr), InputTypeNames::text())) |
wenson_hsieh@apple.com | f0ad7db | 2020-06-03 21:08:21 +0000 | [diff] [blame] | 248 | return !input.lastChangeWasUserEdit(); |
| 249 | |
| 250 | return input.isTextButton(); |
| 251 | } |
| 252 | |
| 253 | static bool isAttributeForTextManipulation(const QualifiedName& nameToCheck) |
rniwa@webkit.org | c46d21d | 2019-12-16 22:36:55 +0000 | [diff] [blame] | 254 | { |
rniwa@webkit.org | 640b073 | 2020-03-08 03:52:06 +0000 | [diff] [blame] | 255 | using namespace HTMLNames; |
| 256 | static const QualifiedName* const attributeNames[] = { |
| 257 | &titleAttr.get(), |
| 258 | &altAttr.get(), |
| 259 | &placeholderAttr.get(), |
| 260 | &aria_labelAttr.get(), |
| 261 | &aria_placeholderAttr.get(), |
| 262 | &aria_roledescriptionAttr.get(), |
| 263 | &aria_valuetextAttr.get(), |
| 264 | }; |
| 265 | for (auto& entry : attributeNames) { |
| 266 | if (*entry == nameToCheck) |
| 267 | return true; |
| 268 | } |
| 269 | return false; |
| 270 | } |
| 271 | |
wenson_hsieh@apple.com | 2d16b75 | 2020-04-20 20:19:54 +0000 | [diff] [blame] | 272 | static bool canPerformTextManipulationByReplacingEntireTextContent(const Element& element) |
| 273 | { |
| 274 | return element.hasTagName(HTMLNames::titleTag) || element.hasTagName(HTMLNames::optionTag); |
| 275 | } |
| 276 | |
wenson_hsieh@apple.com | eeb352d | 2020-04-29 05:16:23 +0000 | [diff] [blame] | 277 | static Optional<TextManipulationController::ManipulationTokenInfo> tokenInfo(Node* node) |
| 278 | { |
| 279 | if (!node) |
| 280 | return WTF::nullopt; |
| 281 | |
| 282 | TextManipulationController::ManipulationTokenInfo result; |
| 283 | result.documentURL = node->document().url(); |
| 284 | if (auto element = is<Element>(node) ? makeRefPtr(downcast<Element>(*node)) : makeRefPtr(node->parentElement())) { |
| 285 | result.tagName = element->tagName(); |
| 286 | if (element->hasAttributeWithoutSynchronization(HTMLNames::roleAttr)) |
| 287 | result.roleAttribute = element->attributeWithoutSynchronization(HTMLNames::roleAttr); |
| 288 | } |
| 289 | return result; |
| 290 | } |
| 291 | |
sihui_liu@apple.com | 12ab7a7 | 2020-06-01 23:54:34 +0000 | [diff] [blame] | 292 | static bool isEnclosingItemBoundaryElement(const Element& element) |
| 293 | { |
| 294 | auto* renderer = element.renderer(); |
| 295 | if (!renderer) |
| 296 | return false; |
| 297 | |
| 298 | auto role = [](const Element& element) -> AccessibilityRole { |
| 299 | return AccessibilityObject::ariaRoleToWebCoreRole(element.attributeWithoutSynchronization(HTMLNames::roleAttr)); |
| 300 | }; |
| 301 | |
| 302 | if (element.hasTagName(HTMLNames::buttonTag) || role(element) == AccessibilityRole::Button) |
| 303 | return true; |
| 304 | |
| 305 | if (element.hasTagName(HTMLNames::liTag) || element.hasTagName(HTMLNames::aTag)) { |
| 306 | auto displayType = renderer->style().display(); |
| 307 | if (displayType == DisplayType::Block || displayType == DisplayType::InlineBlock) |
| 308 | return true; |
| 309 | |
| 310 | for (auto parent = makeRefPtr(element.parentElement()); parent; parent = parent->parentElement()) { |
| 311 | if (parent->hasTagName(HTMLNames::navTag) || role(*parent) == AccessibilityRole::LandmarkNavigation) |
| 312 | return true; |
| 313 | } |
| 314 | } |
| 315 | |
| 316 | return false; |
| 317 | } |
| 318 | |
sihui_liu@apple.com | 0e39ec0 | 2020-06-09 06:54:20 +0000 | [diff] [blame] | 319 | TextManipulationController::ManipulationUnit TextManipulationController::createUnit(const Vector<String>& text, Node& textNode) |
wenson_hsieh@apple.com | 600d5b6 | 2020-06-05 19:59:00 +0000 | [diff] [blame] | 320 | { |
sihui_liu@apple.com | 0e39ec0 | 2020-06-09 06:54:20 +0000 | [diff] [blame] | 321 | ManipulationUnit unit = { textNode, { } }; |
| 322 | for (auto& textEntry : text) { |
| 323 | if (!textEntry.isNull()) |
| 324 | parse(unit, textEntry, textNode); |
| 325 | else { |
| 326 | if (unit.tokens.isEmpty()) |
| 327 | unit.firstTokenContainsDelimiter = true; |
| 328 | unit.lastTokenContainsDelimiter = true; |
| 329 | } |
| 330 | } |
| 331 | return unit; |
wenson_hsieh@apple.com | 600d5b6 | 2020-06-05 19:59:00 +0000 | [diff] [blame] | 332 | } |
| 333 | |
wenson_hsieh@apple.com | f2cc5ed1 | 2020-06-25 20:37:34 +0000 | [diff] [blame^] | 334 | bool TextManipulationController::shouldExcludeNodeBasedOnStyle(const Node& node) |
| 335 | { |
| 336 | auto* style = node.renderStyle(); |
| 337 | if (!style) |
| 338 | return false; |
| 339 | |
| 340 | auto& font = style->fontCascade().primaryFont(); |
| 341 | auto familyName = font.platformData().familyName(); |
| 342 | if (familyName.isEmpty()) |
| 343 | return false; |
| 344 | |
| 345 | auto iter = m_cachedFontFamilyExclusionResults.find(familyName); |
| 346 | if (iter != m_cachedFontFamilyExclusionResults.end()) |
| 347 | return iter->value; |
| 348 | |
| 349 | // FIXME: We should reconsider whether a node should be excluded if the primary font |
| 350 | // used to render the node changes, since this "icon font" heuristic may return a |
| 351 | // different result. |
| 352 | bool result = font.isProbablyOnlyUsedToRenderIcons(); |
| 353 | m_cachedFontFamilyExclusionResults.set(familyName, result); |
| 354 | return result; |
| 355 | } |
| 356 | |
sihui_liu@apple.com | 0e39ec0 | 2020-06-09 06:54:20 +0000 | [diff] [blame] | 357 | void TextManipulationController::parse(ManipulationUnit& unit, const String& text, Node& textNode) |
wenson_hsieh@apple.com | 600d5b6 | 2020-06-05 19:59:00 +0000 | [diff] [blame] | 358 | { |
sihui_liu@apple.com | 12ab7a7 | 2020-06-01 23:54:34 +0000 | [diff] [blame] | 359 | ExclusionRuleMatcher exclusionRuleMatcher(m_exclusionRules); |
wenson_hsieh@apple.com | f2cc5ed1 | 2020-06-25 20:37:34 +0000 | [diff] [blame^] | 360 | bool isNodeExcluded = exclusionRuleMatcher.isExcluded(&textNode) || shouldExcludeNodeBasedOnStyle(textNode); |
sihui_liu@apple.com | 12ab7a7 | 2020-06-01 23:54:34 +0000 | [diff] [blame] | 361 | size_t positionOfLastNonHTMLSpace = WTF::notFound; |
| 362 | size_t startPositionOfCurrentToken = 0; |
sihui_liu@apple.com | 12ab7a7 | 2020-06-01 23:54:34 +0000 | [diff] [blame] | 363 | size_t index = 0; |
| 364 | for (; index < text.length(); ++index) { |
| 365 | auto character = text[index]; |
wenson_hsieh@apple.com | 600d5b6 | 2020-06-05 19:59:00 +0000 | [diff] [blame] | 366 | if (isTokenDelimiter(character)) { |
sihui_liu@apple.com | 12ab7a7 | 2020-06-01 23:54:34 +0000 | [diff] [blame] | 367 | if (positionOfLastNonHTMLSpace != WTF::notFound && startPositionOfCurrentToken <= positionOfLastNonHTMLSpace) { |
sihui_liu@apple.com | 0e39ec0 | 2020-06-09 06:54:20 +0000 | [diff] [blame] | 368 | auto stringForToken = text.substring(startPositionOfCurrentToken, positionOfLastNonHTMLSpace + 1 - startPositionOfCurrentToken); |
| 369 | unit.tokens.append(ManipulationToken { m_tokenIdentifier.generate(), stringForToken, tokenInfo(&textNode), isNodeExcluded }); |
sihui_liu@apple.com | 12ab7a7 | 2020-06-01 23:54:34 +0000 | [diff] [blame] | 370 | startPositionOfCurrentToken = positionOfLastNonHTMLSpace + 1; |
| 371 | } |
| 372 | |
wenson_hsieh@apple.com | 600d5b6 | 2020-06-05 19:59:00 +0000 | [diff] [blame] | 373 | while (index < text.length() && (isHTMLSpace(text[index]) || isInPrivateUseArea(text[index]))) |
sihui_liu@apple.com | 12ab7a7 | 2020-06-01 23:54:34 +0000 | [diff] [blame] | 374 | ++index; |
| 375 | |
| 376 | --index; |
| 377 | |
sihui_liu@apple.com | 0e39ec0 | 2020-06-09 06:54:20 +0000 | [diff] [blame] | 378 | auto stringForToken = text.substring(startPositionOfCurrentToken, index + 1 - startPositionOfCurrentToken); |
| 379 | if (unit.tokens.isEmpty() && !unit.firstTokenContainsDelimiter) |
| 380 | unit.firstTokenContainsDelimiter = true; |
| 381 | unit.tokens.append(ManipulationToken { m_tokenIdentifier.generate(), stringForToken, tokenInfo(&textNode), true }); |
sihui_liu@apple.com | 12ab7a7 | 2020-06-01 23:54:34 +0000 | [diff] [blame] | 382 | startPositionOfCurrentToken = index + 1; |
sihui_liu@apple.com | 0e39ec0 | 2020-06-09 06:54:20 +0000 | [diff] [blame] | 383 | unit.lastTokenContainsDelimiter = true; |
wenson_hsieh@apple.com | 600d5b6 | 2020-06-05 19:59:00 +0000 | [diff] [blame] | 384 | } else if (isNotHTMLSpace(character)) { |
sihui_liu@apple.com | 0e39ec0 | 2020-06-09 06:54:20 +0000 | [diff] [blame] | 385 | if (!isNodeExcluded) |
| 386 | unit.areAllTokensExcluded = false; |
wenson_hsieh@apple.com | 600d5b6 | 2020-06-05 19:59:00 +0000 | [diff] [blame] | 387 | positionOfLastNonHTMLSpace = index; |
sihui_liu@apple.com | 12ab7a7 | 2020-06-01 23:54:34 +0000 | [diff] [blame] | 388 | } |
| 389 | } |
| 390 | |
| 391 | if (startPositionOfCurrentToken < text.length()) { |
sihui_liu@apple.com | 0e39ec0 | 2020-06-09 06:54:20 +0000 | [diff] [blame] | 392 | auto stringForToken = text.substring(startPositionOfCurrentToken, index + 1 - startPositionOfCurrentToken); |
| 393 | unit.tokens.append(ManipulationToken { m_tokenIdentifier.generate(), stringForToken, tokenInfo(&textNode), isNodeExcluded }); |
| 394 | unit.lastTokenContainsDelimiter = false; |
sihui_liu@apple.com | 12ab7a7 | 2020-06-01 23:54:34 +0000 | [diff] [blame] | 395 | } |
sihui_liu@apple.com | 33d4522 | 2020-06-05 06:13:30 +0000 | [diff] [blame] | 396 | } |
| 397 | |
| 398 | void TextManipulationController::addItemIfPossible(Vector<ManipulationUnit>&& units) |
| 399 | { |
| 400 | if (units.isEmpty()) |
| 401 | return; |
| 402 | |
| 403 | size_t index = 0; |
| 404 | size_t end = units.size(); |
| 405 | while (index < units.size() && units[index].areAllTokensExcluded) |
| 406 | ++index; |
| 407 | |
| 408 | while (end > 0 && units[end - 1].areAllTokensExcluded) |
| 409 | --end; |
| 410 | |
| 411 | if (index == end) |
| 412 | return; |
| 413 | |
sihui_liu@apple.com | a642180 | 2020-06-09 07:11:12 +0000 | [diff] [blame] | 414 | ASSERT(end); |
| 415 | auto startPosition = firstPositionInOrBeforeNode(units[index].node.ptr()); |
| 416 | auto endPosition = positionAfterNode(units[end - 1].node.ptr()); |
sihui_liu@apple.com | 33d4522 | 2020-06-05 06:13:30 +0000 | [diff] [blame] | 417 | Vector<ManipulationToken> tokens; |
| 418 | for (; index < end; ++index) |
| 419 | tokens.appendVector(WTFMove(units[index].tokens)); |
| 420 | |
| 421 | addItem(ManipulationItemData { startPosition, endPosition, nullptr, nullQName(), WTFMove(tokens) }); |
sihui_liu@apple.com | 12ab7a7 | 2020-06-01 23:54:34 +0000 | [diff] [blame] | 422 | } |
| 423 | |
rniwa@webkit.org | 640b073 | 2020-03-08 03:52:06 +0000 | [diff] [blame] | 424 | void TextManipulationController::observeParagraphs(const Position& start, const Position& end) |
| 425 | { |
wenson_hsieh@apple.com | c626f5c | 2020-04-08 23:59:31 +0000 | [diff] [blame] | 426 | if (start.isNull() || end.isNull()) |
| 427 | return; |
| 428 | |
rniwa@webkit.org | 640b073 | 2020-03-08 03:52:06 +0000 | [diff] [blame] | 429 | auto document = makeRefPtr(start.document()); |
rniwa@webkit.org | c46d21d | 2019-12-16 22:36:55 +0000 | [diff] [blame] | 430 | ASSERT(document); |
sihui_liu@apple.com | 12ab7a7 | 2020-06-01 23:54:34 +0000 | [diff] [blame] | 431 | // TextIterator's constructor may have updated the layout and executed arbitrary scripts. |
rniwa@webkit.org | 640b073 | 2020-03-08 03:52:06 +0000 | [diff] [blame] | 432 | if (document != start.document() || document != end.document()) |
sihui_liu@apple.com | 12ab7a7 | 2020-06-01 23:54:34 +0000 | [diff] [blame] | 433 | return; |
rniwa@webkit.org | e9da7a8 | 2019-10-25 00:08:13 +0000 | [diff] [blame] | 434 | |
sihui_liu@apple.com | 33d4522 | 2020-06-05 06:13:30 +0000 | [diff] [blame] | 435 | Vector<ManipulationUnit> unitsInCurrentParagraph; |
wenson_hsieh@apple.com | 5f606e4 | 2020-06-16 18:11:23 +0000 | [diff] [blame] | 436 | Vector<Ref<Element>> enclosingItemBoundaryElements; |
sihui_liu@apple.com | 33d4522 | 2020-06-05 06:13:30 +0000 | [diff] [blame] | 437 | ParagraphContentIterator iterator { start, end }; |
rniwa@webkit.org | cffb789 | 2020-03-07 01:14:23 +0000 | [diff] [blame] | 438 | for (; !iterator.atEnd(); iterator.advance()) { |
| 439 | auto content = iterator.currentContent(); |
sihui_liu@apple.com | 12ab7a7 | 2020-06-01 23:54:34 +0000 | [diff] [blame] | 440 | auto* contentNode = content.node.get(); |
| 441 | ASSERT(contentNode); |
wenson_hsieh@apple.com | f9a7233 | 2020-04-24 23:18:05 +0000 | [diff] [blame] | 442 | |
wenson_hsieh@apple.com | 5f606e4 | 2020-06-16 18:11:23 +0000 | [diff] [blame] | 443 | while (!enclosingItemBoundaryElements.isEmpty() && !enclosingItemBoundaryElements.last()->contains(contentNode)) { |
sihui_liu@apple.com | 33d4522 | 2020-06-05 06:13:30 +0000 | [diff] [blame] | 444 | addItemIfPossible(std::exchange(unitsInCurrentParagraph, { })); |
wenson_hsieh@apple.com | 5f606e4 | 2020-06-16 18:11:23 +0000 | [diff] [blame] | 445 | enclosingItemBoundaryElements.removeLast(); |
sihui_liu@apple.com | 12ab7a7 | 2020-06-01 23:54:34 +0000 | [diff] [blame] | 446 | } |
rniwa@webkit.org | cffb789 | 2020-03-07 01:14:23 +0000 | [diff] [blame] | 447 | |
sihui_liu@apple.com | 12ab7a7 | 2020-06-01 23:54:34 +0000 | [diff] [blame] | 448 | if (RefPtr<Element> currentElementAncestor = is<Element>(*contentNode) ? downcast<Element>(contentNode) : contentNode->parentOrShadowHostElement()) { |
sihui_liu@apple.com | 2016ded | 2020-06-15 17:51:38 +0000 | [diff] [blame] | 449 | if (m_manipulatedElements.contains(*currentElementAncestor)) |
sihui_liu@apple.com | 12ab7a7 | 2020-06-01 23:54:34 +0000 | [diff] [blame] | 450 | return; |
| 451 | } |
| 452 | |
| 453 | if (is<Element>(*contentNode)) { |
| 454 | auto& currentElement = downcast<Element>(*contentNode); |
| 455 | if (!content.isTextContent && canPerformTextManipulationByReplacingEntireTextContent(currentElement)) |
| 456 | addItem(ManipulationItemData { Position(), Position(), makeWeakPtr(currentElement), nullQName(), { ManipulationToken { m_tokenIdentifier.generate(), currentElement.textContent(), tokenInfo(¤tElement) } } }); |
| 457 | |
| 458 | if (currentElement.hasAttributes()) { |
| 459 | for (auto& attribute : currentElement.attributesIterator()) { |
wenson_hsieh@apple.com | f0ad7db | 2020-06-03 21:08:21 +0000 | [diff] [blame] | 460 | if (isAttributeForTextManipulation(attribute.name())) |
sihui_liu@apple.com | 12ab7a7 | 2020-06-01 23:54:34 +0000 | [diff] [blame] | 461 | addItem(ManipulationItemData { Position(), Position(), makeWeakPtr(currentElement), attribute.name(), { ManipulationToken { m_tokenIdentifier.generate(), attribute.value(), tokenInfo(¤tElement) } } }); |
rniwa@webkit.org | 640b073 | 2020-03-08 03:52:06 +0000 | [diff] [blame] | 462 | } |
rniwa@webkit.org | 640b073 | 2020-03-08 03:52:06 +0000 | [diff] [blame] | 463 | } |
wenson_hsieh@apple.com | f0ad7db | 2020-06-03 21:08:21 +0000 | [diff] [blame] | 464 | |
| 465 | if (is<HTMLInputElement>(currentElement)) { |
| 466 | auto& input = downcast<HTMLInputElement>(currentElement); |
| 467 | if (shouldExtractValueForTextManipulation(input)) |
aperez@igalia.com | cb5f4671 | 2020-06-05 15:47:41 +0000 | [diff] [blame] | 468 | addItem(ManipulationItemData { { }, { }, makeWeakPtr(currentElement), HTMLNames::valueAttr, { ManipulationToken { m_tokenIdentifier.generate(), input.value(), tokenInfo(¤tElement) } } }); |
wenson_hsieh@apple.com | f0ad7db | 2020-06-03 21:08:21 +0000 | [diff] [blame] | 469 | } |
| 470 | |
wenson_hsieh@apple.com | 5f606e4 | 2020-06-16 18:11:23 +0000 | [diff] [blame] | 471 | if (isEnclosingItemBoundaryElement(currentElement)) { |
| 472 | addItemIfPossible(std::exchange(unitsInCurrentParagraph, { })); |
| 473 | enclosingItemBoundaryElements.append(currentElement); |
| 474 | } |
rniwa@webkit.org | 9538698 | 2020-02-28 03:37:22 +0000 | [diff] [blame] | 475 | } |
| 476 | |
rniwa@webkit.org | cffb789 | 2020-03-07 01:14:23 +0000 | [diff] [blame] | 477 | if (content.isReplacedContent) { |
sihui_liu@apple.com | 33d4522 | 2020-06-05 06:13:30 +0000 | [diff] [blame] | 478 | if (!unitsInCurrentParagraph.isEmpty()) |
sihui_liu@apple.com | 0e39ec0 | 2020-06-09 06:54:20 +0000 | [diff] [blame] | 479 | unitsInCurrentParagraph.append(ManipulationUnit { *contentNode, { ManipulationToken { m_tokenIdentifier.generate(), "[]", tokenInfo(content.node.get()), true } } }); |
rniwa@webkit.org | cffb789 | 2020-03-07 01:14:23 +0000 | [diff] [blame] | 480 | continue; |
| 481 | } |
| 482 | |
| 483 | if (!content.isTextContent) |
| 484 | continue; |
| 485 | |
sihui_liu@apple.com | 0e39ec0 | 2020-06-09 06:54:20 +0000 | [diff] [blame] | 486 | auto currentUnit = createUnit(content.text, *contentNode); |
| 487 | if (currentUnit.firstTokenContainsDelimiter) |
sihui_liu@apple.com | 33d4522 | 2020-06-05 06:13:30 +0000 | [diff] [blame] | 488 | addItemIfPossible(std::exchange(unitsInCurrentParagraph, { })); |
sihui_liu@apple.com | 5a2c1d3 | 2020-04-23 17:23:54 +0000 | [diff] [blame] | 489 | |
sihui_liu@apple.com | 0e39ec0 | 2020-06-09 06:54:20 +0000 | [diff] [blame] | 490 | if (unitsInCurrentParagraph.isEmpty() && currentUnit.areAllTokensExcluded) |
| 491 | continue; |
rniwa@webkit.org | e9da7a8 | 2019-10-25 00:08:13 +0000 | [diff] [blame] | 492 | |
sihui_liu@apple.com | 0e39ec0 | 2020-06-09 06:54:20 +0000 | [diff] [blame] | 493 | bool currentUnitEndsWithDelimiter = currentUnit.lastTokenContainsDelimiter; |
| 494 | unitsInCurrentParagraph.append(WTFMove(currentUnit)); |
sihui_liu@apple.com | 12ab7a7 | 2020-06-01 23:54:34 +0000 | [diff] [blame] | 495 | |
sihui_liu@apple.com | 0e39ec0 | 2020-06-09 06:54:20 +0000 | [diff] [blame] | 496 | if (currentUnitEndsWithDelimiter) |
sihui_liu@apple.com | 33d4522 | 2020-06-05 06:13:30 +0000 | [diff] [blame] | 497 | addItemIfPossible(std::exchange(unitsInCurrentParagraph, { })); |
rniwa@webkit.org | e9da7a8 | 2019-10-25 00:08:13 +0000 | [diff] [blame] | 498 | } |
| 499 | |
sihui_liu@apple.com | 33d4522 | 2020-06-05 06:13:30 +0000 | [diff] [blame] | 500 | addItemIfPossible(std::exchange(unitsInCurrentParagraph, { })); |
rniwa@webkit.org | e9da7a8 | 2019-10-25 00:08:13 +0000 | [diff] [blame] | 501 | } |
| 502 | |
rniwa@webkit.org | c46d21d | 2019-12-16 22:36:55 +0000 | [diff] [blame] | 503 | void TextManipulationController::didCreateRendererForElement(Element& element) |
| 504 | { |
sihui_liu@apple.com | 2016ded | 2020-06-15 17:51:38 +0000 | [diff] [blame] | 505 | if (m_manipulatedElements.contains(element)) |
wenson_hsieh@apple.com | 54632b7 | 2020-01-10 00:40:14 +0000 | [diff] [blame] | 506 | return; |
| 507 | |
rniwa@webkit.org | 9538698 | 2020-02-28 03:37:22 +0000 | [diff] [blame] | 508 | if (m_elementsWithNewRenderer.computesEmpty()) |
wenson_hsieh@apple.com | d0ed2bd | 2020-04-24 00:07:25 +0000 | [diff] [blame] | 509 | scheduleObservationUpdate(); |
rniwa@webkit.org | 900f560 | 2019-12-21 02:09:50 +0000 | [diff] [blame] | 510 | |
| 511 | if (is<PseudoElement>(element)) { |
| 512 | if (auto* host = downcast<PseudoElement>(element).hostElement()) |
rniwa@webkit.org | 9538698 | 2020-02-28 03:37:22 +0000 | [diff] [blame] | 513 | m_elementsWithNewRenderer.add(*host); |
rniwa@webkit.org | 900f560 | 2019-12-21 02:09:50 +0000 | [diff] [blame] | 514 | } else |
rniwa@webkit.org | 9538698 | 2020-02-28 03:37:22 +0000 | [diff] [blame] | 515 | m_elementsWithNewRenderer.add(element); |
rniwa@webkit.org | c46d21d | 2019-12-16 22:36:55 +0000 | [diff] [blame] | 516 | } |
| 517 | |
| 518 | using PositionTuple = std::tuple<RefPtr<Node>, unsigned, unsigned>; |
| 519 | static const PositionTuple makePositionTuple(const Position& position) |
| 520 | { |
aperez@igalia.com | cb5f4671 | 2020-06-05 15:47:41 +0000 | [diff] [blame] | 521 | return { position.anchorNode(), static_cast<unsigned>(position.anchorType()), position.anchorType() == Position::PositionIsOffsetInAnchor ? position.offsetInContainerNode() : 0 }; |
rniwa@webkit.org | c46d21d | 2019-12-16 22:36:55 +0000 | [diff] [blame] | 522 | } |
| 523 | |
sihui_liu@apple.com | 8f34760 | 2020-06-11 00:28:06 +0000 | [diff] [blame] | 524 | static const std::pair<PositionTuple, PositionTuple> makeHashablePositionRange(const Position& start, const Position& end) |
rniwa@webkit.org | c46d21d | 2019-12-16 22:36:55 +0000 | [diff] [blame] | 525 | { |
sihui_liu@apple.com | 8f34760 | 2020-06-11 00:28:06 +0000 | [diff] [blame] | 526 | return { makePositionTuple(start), makePositionTuple(end) }; |
rniwa@webkit.org | c46d21d | 2019-12-16 22:36:55 +0000 | [diff] [blame] | 527 | } |
| 528 | |
wenson_hsieh@apple.com | d0ed2bd | 2020-04-24 00:07:25 +0000 | [diff] [blame] | 529 | void TextManipulationController::scheduleObservationUpdate() |
rniwa@webkit.org | c46d21d | 2019-12-16 22:36:55 +0000 | [diff] [blame] | 530 | { |
| 531 | if (!m_document) |
| 532 | return; |
| 533 | |
| 534 | m_document->eventLoop().queueTask(TaskSource::InternalAsyncTask, [weakThis = makeWeakPtr(*this)] { |
| 535 | auto* controller = weakThis.get(); |
| 536 | if (!controller) |
| 537 | return; |
| 538 | |
| 539 | HashSet<Ref<Element>> mutatedElements; |
rniwa@webkit.org | 9538698 | 2020-02-28 03:37:22 +0000 | [diff] [blame] | 540 | for (auto& weakElement : controller->m_elementsWithNewRenderer) |
rniwa@webkit.org | c46d21d | 2019-12-16 22:36:55 +0000 | [diff] [blame] | 541 | mutatedElements.add(weakElement); |
rniwa@webkit.org | 9538698 | 2020-02-28 03:37:22 +0000 | [diff] [blame] | 542 | controller->m_elementsWithNewRenderer.clear(); |
rniwa@webkit.org | c46d21d | 2019-12-16 22:36:55 +0000 | [diff] [blame] | 543 | |
| 544 | HashSet<Ref<Element>> filteredElements; |
| 545 | for (auto& element : mutatedElements) { |
| 546 | auto* parentElement = element->parentElement(); |
| 547 | if (!parentElement || !mutatedElements.contains(parentElement)) |
| 548 | filteredElements.add(element.copyRef()); |
| 549 | } |
| 550 | mutatedElements.clear(); |
| 551 | |
| 552 | HashSet<std::pair<PositionTuple, PositionTuple>> paragraphSets; |
| 553 | for (auto& element : filteredElements) { |
sihui_liu@apple.com | 8f34760 | 2020-06-11 00:28:06 +0000 | [diff] [blame] | 554 | auto start = firstPositionInOrBeforeNode(element.ptr()); |
| 555 | auto end = lastPositionInOrAfterNode(element.ptr()); |
rniwa@webkit.org | c46d21d | 2019-12-16 22:36:55 +0000 | [diff] [blame] | 556 | |
wenson_hsieh@apple.com | 6b6c8fb | 2020-04-09 20:16:38 +0000 | [diff] [blame] | 557 | if (start.isNull() || end.isNull()) |
| 558 | continue; |
| 559 | |
rniwa@webkit.org | c46d21d | 2019-12-16 22:36:55 +0000 | [diff] [blame] | 560 | auto key = makeHashablePositionRange(start, end); |
| 561 | if (!paragraphSets.add(key).isNewEntry) |
| 562 | continue; |
| 563 | |
| 564 | auto* controller = weakThis.get(); |
| 565 | if (!controller) |
sihui_liu@apple.com | 8f34760 | 2020-06-11 00:28:06 +0000 | [diff] [blame] | 566 | return; |
rniwa@webkit.org | c46d21d | 2019-12-16 22:36:55 +0000 | [diff] [blame] | 567 | |
sihui_liu@apple.com | 8f34760 | 2020-06-11 00:28:06 +0000 | [diff] [blame] | 568 | controller->observeParagraphs(start, end); |
rniwa@webkit.org | c46d21d | 2019-12-16 22:36:55 +0000 | [diff] [blame] | 569 | } |
rniwa@webkit.org | e44b9135 | 2020-03-04 05:22:14 +0000 | [diff] [blame] | 570 | controller->flushPendingItemsForCallback(); |
rniwa@webkit.org | c46d21d | 2019-12-16 22:36:55 +0000 | [diff] [blame] | 571 | }); |
| 572 | } |
| 573 | |
rniwa@webkit.org | 640b073 | 2020-03-08 03:52:06 +0000 | [diff] [blame] | 574 | void TextManipulationController::addItem(ManipulationItemData&& itemData) |
rniwa@webkit.org | e9da7a8 | 2019-10-25 00:08:13 +0000 | [diff] [blame] | 575 | { |
rniwa@webkit.org | e44b9135 | 2020-03-04 05:22:14 +0000 | [diff] [blame] | 576 | const unsigned itemCallbackBatchingSize = 128; |
| 577 | |
rniwa@webkit.org | e9da7a8 | 2019-10-25 00:08:13 +0000 | [diff] [blame] | 578 | ASSERT(m_document); |
sihui_liu@apple.com | 12ab7a7 | 2020-06-01 23:54:34 +0000 | [diff] [blame] | 579 | ASSERT(!itemData.tokens.isEmpty()); |
rniwa@webkit.org | e44b9135 | 2020-03-04 05:22:14 +0000 | [diff] [blame] | 580 | auto newID = m_itemIdentifier.generate(); |
| 581 | m_pendingItemsForCallback.append(ManipulationItem { |
| 582 | newID, |
rniwa@webkit.org | 640b073 | 2020-03-08 03:52:06 +0000 | [diff] [blame] | 583 | itemData.tokens.map([](auto& token) { return token; }) |
rniwa@webkit.org | e44b9135 | 2020-03-04 05:22:14 +0000 | [diff] [blame] | 584 | }); |
rniwa@webkit.org | 640b073 | 2020-03-08 03:52:06 +0000 | [diff] [blame] | 585 | m_items.add(newID, WTFMove(itemData)); |
rniwa@webkit.org | e44b9135 | 2020-03-04 05:22:14 +0000 | [diff] [blame] | 586 | |
| 587 | if (m_pendingItemsForCallback.size() >= itemCallbackBatchingSize) |
| 588 | flushPendingItemsForCallback(); |
rniwa@webkit.org | e9da7a8 | 2019-10-25 00:08:13 +0000 | [diff] [blame] | 589 | } |
| 590 | |
rniwa@webkit.org | e44b9135 | 2020-03-04 05:22:14 +0000 | [diff] [blame] | 591 | void TextManipulationController::flushPendingItemsForCallback() |
rniwa@webkit.org | e9da7a8 | 2019-10-25 00:08:13 +0000 | [diff] [blame] | 592 | { |
rniwa@webkit.org | e44b9135 | 2020-03-04 05:22:14 +0000 | [diff] [blame] | 593 | m_callback(*m_document, m_pendingItemsForCallback); |
| 594 | m_pendingItemsForCallback.clear(); |
| 595 | } |
rniwa@webkit.org | e9da7a8 | 2019-10-25 00:08:13 +0000 | [diff] [blame] | 596 | |
rniwa@webkit.org | e44b9135 | 2020-03-04 05:22:14 +0000 | [diff] [blame] | 597 | auto TextManipulationController::completeManipulation(const Vector<WebCore::TextManipulationController::ManipulationItem>& completionItems) -> Vector<ManipulationFailure> |
| 598 | { |
| 599 | Vector<ManipulationFailure> failures; |
| 600 | for (unsigned i = 0; i < completionItems.size(); ++i) { |
| 601 | auto& itemToComplete = completionItems[i]; |
| 602 | auto identifier = itemToComplete.identifier; |
| 603 | if (!identifier) { |
| 604 | failures.append(ManipulationFailure { identifier, i, ManipulationFailureType::InvalidItem }); |
| 605 | continue; |
| 606 | } |
rniwa@webkit.org | e9da7a8 | 2019-10-25 00:08:13 +0000 | [diff] [blame] | 607 | |
rniwa@webkit.org | e44b9135 | 2020-03-04 05:22:14 +0000 | [diff] [blame] | 608 | auto itemDataIterator = m_items.find(identifier); |
| 609 | if (itemDataIterator == m_items.end()) { |
| 610 | failures.append(ManipulationFailure { identifier, i, ManipulationFailureType::InvalidItem }); |
| 611 | continue; |
| 612 | } |
rniwa@webkit.org | e9da7a8 | 2019-10-25 00:08:13 +0000 | [diff] [blame] | 613 | |
rniwa@webkit.org | e44b9135 | 2020-03-04 05:22:14 +0000 | [diff] [blame] | 614 | ManipulationItemData itemData; |
| 615 | std::exchange(itemData, itemDataIterator->value); |
| 616 | m_items.remove(itemDataIterator); |
| 617 | |
| 618 | auto failureOrNullopt = replace(itemData, itemToComplete.tokens); |
| 619 | if (failureOrNullopt) |
| 620 | failures.append(ManipulationFailure { identifier, i, *failureOrNullopt }); |
| 621 | } |
| 622 | return failures; |
rniwa@webkit.org | e9da7a8 | 2019-10-25 00:08:13 +0000 | [diff] [blame] | 623 | } |
| 624 | |
rniwa@webkit.org | 900f560 | 2019-12-21 02:09:50 +0000 | [diff] [blame] | 625 | struct TokenExchangeData { |
| 626 | RefPtr<Node> node; |
| 627 | String originalContent; |
| 628 | bool isExcluded { false }; |
| 629 | bool isConsumed { false }; |
| 630 | }; |
| 631 | |
| 632 | struct ReplacementData { |
| 633 | Ref<Node> originalNode; |
rniwa@webkit.org | 7afdb0b | 2019-10-25 20:41:15 +0000 | [diff] [blame] | 634 | String newData; |
| 635 | }; |
| 636 | |
sihui_liu@apple.com | 12ab7a7 | 2020-06-01 23:54:34 +0000 | [diff] [blame] | 637 | Vector<Ref<Node>> TextManipulationController::getPath(Node* ancestor, Node* node) |
| 638 | { |
| 639 | Vector<Ref<Node>> path; |
| 640 | RefPtr<ContainerNode> containerNode = is<ContainerNode>(*node) ? &downcast<ContainerNode>(*node) : node->parentNode(); |
| 641 | for (; containerNode && containerNode != ancestor; containerNode = containerNode->parentNode()) |
| 642 | path.append(*containerNode); |
| 643 | path.reverse(); |
| 644 | return path; |
| 645 | } |
| 646 | |
sihui_liu@apple.com | 2016ded | 2020-06-15 17:51:38 +0000 | [diff] [blame] | 647 | void TextManipulationController::updateInsertions(Vector<NodeEntry>& lastTopDownPath, const Vector<Ref<Node>>& currentTopDownPath, Node* currentNode, HashSet<Ref<Node>>& insertedNodes, Vector<NodeInsertion>& insertions, IsNodeManipulated isNodeManipulated) |
sihui_liu@apple.com | 12ab7a7 | 2020-06-01 23:54:34 +0000 | [diff] [blame] | 648 | { |
wenson_hsieh@apple.com | e0dde55 | 2020-06-17 01:34:01 +0000 | [diff] [blame] | 649 | size_t i = 0; |
sihui_liu@apple.com | 12ab7a7 | 2020-06-01 23:54:34 +0000 | [diff] [blame] | 650 | while (i < lastTopDownPath.size() && i < currentTopDownPath.size() && lastTopDownPath[i].first.ptr() == currentTopDownPath[i].ptr()) |
| 651 | ++i; |
| 652 | |
| 653 | if (i != lastTopDownPath.size() || i != currentTopDownPath.size()) { |
| 654 | if (i < lastTopDownPath.size()) |
| 655 | lastTopDownPath.shrink(i); |
| 656 | |
| 657 | for (;i < currentTopDownPath.size(); ++i) { |
| 658 | Ref<Node> node = currentTopDownPath[i]; |
| 659 | if (!insertedNodes.add(node.copyRef()).isNewEntry) { |
| 660 | auto clonedNode = node->cloneNodeInternal(node->document(), Node::CloningOperation::OnlySelf); |
| 661 | if (auto* data = node->eventTargetData()) |
| 662 | data->eventListenerMap.copyEventListenersNotCreatedFromMarkupToTarget(clonedNode.ptr()); |
| 663 | node = WTFMove(clonedNode); |
| 664 | } |
| 665 | insertions.append(NodeInsertion { lastTopDownPath.size() ? lastTopDownPath.last().second.ptr() : nullptr, node.copyRef() }); |
| 666 | lastTopDownPath.append({ currentTopDownPath[i].copyRef(), WTFMove(node) }); |
| 667 | } |
| 668 | } |
| 669 | |
| 670 | if (currentNode) |
sihui_liu@apple.com | 2016ded | 2020-06-15 17:51:38 +0000 | [diff] [blame] | 671 | insertions.append(NodeInsertion { lastTopDownPath.size() ? lastTopDownPath.last().second.ptr() : nullptr, *currentNode, isNodeManipulated }); |
sihui_liu@apple.com | 12ab7a7 | 2020-06-01 23:54:34 +0000 | [diff] [blame] | 672 | } |
rniwa@webkit.org | 900f560 | 2019-12-21 02:09:50 +0000 | [diff] [blame] | 673 | |
rniwa@webkit.org | e44b9135 | 2020-03-04 05:22:14 +0000 | [diff] [blame] | 674 | auto TextManipulationController::replace(const ManipulationItemData& item, const Vector<ManipulationToken>& replacementTokens) -> Optional<ManipulationFailureType> |
rniwa@webkit.org | e9da7a8 | 2019-10-25 00:08:13 +0000 | [diff] [blame] | 675 | { |
rniwa@webkit.org | 900f560 | 2019-12-21 02:09:50 +0000 | [diff] [blame] | 676 | if (item.start.isOrphan() || item.end.isOrphan()) |
rniwa@webkit.org | e44b9135 | 2020-03-04 05:22:14 +0000 | [diff] [blame] | 677 | return ManipulationFailureType::ContentChanged; |
rniwa@webkit.org | 900f560 | 2019-12-21 02:09:50 +0000 | [diff] [blame] | 678 | |
darin@apple.com | d2f1a00 | 2020-03-16 23:02:10 +0000 | [diff] [blame] | 679 | if (item.start.isNull() || item.end.isNull()) { |
rniwa@webkit.org | 640b073 | 2020-03-08 03:52:06 +0000 | [diff] [blame] | 680 | RELEASE_ASSERT(item.tokens.size() == 1); |
| 681 | auto element = makeRefPtr(item.element.get()); |
| 682 | if (!element) |
| 683 | return ManipulationFailureType::ContentChanged; |
sihui_liu@apple.com | 33fbdee | 2020-06-03 19:04:28 +0000 | [diff] [blame] | 684 | if (replacementTokens.size() > 1 && !canPerformTextManipulationByReplacingEntireTextContent(*element) && item.attributeName == nullQName()) |
rniwa@webkit.org | 640b073 | 2020-03-08 03:52:06 +0000 | [diff] [blame] | 685 | return ManipulationFailureType::InvalidToken; |
wenson_hsieh@apple.com | 2d16b75 | 2020-04-20 20:19:54 +0000 | [diff] [blame] | 686 | auto expectedTokenIdentifier = item.tokens[0].identifier; |
| 687 | StringBuilder newValue; |
| 688 | for (size_t i = 0; i < replacementTokens.size(); ++i) { |
| 689 | if (replacementTokens[i].identifier != expectedTokenIdentifier) |
rniwa@webkit.org | 640b073 | 2020-03-08 03:52:06 +0000 | [diff] [blame] | 690 | return ManipulationFailureType::InvalidToken; |
wenson_hsieh@apple.com | 2d16b75 | 2020-04-20 20:19:54 +0000 | [diff] [blame] | 691 | if (i) |
| 692 | newValue.append(' '); |
| 693 | newValue.append(replacementTokens[i].content); |
rniwa@webkit.org | 640b073 | 2020-03-08 03:52:06 +0000 | [diff] [blame] | 694 | } |
| 695 | if (item.attributeName == nullQName()) |
wenson_hsieh@apple.com | 2d16b75 | 2020-04-20 20:19:54 +0000 | [diff] [blame] | 696 | element->setTextContent(newValue.toString()); |
aperez@igalia.com | cb5f4671 | 2020-06-05 15:47:41 +0000 | [diff] [blame] | 697 | else if (item.attributeName == HTMLNames::valueAttr && is<HTMLInputElement>(*element)) |
wenson_hsieh@apple.com | f0ad7db | 2020-06-03 21:08:21 +0000 | [diff] [blame] | 698 | downcast<HTMLInputElement>(*element).setValue(newValue.toString()); |
rniwa@webkit.org | 640b073 | 2020-03-08 03:52:06 +0000 | [diff] [blame] | 699 | else |
wenson_hsieh@apple.com | 2d16b75 | 2020-04-20 20:19:54 +0000 | [diff] [blame] | 700 | element->setAttribute(item.attributeName, newValue.toString()); |
rniwa@webkit.org | 640b073 | 2020-03-08 03:52:06 +0000 | [diff] [blame] | 701 | return WTF::nullopt; |
| 702 | } |
| 703 | |
sihui_liu@apple.com | 12ab7a7 | 2020-06-01 23:54:34 +0000 | [diff] [blame] | 704 | size_t currentTokenIndex = 0; |
| 705 | HashMap<TokenIdentifier, TokenExchangeData> tokenExchangeMap; |
rniwa@webkit.org | 900f560 | 2019-12-21 02:09:50 +0000 | [diff] [blame] | 706 | RefPtr<Node> commonAncestor; |
rniwa@webkit.org | bf66d0a | 2020-03-12 23:29:31 +0000 | [diff] [blame] | 707 | RefPtr<Node> firstContentNode; |
sihui_liu@apple.com | 12ab7a7 | 2020-06-01 23:54:34 +0000 | [diff] [blame] | 708 | RefPtr<Node> lastChildOfCommonAncestorInRange; |
rniwa@webkit.org | bf66d0a | 2020-03-12 23:29:31 +0000 | [diff] [blame] | 709 | HashSet<Ref<Node>> nodesToRemove; |
sihui_liu@apple.com | 12ab7a7 | 2020-06-01 23:54:34 +0000 | [diff] [blame] | 710 | ParagraphContentIterator iterator { item.start, item.end }; |
rniwa@webkit.org | cffb789 | 2020-03-07 01:14:23 +0000 | [diff] [blame] | 711 | for (; !iterator.atEnd(); iterator.advance()) { |
| 712 | auto content = iterator.currentContent(); |
sihui_liu@apple.com | 12ab7a7 | 2020-06-01 23:54:34 +0000 | [diff] [blame] | 713 | ASSERT(content.node); |
| 714 | |
| 715 | lastChildOfCommonAncestorInRange = content.node; |
| 716 | nodesToRemove.add(*content.node); |
rniwa@webkit.org | cffb789 | 2020-03-07 01:14:23 +0000 | [diff] [blame] | 717 | |
| 718 | if (!content.isReplacedContent && !content.isTextContent) |
| 719 | continue; |
| 720 | |
sihui_liu@apple.com | 12ab7a7 | 2020-06-01 23:54:34 +0000 | [diff] [blame] | 721 | Vector<ManipulationToken> tokensInCurrentNode; |
sihui_liu@apple.com | 5f91c3b | 2020-06-04 23:58:32 +0000 | [diff] [blame] | 722 | if (content.isReplacedContent) { |
| 723 | if (currentTokenIndex >= item.tokens.size()) |
| 724 | return ManipulationFailureType::ContentChanged; |
| 725 | |
sihui_liu@apple.com | 12ab7a7 | 2020-06-01 23:54:34 +0000 | [diff] [blame] | 726 | tokensInCurrentNode.append(item.tokens[currentTokenIndex]); |
sihui_liu@apple.com | 5f91c3b | 2020-06-04 23:58:32 +0000 | [diff] [blame] | 727 | } else |
sihui_liu@apple.com | 0e39ec0 | 2020-06-09 06:54:20 +0000 | [diff] [blame] | 728 | tokensInCurrentNode = createUnit(content.text, *content.node).tokens; |
rniwa@webkit.org | cffb789 | 2020-03-07 01:14:23 +0000 | [diff] [blame] | 729 | |
sihui_liu@apple.com | 12ab7a7 | 2020-06-01 23:54:34 +0000 | [diff] [blame] | 730 | bool isNodeIncluded = WTF::anyOf(tokensInCurrentNode, [] (auto& token) { |
| 731 | return !token.isExcluded; |
| 732 | }); |
| 733 | for (auto& token : tokensInCurrentNode) { |
sihui_liu@apple.com | 5f91c3b | 2020-06-04 23:58:32 +0000 | [diff] [blame] | 734 | if (currentTokenIndex >= item.tokens.size()) |
sihui_liu@apple.com | 5a2c1d3 | 2020-04-23 17:23:54 +0000 | [diff] [blame] | 735 | return ManipulationFailureType::ContentChanged; |
sihui_liu@apple.com | 12ab7a7 | 2020-06-01 23:54:34 +0000 | [diff] [blame] | 736 | |
| 737 | auto& currentToken = item.tokens[currentTokenIndex++]; |
| 738 | if (!content.isReplacedContent && currentToken.content != token.content) |
| 739 | return ManipulationFailureType::ContentChanged; |
| 740 | |
| 741 | tokenExchangeMap.set(currentToken.identifier, TokenExchangeData { content.node.copyRef(), currentToken.content, !isNodeIncluded }); |
sihui_liu@apple.com | 5a2c1d3 | 2020-04-23 17:23:54 +0000 | [diff] [blame] | 742 | } |
| 743 | |
rniwa@webkit.org | bf66d0a | 2020-03-12 23:29:31 +0000 | [diff] [blame] | 744 | if (!firstContentNode) |
| 745 | firstContentNode = content.node; |
rniwa@webkit.org | 900f560 | 2019-12-21 02:09:50 +0000 | [diff] [blame] | 746 | |
sihui_liu@apple.com | 12ab7a7 | 2020-06-01 23:54:34 +0000 | [diff] [blame] | 747 | auto parentNode = content.node->parentNode(); |
| 748 | if (!commonAncestor) |
| 749 | commonAncestor = parentNode; |
| 750 | else if (!parentNode->isDescendantOf(commonAncestor.get())) { |
| 751 | commonAncestor = commonInclusiveAncestor(*commonAncestor, *parentNode); |
| 752 | ASSERT(commonAncestor); |
rniwa@webkit.org | 900f560 | 2019-12-21 02:09:50 +0000 | [diff] [blame] | 753 | } |
rniwa@webkit.org | e9da7a8 | 2019-10-25 00:08:13 +0000 | [diff] [blame] | 754 | } |
| 755 | |
sihui_liu@apple.com | 12ab7a7 | 2020-06-01 23:54:34 +0000 | [diff] [blame] | 756 | while (lastChildOfCommonAncestorInRange && lastChildOfCommonAncestorInRange->parentNode() != commonAncestor) |
| 757 | lastChildOfCommonAncestorInRange = lastChildOfCommonAncestorInRange->parentNode(); |
| 758 | |
rniwa@webkit.org | bf66d0a | 2020-03-12 23:29:31 +0000 | [diff] [blame] | 759 | for (auto node = commonAncestor; node; node = node->parentNode()) |
| 760 | nodesToRemove.remove(*node); |
rniwa@webkit.org | e9da7a8 | 2019-10-25 00:08:13 +0000 | [diff] [blame] | 761 | |
rniwa@webkit.org | 900f560 | 2019-12-21 02:09:50 +0000 | [diff] [blame] | 762 | HashSet<Ref<Node>> reusedOriginalNodes; |
sihui_liu@apple.com | 12ab7a7 | 2020-06-01 23:54:34 +0000 | [diff] [blame] | 763 | Vector<NodeEntry> lastTopDownPath; |
rniwa@webkit.org | 900f560 | 2019-12-21 02:09:50 +0000 | [diff] [blame] | 764 | Vector<NodeInsertion> insertions; |
sihui_liu@apple.com | 12ab7a7 | 2020-06-01 23:54:34 +0000 | [diff] [blame] | 765 | for (size_t index = 0; index < replacementTokens.size(); ++index) { |
| 766 | auto& replacementToken = replacementTokens[index]; |
| 767 | auto it = tokenExchangeMap.find(replacementToken.identifier); |
rniwa@webkit.org | 900f560 | 2019-12-21 02:09:50 +0000 | [diff] [blame] | 768 | if (it == tokenExchangeMap.end()) |
rniwa@webkit.org | e44b9135 | 2020-03-04 05:22:14 +0000 | [diff] [blame] | 769 | return ManipulationFailureType::InvalidToken; |
rniwa@webkit.org | 900f560 | 2019-12-21 02:09:50 +0000 | [diff] [blame] | 770 | |
| 771 | auto& exchangeData = it->value; |
sihui_liu@apple.com | 12ab7a7 | 2020-06-01 23:54:34 +0000 | [diff] [blame] | 772 | auto* originalNode = exchangeData.node.get(); |
| 773 | ASSERT(originalNode); |
| 774 | auto replacementText = replacementToken.content; |
rniwa@webkit.org | 900f560 | 2019-12-21 02:09:50 +0000 | [diff] [blame] | 775 | |
sihui_liu@apple.com | 12ab7a7 | 2020-06-01 23:54:34 +0000 | [diff] [blame] | 776 | RefPtr<Node> replacementNode; |
rniwa@webkit.org | 900f560 | 2019-12-21 02:09:50 +0000 | [diff] [blame] | 777 | if (exchangeData.isExcluded) { |
| 778 | if (exchangeData.isConsumed) |
rniwa@webkit.org | e44b9135 | 2020-03-04 05:22:14 +0000 | [diff] [blame] | 779 | return ManipulationFailureType::ExclusionViolation; |
rniwa@webkit.org | 900f560 | 2019-12-21 02:09:50 +0000 | [diff] [blame] | 780 | exchangeData.isConsumed = true; |
sihui_liu@apple.com | 12ab7a7 | 2020-06-01 23:54:34 +0000 | [diff] [blame] | 781 | |
| 782 | if (!replacementToken.content.isNull() && replacementToken.content != exchangeData.originalContent) |
rniwa@webkit.org | e44b9135 | 2020-03-04 05:22:14 +0000 | [diff] [blame] | 783 | return ManipulationFailureType::ExclusionViolation; |
sihui_liu@apple.com | 12ab7a7 | 2020-06-01 23:54:34 +0000 | [diff] [blame] | 784 | |
| 785 | replacementNode = originalNode; |
| 786 | for (RefPtr<Node> descendentNode = NodeTraversal::next(*originalNode, originalNode); descendentNode; descendentNode = NodeTraversal::next(*descendentNode, originalNode)) |
| 787 | nodesToRemove.remove(*descendentNode); |
rniwa@webkit.org | 900f560 | 2019-12-21 02:09:50 +0000 | [diff] [blame] | 788 | } else |
sihui_liu@apple.com | 12ab7a7 | 2020-06-01 23:54:34 +0000 | [diff] [blame] | 789 | replacementNode = Text::create(commonAncestor->document(), replacementText); |
rniwa@webkit.org | 900f560 | 2019-12-21 02:09:50 +0000 | [diff] [blame] | 790 | |
sihui_liu@apple.com | 12ab7a7 | 2020-06-01 23:54:34 +0000 | [diff] [blame] | 791 | auto topDownPath = getPath(commonAncestor.get(), originalNode); |
| 792 | updateInsertions(lastTopDownPath, topDownPath, replacementNode.get(), reusedOriginalNodes, insertions); |
rniwa@webkit.org | 900f560 | 2019-12-21 02:09:50 +0000 | [diff] [blame] | 793 | } |
sihui_liu@apple.com | 12ab7a7 | 2020-06-01 23:54:34 +0000 | [diff] [blame] | 794 | |
| 795 | auto end = lastChildOfCommonAncestorInRange ? positionInParentAfterNode(lastChildOfCommonAncestorInRange.get()) : positionAfterNode(commonAncestor.get()); |
| 796 | RefPtr<Node> node = item.end.firstNode(); |
| 797 | RefPtr<Node> endNode = end.firstNode(); |
| 798 | if (node && node != endNode) { |
| 799 | auto topDownPath = getPath(commonAncestor.get(), node->parentNode()); |
sihui_liu@apple.com | 2016ded | 2020-06-15 17:51:38 +0000 | [diff] [blame] | 800 | updateInsertions(lastTopDownPath, topDownPath, nullptr, reusedOriginalNodes, insertions, IsNodeManipulated::No); |
sihui_liu@apple.com | 12ab7a7 | 2020-06-01 23:54:34 +0000 | [diff] [blame] | 801 | } |
| 802 | while (node != endNode) { |
| 803 | Ref<Node> parentNode = *node->parentNode(); |
| 804 | while (!lastTopDownPath.isEmpty() && lastTopDownPath.last().first.ptr() != parentNode.ptr()) |
| 805 | lastTopDownPath.removeLast(); |
| 806 | |
sihui_liu@apple.com | 2016ded | 2020-06-15 17:51:38 +0000 | [diff] [blame] | 807 | insertions.append(NodeInsertion { lastTopDownPath.size() ? lastTopDownPath.last().second.ptr() : nullptr, *node, IsNodeManipulated::No }); |
sihui_liu@apple.com | 12ab7a7 | 2020-06-01 23:54:34 +0000 | [diff] [blame] | 808 | lastTopDownPath.append({ *node, *node }); |
| 809 | node = NodeTraversal::next(*node); |
| 810 | } |
rniwa@webkit.org | 900f560 | 2019-12-21 02:09:50 +0000 | [diff] [blame] | 811 | |
rniwa@webkit.org | bf66d0a | 2020-03-12 23:29:31 +0000 | [diff] [blame] | 812 | Position insertionPoint = positionBeforeNode(firstContentNode.get()).parentAnchoredEquivalent(); |
rniwa@webkit.org | 900f560 | 2019-12-21 02:09:50 +0000 | [diff] [blame] | 813 | while (insertionPoint.containerNode() != commonAncestor) |
| 814 | insertionPoint = positionInParentBeforeNode(insertionPoint.containerNode()); |
rniwa@webkit.org | 900f560 | 2019-12-21 02:09:50 +0000 | [diff] [blame] | 815 | |
| 816 | for (auto& node : nodesToRemove) |
| 817 | node->remove(); |
| 818 | |
wenson_hsieh@apple.com | e0dde55 | 2020-06-17 01:34:01 +0000 | [diff] [blame] | 819 | HashSet<Ref<Node>> parentNodesWithOnlyManipulatedChildren; |
rniwa@webkit.org | 900f560 | 2019-12-21 02:09:50 +0000 | [diff] [blame] | 820 | for (auto& insertion : insertions) { |
wenson_hsieh@apple.com | e0dde55 | 2020-06-17 01:34:01 +0000 | [diff] [blame] | 821 | RefPtr<Node> parent; |
rniwa@webkit.org | cffb789 | 2020-03-07 01:14:23 +0000 | [diff] [blame] | 822 | if (!insertion.parentIfDifferentFromCommonAncestor) { |
wenson_hsieh@apple.com | e0dde55 | 2020-06-17 01:34:01 +0000 | [diff] [blame] | 823 | parent = insertionPoint.containerNode(); |
| 824 | parent->insertBefore(insertion.child, insertionPoint.computeNodeAfterPosition()); |
rniwa@webkit.org | cffb789 | 2020-03-07 01:14:23 +0000 | [diff] [blame] | 825 | insertionPoint = positionInParentAfterNode(insertion.child.ptr()); |
wenson_hsieh@apple.com | e0dde55 | 2020-06-17 01:34:01 +0000 | [diff] [blame] | 826 | } else { |
| 827 | parent = insertion.parentIfDifferentFromCommonAncestor; |
| 828 | parent->appendChild(insertion.child); |
| 829 | } |
| 830 | |
| 831 | if (insertion.isChildManipulated == IsNodeManipulated::No) { |
| 832 | parentNodesWithOnlyManipulatedChildren.remove(*parent); |
| 833 | continue; |
| 834 | } |
| 835 | |
| 836 | if (is<Element>(insertion.child.get())) |
rniwa@webkit.org | 9538698 | 2020-02-28 03:37:22 +0000 | [diff] [blame] | 837 | m_manipulatedElements.add(downcast<Element>(insertion.child.get())); |
wenson_hsieh@apple.com | e0dde55 | 2020-06-17 01:34:01 +0000 | [diff] [blame] | 838 | else if (parent->firstChild() == parent->lastChild()) |
| 839 | parentNodesWithOnlyManipulatedChildren.add(*parent); |
| 840 | } |
| 841 | |
| 842 | for (auto& node : parentNodesWithOnlyManipulatedChildren) { |
| 843 | if (is<Element>(node.get())) |
| 844 | m_manipulatedElements.add(downcast<Element>(node.get())); |
rniwa@webkit.org | 900f560 | 2019-12-21 02:09:50 +0000 | [diff] [blame] | 845 | } |
rniwa@webkit.org | 7afdb0b | 2019-10-25 20:41:15 +0000 | [diff] [blame] | 846 | |
rniwa@webkit.org | e44b9135 | 2020-03-04 05:22:14 +0000 | [diff] [blame] | 847 | return WTF::nullopt; |
rniwa@webkit.org | e9da7a8 | 2019-10-25 00:08:13 +0000 | [diff] [blame] | 848 | } |
| 849 | |
| 850 | } // namespace WebCore |