forked from WebKit/WebKit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHTMLBodyElement.cpp
More file actions
202 lines (177 loc) · 8.19 KB
/
HTMLBodyElement.cpp
File metadata and controls
202 lines (177 loc) · 8.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
/*
* Copyright (C) 1999 Lars Knoll (knoll@kde.org)
* (C) 1999 Antti Koivisto (koivisto@kde.org)
* (C) 2000 Simon Hausmann (hausmann@kde.org)
* (C) 2001 Dirk Mueller (mueller@kde.org)
* Copyright (C) 2004-2021 Apple Inc. All rights reserved.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public License
* along with this library; see the file COPYING.LIB. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#include "config.h"
#include "HTMLBodyElement.h"
#include "CSSImageValue.h"
#include "CSSParser.h"
#include "CSSValueKeywords.h"
#include "DOMWindow.h"
#include "DOMWrapperWorld.h"
#include "ElementInlines.h"
#include "EventNames.h"
#include "HTMLFrameElement.h"
#include "HTMLIFrameElement.h"
#include "HTMLNames.h"
#include "HTMLParserIdioms.h"
#include "JSHTMLBodyElement.h"
#include "StyleProperties.h"
#include <wtf/IsoMallocInlines.h>
#include <wtf/NeverDestroyed.h>
namespace WebCore {
WTF_MAKE_ISO_ALLOCATED_IMPL(HTMLBodyElement);
using namespace HTMLNames;
HTMLBodyElement::HTMLBodyElement(const QualifiedName& tagName, Document& document)
: HTMLElement(tagName, document)
{
ASSERT(hasTagName(bodyTag));
}
Ref<HTMLBodyElement> HTMLBodyElement::create(Document& document)
{
return adoptRef(*new HTMLBodyElement(bodyTag, document));
}
Ref<HTMLBodyElement> HTMLBodyElement::create(const QualifiedName& tagName, Document& document)
{
return adoptRef(*new HTMLBodyElement(tagName, document));
}
HTMLBodyElement::~HTMLBodyElement() = default;
bool HTMLBodyElement::hasPresentationalHintsForAttribute(const QualifiedName& name) const
{
if (name == backgroundAttr || name == marginwidthAttr || name == leftmarginAttr || name == marginheightAttr || name == topmarginAttr || name == bgcolorAttr || name == textAttr || name == bgpropertiesAttr)
return true;
return HTMLElement::hasPresentationalHintsForAttribute(name);
}
void HTMLBodyElement::collectPresentationalHintsForAttribute(const QualifiedName& name, const AtomString& value, MutableStyleProperties& style)
{
if (name == backgroundAttr) {
String url = stripLeadingAndTrailingHTMLSpaces(value);
if (!url.isEmpty()) {
auto imageValue = CSSImageValue::create(document().completeURL(url), LoadedFromOpaqueSource::No);
imageValue.get().setInitiator(localName());
style.setProperty(CSSProperty(CSSPropertyBackgroundImage, WTFMove(imageValue)));
}
} else if (name == marginwidthAttr || name == leftmarginAttr) {
addHTMLLengthToStyle(style, CSSPropertyMarginRight, value);
addHTMLLengthToStyle(style, CSSPropertyMarginLeft, value);
} else if (name == marginheightAttr || name == topmarginAttr) {
addHTMLLengthToStyle(style, CSSPropertyMarginBottom, value);
addHTMLLengthToStyle(style, CSSPropertyMarginTop, value);
} else if (name == bgcolorAttr) {
addHTMLColorToStyle(style, CSSPropertyBackgroundColor, value);
} else if (name == textAttr) {
addHTMLColorToStyle(style, CSSPropertyColor, value);
} else if (name == bgpropertiesAttr) {
if (equalLettersIgnoringASCIICase(value, "fixed"_s))
addPropertyToPresentationalHintStyle(style, CSSPropertyBackgroundAttachment, CSSValueFixed);
} else
HTMLElement::collectPresentationalHintsForAttribute(name, value, style);
}
const AtomString& HTMLBodyElement::eventNameForWindowEventHandlerAttribute(const QualifiedName& attributeName)
{
static NeverDestroyed map = [] {
EventHandlerNameMap map;
JSHTMLBodyElement::forEachWindowEventHandlerContentAttribute([&] (const AtomString& attributeName, const AtomString& eventName) {
// FIXME: Remove these special cases. These have has an [WindowEventHandler] line in the IDL but were not in this map before, so this preserves behavior.
if (attributeName == onrejectionhandledAttr.get().localName() || attributeName == onunhandledrejectionAttr.get().localName())
return;
map.add(attributeName.impl(), eventName);
});
return map;
}();
return eventNameForEventHandlerAttribute(attributeName, map);
}
void HTMLBodyElement::parseAttribute(const QualifiedName& name, const AtomString& value)
{
if (name == vlinkAttr || name == alinkAttr || name == linkAttr) {
if (value.isNull()) {
if (name == linkAttr)
document().resetLinkColor();
else if (name == vlinkAttr)
document().resetVisitedLinkColor();
else
document().resetActiveLinkColor();
} else {
Color color = CSSParser::parseColorWithoutContext(value, !document().inQuirksMode());
if (color.isValid()) {
if (name == linkAttr)
document().setLinkColor(color);
else if (name == vlinkAttr)
document().setVisitedLinkColor(color);
else
document().setActiveLinkColor(color);
}
}
invalidateStyleForSubtree();
return;
}
// FIXME: Emit "selectionchange" event at <input> / <textarea> elements and remove this special-case.
// https://bugs.webkit.org/show_bug.cgi?id=234348
if (name == onselectionchangeAttr) {
document().setAttributeEventListener(eventNames().selectionchangeEvent, name, value, mainThreadNormalWorld());
return;
}
auto& eventName = eventNameForWindowEventHandlerAttribute(name);
if (!eventName.isNull()) {
document().setWindowAttributeEventListener(eventName, name, value, mainThreadNormalWorld());
return;
}
HTMLElement::parseAttribute(name, value);
}
Node::InsertedIntoAncestorResult HTMLBodyElement::insertedIntoAncestor(InsertionType insertionType, ContainerNode& parentOfInsertedTree)
{
HTMLElement::insertedIntoAncestor(insertionType, parentOfInsertedTree);
if (!insertionType.connectedToDocument)
return InsertedIntoAncestorResult::Done;
if (!is<HTMLFrameElementBase>(document().ownerElement()))
return InsertedIntoAncestorResult::Done;
return InsertedIntoAncestorResult::NeedsPostInsertionCallback;
}
void HTMLBodyElement::didFinishInsertingNode()
{
ASSERT(is<HTMLFrameElementBase>(document().ownerElement()));
Ref ownerElement = *document().ownerElement();
// FIXME: It's surprising this is web compatible since it means marginwidth and marginheight attributes
// appear or get overwritten on body elements of a document embedded through <iframe> or <frame>.
// Better to find a way to do addHTMLLengthToStyle based on the attributes from the frame element,
// without modifying the body element's attributes. Could also add code so we can respond to updates
// to the frame element attributes.
auto marginWidth = ownerElement->attributeWithoutSynchronization(marginwidthAttr);
if (!marginWidth.isNull())
setAttributeWithoutSynchronization(marginwidthAttr, marginWidth);
auto marginHeight = ownerElement->attributeWithoutSynchronization(marginheightAttr);
if (!marginHeight.isNull())
setAttributeWithoutSynchronization(marginheightAttr, marginHeight);
}
bool HTMLBodyElement::isURLAttribute(const Attribute& attribute) const
{
return attribute.name() == backgroundAttr || HTMLElement::isURLAttribute(attribute);
}
bool HTMLBodyElement::supportsFocus() const
{
return hasEditableStyle() || HTMLElement::supportsFocus();
}
void HTMLBodyElement::addSubresourceAttributeURLs(ListHashSet<URL>& urls) const
{
HTMLElement::addSubresourceAttributeURLs(urls);
addSubresourceURL(urls, document().completeURL(attributeWithoutSynchronization(backgroundAttr)));
}
} // namespace WebCore