1 /**
2 * Copyright (C) 2006, 2007 Apple Inc. All rights reserved.
3 * (C) 2008 Torch Mobile Inc. All rights reserved. (http://www.torchmobile.com/)
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more details.
14 *
15 * You should have received a copy of the GNU Library General Public License
16 * along with this library; see the file COPYING.LIB. If not, write to
17 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
19 *
20 */
21
22 #include "config.h"
23 #include "core/rendering/RenderTextControl.h"
24
25 #include "core/html/HTMLTextFormControlElement.h"
26 #include "core/rendering/HitTestResult.h"
27 #include "core/rendering/RenderTheme.h"
28 #include "platform/scroll/ScrollbarTheme.h"
29 #include "wtf/unicode/CharacterNames.h"
30
31 using namespace std;
32
33 namespace WebCore {
34
RenderTextControl(HTMLTextFormControlElement * element)35 RenderTextControl::RenderTextControl(HTMLTextFormControlElement* element)
36 : RenderBlockFlow(element)
37 {
38 ASSERT(element);
39 }
40
~RenderTextControl()41 RenderTextControl::~RenderTextControl()
42 {
43 }
44
textFormControlElement() const45 HTMLTextFormControlElement* RenderTextControl::textFormControlElement() const
46 {
47 return toHTMLTextFormControlElement(node());
48 }
49
innerEditorElement() const50 HTMLElement* RenderTextControl::innerEditorElement() const
51 {
52 return textFormControlElement()->innerEditorElement();
53 }
54
addChild(RenderObject * newChild,RenderObject * beforeChild)55 void RenderTextControl::addChild(RenderObject* newChild, RenderObject* beforeChild)
56 {
57 // FIXME: This is a terrible hack to get the caret over the placeholder text since it'll
58 // make us paint the placeholder first. (See https://trac.webkit.org/changeset/118733)
59 Node* node = newChild->node();
60 if (node && node->isElementNode() && toElement(node)->shadowPseudoId() == "-webkit-input-placeholder")
61 RenderBlockFlow::addChild(newChild, firstChild());
62 else
63 RenderBlockFlow::addChild(newChild, beforeChild);
64 }
65
styleDidChange(StyleDifference diff,const RenderStyle * oldStyle)66 void RenderTextControl::styleDidChange(StyleDifference diff, const RenderStyle* oldStyle)
67 {
68 RenderBlockFlow::styleDidChange(diff, oldStyle);
69 Element* innerEditor = innerEditorElement();
70 if (!innerEditor)
71 return;
72 RenderBlock* innerEditorRenderer = toRenderBlock(innerEditor->renderer());
73 if (innerEditorRenderer) {
74 // We may have set the width and the height in the old style in layout().
75 // Reset them now to avoid getting a spurious layout hint.
76 innerEditorRenderer->style()->setHeight(Length());
77 innerEditorRenderer->style()->setWidth(Length());
78 innerEditorRenderer->setStyle(createInnerEditorStyle(style()));
79 innerEditor->setNeedsStyleRecalc(SubtreeStyleChange);
80 }
81 textFormControlElement()->updatePlaceholderVisibility(false);
82 }
83
updateUserModifyProperty(HTMLTextFormControlElement * node,RenderStyle * style)84 static inline void updateUserModifyProperty(HTMLTextFormControlElement* node, RenderStyle* style)
85 {
86 style->setUserModify(node->isDisabledOrReadOnly() ? READ_ONLY : READ_WRITE_PLAINTEXT_ONLY);
87 }
88
adjustInnerEditorStyle(RenderStyle * textBlockStyle) const89 void RenderTextControl::adjustInnerEditorStyle(RenderStyle* textBlockStyle) const
90 {
91 // The inner block, if present, always has its direction set to LTR,
92 // so we need to inherit the direction and unicode-bidi style from the element.
93 textBlockStyle->setDirection(style()->direction());
94 textBlockStyle->setUnicodeBidi(style()->unicodeBidi());
95
96 updateUserModifyProperty(textFormControlElement(), textBlockStyle);
97 }
98
textBlockLogicalHeight() const99 int RenderTextControl::textBlockLogicalHeight() const
100 {
101 return logicalHeight() - borderAndPaddingLogicalHeight();
102 }
103
textBlockLogicalWidth() const104 int RenderTextControl::textBlockLogicalWidth() const
105 {
106 Element* innerEditor = innerEditorElement();
107 ASSERT(innerEditor);
108
109 LayoutUnit unitWidth = logicalWidth() - borderAndPaddingLogicalWidth();
110 if (innerEditor->renderer())
111 unitWidth -= innerEditor->renderBox()->paddingStart() + innerEditor->renderBox()->paddingEnd();
112
113 return unitWidth;
114 }
115
updateFromElement()116 void RenderTextControl::updateFromElement()
117 {
118 Element* innerEditor = innerEditorElement();
119 if (innerEditor && innerEditor->renderer())
120 updateUserModifyProperty(textFormControlElement(), innerEditor->renderer()->style());
121 }
122
scrollbarThickness() const123 int RenderTextControl::scrollbarThickness() const
124 {
125 // FIXME: We should get the size of the scrollbar from the RenderTheme instead.
126 return ScrollbarTheme::theme()->scrollbarThickness();
127 }
128
computeLogicalHeight(LayoutUnit logicalHeight,LayoutUnit logicalTop,LogicalExtentComputedValues & computedValues) const129 void RenderTextControl::computeLogicalHeight(LayoutUnit logicalHeight, LayoutUnit logicalTop, LogicalExtentComputedValues& computedValues) const
130 {
131 HTMLElement* innerEditor = innerEditorElement();
132 ASSERT(innerEditor);
133 if (RenderBox* innerEditorBox = innerEditor->renderBox()) {
134 LayoutUnit nonContentHeight = innerEditorBox->borderAndPaddingHeight() + innerEditorBox->marginHeight();
135 logicalHeight = computeControlLogicalHeight(innerEditorBox->lineHeight(true, HorizontalLine, PositionOfInteriorLineBoxes), nonContentHeight);
136
137 // We are able to have a horizontal scrollbar if the overflow style is scroll, or if its auto and there's no word wrap.
138 if ((isHorizontalWritingMode() && (style()->overflowX() == OSCROLL || (style()->overflowX() == OAUTO && innerEditor->renderer()->style()->overflowWrap() == NormalOverflowWrap)))
139 || (!isHorizontalWritingMode() && (style()->overflowY() == OSCROLL || (style()->overflowY() == OAUTO && innerEditor->renderer()->style()->overflowWrap() == NormalOverflowWrap))))
140 logicalHeight += scrollbarThickness();
141
142 // FIXME: The logical height of the inner text box should have been added before calling computeLogicalHeight to
143 // avoid this hack.
144 updateIntrinsicContentLogicalHeight(logicalHeight);
145
146 logicalHeight += borderAndPaddingHeight();
147 }
148
149 RenderBox::computeLogicalHeight(logicalHeight, logicalTop, computedValues);
150 }
151
hitInnerEditorElement(HitTestResult & result,const LayoutPoint & pointInContainer,const LayoutPoint & accumulatedOffset)152 void RenderTextControl::hitInnerEditorElement(HitTestResult& result, const LayoutPoint& pointInContainer, const LayoutPoint& accumulatedOffset)
153 {
154 HTMLElement* innerEditor = innerEditorElement();
155 if (!innerEditor->renderer())
156 return;
157
158 LayoutPoint adjustedLocation = accumulatedOffset + location();
159 LayoutPoint localPoint = pointInContainer - toLayoutSize(adjustedLocation + innerEditor->renderBox()->location());
160 if (hasOverflowClip())
161 localPoint += scrolledContentOffset();
162 result.setInnerNode(innerEditor);
163 result.setInnerNonSharedNode(innerEditor);
164 result.setLocalPoint(localPoint);
165 }
166
167 static const char* const fontFamiliesWithInvalidCharWidth[] = {
168 "American Typewriter",
169 "Arial Hebrew",
170 "Chalkboard",
171 "Cochin",
172 "Corsiva Hebrew",
173 "Courier",
174 "Euphemia UCAS",
175 "Geneva",
176 "Gill Sans",
177 "Hei",
178 "Helvetica",
179 "Hoefler Text",
180 "InaiMathi",
181 "Kai",
182 "Lucida Grande",
183 "Marker Felt",
184 "Monaco",
185 "Mshtakan",
186 "New Peninim MT",
187 "Osaka",
188 "Raanana",
189 "STHeiti",
190 "Symbol",
191 "Times",
192 "Apple Braille",
193 "Apple LiGothic",
194 "Apple LiSung",
195 "Apple Symbols",
196 "AppleGothic",
197 "AppleMyungjo",
198 "#GungSeo",
199 "#HeadLineA",
200 "#PCMyungjo",
201 "#PilGi",
202 };
203
204 // For font families where any of the fonts don't have a valid entry in the OS/2 table
205 // for avgCharWidth, fallback to the legacy webkit behavior of getting the avgCharWidth
206 // from the width of a '0'. This only seems to apply to a fixed number of Mac fonts,
207 // but, in order to get similar rendering across platforms, we do this check for
208 // all platforms.
hasValidAvgCharWidth(AtomicString family)209 bool RenderTextControl::hasValidAvgCharWidth(AtomicString family)
210 {
211 static HashSet<AtomicString>* fontFamiliesWithInvalidCharWidthMap = 0;
212
213 if (family.isEmpty())
214 return false;
215
216 if (!fontFamiliesWithInvalidCharWidthMap) {
217 fontFamiliesWithInvalidCharWidthMap = new HashSet<AtomicString>;
218
219 for (size_t i = 0; i < WTF_ARRAY_LENGTH(fontFamiliesWithInvalidCharWidth); ++i)
220 fontFamiliesWithInvalidCharWidthMap->add(AtomicString(fontFamiliesWithInvalidCharWidth[i]));
221 }
222
223 return !fontFamiliesWithInvalidCharWidthMap->contains(family);
224 }
225
getAvgCharWidth(AtomicString family)226 float RenderTextControl::getAvgCharWidth(AtomicString family)
227 {
228 if (hasValidAvgCharWidth(family))
229 return roundf(style()->font().primaryFont()->avgCharWidth());
230
231 const UChar ch = '0';
232 const String str = String(&ch, 1);
233 const Font& font = style()->font();
234 TextRun textRun = constructTextRun(this, font, str, style(), TextRun::AllowTrailingExpansion);
235 textRun.disableRoundingHacks();
236 return font.width(textRun);
237 }
238
scaleEmToUnits(int x) const239 float RenderTextControl::scaleEmToUnits(int x) const
240 {
241 // This matches the unitsPerEm value for MS Shell Dlg and Courier New from the "head" font table.
242 float unitsPerEm = 2048.0f;
243 return roundf(style()->font().fontDescription().computedSize() * x / unitsPerEm);
244 }
245
computeIntrinsicLogicalWidths(LayoutUnit & minLogicalWidth,LayoutUnit & maxLogicalWidth) const246 void RenderTextControl::computeIntrinsicLogicalWidths(LayoutUnit& minLogicalWidth, LayoutUnit& maxLogicalWidth) const
247 {
248 // Use average character width. Matches IE.
249 AtomicString family = style()->font().fontDescription().family().family();
250 maxLogicalWidth = preferredContentLogicalWidth(const_cast<RenderTextControl*>(this)->getAvgCharWidth(family));
251 if (RenderBox* innerEditorRenderBox = innerEditorElement()->renderBox())
252 maxLogicalWidth += innerEditorRenderBox->paddingStart() + innerEditorRenderBox->paddingEnd();
253 if (!style()->logicalWidth().isPercent())
254 minLogicalWidth = maxLogicalWidth;
255 }
256
computePreferredLogicalWidths()257 void RenderTextControl::computePreferredLogicalWidths()
258 {
259 ASSERT(preferredLogicalWidthsDirty());
260
261 m_minPreferredLogicalWidth = 0;
262 m_maxPreferredLogicalWidth = 0;
263 RenderStyle* styleToUse = style();
264
265 if (styleToUse->logicalWidth().isFixed() && styleToUse->logicalWidth().value() >= 0)
266 m_minPreferredLogicalWidth = m_maxPreferredLogicalWidth = adjustContentBoxLogicalWidthForBoxSizing(styleToUse->logicalWidth().value());
267 else
268 computeIntrinsicLogicalWidths(m_minPreferredLogicalWidth, m_maxPreferredLogicalWidth);
269
270 if (styleToUse->logicalMinWidth().isFixed() && styleToUse->logicalMinWidth().value() > 0) {
271 m_maxPreferredLogicalWidth = max(m_maxPreferredLogicalWidth, adjustContentBoxLogicalWidthForBoxSizing(styleToUse->logicalMinWidth().value()));
272 m_minPreferredLogicalWidth = max(m_minPreferredLogicalWidth, adjustContentBoxLogicalWidthForBoxSizing(styleToUse->logicalMinWidth().value()));
273 }
274
275 if (styleToUse->logicalMaxWidth().isFixed()) {
276 m_maxPreferredLogicalWidth = min(m_maxPreferredLogicalWidth, adjustContentBoxLogicalWidthForBoxSizing(styleToUse->logicalMaxWidth().value()));
277 m_minPreferredLogicalWidth = min(m_minPreferredLogicalWidth, adjustContentBoxLogicalWidthForBoxSizing(styleToUse->logicalMaxWidth().value()));
278 }
279
280 LayoutUnit toAdd = borderAndPaddingLogicalWidth();
281
282 m_minPreferredLogicalWidth += toAdd;
283 m_maxPreferredLogicalWidth += toAdd;
284
285 clearPreferredLogicalWidthsDirty();
286 }
287
addFocusRingRects(Vector<IntRect> & rects,const LayoutPoint & additionalOffset,const RenderLayerModelObject *)288 void RenderTextControl::addFocusRingRects(Vector<IntRect>& rects, const LayoutPoint& additionalOffset, const RenderLayerModelObject*)
289 {
290 if (!size().isEmpty())
291 rects.append(pixelSnappedIntRect(additionalOffset, size()));
292 }
293
layoutSpecialExcludedChild(bool relayoutChildren,SubtreeLayoutScope & layoutScope)294 RenderObject* RenderTextControl::layoutSpecialExcludedChild(bool relayoutChildren, SubtreeLayoutScope& layoutScope)
295 {
296 HTMLElement* placeholder = toHTMLTextFormControlElement(node())->placeholderElement();
297 RenderObject* placeholderRenderer = placeholder ? placeholder->renderer() : 0;
298 if (!placeholderRenderer)
299 return 0;
300 if (relayoutChildren)
301 layoutScope.setChildNeedsLayout(placeholderRenderer);
302 return placeholderRenderer;
303 }
304
305 } // namespace WebCore
306