|
1 /** |
|
2 * Copyright (C) 1999 Lars Knoll (knoll@kde.org) |
|
3 * (C) 1999 Antti Koivisto (koivisto@kde.org) |
|
4 * (C) 2000 Simon Hausmann (hausmann@kde.org) |
|
5 * (C) 2001 Dirk Mueller (mueller@kde.org) |
|
6 * Copyright (C) 2004, 2006, 2007 Apple Inc. All rights reserved. |
|
7 * |
|
8 * This library is free software; you can redistribute it and/or |
|
9 * modify it under the terms of the GNU Library General Public |
|
10 * License as published by the Free Software Foundation; either |
|
11 * version 2 of the License, or (at your option) any later version. |
|
12 * |
|
13 * This library is distributed in the hope that it will be useful, |
|
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
16 * Library General Public License for more details. |
|
17 * |
|
18 * You should have received a copy of the GNU Library General Public License |
|
19 * along with this library; see the file COPYING.LIB. If not, write to |
|
20 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
|
21 * Boston, MA 02110-1301, USA. |
|
22 */ |
|
23 |
|
24 #include "config.h" |
|
25 #include "HTMLBodyElement.h" |
|
26 |
|
27 #include "CSSHelper.h" |
|
28 #include "CSSMutableStyleDeclaration.h" |
|
29 #include "CSSPropertyNames.h" |
|
30 #include "CSSStyleSelector.h" |
|
31 #include "CSSStyleSheet.h" |
|
32 #include "CSSValueKeywords.h" |
|
33 #include "Document.h" |
|
34 #include "EventNames.h" |
|
35 #include "FrameView.h" |
|
36 #include "HTMLFrameElementBase.h" |
|
37 #include "HTMLNames.h" |
|
38 |
|
39 namespace WebCore { |
|
40 |
|
41 using namespace EventNames; |
|
42 using namespace HTMLNames; |
|
43 |
|
44 HTMLBodyElement::HTMLBodyElement(Document* doc) |
|
45 : HTMLElement(bodyTag, doc) |
|
46 { |
|
47 } |
|
48 |
|
49 HTMLBodyElement::~HTMLBodyElement() |
|
50 { |
|
51 if (m_linkDecl) { |
|
52 m_linkDecl->setNode(0); |
|
53 m_linkDecl->setParent(0); |
|
54 } |
|
55 } |
|
56 |
|
57 void HTMLBodyElement::createLinkDecl() |
|
58 { |
|
59 m_linkDecl = new CSSMutableStyleDeclaration; |
|
60 m_linkDecl->setParent(document()->elementSheet()); |
|
61 m_linkDecl->setNode(this); |
|
62 m_linkDecl->setStrictParsing(!document()->inCompatMode()); |
|
63 } |
|
64 |
|
65 bool HTMLBodyElement::mapToEntry(const QualifiedName& attrName, MappedAttributeEntry& result) const |
|
66 { |
|
67 if (attrName == backgroundAttr) { |
|
68 result = (MappedAttributeEntry)(eLastEntry + document()->docID()); |
|
69 return false; |
|
70 } |
|
71 |
|
72 if (attrName == bgcolorAttr || |
|
73 attrName == textAttr || |
|
74 attrName == marginwidthAttr || |
|
75 attrName == leftmarginAttr || |
|
76 attrName == marginheightAttr || |
|
77 attrName == topmarginAttr || |
|
78 attrName == bgpropertiesAttr) { |
|
79 result = eUniversal; |
|
80 return false; |
|
81 } |
|
82 |
|
83 return HTMLElement::mapToEntry(attrName, result); |
|
84 } |
|
85 |
|
86 void HTMLBodyElement::parseMappedAttribute(MappedAttribute *attr) |
|
87 { |
|
88 if (attr->name() == backgroundAttr) { |
|
89 String url = parseURL(attr->value()); |
|
90 if (!url.isEmpty()) |
|
91 addCSSImageProperty(attr, CSS_PROP_BACKGROUND_IMAGE, document()->completeURL(url)); |
|
92 } else if (attr->name() == marginwidthAttr || attr->name() == leftmarginAttr) { |
|
93 addCSSLength(attr, CSS_PROP_MARGIN_RIGHT, attr->value()); |
|
94 addCSSLength(attr, CSS_PROP_MARGIN_LEFT, attr->value()); |
|
95 } else if (attr->name() == marginheightAttr || attr->name() == topmarginAttr) { |
|
96 addCSSLength(attr, CSS_PROP_MARGIN_BOTTOM, attr->value()); |
|
97 addCSSLength(attr, CSS_PROP_MARGIN_TOP, attr->value()); |
|
98 } else if (attr->name() == bgcolorAttr) { |
|
99 addCSSColor(attr, CSS_PROP_BACKGROUND_COLOR, attr->value()); |
|
100 } else if (attr->name() == textAttr) { |
|
101 addCSSColor(attr, CSS_PROP_COLOR, attr->value()); |
|
102 } else if (attr->name() == bgpropertiesAttr) { |
|
103 if (equalIgnoringCase(attr->value(), "fixed")) |
|
104 addCSSProperty(attr, CSS_PROP_BACKGROUND_ATTACHMENT, CSS_VAL_FIXED); |
|
105 } else if (attr->name() == vlinkAttr || |
|
106 attr->name() == alinkAttr || |
|
107 attr->name() == linkAttr) { |
|
108 if (attr->isNull()) { |
|
109 if (attr->name() == linkAttr) |
|
110 document()->resetLinkColor(); |
|
111 else if (attr->name() == vlinkAttr) |
|
112 document()->resetVisitedLinkColor(); |
|
113 else |
|
114 document()->resetActiveLinkColor(); |
|
115 } else { |
|
116 if (!m_linkDecl) |
|
117 createLinkDecl(); |
|
118 m_linkDecl->setProperty(CSS_PROP_COLOR, attr->value(), false, false); |
|
119 RefPtr<CSSValue> val = m_linkDecl->getPropertyCSSValue(CSS_PROP_COLOR); |
|
120 if (val && val->isPrimitiveValue()) { |
|
121 Color col = document()->styleSelector()->getColorFromPrimitiveValue(static_cast<CSSPrimitiveValue*>(val.get())); |
|
122 if (attr->name() == linkAttr) |
|
123 document()->setLinkColor(col); |
|
124 else if (attr->name() == vlinkAttr) |
|
125 document()->setVisitedLinkColor(col); |
|
126 else |
|
127 document()->setActiveLinkColor(col); |
|
128 } |
|
129 } |
|
130 |
|
131 if (attached()) |
|
132 document()->recalcStyle(Force); |
|
133 } else if (attr->name() == onloadAttr) { |
|
134 document()->setHTMLWindowEventListener(loadEvent, attr); |
|
135 } else if (attr->name() == onbeforeunloadAttr) { |
|
136 document()->setHTMLWindowEventListener(beforeunloadEvent, attr); |
|
137 } else if (attr->name() == onunloadAttr) { |
|
138 document()->setHTMLWindowEventListener(unloadEvent, attr); |
|
139 } else if (attr->name() == onblurAttr) { |
|
140 document()->setHTMLWindowEventListener(blurEvent, attr); |
|
141 } else if (attr->name() == onfocusAttr) { |
|
142 document()->setHTMLWindowEventListener(focusEvent, attr); |
|
143 } else if (attr->name() == onresizeAttr) { |
|
144 document()->setHTMLWindowEventListener(resizeEvent, attr); |
|
145 } else if (attr->name() == onscrollAttr) { |
|
146 document()->setHTMLWindowEventListener(scrollEvent, attr); |
|
147 } else |
|
148 HTMLElement::parseMappedAttribute(attr); |
|
149 } |
|
150 |
|
151 void HTMLBodyElement::insertedIntoDocument() |
|
152 { |
|
153 HTMLElement::insertedIntoDocument(); |
|
154 |
|
155 // FIXME: Perhaps this code should be in attach() instead of here. |
|
156 Element* ownerElement = document()->ownerElement(); |
|
157 if (ownerElement && (ownerElement->hasTagName(frameTag) || ownerElement->hasTagName(iframeTag))) { |
|
158 HTMLFrameElementBase* ownerFrameElement = static_cast<HTMLFrameElementBase*>(ownerElement); |
|
159 int marginWidth = ownerFrameElement->getMarginWidth(); |
|
160 if (marginWidth != -1) |
|
161 setAttribute(marginwidthAttr, String::number(marginWidth)); |
|
162 int marginHeight = ownerFrameElement->getMarginHeight(); |
|
163 if (marginHeight != -1) |
|
164 setAttribute(marginheightAttr, String::number(marginHeight)); |
|
165 } |
|
166 |
|
167 // FIXME: This call to scheduleRelayout should not be needed here. |
|
168 // But without it we hang during WebKit tests; need to fix that and remove this. |
|
169 if (FrameView* view = document()->view()) |
|
170 view->scheduleRelayout(); |
|
171 } |
|
172 |
|
173 bool HTMLBodyElement::isURLAttribute(Attribute *attr) const |
|
174 { |
|
175 return attr->name() == backgroundAttr; |
|
176 } |
|
177 |
|
178 String HTMLBodyElement::aLink() const |
|
179 { |
|
180 return getAttribute(alinkAttr); |
|
181 } |
|
182 |
|
183 void HTMLBodyElement::setALink(const String &value) |
|
184 { |
|
185 setAttribute(alinkAttr, value); |
|
186 } |
|
187 |
|
188 String HTMLBodyElement::background() const |
|
189 { |
|
190 return getAttribute(backgroundAttr); |
|
191 } |
|
192 |
|
193 void HTMLBodyElement::setBackground(const String &value) |
|
194 { |
|
195 setAttribute(backgroundAttr, value); |
|
196 } |
|
197 |
|
198 String HTMLBodyElement::bgColor() const |
|
199 { |
|
200 return getAttribute(bgcolorAttr); |
|
201 } |
|
202 |
|
203 void HTMLBodyElement::setBgColor(const String &value) |
|
204 { |
|
205 setAttribute(bgcolorAttr, value); |
|
206 } |
|
207 |
|
208 String HTMLBodyElement::link() const |
|
209 { |
|
210 return getAttribute(linkAttr); |
|
211 } |
|
212 |
|
213 void HTMLBodyElement::setLink(const String &value) |
|
214 { |
|
215 setAttribute(linkAttr, value); |
|
216 } |
|
217 |
|
218 String HTMLBodyElement::text() const |
|
219 { |
|
220 return getAttribute(textAttr); |
|
221 } |
|
222 |
|
223 void HTMLBodyElement::setText(const String &value) |
|
224 { |
|
225 setAttribute(textAttr, value); |
|
226 } |
|
227 |
|
228 String HTMLBodyElement::vLink() const |
|
229 { |
|
230 return getAttribute(vlinkAttr); |
|
231 } |
|
232 |
|
233 void HTMLBodyElement::setVLink(const String &value) |
|
234 { |
|
235 setAttribute(vlinkAttr, value); |
|
236 } |
|
237 |
|
238 int HTMLBodyElement::scrollLeft() const |
|
239 { |
|
240 // Update the document's layout. |
|
241 Document* doc = document(); |
|
242 doc->updateLayoutIgnorePendingStylesheets(); |
|
243 FrameView* view = doc->view(); |
|
244 |
|
245 return view ? view->contentsX() : 0; |
|
246 } |
|
247 |
|
248 void HTMLBodyElement::setScrollLeft(int scrollLeft) |
|
249 { |
|
250 FrameView* sview = ownerDocument()->view(); |
|
251 if (sview) { |
|
252 // Update the document's layout |
|
253 document()->updateLayoutIgnorePendingStylesheets(); |
|
254 sview->setContentsPos(scrollLeft, sview->contentsY()); |
|
255 } |
|
256 } |
|
257 |
|
258 int HTMLBodyElement::scrollTop() const |
|
259 { |
|
260 // Update the document's layout. |
|
261 Document* doc = document(); |
|
262 doc->updateLayoutIgnorePendingStylesheets(); |
|
263 FrameView* view = doc->view(); |
|
264 |
|
265 return view ? view->contentsY() : 0; |
|
266 } |
|
267 |
|
268 void HTMLBodyElement::setScrollTop(int scrollTop) |
|
269 { |
|
270 FrameView* sview = ownerDocument()->view(); |
|
271 if (sview) { |
|
272 // Update the document's layout |
|
273 document()->updateLayoutIgnorePendingStylesheets(); |
|
274 sview->setContentsPos(sview->contentsX(), scrollTop); |
|
275 } |
|
276 } |
|
277 |
|
278 int HTMLBodyElement::scrollHeight() const |
|
279 { |
|
280 // Update the document's layout. |
|
281 Document* doc = document(); |
|
282 doc->updateLayoutIgnorePendingStylesheets(); |
|
283 FrameView* view = doc->view(); |
|
284 |
|
285 return view ? view->contentsHeight() : 0; |
|
286 } |
|
287 |
|
288 int HTMLBodyElement::scrollWidth() const |
|
289 { |
|
290 // Update the document's layout. |
|
291 Document* doc = document(); |
|
292 doc->updateLayoutIgnorePendingStylesheets(); |
|
293 FrameView* view = doc->view(); |
|
294 |
|
295 return view ? view->contentsWidth() : 0; |
|
296 } |
|
297 |
|
298 } |