OLD | NEW |
---|---|
1 /* | 1 /* |
2 * Copyright (C) 2004, 2005, 2006, 2007 Apple Inc. All rights reserved. | 2 * Copyright (C) 2004, 2005, 2006, 2007 Apple Inc. All rights reserved. |
3 * | 3 * |
4 * Redistribution and use in source and binary forms, with or without | 4 * Redistribution and use in source and binary forms, with or without |
5 * modification, are permitted provided that the following conditions | 5 * modification, are permitted provided that the following conditions |
6 * are met: | 6 * are met: |
7 * 1. Redistributions of source code must retain the above copyright | 7 * 1. Redistributions of source code must retain the above copyright |
8 * notice, this list of conditions and the following disclaimer. | 8 * notice, this list of conditions and the following disclaimer. |
9 * 2. Redistributions in binary form must reproduce the above copyright | 9 * 2. Redistributions in binary form must reproduce the above copyright |
10 * notice, this list of conditions and the following disclaimer in the | 10 * notice, this list of conditions and the following disclaimer in the |
(...skipping 23 matching lines...) Expand all Loading... | |
34 #include "core/dom/Range.h" | 34 #include "core/dom/Range.h" |
35 #include "core/dom/Text.h" | 35 #include "core/dom/Text.h" |
36 #include "core/dom/shadow/ShadowRoot.h" | 36 #include "core/dom/shadow/ShadowRoot.h" |
37 #include "core/editing/EditingStrategy.h" | 37 #include "core/editing/EditingStrategy.h" |
38 #include "core/editing/Editor.h" | 38 #include "core/editing/Editor.h" |
39 #include "core/editing/PlainTextRange.h" | 39 #include "core/editing/PlainTextRange.h" |
40 #include "core/editing/PositionIterator.h" | 40 #include "core/editing/PositionIterator.h" |
41 #include "core/editing/VisiblePosition.h" | 41 #include "core/editing/VisiblePosition.h" |
42 #include "core/editing/VisibleSelection.h" | 42 #include "core/editing/VisibleSelection.h" |
43 #include "core/editing/VisibleUnits.h" | 43 #include "core/editing/VisibleUnits.h" |
44 #include "core/editing/commands/CompositeEditCommand.h" | |
45 #include "core/editing/commands/TypingCommand.h" | |
44 #include "core/editing/iterators/TextIterator.h" | 46 #include "core/editing/iterators/TextIterator.h" |
45 #include "core/editing/serializers/HTMLInterchange.h" | 47 #include "core/editing/serializers/HTMLInterchange.h" |
46 #include "core/editing/state_machines/BackspaceStateMachine.h" | 48 #include "core/editing/state_machines/BackspaceStateMachine.h" |
47 #include "core/editing/state_machines/BackwardGraphemeBoundaryStateMachine.h" | 49 #include "core/editing/state_machines/BackwardGraphemeBoundaryStateMachine.h" |
48 #include "core/editing/state_machines/ForwardGraphemeBoundaryStateMachine.h" | 50 #include "core/editing/state_machines/ForwardGraphemeBoundaryStateMachine.h" |
49 #include "core/frame/FrameView.h" | 51 #include "core/frame/FrameView.h" |
50 #include "core/frame/LocalFrame.h" | 52 #include "core/frame/LocalFrame.h" |
51 #include "core/frame/UseCounter.h" | 53 #include "core/frame/UseCounter.h" |
52 #include "core/html/HTMLBRElement.h" | 54 #include "core/html/HTMLBRElement.h" |
53 #include "core/html/HTMLDivElement.h" | 55 #include "core/html/HTMLDivElement.h" |
54 #include "core/html/HTMLLIElement.h" | 56 #include "core/html/HTMLLIElement.h" |
55 #include "core/html/HTMLParagraphElement.h" | 57 #include "core/html/HTMLParagraphElement.h" |
56 #include "core/html/HTMLSpanElement.h" | 58 #include "core/html/HTMLSpanElement.h" |
57 #include "core/html/HTMLTableCellElement.h" | 59 #include "core/html/HTMLTableCellElement.h" |
58 #include "core/html/HTMLUListElement.h" | 60 #include "core/html/HTMLUListElement.h" |
59 #include "core/layout/LayoutObject.h" | 61 #include "core/layout/LayoutObject.h" |
60 #include "core/layout/LayoutTableCell.h" | 62 #include "core/layout/LayoutTableCell.h" |
61 #include "platform/clipboard/ClipboardMimeTypes.h" | |
62 #include "wtf/Assertions.h" | 63 #include "wtf/Assertions.h" |
63 #include "wtf/StdLibExtras.h" | 64 #include "wtf/StdLibExtras.h" |
64 #include "wtf/text/StringBuilder.h" | 65 #include "wtf/text/StringBuilder.h" |
65 #include "wtf/text/Unicode.h" | 66 #include "wtf/text/Unicode.h" |
66 | 67 |
67 namespace blink { | 68 namespace blink { |
68 | 69 |
69 using namespace HTMLNames; | 70 using namespace HTMLNames; |
70 | 71 |
71 namespace { | 72 namespace { |
(...skipping 1968 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
2040 // otherwise, make sure to be at the start of the first selected node, | 2041 // otherwise, make sure to be at the start of the first selected node, |
2041 // instead of possibly at the end of the last node before the selection | 2042 // instead of possibly at the end of the last node before the selection |
2042 return mostForwardCaretPosition(visiblePosition.deepEquivalent()); | 2043 return mostForwardCaretPosition(visiblePosition.deepEquivalent()); |
2043 } | 2044 } |
2044 | 2045 |
2045 bool isTextSecurityNode(const Node* node) { | 2046 bool isTextSecurityNode(const Node* node) { |
2046 return node && node->layoutObject() && | 2047 return node && node->layoutObject() && |
2047 node->layoutObject()->style()->textSecurity() != TSNONE; | 2048 node->layoutObject()->style()->textSecurity() != TSNONE; |
2048 } | 2049 } |
2049 | 2050 |
2050 DispatchEventResult dispatchBeforeInputInsertText(EventTarget* target, | |
chongz
2016/12/20 23:27:51
See header file.
| |
2051 const String& data) { | |
2052 if (!RuntimeEnabledFeatures::inputEventEnabled()) | |
2053 return DispatchEventResult::NotCanceled; | |
2054 if (!target) | |
2055 return DispatchEventResult::NotCanceled; | |
2056 // TODO(chongz): Pass appropriate |ranges| after it's defined on spec. | |
2057 // http://w3c.github.io/editing/input-events.html#dom-inputevent-inputtype | |
2058 InputEvent* beforeInputEvent = InputEvent::createBeforeInput( | |
2059 InputEvent::InputType::InsertText, data, | |
2060 InputEvent::EventCancelable::IsCancelable, | |
2061 InputEvent::EventIsComposing::NotComposing, nullptr); | |
2062 return target->dispatchEvent(beforeInputEvent); | |
2063 } | |
2064 | |
2065 DispatchEventResult dispatchBeforeInputFromComposition( | |
2066 EventTarget* target, | |
2067 InputEvent::InputType inputType, | |
2068 const String& data, | |
2069 InputEvent::EventCancelable cancelable) { | |
2070 if (!RuntimeEnabledFeatures::inputEventEnabled()) | |
2071 return DispatchEventResult::NotCanceled; | |
2072 if (!target) | |
2073 return DispatchEventResult::NotCanceled; | |
2074 // TODO(chongz): Pass appropriate |ranges| after it's defined on spec. | |
2075 // http://w3c.github.io/editing/input-events.html#dom-inputevent-inputtype | |
2076 InputEvent* beforeInputEvent = InputEvent::createBeforeInput( | |
2077 inputType, data, cancelable, InputEvent::EventIsComposing::IsComposing, | |
2078 nullptr); | |
2079 return target->dispatchEvent(beforeInputEvent); | |
2080 } | |
2081 | |
2082 DispatchEventResult dispatchBeforeInputEditorCommand( | |
2083 EventTarget* target, | |
2084 InputEvent::InputType inputType, | |
2085 const RangeVector* ranges) { | |
2086 if (!RuntimeEnabledFeatures::inputEventEnabled()) | |
2087 return DispatchEventResult::NotCanceled; | |
2088 if (!target) | |
2089 return DispatchEventResult::NotCanceled; | |
2090 InputEvent* beforeInputEvent = InputEvent::createBeforeInput( | |
2091 inputType, nullAtom, InputEvent::EventCancelable::IsCancelable, | |
2092 InputEvent::EventIsComposing::NotComposing, ranges); | |
2093 return target->dispatchEvent(beforeInputEvent); | |
2094 } | |
2095 | |
2096 DispatchEventResult dispatchBeforeInputDataTransfer( | |
2097 EventTarget* target, | |
2098 InputEvent::InputType inputType, | |
2099 DataTransfer* dataTransfer, | |
2100 const RangeVector* ranges) { | |
2101 if (!RuntimeEnabledFeatures::inputEventEnabled()) | |
2102 return DispatchEventResult::NotCanceled; | |
2103 if (!target) | |
2104 return DispatchEventResult::NotCanceled; | |
2105 | |
2106 DCHECK(inputType == InputEvent::InputType::InsertFromPaste || | |
2107 inputType == InputEvent::InputType::InsertReplacementText || | |
2108 inputType == InputEvent::InputType::InsertFromDrop || | |
2109 inputType == InputEvent::InputType::DeleteByCut) | |
2110 << "Unsupported inputType: " << (int)inputType; | |
2111 | |
2112 InputEvent* beforeInputEvent; | |
2113 | |
2114 if (hasRichlyEditableStyle(*(target->toNode())) || !dataTransfer) { | |
chongz
2016/12/21 23:59:24
This logic for data vs. dataTransfer are only used
| |
2115 beforeInputEvent = InputEvent::createBeforeInput( | |
2116 inputType, dataTransfer, InputEvent::EventCancelable::IsCancelable, | |
2117 InputEvent::EventIsComposing::NotComposing, ranges); | |
2118 } else { | |
2119 const String& data = dataTransfer->getData(mimeTypeTextPlain); | |
2120 // TODO(chongz): Pass appropriate |ranges| after it's defined on spec. | |
2121 // http://w3c.github.io/editing/input-events.html#dom-inputevent-inputtype | |
2122 beforeInputEvent = InputEvent::createBeforeInput( | |
2123 inputType, data, InputEvent::EventCancelable::IsCancelable, | |
2124 InputEvent::EventIsComposing::NotComposing, nullptr); | |
2125 } | |
2126 return target->dispatchEvent(beforeInputEvent); | |
2127 } | |
2128 | |
2129 InputEvent::InputType deletionInputTypeFromTextGranularity( | 2051 InputEvent::InputType deletionInputTypeFromTextGranularity( |
2130 DeleteDirection direction, | 2052 DeleteDirection direction, |
2131 TextGranularity granularity) { | 2053 TextGranularity granularity) { |
2132 using InputType = InputEvent::InputType; | 2054 using InputType = InputEvent::InputType; |
2133 switch (direction) { | 2055 switch (direction) { |
2134 case DeleteDirection::Forward: | 2056 case DeleteDirection::Forward: |
2135 if (granularity == WordGranularity) | 2057 if (granularity == WordGranularity) |
2136 return InputType::DeleteWordForward; | 2058 return InputType::DeleteWordForward; |
2137 if (granularity == LineBoundary) | 2059 if (granularity == LineBoundary) |
2138 return InputType::DeleteLineForward; | 2060 return InputType::DeleteLineForward; |
2139 return InputType::DeleteContentForward; | 2061 return InputType::DeleteContentForward; |
2140 case DeleteDirection::Backward: | 2062 case DeleteDirection::Backward: |
2141 if (granularity == WordGranularity) | 2063 if (granularity == WordGranularity) |
2142 return InputType::DeleteWordBackward; | 2064 return InputType::DeleteWordBackward; |
2143 if (granularity == LineBoundary) | 2065 if (granularity == LineBoundary) |
2144 return InputType::DeleteLineBackward; | 2066 return InputType::DeleteLineBackward; |
2145 return InputType::DeleteContentBackward; | 2067 return InputType::DeleteContentBackward; |
2146 default: | 2068 default: |
2147 return InputType::None; | 2069 return InputType::None; |
2148 } | 2070 } |
2149 } | 2071 } |
2150 | 2072 |
2073 InputEvent::EventIsComposing isComposingFromCommand( | |
chongz
2016/12/20 23:27:51
See header file.
| |
2074 const CompositeEditCommand* command) { | |
2075 if (command->isTypingCommand() && | |
2076 toTypingCommand(command)->compositionType() != | |
2077 TypingCommand::TextCompositionNone) | |
2078 return InputEvent::IsComposing; | |
2079 return InputEvent::NotComposing; | |
2080 } | |
2081 | |
2082 InputEvent::EventCancelable isCancelableFromCommand( | |
2083 const CompositeEditCommand* command) { | |
2084 if (command->isTypingCommand() && | |
2085 toTypingCommand(command)->compositionType() == | |
2086 TypingCommand::TextCompositionUpdate) | |
2087 return InputEvent::NotCancelable; | |
2088 return InputEvent::IsCancelable; | |
2089 } | |
2090 | |
2091 bool dispatchBeforeInputEvent(EditCommandSource source, | |
2092 LocalFrame* frame, | |
2093 Node* target, | |
2094 InputEvent::InputType inputType, | |
2095 const String& data, | |
2096 DataTransfer* dataTransfer, | |
2097 InputEvent::EventCancelable isCancelable, | |
2098 InputEvent::EventIsComposing isComposing, | |
2099 const RangeVector* ranges) { | |
2100 // Don't fire 'beforeinput', return true to continue. | |
2101 if (source != EditCommandSource::kMenuOrKeyBinding || | |
2102 inputType == InputEvent::InputType::None) | |
2103 return true; | |
2104 if (!RuntimeEnabledFeatures::inputEventEnabled()) | |
2105 return true; | |
2106 if (!frame || !target || !target->isConnected()) | |
2107 return true; | |
2108 | |
2109 if (!hasRichlyEditableStyle(*target)) { | |
2110 // Plain-text only elements (<input>, <textarea>, etc.) don't support | |
2111 // DataTransfer or Range. | |
2112 dataTransfer = nullptr; | |
2113 ranges = nullptr; | |
2114 } | |
2115 | |
2116 InputEvent* beforeInputEvent; | |
2117 | |
2118 if (dataTransfer) { | |
2119 beforeInputEvent = InputEvent::createBeforeInput( | |
2120 inputType, dataTransfer, isCancelable, isComposing, ranges); | |
2121 } else { | |
2122 beforeInputEvent = InputEvent::createBeforeInput( | |
2123 inputType, data, isCancelable, isComposing, ranges); | |
2124 } | |
2125 | |
2126 DispatchEventResult result = target->dispatchEvent(beforeInputEvent); | |
2127 // 'beforeinput' event handler may destroy target frame. | |
2128 if (frame->document()->frame() != frame) | |
Xiaocheng
2016/12/21 02:56:38
Is |frame->document()| still non-null after the ev
chongz
2016/12/21 23:59:24
Added check for |frame->document()|, but according
Xiaocheng
2016/12/22 06:26:20
Thanks for the clarification.
I thought |frame->d
| |
2129 return false; | |
2130 return result == DispatchEventResult::NotCanceled; | |
2131 } | |
2132 | |
2151 } // namespace blink | 2133 } // namespace blink |
OLD | NEW |