1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 /* code for HTML client-side image maps */
9 #include "nsImageMap.h"
11 #include "mozilla/dom/Element.h"
12 #include "mozilla/dom/Event.h" // for Event
13 #include "mozilla/dom/HTMLAreaElement.h"
14 #include "mozilla/gfx/PathHelpers.h"
15 #include "mozilla/UniquePtr.h"
17 #include "nsReadableUtils.h"
18 #include "nsPresContext.h"
19 #include "nsNameSpaceManager.h"
20 #include "nsGkAtoms.h"
21 #include "nsImageFrame.h"
23 #include "nsIContentInlines.h"
24 #include "nsIScriptError.h"
25 #include "nsIStringBundle.h"
26 #include "nsContentUtils.h"
27 #include "ImageLayers.h"
30 # include "nsAccessibilityService.h"
33 using namespace mozilla
;
34 using namespace mozilla::gfx
;
35 using namespace mozilla::dom
;
39 explicit Area(HTMLAreaElement
* aArea
);
42 virtual void ParseCoords(const nsAString
& aSpec
);
44 virtual bool IsInside(nscoord x
, nscoord y
) const = 0;
45 virtual void Draw(nsIFrame
* aFrame
, DrawTarget
& aDrawTarget
,
46 const ColorPattern
& aColor
,
47 const StrokeOptions
& aStrokeOptions
) = 0;
48 virtual void GetRect(nsIFrame
* aFrame
, nsRect
& aRect
) = 0;
50 void HasFocus(bool aHasFocus
);
52 RefPtr
<HTMLAreaElement
> mArea
;
53 UniquePtr
<nscoord
[]> mCoords
;
58 Area::Area(HTMLAreaElement
* aArea
) : mArea(aArea
) {
60 MOZ_ASSERT(mArea
, "How did that happen?");
65 Area::~Area() { MOZ_COUNT_DTOR(Area
); }
69 inline bool is_space(char c
) {
70 return (c
== ' ' || c
== '\f' || c
== '\n' || c
== '\r' || c
== '\t' ||
74 static void logMessage(nsIContent
* aContent
, const nsAString
& aCoordsSpec
,
75 int32_t aFlags
, const char* aMessageName
) {
76 nsContentUtils::ReportToConsole(
77 aFlags
, NS_LITERAL_CSTRING("Layout: ImageMap"), aContent
->OwnerDoc(),
78 nsContentUtils::eLAYOUT_PROPERTIES
, aMessageName
, nullptr, /* params */
79 0, /* params length */
81 PromiseFlatString(NS_LITERAL_STRING("coords=\"") + aCoordsSpec
+
82 NS_LITERAL_STRING("\""))); /* source line */
85 void Area::ParseCoords(const nsAString
& aSpec
) {
86 char* cp
= ToNewUTF8String(aSpec
);
93 * Nothing in an empty list
103 * Skip beginning whitespace, all whitespace is empty list.
106 while (is_space(*n_str
)) {
109 if (*n_str
== '\0') {
115 * Make a pass where any two numbers separated by just whitespace
116 * are given a comma separator. Count entries while passing.
119 while (*n_str
!= '\0') {
123 * Skip to a separator
126 while (!is_space(*tptr
) && *tptr
!= ',' && *tptr
!= '\0') {
132 * If no more entries, break out here
134 if (*n_str
== '\0') {
139 * Skip to the end of the separator, noting if we have a
143 while (is_space(*tptr
) || *tptr
== ',') {
154 * If this was trailing whitespace we skipped, we are done.
156 if ((*tptr
== '\0') && !has_comma
) {
160 * Else if the separator is all whitespace, and this is not the
161 * end of the string, add a comma to the separator.
163 else if (!has_comma
) {
168 * count the entry skipped.
175 * count the last entry in the list.
180 * Allocate space for the coordinate array.
182 UniquePtr
<nscoord
[]> value_list
= MakeUnique
<nscoord
[]>(cnt
);
189 * Second pass to copy integer values into list.
192 for (i
= 0; i
< cnt
; i
++) {
195 ptr
= strchr(tptr
, ',');
200 * Strip whitespace in front of number because I don't
201 * trust atoi to do it on all platforms.
203 while (is_space(*tptr
)) {
209 value_list
[i
] = (nscoord
)::atoi(tptr
);
218 mCoords
= std::move(value_list
);
224 void Area::HasFocus(bool aHasFocus
) { mHasFocus
= aHasFocus
; }
226 //----------------------------------------------------------------------
228 class DefaultArea final
: public Area
{
230 explicit DefaultArea(HTMLAreaElement
* aArea
);
232 virtual bool IsInside(nscoord x
, nscoord y
) const override
;
233 virtual void Draw(nsIFrame
* aFrame
, DrawTarget
& aDrawTarget
,
234 const ColorPattern
& aColor
,
235 const StrokeOptions
& aStrokeOptions
) override
;
236 virtual void GetRect(nsIFrame
* aFrame
, nsRect
& aRect
) override
;
239 DefaultArea::DefaultArea(HTMLAreaElement
* aArea
) : Area(aArea
) {}
241 bool DefaultArea::IsInside(nscoord x
, nscoord y
) const { return true; }
243 void DefaultArea::Draw(nsIFrame
* aFrame
, DrawTarget
& aDrawTarget
,
244 const ColorPattern
& aColor
,
245 const StrokeOptions
& aStrokeOptions
) {
247 nsRect
r(nsPoint(0, 0), aFrame
->GetSize());
248 const nscoord kOnePixel
= nsPresContext::CSSPixelsToAppUnits(1);
249 r
.width
-= kOnePixel
;
250 r
.height
-= kOnePixel
;
251 Rect rect
= ToRect(nsLayoutUtils::RectToGfxRect(
252 r
, aFrame
->PresContext()->AppUnitsPerDevPixel()));
253 StrokeSnappedEdgesOfRect(rect
, aDrawTarget
, aColor
, aStrokeOptions
);
257 void DefaultArea::GetRect(nsIFrame
* aFrame
, nsRect
& aRect
) {
258 aRect
= aFrame
->GetRect();
262 //----------------------------------------------------------------------
264 class RectArea final
: public Area
{
266 explicit RectArea(HTMLAreaElement
* aArea
);
268 virtual void ParseCoords(const nsAString
& aSpec
) override
;
269 virtual bool IsInside(nscoord x
, nscoord y
) const override
;
270 virtual void Draw(nsIFrame
* aFrame
, DrawTarget
& aDrawTarget
,
271 const ColorPattern
& aColor
,
272 const StrokeOptions
& aStrokeOptions
) override
;
273 virtual void GetRect(nsIFrame
* aFrame
, nsRect
& aRect
) override
;
276 RectArea::RectArea(HTMLAreaElement
* aArea
) : Area(aArea
) {}
278 void RectArea::ParseCoords(const nsAString
& aSpec
) {
279 Area::ParseCoords(aSpec
);
281 bool saneRect
= true;
282 int32_t flag
= nsIScriptError::warningFlag
;
283 if (mNumCoords
>= 4) {
284 if (mCoords
[0] > mCoords
[2]) {
285 // x-coords in reversed order
286 nscoord x
= mCoords
[2];
287 mCoords
[2] = mCoords
[0];
292 if (mCoords
[1] > mCoords
[3]) {
293 // y-coords in reversed order
294 nscoord y
= mCoords
[3];
295 mCoords
[3] = mCoords
[1];
300 if (mNumCoords
> 4) {
301 // Someone missed the concept of a rect here
306 flag
= nsIScriptError::errorFlag
;
310 logMessage(mArea
, aSpec
, flag
, "ImageMapRectBoundsError");
314 bool RectArea::IsInside(nscoord x
, nscoord y
) const {
315 if (mNumCoords
>= 4) { // Note: > is for nav compatibility
316 nscoord x1
= mCoords
[0];
317 nscoord y1
= mCoords
[1];
318 nscoord x2
= mCoords
[2];
319 nscoord y2
= mCoords
[3];
320 NS_ASSERTION(x1
<= x2
&& y1
<= y2
,
321 "Someone screwed up RectArea::ParseCoords");
322 if ((x
>= x1
) && (x
<= x2
) && (y
>= y1
) && (y
<= y2
)) {
329 void RectArea::Draw(nsIFrame
* aFrame
, DrawTarget
& aDrawTarget
,
330 const ColorPattern
& aColor
,
331 const StrokeOptions
& aStrokeOptions
) {
333 if (mNumCoords
>= 4) {
334 nscoord x1
= nsPresContext::CSSPixelsToAppUnits(mCoords
[0]);
335 nscoord y1
= nsPresContext::CSSPixelsToAppUnits(mCoords
[1]);
336 nscoord x2
= nsPresContext::CSSPixelsToAppUnits(mCoords
[2]);
337 nscoord y2
= nsPresContext::CSSPixelsToAppUnits(mCoords
[3]);
338 NS_ASSERTION(x1
<= x2
&& y1
<= y2
,
339 "Someone screwed up RectArea::ParseCoords");
340 nsRect
r(x1
, y1
, x2
- x1
, y2
- y1
);
341 Rect rect
= ToRect(nsLayoutUtils::RectToGfxRect(
342 r
, aFrame
->PresContext()->AppUnitsPerDevPixel()));
343 StrokeSnappedEdgesOfRect(rect
, aDrawTarget
, aColor
, aStrokeOptions
);
348 void RectArea::GetRect(nsIFrame
* aFrame
, nsRect
& aRect
) {
349 if (mNumCoords
>= 4) {
350 nscoord x1
= nsPresContext::CSSPixelsToAppUnits(mCoords
[0]);
351 nscoord y1
= nsPresContext::CSSPixelsToAppUnits(mCoords
[1]);
352 nscoord x2
= nsPresContext::CSSPixelsToAppUnits(mCoords
[2]);
353 nscoord y2
= nsPresContext::CSSPixelsToAppUnits(mCoords
[3]);
354 NS_ASSERTION(x1
<= x2
&& y1
<= y2
,
355 "Someone screwed up RectArea::ParseCoords");
357 aRect
.SetRect(x1
, y1
, x2
, y2
);
361 //----------------------------------------------------------------------
363 class PolyArea final
: public Area
{
365 explicit PolyArea(HTMLAreaElement
* aArea
);
367 virtual void ParseCoords(const nsAString
& aSpec
) override
;
368 virtual bool IsInside(nscoord x
, nscoord y
) const override
;
369 virtual void Draw(nsIFrame
* aFrame
, DrawTarget
& aDrawTarget
,
370 const ColorPattern
& aColor
,
371 const StrokeOptions
& aStrokeOptions
) override
;
372 virtual void GetRect(nsIFrame
* aFrame
, nsRect
& aRect
) override
;
375 PolyArea::PolyArea(HTMLAreaElement
* aArea
) : Area(aArea
) {}
377 void PolyArea::ParseCoords(const nsAString
& aSpec
) {
378 Area::ParseCoords(aSpec
);
380 if (mNumCoords
>= 2) {
381 if (mNumCoords
& 1U) {
382 logMessage(mArea
, aSpec
, nsIScriptError::warningFlag
,
383 "ImageMapPolyOddNumberOfCoords");
386 logMessage(mArea
, aSpec
, nsIScriptError::errorFlag
,
387 "ImageMapPolyWrongNumberOfCoords");
391 bool PolyArea::IsInside(nscoord x
, nscoord y
) const {
392 if (mNumCoords
>= 6) {
393 int32_t intersects
= 0;
396 int32_t totalv
= mNumCoords
/ 2;
397 int32_t totalc
= totalv
* 2;
398 nscoord xval
= mCoords
[totalc
- 2];
399 nscoord yval
= mCoords
[totalc
- 1];
400 int32_t end
= totalc
;
403 if ((yval
>= wherey
) != (mCoords
[pointer
] >= wherey
)) {
404 if ((xval
>= wherex
) == (mCoords
[0] >= wherex
)) {
405 intersects
+= (xval
>= wherex
) ? 1 : 0;
407 intersects
+= ((xval
- (yval
- wherey
) * (mCoords
[0] - xval
) /
408 (mCoords
[pointer
] - yval
)) >= wherex
)
414 // XXX I wonder what this is doing; this is a translation of ptinpoly.c
415 while (pointer
< end
) {
416 yval
= mCoords
[pointer
];
418 if (yval
>= wherey
) {
419 while ((pointer
< end
) && (mCoords
[pointer
] >= wherey
)) pointer
+= 2;
420 if (pointer
>= end
) break;
421 if ((mCoords
[pointer
- 3] >= wherex
) ==
422 (mCoords
[pointer
- 1] >= wherex
)) {
423 intersects
+= (mCoords
[pointer
- 3] >= wherex
) ? 1 : 0;
426 ((mCoords
[pointer
- 3] -
427 (mCoords
[pointer
- 2] - wherey
) *
428 (mCoords
[pointer
- 1] - mCoords
[pointer
- 3]) /
429 (mCoords
[pointer
] - mCoords
[pointer
- 2])) >= wherex
)
434 while ((pointer
< end
) && (mCoords
[pointer
] < wherey
)) pointer
+= 2;
435 if (pointer
>= end
) break;
436 if ((mCoords
[pointer
- 3] >= wherex
) ==
437 (mCoords
[pointer
- 1] >= wherex
)) {
438 intersects
+= (mCoords
[pointer
- 3] >= wherex
) ? 1 : 0;
441 ((mCoords
[pointer
- 3] -
442 (mCoords
[pointer
- 2] - wherey
) *
443 (mCoords
[pointer
- 1] - mCoords
[pointer
- 3]) /
444 (mCoords
[pointer
] - mCoords
[pointer
- 2])) >= wherex
)
450 if ((intersects
& 1) != 0) {
457 void PolyArea::Draw(nsIFrame
* aFrame
, DrawTarget
& aDrawTarget
,
458 const ColorPattern
& aColor
,
459 const StrokeOptions
& aStrokeOptions
) {
461 if (mNumCoords
>= 6) {
462 // Where possible, we want all horizontal and vertical lines to align on
463 // pixel rows or columns, and to start at pixel boundaries so that one
464 // pixel dashing neatly sits on pixels to give us neat lines. To achieve
465 // that we draw each line segment as a separate path, snapping it to
466 // device pixels if applicable.
467 nsPresContext
* pc
= aFrame
->PresContext();
468 Point
p1(pc
->CSSPixelsToDevPixels(mCoords
[0]),
469 pc
->CSSPixelsToDevPixels(mCoords
[1]));
470 Point p2
, p1snapped
, p2snapped
;
471 for (int32_t i
= 2; i
< mNumCoords
; i
+= 2) {
472 p2
.x
= pc
->CSSPixelsToDevPixels(mCoords
[i
]);
473 p2
.y
= pc
->CSSPixelsToDevPixels(mCoords
[i
+ 1]);
476 SnapLineToDevicePixelsForStroking(p1snapped
, p2snapped
, aDrawTarget
,
477 aStrokeOptions
.mLineWidth
);
478 aDrawTarget
.StrokeLine(p1snapped
, p2snapped
, aColor
, aStrokeOptions
);
481 p2
.x
= pc
->CSSPixelsToDevPixels(mCoords
[0]);
482 p2
.y
= pc
->CSSPixelsToDevPixels(mCoords
[1]);
485 SnapLineToDevicePixelsForStroking(p1snapped
, p2snapped
, aDrawTarget
,
486 aStrokeOptions
.mLineWidth
);
487 aDrawTarget
.StrokeLine(p1snapped
, p2snapped
, aColor
, aStrokeOptions
);
492 void PolyArea::GetRect(nsIFrame
* aFrame
, nsRect
& aRect
) {
493 if (mNumCoords
>= 6) {
494 nscoord x1
, x2
, y1
, y2
, xtmp
, ytmp
;
495 x1
= x2
= nsPresContext::CSSPixelsToAppUnits(mCoords
[0]);
496 y1
= y2
= nsPresContext::CSSPixelsToAppUnits(mCoords
[1]);
497 for (int32_t i
= 2; i
< mNumCoords
; i
+= 2) {
498 xtmp
= nsPresContext::CSSPixelsToAppUnits(mCoords
[i
]);
499 ytmp
= nsPresContext::CSSPixelsToAppUnits(mCoords
[i
+ 1]);
500 x1
= x1
< xtmp
? x1
: xtmp
;
501 y1
= y1
< ytmp
? y1
: ytmp
;
502 x2
= x2
> xtmp
? x2
: xtmp
;
503 y2
= y2
> ytmp
? y2
: ytmp
;
506 aRect
.SetRect(x1
, y1
, x2
, y2
);
510 //----------------------------------------------------------------------
512 class CircleArea final
: public Area
{
514 explicit CircleArea(HTMLAreaElement
* aArea
);
516 virtual void ParseCoords(const nsAString
& aSpec
) override
;
517 virtual bool IsInside(nscoord x
, nscoord y
) const override
;
518 virtual void Draw(nsIFrame
* aFrame
, DrawTarget
& aDrawTarget
,
519 const ColorPattern
& aColor
,
520 const StrokeOptions
& aStrokeOptions
) override
;
521 virtual void GetRect(nsIFrame
* aFrame
, nsRect
& aRect
) override
;
524 CircleArea::CircleArea(HTMLAreaElement
* aArea
) : Area(aArea
) {}
526 void CircleArea::ParseCoords(const nsAString
& aSpec
) {
527 Area::ParseCoords(aSpec
);
529 bool wrongNumberOfCoords
= false;
530 int32_t flag
= nsIScriptError::warningFlag
;
531 if (mNumCoords
>= 3) {
532 if (mCoords
[2] < 0) {
533 logMessage(mArea
, aSpec
, nsIScriptError::errorFlag
,
534 "ImageMapCircleNegativeRadius");
537 if (mNumCoords
> 3) {
538 wrongNumberOfCoords
= true;
541 wrongNumberOfCoords
= true;
542 flag
= nsIScriptError::errorFlag
;
545 if (wrongNumberOfCoords
) {
546 logMessage(mArea
, aSpec
, flag
, "ImageMapCircleWrongNumberOfCoords");
550 bool CircleArea::IsInside(nscoord x
, nscoord y
) const {
551 // Note: > is for nav compatibility
552 if (mNumCoords
>= 3) {
553 nscoord x1
= mCoords
[0];
554 nscoord y1
= mCoords
[1];
555 nscoord radius
= mCoords
[2];
561 nscoord dist
= (dx
* dx
) + (dy
* dy
);
562 if (dist
<= (radius
* radius
)) {
569 void CircleArea::Draw(nsIFrame
* aFrame
, DrawTarget
& aDrawTarget
,
570 const ColorPattern
& aColor
,
571 const StrokeOptions
& aStrokeOptions
) {
573 if (mNumCoords
>= 3) {
574 Point
center(aFrame
->PresContext()->CSSPixelsToDevPixels(mCoords
[0]),
575 aFrame
->PresContext()->CSSPixelsToDevPixels(mCoords
[1]));
577 2 * aFrame
->PresContext()->CSSPixelsToDevPixels(mCoords
[2]);
581 RefPtr
<PathBuilder
> builder
= aDrawTarget
.CreatePathBuilder();
582 AppendEllipseToPath(builder
, center
, Size(diameter
, diameter
));
583 RefPtr
<Path
> circle
= builder
->Finish();
584 aDrawTarget
.Stroke(circle
, aColor
, aStrokeOptions
);
589 void CircleArea::GetRect(nsIFrame
* aFrame
, nsRect
& aRect
) {
590 if (mNumCoords
>= 3) {
591 nscoord x1
= nsPresContext::CSSPixelsToAppUnits(mCoords
[0]);
592 nscoord y1
= nsPresContext::CSSPixelsToAppUnits(mCoords
[1]);
593 nscoord radius
= nsPresContext::CSSPixelsToAppUnits(mCoords
[2]);
598 aRect
.SetRect(x1
- radius
, y1
- radius
, x1
+ radius
, y1
+ radius
);
602 //----------------------------------------------------------------------
604 nsImageMap::nsImageMap() : mImageFrame(nullptr), mConsiderWholeSubtree(false) {}
606 nsImageMap::~nsImageMap() {
607 NS_ASSERTION(mAreas
.Length() == 0, "Destroy was not called");
610 NS_IMPL_ISUPPORTS(nsImageMap
, nsIMutationObserver
, nsIDOMEventListener
)
612 nsresult
nsImageMap::GetBoundsForAreaContent(nsIContent
* aContent
,
614 NS_ENSURE_TRUE(aContent
&& mImageFrame
, NS_ERROR_INVALID_ARG
);
616 // Find the Area struct associated with this content node, and return bounds
617 for (auto& area
: mAreas
) {
618 if (area
->mArea
== aContent
) {
620 area
->GetRect(mImageFrame
, aBounds
);
624 return NS_ERROR_FAILURE
;
627 void nsImageMap::AreaRemoved(HTMLAreaElement
* aArea
) {
628 if (aArea
->GetPrimaryFrame() == mImageFrame
) {
629 aArea
->SetPrimaryFrame(nullptr);
632 aArea
->RemoveSystemEventListener(NS_LITERAL_STRING("focus"), this, false);
633 aArea
->RemoveSystemEventListener(NS_LITERAL_STRING("blur"), this, false);
636 void nsImageMap::FreeAreas() {
637 for (UniquePtr
<Area
>& area
: mAreas
) {
638 AreaRemoved(area
->mArea
);
644 void nsImageMap::Init(nsImageFrame
* aImageFrame
, nsIContent
* aMap
) {
646 MOZ_ASSERT(aImageFrame
);
648 mImageFrame
= aImageFrame
;
650 mMap
->AddMutationObserver(this);
652 // "Compile" the areas in the map into faster access versions
656 void nsImageMap::SearchForAreas(nsIContent
* aParent
) {
657 // Look for <area> elements.
658 for (nsIContent
* child
= aParent
->GetFirstChild(); child
;
659 child
= child
->GetNextSibling()) {
660 if (auto* area
= HTMLAreaElement::FromNode(child
)) {
663 // Continue to next child. This stops mConsiderWholeSubtree from
664 // getting set. It also makes us ignore children of <area>s which
665 // is consistent with how we react to dynamic insertion of such
670 if (child
->IsElement()) {
671 mConsiderWholeSubtree
= true;
672 SearchForAreas(child
);
677 void nsImageMap::UpdateAreas() {
678 // Get rid of old area data
681 mConsiderWholeSubtree
= false;
682 SearchForAreas(mMap
);
685 if (nsAccessibilityService
* accService
= GetAccService()) {
686 accService
->UpdateImageMap(mImageFrame
);
691 void nsImageMap::AddArea(HTMLAreaElement
* aArea
) {
692 static Element::AttrValuesArray strings
[] = {
693 nsGkAtoms::rect
, nsGkAtoms::rectangle
,
694 nsGkAtoms::circle
, nsGkAtoms::circ
,
695 nsGkAtoms::_default
, nsGkAtoms::poly
,
696 nsGkAtoms::polygon
, nullptr};
698 UniquePtr
<Area
> area
;
699 switch (aArea
->FindAttrValueIn(kNameSpaceID_None
, nsGkAtoms::shape
, strings
,
701 case Element::ATTR_VALUE_NO_MATCH
:
702 case Element::ATTR_MISSING
:
705 area
= MakeUnique
<RectArea
>(aArea
);
709 area
= MakeUnique
<CircleArea
>(aArea
);
712 area
= MakeUnique
<DefaultArea
>(aArea
);
716 area
= MakeUnique
<PolyArea
>(aArea
);
720 MOZ_ASSERT_UNREACHABLE("FindAttrValueIn returned an unexpected value.");
724 // Add focus listener to track area focus changes
725 aArea
->AddSystemEventListener(NS_LITERAL_STRING("focus"), this, false, false);
726 aArea
->AddSystemEventListener(NS_LITERAL_STRING("blur"), this, false, false);
728 // This is a nasty hack. It needs to go away: see bug 135040. Once this is
729 // removed, the code added to RestyleManager::RestyleElement,
730 // nsCSSFrameConstructor::ContentRemoved (both hacks there), and
731 // RestyleManager::ProcessRestyledFrames to work around this issue can
733 aArea
->SetPrimaryFrame(mImageFrame
);
736 aArea
->GetAttr(kNameSpaceID_None
, nsGkAtoms::coords
, coords
);
737 area
->ParseCoords(coords
);
738 mAreas
.AppendElement(std::move(area
));
741 HTMLAreaElement
* nsImageMap::GetArea(nscoord aX
, nscoord aY
) const {
742 NS_ASSERTION(mMap
, "Not initialized");
743 for (const auto& area
: mAreas
) {
744 if (area
->IsInside(aX
, aY
)) {
752 HTMLAreaElement
* nsImageMap::GetAreaAt(uint32_t aIndex
) const {
753 return mAreas
.ElementAt(aIndex
)->mArea
;
756 void nsImageMap::Draw(nsIFrame
* aFrame
, DrawTarget
& aDrawTarget
,
757 const ColorPattern
& aColor
,
758 const StrokeOptions
& aStrokeOptions
) {
759 for (auto& area
: mAreas
) {
760 area
->Draw(aFrame
, aDrawTarget
, aColor
, aStrokeOptions
);
764 void nsImageMap::MaybeUpdateAreas(nsIContent
* aContent
) {
765 if (aContent
== mMap
|| mConsiderWholeSubtree
) {
770 void nsImageMap::AttributeChanged(dom::Element
* aElement
, int32_t aNameSpaceID
,
771 nsAtom
* aAttribute
, int32_t aModType
,
772 const nsAttrValue
* aOldValue
) {
773 // If the parent of the changing content node is our map then update
774 // the map. But only do this if the node is an HTML <area> or <a>
775 // and the attribute that's changing is "shape" or "coords" -- those
776 // are the only cases we care about.
777 if ((aElement
->NodeInfo()->Equals(nsGkAtoms::area
) ||
778 aElement
->NodeInfo()->Equals(nsGkAtoms::a
)) &&
779 aElement
->IsHTMLElement() && aNameSpaceID
== kNameSpaceID_None
&&
780 (aAttribute
== nsGkAtoms::shape
|| aAttribute
== nsGkAtoms::coords
)) {
781 MaybeUpdateAreas(aElement
->GetParent());
782 } else if (aElement
== mMap
&& aNameSpaceID
== kNameSpaceID_None
&&
783 (aAttribute
== nsGkAtoms::name
|| aAttribute
== nsGkAtoms::id
) &&
785 // ID or name has changed. Let ImageFrame recreate ImageMap.
786 mImageFrame
->DisconnectMap();
790 void nsImageMap::ContentAppended(nsIContent
* aFirstNewContent
) {
791 MaybeUpdateAreas(aFirstNewContent
->GetParent());
794 void nsImageMap::ContentInserted(nsIContent
* aChild
) {
795 MaybeUpdateAreas(aChild
->GetParent());
798 static UniquePtr
<Area
> TakeArea(nsImageMap::AreaList
& aAreas
,
799 HTMLAreaElement
* aArea
) {
800 UniquePtr
<Area
> result
;
802 for (UniquePtr
<Area
>& area
: aAreas
) {
803 if (area
->mArea
== aArea
) {
804 result
= std::move(area
);
811 aAreas
.RemoveElementAt(index
);
817 void nsImageMap::ContentRemoved(nsIContent
* aChild
,
818 nsIContent
* aPreviousSibling
) {
819 if (aChild
->GetParent() != mMap
&& !mConsiderWholeSubtree
) {
823 auto* areaElement
= HTMLAreaElement::FromNode(aChild
);
828 UniquePtr
<Area
> area
= TakeArea(mAreas
, areaElement
);
833 AreaRemoved(area
->mArea
);
836 if (nsAccessibilityService
* accService
= GetAccService()) {
837 accService
->UpdateImageMap(mImageFrame
);
842 void nsImageMap::ParentChainChanged(nsIContent
* aContent
) {
843 NS_ASSERTION(aContent
== mMap
, "Unexpected ParentChainChanged notification!");
845 mImageFrame
->DisconnectMap();
849 nsresult
nsImageMap::HandleEvent(Event
* aEvent
) {
850 nsAutoString eventType
;
851 aEvent
->GetType(eventType
);
852 bool focus
= eventType
.EqualsLiteral("focus");
853 MOZ_ASSERT(focus
== !eventType
.EqualsLiteral("blur"),
854 "Unexpected event type");
856 // Set which one of our areas changed focus
857 nsCOMPtr
<nsIContent
> targetContent
= do_QueryInterface(aEvent
->GetTarget());
858 if (!targetContent
) {
862 for (auto& area
: mAreas
) {
863 if (area
->mArea
== targetContent
) {
864 // Set or Remove internal focus
865 area
->HasFocus(focus
);
866 // Now invalidate the rect
868 mImageFrame
->InvalidateFrame();
876 void nsImageMap::Destroy() {
878 mImageFrame
= nullptr;
879 mMap
->RemoveMutationObserver(this);