Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / core / rendering / RenderSearchField.cpp
1 /**
2  * Copyright (C) 2006, 2007, 2010 Apple Inc. All rights reserved.
3  *           (C) 2008 Torch Mobile Inc. All rights reserved. (http://www.torchmobile.com/)
4  * Copyright (C) 2010 Google Inc. All rights reserved.
5  * Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Library General Public
9  * License as published by the Free Software Foundation; either
10  * version 2 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Library General Public License for more details.
16  *
17  * You should have received a copy of the GNU Library General Public License
18  * along with this library; see the file COPYING.LIB.  If not, write to
19  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20  * Boston, MA 02110-1301, USA.
21  *
22  */
23
24 #include "config.h"
25 #include "core/rendering/RenderSearchField.h"
26
27 #include "core/dom/shadow/ShadowRoot.h"
28 #include "core/html/HTMLInputElement.h"
29 #include "core/html/shadow/ShadowElementNames.h"
30
31 namespace blink {
32
33 using namespace HTMLNames;
34
35 // ----------------------------
36
37 RenderSearchField::RenderSearchField(HTMLInputElement* element)
38     : RenderTextControlSingleLine(element)
39 {
40     ASSERT(element->isSearchField());
41 }
42
43 RenderSearchField::~RenderSearchField()
44 {
45 }
46
47 inline Element* RenderSearchField::searchDecorationElement() const
48 {
49     return inputElement()->userAgentShadowRoot()->getElementById(ShadowElementNames::searchDecoration());
50 }
51
52 inline Element* RenderSearchField::cancelButtonElement() const
53 {
54     return inputElement()->userAgentShadowRoot()->getElementById(ShadowElementNames::clearButton());
55 }
56
57 LayoutUnit RenderSearchField::computeControlLogicalHeight(LayoutUnit lineHeight, LayoutUnit nonContentHeight) const
58 {
59     Element* searchDecoration = searchDecorationElement();
60     if (RenderBox* decorationRenderer = searchDecoration ? searchDecoration->renderBox() : 0) {
61         decorationRenderer->updateLogicalHeight();
62         nonContentHeight = max(nonContentHeight, decorationRenderer->borderAndPaddingLogicalHeight() + decorationRenderer->marginLogicalHeight());
63         lineHeight = max(lineHeight, decorationRenderer->logicalHeight());
64     }
65     Element* cancelButton = cancelButtonElement();
66     if (RenderBox* cancelRenderer = cancelButton ? cancelButton->renderBox() : 0) {
67         cancelRenderer->updateLogicalHeight();
68         nonContentHeight = max(nonContentHeight, cancelRenderer->borderAndPaddingLogicalHeight() + cancelRenderer->marginLogicalHeight());
69         lineHeight = max(lineHeight, cancelRenderer->logicalHeight());
70     }
71
72     return lineHeight + nonContentHeight;
73 }
74
75 LayoutUnit RenderSearchField::computeLogicalHeightLimit() const
76 {
77     return logicalHeight();
78 }
79
80 void RenderSearchField::centerContainerIfNeeded(RenderBox* containerRenderer) const
81 {
82     if (!containerRenderer)
83         return;
84
85     if (containerRenderer->logicalHeight() <= contentLogicalHeight())
86         return;
87
88     // A quirk for find-in-page box on Safari Windows.
89     // http://webkit.org/b/63157
90     LayoutUnit logicalHeightDiff = containerRenderer->logicalHeight() - contentLogicalHeight();
91     containerRenderer->setLogicalTop(containerRenderer->logicalTop() - (logicalHeightDiff / 2 + layoutMod(logicalHeightDiff, 2)));
92 }
93
94 }