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
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* 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 "mozilla/dom/BoxObject.h"
#include "nsCOMPtr.h"
#include "nsIDocument.h"
#include "nsIPresShell.h"
#include "nsPresContext.h"
#include "nsIContent.h"
#include "nsContainerFrame.h"
#include "nsIDocShell.h"
#include "nsReadableUtils.h"
#include "nsDOMClassInfoID.h"
#include "nsView.h"
#ifdef MOZ_XUL
#include "nsIDOMXULElement.h"
#else
#include "nsIDOMElement.h"
#endif
#include "nsLayoutUtils.h"
#include "nsISupportsPrimitives.h"
#include "nsSupportsPrimitives.h"
#include "mozilla/dom/Element.h"
#include "nsComponentManagerUtils.h"
#include "mozilla/dom/BoxObjectBinding.h"
// Implementation /////////////////////////////////////////////////////////////////
namespace mozilla {
namespace dom {
// Static member variable initialization
// Implement our nsISupports methods
NS_IMPL_CYCLE_COLLECTION_CLASS(BoxObject)
NS_IMPL_CYCLE_COLLECTING_ADDREF(BoxObject)
NS_IMPL_CYCLE_COLLECTING_RELEASE(BoxObject)
// QueryInterface implementation for BoxObject
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(BoxObject)
NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
NS_INTERFACE_MAP_ENTRY(nsIBoxObject)
NS_INTERFACE_MAP_ENTRY(nsPIBoxObject)
NS_INTERFACE_MAP_ENTRY(nsISupports)
NS_INTERFACE_MAP_END
NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(BoxObject)
// XXX jmorton: why aren't we unlinking mPropertyTable?
NS_IMPL_CYCLE_COLLECTION_UNLINK_PRESERVED_WRAPPER
NS_IMPL_CYCLE_COLLECTION_UNLINK_END
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN(BoxObject)
if (tmp->mPropertyTable) {
for (auto iter = tmp->mPropertyTable->Iter(); !iter.Done(); iter.Next()) {
cb.NoteXPCOMChild(iter.UserData());
}
}
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
NS_IMPL_CYCLE_COLLECTION_TRACE_WRAPPERCACHE(BoxObject)
// Constructors/Destructors
BoxObject::BoxObject()
: mContent(nullptr)
{
}
BoxObject::~BoxObject()
{
}
NS_IMETHODIMP
BoxObject::GetElement(nsIDOMElement** aResult)
{
if (mContent) {
return CallQueryInterface(mContent, aResult);
}
*aResult = nullptr;
return NS_OK;
}
// nsPIBoxObject //////////////////////////////////////////////////////////////////////////
nsresult
BoxObject::Init(nsIContent* aContent)
{
mContent = aContent;
return NS_OK;
}
void
BoxObject::Clear()
{
mPropertyTable = nullptr;
mContent = nullptr;
}
void
BoxObject::ClearCachedValues()
{
}
nsIFrame*
BoxObject::GetFrame(bool aFlushLayout)
{
nsIPresShell* shell = GetPresShell(aFlushLayout);
if (!shell)
return nullptr;
if (!aFlushLayout) {
// If we didn't flush layout when getting the presshell, we should at least
// flush to make sure our frame model is up to date.
// XXXbz should flush on document, no? Except people call this from
// frame code, maybe?
shell->FlushPendingNotifications(Flush_Frames);
}
// The flush might have killed mContent.
if (!mContent) {
return nullptr;
}
return mContent->GetPrimaryFrame();
}
nsIPresShell*
BoxObject::GetPresShell(bool aFlushLayout)
{
if (!mContent) {
return nullptr;
}
nsCOMPtr<nsIDocument> doc = mContent->GetUncomposedDoc();
if (!doc) {
return nullptr;
}
if (aFlushLayout) {
doc->FlushPendingNotifications(Flush_Layout);
}
return doc->GetShell();
}
nsresult
BoxObject::GetOffsetRect(nsIntRect& aRect)
{
aRect.SetRect(0, 0, 0, 0);
if (!mContent)
return NS_ERROR_NOT_INITIALIZED;
// Get the Frame for our content
nsIFrame* frame = GetFrame(true);
if (frame) {
// Get its origin
nsPoint origin = frame->GetPositionIgnoringScrolling();
// Find the frame parent whose content is the document element.
Element* docElement = mContent->GetComposedDoc()->GetRootElement();
nsIFrame* parent = frame->GetParent();
for (;;) {
// If we've hit the document element, break here
if (parent->GetContent() == docElement) {
break;
}
nsIFrame* next = parent->GetParent();
if (!next) {
NS_WARNING("We should have hit the document element...");
origin += parent->GetPosition();
break;
}
// Add the parent's origin to our own to get to the
// right coordinate system
origin += next->GetPositionOfChildIgnoringScrolling(parent);
parent = next;
}
// For the origin, add in the border for the frame
const nsStyleBorder* border = frame->StyleBorder();
origin.x += border->GetComputedBorderWidth(NS_SIDE_LEFT);
origin.y += border->GetComputedBorderWidth(NS_SIDE_TOP);
// And subtract out the border for the parent
const nsStyleBorder* parentBorder = parent->StyleBorder();
origin.x -= parentBorder->GetComputedBorderWidth(NS_SIDE_LEFT);
origin.y -= parentBorder->GetComputedBorderWidth(NS_SIDE_TOP);
aRect.x = nsPresContext::AppUnitsToIntCSSPixels(origin.x);
aRect.y = nsPresContext::AppUnitsToIntCSSPixels(origin.y);
// Get the union of all rectangles in this and continuation frames.
// It doesn't really matter what we use as aRelativeTo here, since
// we only care about the size. Using 'parent' might make things
// a bit faster by speeding up the internal GetOffsetTo operations.
nsRect rcFrame = nsLayoutUtils::GetAllInFlowRectsUnion(frame, parent);
aRect.width = nsPresContext::AppUnitsToIntCSSPixels(rcFrame.width);
aRect.height = nsPresContext::AppUnitsToIntCSSPixels(rcFrame.height);
}
return NS_OK;
}
nsresult
BoxObject::GetScreenPosition(nsIntPoint& aPoint)
{
aPoint.x = aPoint.y = 0;
if (!mContent)
return NS_ERROR_NOT_INITIALIZED;
nsIFrame* frame = GetFrame(true);
if (frame) {
nsIntRect rect = frame->GetScreenRect();
aPoint.x = rect.x;
aPoint.y = rect.y;
}
return NS_OK;
}
NS_IMETHODIMP
BoxObject::GetX(int32_t* aResult)
{
nsIntRect rect;
GetOffsetRect(rect);
*aResult = rect.x;
return NS_OK;
}
NS_IMETHODIMP
BoxObject::GetY(int32_t* aResult)
{
nsIntRect rect;
GetOffsetRect(rect);
*aResult = rect.y;
return NS_OK;
}
NS_IMETHODIMP
BoxObject::GetWidth(int32_t* aResult)
{
nsIntRect rect;
GetOffsetRect(rect);
*aResult = rect.width;
return NS_OK;
}
NS_IMETHODIMP
BoxObject::GetHeight(int32_t* aResult)
{
nsIntRect rect;
GetOffsetRect(rect);
*aResult = rect.height;
return NS_OK;
}
NS_IMETHODIMP
BoxObject::GetScreenX(int32_t *_retval)
{
nsIntPoint position;
nsresult rv = GetScreenPosition(position);
if (NS_FAILED(rv)) return rv;
*_retval = position.x;
return NS_OK;
}
NS_IMETHODIMP
BoxObject::GetScreenY(int32_t *_retval)
{
nsIntPoint position;
nsresult rv = GetScreenPosition(position);
if (NS_FAILED(rv)) return rv;
*_retval = position.y;
return NS_OK;
}
NS_IMETHODIMP
BoxObject::GetPropertyAsSupports(const char16_t* aPropertyName, nsISupports** aResult)
{
NS_ENSURE_ARG(aPropertyName && *aPropertyName);
if (!mPropertyTable) {
*aResult = nullptr;
return NS_OK;
}
nsDependentString propertyName(aPropertyName);
mPropertyTable->Get(propertyName, aResult); // Addref here.
return NS_OK;
}
NS_IMETHODIMP
BoxObject::SetPropertyAsSupports(const char16_t* aPropertyName, nsISupports* aValue)
{
NS_ENSURE_ARG(aPropertyName && *aPropertyName);
if (!mPropertyTable) {
mPropertyTable = new nsInterfaceHashtable<nsStringHashKey,nsISupports>(4);
}
nsDependentString propertyName(aPropertyName);
mPropertyTable->Put(propertyName, aValue);
return NS_OK;
}
NS_IMETHODIMP
BoxObject::GetProperty(const char16_t* aPropertyName, char16_t** aResult)
{
nsCOMPtr<nsISupports> data;
nsresult rv = GetPropertyAsSupports(aPropertyName,getter_AddRefs(data));
NS_ENSURE_SUCCESS(rv, rv);
if (!data) {
*aResult = nullptr;
return NS_OK;
}
nsCOMPtr<nsISupportsString> supportsStr = do_QueryInterface(data);
if (!supportsStr) {
return NS_ERROR_FAILURE;
}
return supportsStr->ToString(aResult);
}
NS_IMETHODIMP
BoxObject::SetProperty(const char16_t* aPropertyName, const char16_t* aPropertyValue)
{
NS_ENSURE_ARG(aPropertyName && *aPropertyName);
nsDependentString propertyName(aPropertyName);
nsDependentString propertyValue;
if (aPropertyValue) {
propertyValue.Rebind(aPropertyValue);
} else {
propertyValue.SetIsVoid(true);
}
nsCOMPtr<nsISupportsString> supportsStr(do_CreateInstance(NS_SUPPORTS_STRING_CONTRACTID));
NS_ENSURE_TRUE(supportsStr, NS_ERROR_OUT_OF_MEMORY);
supportsStr->SetData(propertyValue);
return SetPropertyAsSupports(aPropertyName,supportsStr);
}
NS_IMETHODIMP
BoxObject::RemoveProperty(const char16_t* aPropertyName)
{
NS_ENSURE_ARG(aPropertyName && *aPropertyName);
if (!mPropertyTable) return NS_OK;
nsDependentString propertyName(aPropertyName);
mPropertyTable->Remove(propertyName);
return NS_OK;
}
NS_IMETHODIMP
BoxObject::GetParentBox(nsIDOMElement * *aParentBox)
{
*aParentBox = nullptr;
nsIFrame* frame = GetFrame(false);
if (!frame) return NS_OK;
nsIFrame* parent = frame->GetParent();
if (!parent) return NS_OK;
nsCOMPtr<nsIDOMElement> el = do_QueryInterface(parent->GetContent());
*aParentBox = el;
NS_IF_ADDREF(*aParentBox);
return NS_OK;
}
NS_IMETHODIMP
BoxObject::GetFirstChild(nsIDOMElement * *aFirstVisibleChild)
{
*aFirstVisibleChild = nullptr;
nsIFrame* frame = GetFrame(false);
if (!frame) return NS_OK;
nsIFrame* firstFrame = frame->PrincipalChildList().FirstChild();
if (!firstFrame) return NS_OK;
// get the content for the box and query to a dom element
nsCOMPtr<nsIDOMElement> el = do_QueryInterface(firstFrame->GetContent());
el.swap(*aFirstVisibleChild);
return NS_OK;
}
NS_IMETHODIMP
BoxObject::GetLastChild(nsIDOMElement * *aLastVisibleChild)
{
*aLastVisibleChild = nullptr;
nsIFrame* frame = GetFrame(false);
if (!frame) return NS_OK;
return GetPreviousSibling(frame, nullptr, aLastVisibleChild);
}
NS_IMETHODIMP
BoxObject::GetNextSibling(nsIDOMElement **aNextOrdinalSibling)
{
*aNextOrdinalSibling = nullptr;
nsIFrame* frame = GetFrame(false);
if (!frame) return NS_OK;
nsIFrame* nextFrame = frame->GetNextSibling();
if (!nextFrame) return NS_OK;
// get the content for the box and query to a dom element
nsCOMPtr<nsIDOMElement> el = do_QueryInterface(nextFrame->GetContent());
el.swap(*aNextOrdinalSibling);
return NS_OK;
}
NS_IMETHODIMP
BoxObject::GetPreviousSibling(nsIDOMElement **aPreviousOrdinalSibling)
{
*aPreviousOrdinalSibling = nullptr;
nsIFrame* frame = GetFrame(false);
if (!frame) return NS_OK;
nsIFrame* parentFrame = frame->GetParent();
if (!parentFrame) return NS_OK;
return GetPreviousSibling(parentFrame, frame, aPreviousOrdinalSibling);
}
nsresult
BoxObject::GetPreviousSibling(nsIFrame* aParentFrame, nsIFrame* aFrame,
nsIDOMElement** aResult)
{
*aResult = nullptr;
nsIFrame* nextFrame = aParentFrame->PrincipalChildList().FirstChild();
nsIFrame* prevFrame = nullptr;
while (nextFrame) {
if (nextFrame == aFrame)
break;
prevFrame = nextFrame;
nextFrame = nextFrame->GetNextSibling();
}
if (!prevFrame) return NS_OK;
// get the content for the box and query to a dom element
nsCOMPtr<nsIDOMElement> el = do_QueryInterface(prevFrame->GetContent());
el.swap(*aResult);
return NS_OK;
}
nsIContent*
BoxObject::GetParentObject() const
{
return mContent;
}
JSObject*
BoxObject::WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto)
{
return BoxObjectBinding::Wrap(aCx, this, aGivenProto);
}
Element*
BoxObject::GetElement() const
{
return mContent && mContent->IsElement() ? mContent->AsElement() : nullptr;
}
int32_t
BoxObject::X()
{
int32_t ret = 0;
GetX(&ret);
return ret;
}
int32_t
BoxObject::Y()
{
int32_t ret = 0;
GetY(&ret);
return ret;
}
int32_t
BoxObject::GetScreenX(ErrorResult& aRv)
{
int32_t ret = 0;
aRv = GetScreenX(&ret);
return ret;
}
int32_t
BoxObject::GetScreenY(ErrorResult& aRv)
{
int32_t ret = 0;
aRv = GetScreenY(&ret);
return ret;
}
int32_t
BoxObject::Width()
{
int32_t ret = 0;
GetWidth(&ret);
return ret;
}
int32_t
BoxObject::Height()
{
int32_t ret = 0;
GetHeight(&ret);
return ret;
}
already_AddRefed<nsISupports>
BoxObject::GetPropertyAsSupports(const nsAString& propertyName)
{
nsCOMPtr<nsISupports> ret;
GetPropertyAsSupports(PromiseFlatString(propertyName).get(), getter_AddRefs(ret));
return ret.forget();
}
void
BoxObject::SetPropertyAsSupports(const nsAString& propertyName, nsISupports* value)
{
SetPropertyAsSupports(PromiseFlatString(propertyName).get(), value);
}
void
BoxObject::GetProperty(const nsAString& propertyName, nsString& aRetVal, ErrorResult& aRv)
{
nsCOMPtr<nsISupports> data(GetPropertyAsSupports(propertyName));
if (!data) {
return;
}
nsCOMPtr<nsISupportsString> supportsStr(do_QueryInterface(data));
if (!supportsStr) {
aRv.Throw(NS_ERROR_FAILURE);
return;
}
supportsStr->GetData(aRetVal);
}
void
BoxObject::SetProperty(const nsAString& propertyName, const nsAString& propertyValue)
{
SetProperty(PromiseFlatString(propertyName).get(), PromiseFlatString(propertyValue).get());
}
void
BoxObject::RemoveProperty(const nsAString& propertyName)
{
RemoveProperty(PromiseFlatString(propertyName).get());
}
already_AddRefed<Element>
BoxObject::GetParentBox()
{
nsCOMPtr<nsIDOMElement> el;
GetParentBox(getter_AddRefs(el));
nsCOMPtr<Element> ret(do_QueryInterface(el));
return ret.forget();
}
already_AddRefed<Element>
BoxObject::GetFirstChild()
{
nsCOMPtr<nsIDOMElement> el;
GetFirstChild(getter_AddRefs(el));
nsCOMPtr<Element> ret(do_QueryInterface(el));
return ret.forget();
}
already_AddRefed<Element>
BoxObject::GetLastChild()
{
nsCOMPtr<nsIDOMElement> el;
GetLastChild(getter_AddRefs(el));
nsCOMPtr<Element> ret(do_QueryInterface(el));
return ret.forget();
}
already_AddRefed<Element>
BoxObject::GetNextSibling()
{
nsCOMPtr<nsIDOMElement> el;
GetNextSibling(getter_AddRefs(el));
nsCOMPtr<Element> ret(do_QueryInterface(el));
return ret.forget();
}
already_AddRefed<Element>
BoxObject::GetPreviousSibling()
{
nsCOMPtr<nsIDOMElement> el;
GetPreviousSibling(getter_AddRefs(el));
nsCOMPtr<Element> ret(do_QueryInterface(el));
return ret.forget();
}
} // namespace dom
} // namespace mozilla
// Creation Routine ///////////////////////////////////////////////////////////////////////
using namespace mozilla::dom;
nsresult
NS_NewBoxObject(nsIBoxObject** aResult)
{
NS_ADDREF(*aResult = new BoxObject());
return NS_OK;
}
|