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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "HTMLBodyElement.h"
#include "mozilla/dom/HTMLBodyElementBinding.h"
#include "nsAttrValueInlines.h"
#include "nsGkAtoms.h"
#include "nsStyleConsts.h"
#include "nsPresContext.h"
#include "nsIPresShell.h"
#include "nsIDocument.h"
#include "nsHTMLStyleSheet.h"
#include "nsIEditor.h"
#include "nsMappedAttributes.h"
#include "nsRuleData.h"
#include "nsIDocShell.h"
#include "nsRuleWalker.h"
#include "nsGlobalWindow.h"
NS_IMPL_NS_NEW_HTML_ELEMENT(Body)
namespace mozilla {
namespace dom {
//----------------------------------------------------------------------
BodyRule::BodyRule(HTMLBodyElement* aPart)
: mPart(aPart)
{
}
BodyRule::~BodyRule()
{
}
NS_IMPL_ISUPPORTS(BodyRule, nsIStyleRule)
/* virtual */ void
BodyRule::MapRuleInfoInto(nsRuleData* aData)
{
if (!(aData->mSIDs & NS_STYLE_INHERIT_BIT(Margin)) || !mPart)
return; // We only care about margins.
int32_t bodyMarginWidth = -1;
int32_t bodyMarginHeight = -1;
int32_t bodyTopMargin = -1;
int32_t bodyBottomMargin = -1;
int32_t bodyLeftMargin = -1;
int32_t bodyRightMargin = -1;
// check the mode (fortunately, the ruleData has a presContext for us to use!)
NS_ASSERTION(aData->mPresContext, "null presContext in ruleNode was unexpected");
nsCompatibility mode = aData->mPresContext->CompatibilityMode();
const nsAttrValue* value;
if (mPart->GetAttrCount() > 0) {
// if marginwidth/marginheight are set, reflect them as 'margin'
value = mPart->GetParsedAttr(nsGkAtoms::marginwidth);
if (value && value->Type() == nsAttrValue::eInteger) {
bodyMarginWidth = value->GetIntegerValue();
if (bodyMarginWidth < 0) bodyMarginWidth = 0;
nsCSSValue* marginLeft = aData->ValueForMarginLeft();
if (marginLeft->GetUnit() == eCSSUnit_Null)
marginLeft->SetFloatValue((float)bodyMarginWidth, eCSSUnit_Pixel);
nsCSSValue* marginRight = aData->ValueForMarginRight();
if (marginRight->GetUnit() == eCSSUnit_Null)
marginRight->SetFloatValue((float)bodyMarginWidth, eCSSUnit_Pixel);
}
value = mPart->GetParsedAttr(nsGkAtoms::marginheight);
if (value && value->Type() == nsAttrValue::eInteger) {
bodyMarginHeight = value->GetIntegerValue();
if (bodyMarginHeight < 0) bodyMarginHeight = 0;
nsCSSValue* marginTop = aData->ValueForMarginTop();
if (marginTop->GetUnit() == eCSSUnit_Null)
marginTop->SetFloatValue((float)bodyMarginHeight, eCSSUnit_Pixel);
nsCSSValue* marginBottom = aData->ValueForMarginBottom();
if (marginBottom->GetUnit() == eCSSUnit_Null)
marginBottom->SetFloatValue((float)bodyMarginHeight, eCSSUnit_Pixel);
}
// topmargin (IE-attribute)
value = mPart->GetParsedAttr(nsGkAtoms::topmargin);
if (value && value->Type() == nsAttrValue::eInteger) {
bodyTopMargin = value->GetIntegerValue();
if (bodyTopMargin < 0) bodyTopMargin = 0;
nsCSSValue* marginTop = aData->ValueForMarginTop();
if (marginTop->GetUnit() == eCSSUnit_Null)
marginTop->SetFloatValue((float)bodyTopMargin, eCSSUnit_Pixel);
}
// bottommargin (IE-attribute)
value = mPart->GetParsedAttr(nsGkAtoms::bottommargin);
if (value && value->Type() == nsAttrValue::eInteger) {
bodyBottomMargin = value->GetIntegerValue();
if (bodyBottomMargin < 0) bodyBottomMargin = 0;
nsCSSValue* marginBottom = aData->ValueForMarginBottom();
if (marginBottom->GetUnit() == eCSSUnit_Null)
marginBottom->SetFloatValue((float)bodyBottomMargin, eCSSUnit_Pixel);
}
// leftmargin (IE-attribute)
value = mPart->GetParsedAttr(nsGkAtoms::leftmargin);
if (value && value->Type() == nsAttrValue::eInteger) {
bodyLeftMargin = value->GetIntegerValue();
if (bodyLeftMargin < 0) bodyLeftMargin = 0;
nsCSSValue* marginLeft = aData->ValueForMarginLeft();
if (marginLeft->GetUnit() == eCSSUnit_Null)
marginLeft->SetFloatValue((float)bodyLeftMargin, eCSSUnit_Pixel);
}
// rightmargin (IE-attribute)
value = mPart->GetParsedAttr(nsGkAtoms::rightmargin);
if (value && value->Type() == nsAttrValue::eInteger) {
bodyRightMargin = value->GetIntegerValue();
if (bodyRightMargin < 0) bodyRightMargin = 0;
nsCSSValue* marginRight = aData->ValueForMarginRight();
if (marginRight->GetUnit() == eCSSUnit_Null)
marginRight->SetFloatValue((float)bodyRightMargin, eCSSUnit_Pixel);
}
}
// if marginwidth or marginheight is set in the <frame> and not set in the <body>
// reflect them as margin in the <body>
if (bodyMarginWidth == -1 || bodyMarginHeight == -1) {
nsCOMPtr<nsIDocShell> docShell(aData->mPresContext->GetDocShell());
if (docShell) {
nscoord frameMarginWidth=-1; // default value
nscoord frameMarginHeight=-1; // default value
docShell->GetMarginWidth(&frameMarginWidth); // -1 indicates not set
docShell->GetMarginHeight(&frameMarginHeight);
if ((frameMarginWidth >= 0) && (bodyMarginWidth == -1)) { // set in <frame> & not in <body>
if (eCompatibility_NavQuirks == mode) {
if ((bodyMarginHeight == -1) && (0 > frameMarginHeight)) // nav quirk
frameMarginHeight = 0;
}
}
if ((frameMarginHeight >= 0) && (bodyMarginHeight == -1)) { // set in <frame> & not in <body>
if (eCompatibility_NavQuirks == mode) {
if ((bodyMarginWidth == -1) && (0 > frameMarginWidth)) // nav quirk
frameMarginWidth = 0;
}
}
if ((bodyMarginWidth == -1) && (frameMarginWidth >= 0)) {
nsCSSValue* marginLeft = aData->ValueForMarginLeft();
if (marginLeft->GetUnit() == eCSSUnit_Null)
marginLeft->SetFloatValue((float)frameMarginWidth, eCSSUnit_Pixel);
nsCSSValue* marginRight = aData->ValueForMarginRight();
if (marginRight->GetUnit() == eCSSUnit_Null)
marginRight->SetFloatValue((float)frameMarginWidth, eCSSUnit_Pixel);
}
if ((bodyMarginHeight == -1) && (frameMarginHeight >= 0)) {
nsCSSValue* marginTop = aData->ValueForMarginTop();
if (marginTop->GetUnit() == eCSSUnit_Null)
marginTop->SetFloatValue((float)frameMarginHeight, eCSSUnit_Pixel);
nsCSSValue* marginBottom = aData->ValueForMarginBottom();
if (marginBottom->GetUnit() == eCSSUnit_Null)
marginBottom->SetFloatValue((float)frameMarginHeight, eCSSUnit_Pixel);
}
}
}
}
/* virtual */ bool
BodyRule::MightMapInheritedStyleData()
{
return false;
}
/* virtual */ bool
BodyRule::GetDiscretelyAnimatedCSSValue(nsCSSPropertyID aProperty,
nsCSSValue* aValue)
{
MOZ_ASSERT(false, "GetDiscretelyAnimatedCSSValue is not implemented yet");
return false;
}
#ifdef DEBUG
/* virtual */ void
BodyRule::List(FILE* out, int32_t aIndent) const
{
nsAutoCString indent;
for (int32_t index = aIndent; --index >= 0; ) {
indent.AppendLiteral(" ");
}
fprintf_stderr(out, "%s[body rule] {}\n", indent.get());
}
#endif
//----------------------------------------------------------------------
HTMLBodyElement::~HTMLBodyElement()
{
if (mContentStyleRule) {
mContentStyleRule->mPart = nullptr;
}
}
JSObject*
HTMLBodyElement::WrapNode(JSContext *aCx, JS::Handle<JSObject*> aGivenProto)
{
return HTMLBodyElementBinding::Wrap(aCx, this, aGivenProto);
}
NS_IMPL_ISUPPORTS_INHERITED(HTMLBodyElement, nsGenericHTMLElement,
nsIDOMHTMLBodyElement)
NS_IMPL_ELEMENT_CLONE(HTMLBodyElement)
NS_IMETHODIMP
HTMLBodyElement::SetBackground(const nsAString& aBackground)
{
ErrorResult rv;
SetBackground(aBackground, rv);
return rv.StealNSResult();
}
NS_IMETHODIMP
HTMLBodyElement::GetBackground(nsAString& aBackground)
{
DOMString background;
GetBackground(background);
background.ToString(aBackground);
return NS_OK;
}
NS_IMETHODIMP
HTMLBodyElement::SetVLink(const nsAString& aVLink)
{
ErrorResult rv;
SetVLink(aVLink, rv);
return rv.StealNSResult();
}
NS_IMETHODIMP
HTMLBodyElement::GetVLink(nsAString& aVLink)
{
DOMString vLink;
GetVLink(vLink);
vLink.ToString(aVLink);
return NS_OK;
}
NS_IMETHODIMP
HTMLBodyElement::SetALink(const nsAString& aALink)
{
ErrorResult rv;
SetALink(aALink, rv);
return rv.StealNSResult();
}
NS_IMETHODIMP
HTMLBodyElement::GetALink(nsAString& aALink)
{
DOMString aLink;
GetALink(aLink);
aLink.ToString(aALink);
return NS_OK;
}
NS_IMETHODIMP
HTMLBodyElement::SetLink(const nsAString& aLink)
{
ErrorResult rv;
SetLink(aLink, rv);
return rv.StealNSResult();
}
NS_IMETHODIMP
HTMLBodyElement::GetLink(nsAString& aLink)
{
DOMString link;
GetLink(link);
link.ToString(aLink);
return NS_OK;
}
NS_IMETHODIMP
HTMLBodyElement::SetText(const nsAString& aText)
{
ErrorResult rv;
SetText(aText, rv);
return rv.StealNSResult();
}
NS_IMETHODIMP
HTMLBodyElement::GetText(nsAString& aText)
{
DOMString text;
GetText(text);
text.ToString(aText);
return NS_OK;
}
NS_IMETHODIMP
HTMLBodyElement::SetBgColor(const nsAString& aBgColor)
{
ErrorResult rv;
SetBgColor(aBgColor, rv);
return rv.StealNSResult();
}
NS_IMETHODIMP
HTMLBodyElement::GetBgColor(nsAString& aBgColor)
{
DOMString bgColor;
GetBgColor(bgColor);
bgColor.ToString(aBgColor);
return NS_OK;
}
bool
HTMLBodyElement::ParseAttribute(int32_t aNamespaceID,
nsIAtom* aAttribute,
const nsAString& aValue,
nsAttrValue& aResult)
{
if (aNamespaceID == kNameSpaceID_None) {
if (aAttribute == nsGkAtoms::bgcolor ||
aAttribute == nsGkAtoms::text ||
aAttribute == nsGkAtoms::link ||
aAttribute == nsGkAtoms::alink ||
aAttribute == nsGkAtoms::vlink) {
return aResult.ParseColor(aValue);
}
if (aAttribute == nsGkAtoms::marginwidth ||
aAttribute == nsGkAtoms::marginheight ||
aAttribute == nsGkAtoms::topmargin ||
aAttribute == nsGkAtoms::bottommargin ||
aAttribute == nsGkAtoms::leftmargin ||
aAttribute == nsGkAtoms::rightmargin) {
return aResult.ParseIntWithBounds(aValue, 0);
}
}
return nsGenericHTMLElement::ParseBackgroundAttribute(aNamespaceID,
aAttribute, aValue,
aResult) ||
nsGenericHTMLElement::ParseAttribute(aNamespaceID, aAttribute, aValue,
aResult);
}
void
HTMLBodyElement::UnbindFromTree(bool aDeep, bool aNullParent)
{
if (mContentStyleRule) {
mContentStyleRule->mPart = nullptr;
mContentStyleRule = nullptr;
}
nsGenericHTMLElement::UnbindFromTree(aDeep, aNullParent);
}
void
HTMLBodyElement::MapAttributesIntoRule(const nsMappedAttributes* aAttributes,
nsRuleData* aData)
{
if (aData->mSIDs & NS_STYLE_INHERIT_BIT(Display)) {
// When display if first asked for, go ahead and get our colors set up.
nsIPresShell *presShell = aData->mPresContext->GetPresShell();
if (presShell) {
nsIDocument *doc = presShell->GetDocument();
if (doc) {
nsHTMLStyleSheet* styleSheet = doc->GetAttributeStyleSheet();
if (styleSheet) {
const nsAttrValue* value;
nscolor color;
value = aAttributes->GetAttr(nsGkAtoms::link);
if (value && value->GetColorValue(color)) {
styleSheet->SetLinkColor(color);
}
value = aAttributes->GetAttr(nsGkAtoms::alink);
if (value && value->GetColorValue(color)) {
styleSheet->SetActiveLinkColor(color);
}
value = aAttributes->GetAttr(nsGkAtoms::vlink);
if (value && value->GetColorValue(color)) {
styleSheet->SetVisitedLinkColor(color);
}
}
}
}
}
if (aData->mSIDs & NS_STYLE_INHERIT_BIT(Color)) {
nsCSSValue *colorValue = aData->ValueForColor();
if (colorValue->GetUnit() == eCSSUnit_Null &&
aData->mPresContext->UseDocumentColors()) {
// color: color
nscolor color;
const nsAttrValue* value = aAttributes->GetAttr(nsGkAtoms::text);
if (value && value->GetColorValue(color))
colorValue->SetColorValue(color);
}
}
nsGenericHTMLElement::MapBackgroundAttributesInto(aAttributes, aData);
nsGenericHTMLElement::MapCommonAttributesInto(aAttributes, aData);
}
nsMapRuleToAttributesFunc
HTMLBodyElement::GetAttributeMappingFunction() const
{
return &MapAttributesIntoRule;
}
NS_IMETHODIMP
HTMLBodyElement::WalkContentStyleRules(nsRuleWalker* aRuleWalker)
{
nsGenericHTMLElement::WalkContentStyleRules(aRuleWalker);
if (!mContentStyleRule && IsInUncomposedDoc()) {
// XXXbz should this use OwnerDoc() or GetComposedDoc()?
// sXBL/XBL2 issue!
mContentStyleRule = new BodyRule(this);
}
if (aRuleWalker && mContentStyleRule) {
aRuleWalker->Forward(mContentStyleRule);
}
return NS_OK;
}
NS_IMETHODIMP_(bool)
HTMLBodyElement::IsAttributeMapped(const nsIAtom* aAttribute) const
{
static const MappedAttributeEntry attributes[] = {
{ &nsGkAtoms::link },
{ &nsGkAtoms::vlink },
{ &nsGkAtoms::alink },
{ &nsGkAtoms::text },
// These aren't mapped through attribute mapping, but they are
// mapped through a style rule, so it is attribute dependent style.
// XXXldb But we don't actually replace the body rule when we have
// dynamic changes...
{ &nsGkAtoms::marginwidth },
{ &nsGkAtoms::marginheight },
{ nullptr },
};
static const MappedAttributeEntry* const map[] = {
attributes,
sCommonAttributeMap,
sBackgroundAttributeMap,
};
return FindAttributeDependence(aAttribute, map);
}
already_AddRefed<nsIEditor>
HTMLBodyElement::GetAssociatedEditor()
{
nsCOMPtr<nsIEditor> editor = GetEditorInternal();
if (editor) {
return editor.forget();
}
// Make sure this is the actual body of the document
if (!IsCurrentBodyElement()) {
return nullptr;
}
// For designmode, try to get document's editor
nsPresContext* presContext = GetPresContext(eForComposedDoc);
if (!presContext) {
return nullptr;
}
nsCOMPtr<nsIDocShell> docShell = presContext->GetDocShell();
if (!docShell) {
return nullptr;
}
docShell->GetEditor(getter_AddRefs(editor));
return editor.forget();
}
bool
HTMLBodyElement::IsEventAttributeName(nsIAtom *aName)
{
return nsContentUtils::IsEventAttributeName(aName,
EventNameType_HTML |
EventNameType_HTMLBodyOrFramesetOnly);
}
#define EVENT(name_, id_, type_, struct_) /* nothing; handled by the superclass */
// nsGenericHTMLElement::GetOnError returns
// already_AddRefed<EventHandlerNonNull> while other getters return
// EventHandlerNonNull*, so allow passing in the type to use here.
#define WINDOW_EVENT_HELPER(name_, type_) \
type_* \
HTMLBodyElement::GetOn##name_() \
{ \
if (nsPIDOMWindowInner* win = OwnerDoc()->GetInnerWindow()) { \
nsGlobalWindow* globalWin = nsGlobalWindow::Cast(win); \
return globalWin->GetOn##name_(); \
} \
return nullptr; \
} \
void \
HTMLBodyElement::SetOn##name_(type_* handler) \
{ \
nsPIDOMWindowInner* win = OwnerDoc()->GetInnerWindow(); \
if (!win) { \
return; \
} \
\
nsGlobalWindow* globalWin = nsGlobalWindow::Cast(win); \
return globalWin->SetOn##name_(handler); \
}
#define WINDOW_EVENT(name_, id_, type_, struct_) \
WINDOW_EVENT_HELPER(name_, EventHandlerNonNull)
#define BEFOREUNLOAD_EVENT(name_, id_, type_, struct_) \
WINDOW_EVENT_HELPER(name_, OnBeforeUnloadEventHandlerNonNull)
#include "mozilla/EventNameList.h" // IWYU pragma: keep
#undef BEFOREUNLOAD_EVENT
#undef WINDOW_EVENT
#undef WINDOW_EVENT_HELPER
#undef EVENT
} // namespace dom
} // namespace mozilla
|