| OLD | NEW |
| (Empty) |
| 1 /* | |
| 2 * Copyright (C) 2006, 2008, 2010 Apple Inc. All rights reserved. | |
| 3 * Copyright (C) 2010 Google Inc. All rights reserved. | |
| 4 * | |
| 5 * Redistribution and use in source and binary forms, with or without | |
| 6 * modification, are permitted provided that the following conditions | |
| 7 * are met: | |
| 8 * 1. Redistributions of source code must retain the above copyright | |
| 9 * notice, this list of conditions and the following disclaimer. | |
| 10 * 2. Redistributions in binary form must reproduce the above copyright | |
| 11 * notice, this list of conditions and the following disclaimer in the | |
| 12 * documentation and/or other materials provided with the distribution. | |
| 13 * | |
| 14 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY | |
| 15 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
| 16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | |
| 17 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR | |
| 18 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, | |
| 19 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, | |
| 20 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR | |
| 21 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY | |
| 22 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
| 23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | |
| 24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
| 25 */ | |
| 26 | |
| 27 #include "core/html/shadow/TextControlInnerElements.h" | |
| 28 | |
| 29 #include "core/HTMLNames.h" | |
| 30 #include "core/css/resolver/StyleAdjuster.h" | |
| 31 #include "core/dom/Document.h" | |
| 32 #include "core/dom/NodeComputedStyle.h" | |
| 33 #include "core/events/MouseEvent.h" | |
| 34 #include "core/events/TextEvent.h" | |
| 35 #include "core/events/TextEventInputType.h" | |
| 36 #include "core/frame/LocalFrame.h" | |
| 37 #include "core/html/HTMLInputElement.h" | |
| 38 #include "core/html/shadow/ShadowElementNames.h" | |
| 39 #include "core/input/EventHandler.h" | |
| 40 #include "core/layout/LayoutTextControlSingleLine.h" | |
| 41 #include "core/layout/api/LayoutTextControlItem.h" | |
| 42 #include "platform/UserGestureIndicator.h" | |
| 43 | |
| 44 namespace blink { | |
| 45 | |
| 46 using namespace HTMLNames; | |
| 47 | |
| 48 TextControlInnerContainer::TextControlInnerContainer(Document& document) | |
| 49 : HTMLDivElement(document) {} | |
| 50 | |
| 51 TextControlInnerContainer* TextControlInnerContainer::Create( | |
| 52 Document& document) { | |
| 53 TextControlInnerContainer* element = new TextControlInnerContainer(document); | |
| 54 element->setAttribute(idAttr, ShadowElementNames::TextFieldContainer()); | |
| 55 return element; | |
| 56 } | |
| 57 | |
| 58 LayoutObject* TextControlInnerContainer::CreateLayoutObject( | |
| 59 const ComputedStyle&) { | |
| 60 return new LayoutTextControlInnerContainer(this); | |
| 61 } | |
| 62 | |
| 63 // --------------------------- | |
| 64 | |
| 65 EditingViewPortElement::EditingViewPortElement(Document& document) | |
| 66 : HTMLDivElement(document) { | |
| 67 SetHasCustomStyleCallbacks(); | |
| 68 } | |
| 69 | |
| 70 EditingViewPortElement* EditingViewPortElement::Create(Document& document) { | |
| 71 EditingViewPortElement* element = new EditingViewPortElement(document); | |
| 72 element->setAttribute(idAttr, ShadowElementNames::EditingViewPort()); | |
| 73 return element; | |
| 74 } | |
| 75 | |
| 76 PassRefPtr<ComputedStyle> EditingViewPortElement::CustomStyleForLayoutObject() { | |
| 77 // FXIME: Move these styles to html.css. | |
| 78 | |
| 79 RefPtr<ComputedStyle> style = ComputedStyle::Create(); | |
| 80 style->InheritFrom(OwnerShadowHost()->ComputedStyleRef()); | |
| 81 | |
| 82 style->SetFlexGrow(1); | |
| 83 style->SetMinWidth(Length(0, kFixed)); | |
| 84 style->SetDisplay(EDisplay::kBlock); | |
| 85 style->SetDirection(TextDirection::kLtr); | |
| 86 | |
| 87 // We don't want the shadow dom to be editable, so we set this block to | |
| 88 // read-only in case the input itself is editable. | |
| 89 style->SetUserModify(READ_ONLY); | |
| 90 style->SetUnique(); | |
| 91 | |
| 92 if (const ComputedStyle* parent_style = ParentComputedStyle()) | |
| 93 StyleAdjuster::AdjustStyleForAlignment(*style, *parent_style); | |
| 94 | |
| 95 return style.Release(); | |
| 96 } | |
| 97 | |
| 98 // --------------------------- | |
| 99 | |
| 100 inline TextControlInnerEditorElement::TextControlInnerEditorElement( | |
| 101 Document& document) | |
| 102 : HTMLDivElement(document) { | |
| 103 SetHasCustomStyleCallbacks(); | |
| 104 } | |
| 105 | |
| 106 TextControlInnerEditorElement* TextControlInnerEditorElement::Create( | |
| 107 Document& document) { | |
| 108 TextControlInnerEditorElement* element = | |
| 109 new TextControlInnerEditorElement(document); | |
| 110 element->setAttribute(idAttr, ShadowElementNames::InnerEditor()); | |
| 111 return element; | |
| 112 } | |
| 113 | |
| 114 void TextControlInnerEditorElement::DefaultEventHandler(Event* event) { | |
| 115 // FIXME: In the future, we should add a way to have default event listeners. | |
| 116 // Then we would add one to the text field's inner div, and we wouldn't need | |
| 117 // this subclass. | |
| 118 // Or possibly we could just use a normal event listener. | |
| 119 if (event->IsBeforeTextInsertedEvent() || | |
| 120 event->type() == EventTypeNames::webkitEditableContentChanged) { | |
| 121 Element* shadow_ancestor = OwnerShadowHost(); | |
| 122 // A TextControlInnerTextElement can have no host if its been detached, | |
| 123 // but kept alive by an EditCommand. In this case, an undo/redo can | |
| 124 // cause events to be sent to the TextControlInnerTextElement. To | |
| 125 // prevent an infinite loop, we must check for this case before sending | |
| 126 // the event up the chain. | |
| 127 if (shadow_ancestor) | |
| 128 shadow_ancestor->DefaultEventHandler(event); | |
| 129 } | |
| 130 if (!event->DefaultHandled()) | |
| 131 HTMLDivElement::DefaultEventHandler(event); | |
| 132 } | |
| 133 | |
| 134 LayoutObject* TextControlInnerEditorElement::CreateLayoutObject( | |
| 135 const ComputedStyle&) { | |
| 136 return new LayoutTextControlInnerEditor(this); | |
| 137 } | |
| 138 | |
| 139 PassRefPtr<ComputedStyle> | |
| 140 TextControlInnerEditorElement::CustomStyleForLayoutObject() { | |
| 141 LayoutObject* parent_layout_object = OwnerShadowHost()->GetLayoutObject(); | |
| 142 if (!parent_layout_object || !parent_layout_object->IsTextControl()) | |
| 143 return OriginalStyleForLayoutObject(); | |
| 144 LayoutTextControlItem text_control_layout_item = | |
| 145 LayoutTextControlItem(ToLayoutTextControl(parent_layout_object)); | |
| 146 RefPtr<ComputedStyle> inner_editor_style = | |
| 147 text_control_layout_item.CreateInnerEditorStyle( | |
| 148 text_control_layout_item.StyleRef()); | |
| 149 // Using StyleAdjuster::adjustComputedStyle updates unwanted style. We'd like | |
| 150 // to apply only editing-related and alignment-related. | |
| 151 StyleAdjuster::AdjustStyleForEditing(*inner_editor_style); | |
| 152 if (const ComputedStyle* parent_style = ParentComputedStyle()) | |
| 153 StyleAdjuster::AdjustStyleForAlignment(*inner_editor_style, *parent_style); | |
| 154 return inner_editor_style.Release(); | |
| 155 } | |
| 156 | |
| 157 // ---------------------------- | |
| 158 | |
| 159 inline SearchFieldCancelButtonElement::SearchFieldCancelButtonElement( | |
| 160 Document& document) | |
| 161 : HTMLDivElement(document), capturing_(false) {} | |
| 162 | |
| 163 SearchFieldCancelButtonElement* SearchFieldCancelButtonElement::Create( | |
| 164 Document& document) { | |
| 165 SearchFieldCancelButtonElement* element = | |
| 166 new SearchFieldCancelButtonElement(document); | |
| 167 element->SetShadowPseudoId(AtomicString("-webkit-search-cancel-button")); | |
| 168 element->setAttribute(idAttr, ShadowElementNames::SearchClearButton()); | |
| 169 return element; | |
| 170 } | |
| 171 | |
| 172 void SearchFieldCancelButtonElement::DetachLayoutTree( | |
| 173 const AttachContext& context) { | |
| 174 if (capturing_) { | |
| 175 if (LocalFrame* frame = GetDocument().GetFrame()) | |
| 176 frame->GetEventHandler().SetCapturingMouseEventsNode(nullptr); | |
| 177 } | |
| 178 HTMLDivElement::DetachLayoutTree(context); | |
| 179 } | |
| 180 | |
| 181 void SearchFieldCancelButtonElement::DefaultEventHandler(Event* event) { | |
| 182 // If the element is visible, on mouseup, clear the value, and set selection | |
| 183 HTMLInputElement* input(toHTMLInputElement(OwnerShadowHost())); | |
| 184 if (!input || input->IsDisabledOrReadOnly()) { | |
| 185 if (!event->DefaultHandled()) | |
| 186 HTMLDivElement::DefaultEventHandler(event); | |
| 187 return; | |
| 188 } | |
| 189 | |
| 190 if (event->type() == EventTypeNames::click && event->IsMouseEvent() && | |
| 191 ToMouseEvent(event)->button() == | |
| 192 static_cast<short>(WebPointerProperties::Button::kLeft)) { | |
| 193 input->SetValueForUser(""); | |
| 194 input->SetAutofilled(false); | |
| 195 input->OnSearch(); | |
| 196 event->SetDefaultHandled(); | |
| 197 } | |
| 198 | |
| 199 if (!event->DefaultHandled()) | |
| 200 HTMLDivElement::DefaultEventHandler(event); | |
| 201 } | |
| 202 | |
| 203 bool SearchFieldCancelButtonElement::WillRespondToMouseClickEvents() { | |
| 204 const HTMLInputElement* input = toHTMLInputElement(OwnerShadowHost()); | |
| 205 if (input && !input->IsDisabledOrReadOnly()) | |
| 206 return true; | |
| 207 | |
| 208 return HTMLDivElement::WillRespondToMouseClickEvents(); | |
| 209 } | |
| 210 | |
| 211 } // namespace blink | |
| OLD | NEW |