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 "nsContentUtils.h"
26 #include "nsLayoutUtils.h"
29 # include "nsAccessibilityService.h"
32 using namespace mozilla
;
33 using namespace mozilla::gfx
;
34 using namespace mozilla::dom
;
38 explicit Area(HTMLAreaElement
* aArea
);
41 virtual void ParseCoords(const nsAString
& aSpec
);
43 virtual bool IsInside(nscoord x
, nscoord y
) const = 0;
44 virtual void Draw(nsIFrame
* aFrame
, DrawTarget
& aDrawTarget
,
45 const ColorPattern
& aColor
,
46 const StrokeOptions
& aStrokeOptions
) = 0;
47 virtual void GetRect(nsIFrame
* aFrame
, nsRect
& aRect
) = 0;
49 void HasFocus(bool aHasFocus
);
51 RefPtr
<HTMLAreaElement
> mArea
;
52 UniquePtr
<nscoord
[]> mCoords
;
57 Area::Area(HTMLAreaElement
* aArea
) : mArea(aArea
) {
59 MOZ_ASSERT(mArea
, "How did that happen?");
64 Area::~Area() { MOZ_COUNT_DTOR(Area
); }
68 inline bool is_space(char c
) {
69 return (c
== ' ' || c
== '\f' || c
== '\n' || c
== '\r' || c
== '\t' ||
73 static void logMessage(nsIContent
* aContent
, const nsAString
& aCoordsSpec
,
74 int32_t aFlags
, const char* aMessageName
) {
75 nsContentUtils::ReportToConsole(
76 aFlags
, "Layout: ImageMap"_ns
, aContent
->OwnerDoc(),
77 nsContentUtils::eLAYOUT_PROPERTIES
, aMessageName
);
80 void Area::ParseCoords(const nsAString
& aSpec
) {
81 char* cp
= ToNewUTF8String(aSpec
);
88 * Nothing in an empty list
98 * Skip beginning whitespace, all whitespace is empty list.
101 while (is_space(*n_str
)) {
104 if (*n_str
== '\0') {
110 * Make a pass where any two numbers separated by just whitespace
111 * are given a comma separator. Count entries while passing.
114 while (*n_str
!= '\0') {
118 * Skip to a separator
121 while (!is_space(*tptr
) && *tptr
!= ',' && *tptr
!= '\0') {
127 * If no more entries, break out here
129 if (*n_str
== '\0') {
134 * Skip to the end of the separator, noting if we have a
138 while (is_space(*tptr
) || *tptr
== ',') {
149 * If this was trailing whitespace we skipped, we are done.
151 if ((*tptr
== '\0') && !has_comma
) {
155 * Else if the separator is all whitespace, and this is not the
156 * end of the string, add a comma to the separator.
158 else if (!has_comma
) {
163 * count the entry skipped.
170 * count the last entry in the list.
175 * Allocate space for the coordinate array.
177 UniquePtr
<nscoord
[]> value_list
= MakeUnique
<nscoord
[]>(cnt
);
184 * Second pass to copy integer values into list.
187 for (i
= 0; i
< cnt
; i
++) {
190 ptr
= strchr(tptr
, ',');
195 * Strip whitespace in front of number because I don't
196 * trust atoi to do it on all platforms.
198 while (is_space(*tptr
)) {
204 value_list
[i
] = (nscoord
)::atoi(tptr
);
213 mCoords
= std::move(value_list
);
219 void Area::HasFocus(bool aHasFocus
) { mHasFocus
= aHasFocus
; }
221 //----------------------------------------------------------------------
223 class DefaultArea final
: public Area
{
225 explicit DefaultArea(HTMLAreaElement
* aArea
);
227 virtual bool IsInside(nscoord x
, nscoord y
) const override
;
228 virtual void Draw(nsIFrame
* aFrame
, DrawTarget
& aDrawTarget
,
229 const ColorPattern
& aColor
,
230 const StrokeOptions
& aStrokeOptions
) override
;
231 virtual void GetRect(nsIFrame
* aFrame
, nsRect
& aRect
) override
;
234 DefaultArea::DefaultArea(HTMLAreaElement
* aArea
) : Area(aArea
) {}
236 bool DefaultArea::IsInside(nscoord x
, nscoord y
) const { return true; }
238 void DefaultArea::Draw(nsIFrame
* aFrame
, DrawTarget
& aDrawTarget
,
239 const ColorPattern
& aColor
,
240 const StrokeOptions
& aStrokeOptions
) {
242 nsRect
r(nsPoint(0, 0), aFrame
->GetSize());
243 const nscoord kOnePixel
= nsPresContext::CSSPixelsToAppUnits(1);
244 r
.width
-= kOnePixel
;
245 r
.height
-= kOnePixel
;
246 Rect rect
= ToRect(nsLayoutUtils::RectToGfxRect(
247 r
, aFrame
->PresContext()->AppUnitsPerDevPixel()));
248 StrokeSnappedEdgesOfRect(rect
, aDrawTarget
, aColor
, aStrokeOptions
);
252 void DefaultArea::GetRect(nsIFrame
* aFrame
, nsRect
& aRect
) {
253 aRect
= aFrame
->GetRect();
257 //----------------------------------------------------------------------
259 class RectArea final
: public Area
{
261 explicit RectArea(HTMLAreaElement
* aArea
);
263 virtual void ParseCoords(const nsAString
& aSpec
) override
;
264 virtual bool IsInside(nscoord x
, nscoord y
) const override
;
265 virtual void Draw(nsIFrame
* aFrame
, DrawTarget
& aDrawTarget
,
266 const ColorPattern
& aColor
,
267 const StrokeOptions
& aStrokeOptions
) override
;
268 virtual void GetRect(nsIFrame
* aFrame
, nsRect
& aRect
) override
;
271 RectArea::RectArea(HTMLAreaElement
* aArea
) : Area(aArea
) {}
273 void RectArea::ParseCoords(const nsAString
& aSpec
) {
274 Area::ParseCoords(aSpec
);
276 bool saneRect
= true;
277 int32_t flag
= nsIScriptError::warningFlag
;
278 if (mNumCoords
>= 4) {
279 if (mCoords
[0] > mCoords
[2]) {
280 // x-coords in reversed order
281 nscoord x
= mCoords
[2];
282 mCoords
[2] = mCoords
[0];
287 if (mCoords
[1] > mCoords
[3]) {
288 // y-coords in reversed order
289 nscoord y
= mCoords
[3];
290 mCoords
[3] = mCoords
[1];
295 if (mNumCoords
> 4) {
296 // Someone missed the concept of a rect here
301 flag
= nsIScriptError::errorFlag
;
305 logMessage(mArea
, aSpec
, flag
, "ImageMapRectBoundsError");
309 bool RectArea::IsInside(nscoord x
, nscoord y
) const {
310 if (mNumCoords
>= 4) { // Note: > is for nav compatibility
311 nscoord x1
= mCoords
[0];
312 nscoord y1
= mCoords
[1];
313 nscoord x2
= mCoords
[2];
314 nscoord y2
= mCoords
[3];
315 NS_ASSERTION(x1
<= x2
&& y1
<= y2
,
316 "Someone screwed up RectArea::ParseCoords");
317 if ((x
>= x1
) && (x
<= x2
) && (y
>= y1
) && (y
<= y2
)) {
324 void RectArea::Draw(nsIFrame
* aFrame
, DrawTarget
& aDrawTarget
,
325 const ColorPattern
& aColor
,
326 const StrokeOptions
& aStrokeOptions
) {
328 if (mNumCoords
>= 4) {
329 nscoord x1
= nsPresContext::CSSPixelsToAppUnits(mCoords
[0]);
330 nscoord y1
= nsPresContext::CSSPixelsToAppUnits(mCoords
[1]);
331 nscoord x2
= nsPresContext::CSSPixelsToAppUnits(mCoords
[2]);
332 nscoord y2
= nsPresContext::CSSPixelsToAppUnits(mCoords
[3]);
333 NS_ASSERTION(x1
<= x2
&& y1
<= y2
,
334 "Someone screwed up RectArea::ParseCoords");
335 nsRect
r(x1
, y1
, x2
- x1
, y2
- y1
);
336 Rect rect
= ToRect(nsLayoutUtils::RectToGfxRect(
337 r
, aFrame
->PresContext()->AppUnitsPerDevPixel()));
338 StrokeSnappedEdgesOfRect(rect
, aDrawTarget
, aColor
, aStrokeOptions
);
343 void RectArea::GetRect(nsIFrame
* aFrame
, nsRect
& aRect
) {
344 if (mNumCoords
>= 4) {
345 nscoord x1
= nsPresContext::CSSPixelsToAppUnits(mCoords
[0]);
346 nscoord y1
= nsPresContext::CSSPixelsToAppUnits(mCoords
[1]);
347 nscoord x2
= nsPresContext::CSSPixelsToAppUnits(mCoords
[2]);
348 nscoord y2
= nsPresContext::CSSPixelsToAppUnits(mCoords
[3]);
349 NS_ASSERTION(x1
<= x2
&& y1
<= y2
,
350 "Someone screwed up RectArea::ParseCoords");
352 aRect
.SetRect(x1
, y1
, x2
, y2
);
356 //----------------------------------------------------------------------
358 class PolyArea final
: public Area
{
360 explicit PolyArea(HTMLAreaElement
* aArea
);
362 virtual void ParseCoords(const nsAString
& aSpec
) override
;
363 virtual bool IsInside(nscoord x
, nscoord y
) const override
;
364 virtual void Draw(nsIFrame
* aFrame
, DrawTarget
& aDrawTarget
,
365 const ColorPattern
& aColor
,
366 const StrokeOptions
& aStrokeOptions
) override
;
367 virtual void GetRect(nsIFrame
* aFrame
, nsRect
& aRect
) override
;
370 PolyArea::PolyArea(HTMLAreaElement
* aArea
) : Area(aArea
) {}
372 void PolyArea::ParseCoords(const nsAString
& aSpec
) {
373 Area::ParseCoords(aSpec
);
375 if (mNumCoords
>= 2) {
376 if (mNumCoords
& 1U) {
377 logMessage(mArea
, aSpec
, nsIScriptError::warningFlag
,
378 "ImageMapPolyOddNumberOfCoords");
381 logMessage(mArea
, aSpec
, nsIScriptError::errorFlag
,
382 "ImageMapPolyWrongNumberOfCoords");
386 bool PolyArea::IsInside(nscoord x
, nscoord y
) const {
387 if (mNumCoords
>= 6) {
388 int32_t intersects
= 0;
391 int32_t totalv
= mNumCoords
/ 2;
392 int32_t totalc
= totalv
* 2;
393 nscoord xval
= mCoords
[totalc
- 2];
394 nscoord yval
= mCoords
[totalc
- 1];
395 int32_t end
= totalc
;
398 if ((yval
>= wherey
) != (mCoords
[pointer
] >= wherey
)) {
399 if ((xval
>= wherex
) == (mCoords
[0] >= wherex
)) {
400 intersects
+= (xval
>= wherex
) ? 1 : 0;
402 intersects
+= ((xval
- (yval
- wherey
) * (mCoords
[0] - xval
) /
403 (mCoords
[pointer
] - yval
)) >= wherex
)
409 // XXX I wonder what this is doing; this is a translation of ptinpoly.c
410 while (pointer
< end
) {
411 yval
= mCoords
[pointer
];
413 if (yval
>= wherey
) {
414 while ((pointer
< end
) && (mCoords
[pointer
] >= wherey
)) pointer
+= 2;
415 if (pointer
>= end
) break;
416 if ((mCoords
[pointer
- 3] >= wherex
) ==
417 (mCoords
[pointer
- 1] >= wherex
)) {
418 intersects
+= (mCoords
[pointer
- 3] >= wherex
) ? 1 : 0;
421 ((mCoords
[pointer
- 3] -
422 (mCoords
[pointer
- 2] - wherey
) *
423 (mCoords
[pointer
- 1] - mCoords
[pointer
- 3]) /
424 (mCoords
[pointer
] - mCoords
[pointer
- 2])) >= wherex
)
429 while ((pointer
< end
) && (mCoords
[pointer
] < wherey
)) pointer
+= 2;
430 if (pointer
>= end
) break;
431 if ((mCoords
[pointer
- 3] >= wherex
) ==
432 (mCoords
[pointer
- 1] >= wherex
)) {
433 intersects
+= (mCoords
[pointer
- 3] >= wherex
) ? 1 : 0;
436 ((mCoords
[pointer
- 3] -
437 (mCoords
[pointer
- 2] - wherey
) *
438 (mCoords
[pointer
- 1] - mCoords
[pointer
- 3]) /
439 (mCoords
[pointer
] - mCoords
[pointer
- 2])) >= wherex
)
445 if ((intersects
& 1) != 0) {
452 void PolyArea::Draw(nsIFrame
* aFrame
, DrawTarget
& aDrawTarget
,
453 const ColorPattern
& aColor
,
454 const StrokeOptions
& aStrokeOptions
) {
456 if (mNumCoords
>= 6) {
457 // Where possible, we want all horizontal and vertical lines to align on
458 // pixel rows or columns, and to start at pixel boundaries so that one
459 // pixel dashing neatly sits on pixels to give us neat lines. To achieve
460 // that we draw each line segment as a separate path, snapping it to
461 // device pixels if applicable.
462 nsPresContext
* pc
= aFrame
->PresContext();
463 Point
p1(pc
->CSSPixelsToDevPixels(mCoords
[0]),
464 pc
->CSSPixelsToDevPixels(mCoords
[1]));
465 Point p2
, p1snapped
, p2snapped
;
466 for (int32_t i
= 2; i
< mNumCoords
- 1; i
+= 2) {
467 p2
.x
= pc
->CSSPixelsToDevPixels(mCoords
[i
]);
468 p2
.y
= pc
->CSSPixelsToDevPixels(mCoords
[i
+ 1]);
471 SnapLineToDevicePixelsForStroking(p1snapped
, p2snapped
, aDrawTarget
,
472 aStrokeOptions
.mLineWidth
);
473 aDrawTarget
.StrokeLine(p1snapped
, p2snapped
, aColor
, aStrokeOptions
);
476 p2
.x
= pc
->CSSPixelsToDevPixels(mCoords
[0]);
477 p2
.y
= pc
->CSSPixelsToDevPixels(mCoords
[1]);
480 SnapLineToDevicePixelsForStroking(p1snapped
, p2snapped
, aDrawTarget
,
481 aStrokeOptions
.mLineWidth
);
482 aDrawTarget
.StrokeLine(p1snapped
, p2snapped
, aColor
, aStrokeOptions
);
487 void PolyArea::GetRect(nsIFrame
* aFrame
, nsRect
& aRect
) {
488 if (mNumCoords
>= 6) {
489 nscoord x1
, x2
, y1
, y2
, xtmp
, ytmp
;
490 x1
= x2
= nsPresContext::CSSPixelsToAppUnits(mCoords
[0]);
491 y1
= y2
= nsPresContext::CSSPixelsToAppUnits(mCoords
[1]);
492 for (int32_t i
= 2; i
< mNumCoords
- 1; i
+= 2) {
493 xtmp
= nsPresContext::CSSPixelsToAppUnits(mCoords
[i
]);
494 ytmp
= nsPresContext::CSSPixelsToAppUnits(mCoords
[i
+ 1]);
495 x1
= x1
< xtmp
? x1
: xtmp
;
496 y1
= y1
< ytmp
? y1
: ytmp
;
497 x2
= x2
> xtmp
? x2
: xtmp
;
498 y2
= y2
> ytmp
? y2
: ytmp
;
501 aRect
.SetRect(x1
, y1
, x2
, y2
);
505 //----------------------------------------------------------------------
507 class CircleArea final
: public Area
{
509 explicit CircleArea(HTMLAreaElement
* aArea
);
511 virtual void ParseCoords(const nsAString
& aSpec
) override
;
512 virtual bool IsInside(nscoord x
, nscoord y
) const override
;
513 virtual void Draw(nsIFrame
* aFrame
, DrawTarget
& aDrawTarget
,
514 const ColorPattern
& aColor
,
515 const StrokeOptions
& aStrokeOptions
) override
;
516 virtual void GetRect(nsIFrame
* aFrame
, nsRect
& aRect
) override
;
519 CircleArea::CircleArea(HTMLAreaElement
* aArea
) : Area(aArea
) {}
521 void CircleArea::ParseCoords(const nsAString
& aSpec
) {
522 Area::ParseCoords(aSpec
);
524 bool wrongNumberOfCoords
= false;
525 int32_t flag
= nsIScriptError::warningFlag
;
526 if (mNumCoords
>= 3) {
527 if (mCoords
[2] < 0) {
528 logMessage(mArea
, aSpec
, nsIScriptError::errorFlag
,
529 "ImageMapCircleNegativeRadius");
532 if (mNumCoords
> 3) {
533 wrongNumberOfCoords
= true;
536 wrongNumberOfCoords
= true;
537 flag
= nsIScriptError::errorFlag
;
540 if (wrongNumberOfCoords
) {
541 logMessage(mArea
, aSpec
, flag
, "ImageMapCircleWrongNumberOfCoords");
545 bool CircleArea::IsInside(nscoord x
, nscoord y
) const {
546 // Note: > is for nav compatibility
547 if (mNumCoords
>= 3) {
548 nscoord x1
= mCoords
[0];
549 nscoord y1
= mCoords
[1];
550 nscoord radius
= mCoords
[2];
556 nscoord dist
= (dx
* dx
) + (dy
* dy
);
557 if (dist
<= (radius
* radius
)) {
564 void CircleArea::Draw(nsIFrame
* aFrame
, DrawTarget
& aDrawTarget
,
565 const ColorPattern
& aColor
,
566 const StrokeOptions
& aStrokeOptions
) {
568 if (mNumCoords
>= 3) {
569 Point
center(aFrame
->PresContext()->CSSPixelsToDevPixels(mCoords
[0]),
570 aFrame
->PresContext()->CSSPixelsToDevPixels(mCoords
[1]));
572 2 * aFrame
->PresContext()->CSSPixelsToDevPixels(mCoords
[2]);
576 RefPtr
<PathBuilder
> builder
= aDrawTarget
.CreatePathBuilder();
577 AppendEllipseToPath(builder
, center
, Size(diameter
, diameter
));
578 RefPtr
<Path
> circle
= builder
->Finish();
579 aDrawTarget
.Stroke(circle
, aColor
, aStrokeOptions
);
584 void CircleArea::GetRect(nsIFrame
* aFrame
, nsRect
& aRect
) {
585 if (mNumCoords
>= 3) {
586 nscoord x1
= nsPresContext::CSSPixelsToAppUnits(mCoords
[0]);
587 nscoord y1
= nsPresContext::CSSPixelsToAppUnits(mCoords
[1]);
588 nscoord radius
= nsPresContext::CSSPixelsToAppUnits(mCoords
[2]);
593 aRect
.SetRect(x1
- radius
, y1
- radius
, x1
+ radius
, y1
+ radius
);
597 //----------------------------------------------------------------------
599 nsImageMap::nsImageMap() : mImageFrame(nullptr), mConsiderWholeSubtree(false) {}
601 nsImageMap::~nsImageMap() {
602 NS_ASSERTION(mAreas
.Length() == 0, "Destroy was not called");
605 NS_IMPL_ISUPPORTS(nsImageMap
, nsIMutationObserver
, nsIDOMEventListener
)
607 nsresult
nsImageMap::GetBoundsForAreaContent(nsIContent
* aContent
,
609 NS_ENSURE_TRUE(aContent
&& mImageFrame
, NS_ERROR_INVALID_ARG
);
611 // Find the Area struct associated with this content node, and return bounds
612 for (auto& area
: mAreas
) {
613 if (area
->mArea
== aContent
) {
615 area
->GetRect(mImageFrame
, aBounds
);
619 return NS_ERROR_FAILURE
;
622 void nsImageMap::AreaRemoved(HTMLAreaElement
* aArea
) {
623 if (aArea
->GetPrimaryFrame() == mImageFrame
) {
624 aArea
->SetPrimaryFrame(nullptr);
627 aArea
->RemoveSystemEventListener(u
"focus"_ns
, this, false);
628 aArea
->RemoveSystemEventListener(u
"blur"_ns
, this, false);
631 void nsImageMap::FreeAreas() {
632 for (UniquePtr
<Area
>& area
: mAreas
) {
633 AreaRemoved(area
->mArea
);
639 void nsImageMap::Init(nsImageFrame
* aImageFrame
, nsIContent
* aMap
) {
641 MOZ_ASSERT(aImageFrame
);
643 mImageFrame
= aImageFrame
;
645 mMap
->AddMutationObserver(this);
647 // "Compile" the areas in the map into faster access versions
651 void nsImageMap::SearchForAreas(nsIContent
* aParent
) {
652 // Look for <area> elements.
653 for (nsIContent
* child
= aParent
->GetFirstChild(); child
;
654 child
= child
->GetNextSibling()) {
655 if (auto* area
= HTMLAreaElement::FromNode(child
)) {
658 // Continue to next child. This stops mConsiderWholeSubtree from
659 // getting set. It also makes us ignore children of <area>s which
660 // is consistent with how we react to dynamic insertion of such
665 if (child
->IsElement()) {
666 mConsiderWholeSubtree
= true;
667 SearchForAreas(child
);
672 void nsImageMap::UpdateAreas() {
673 // Get rid of old area data
676 mConsiderWholeSubtree
= false;
677 SearchForAreas(mMap
);
680 if (nsAccessibilityService
* accService
= GetAccService()) {
681 accService
->UpdateImageMap(mImageFrame
);
686 void nsImageMap::AddArea(HTMLAreaElement
* aArea
) {
687 static AttrArray::AttrValuesArray strings
[] = {
688 nsGkAtoms::rect
, nsGkAtoms::rectangle
,
689 nsGkAtoms::circle
, nsGkAtoms::circ
,
690 nsGkAtoms::_default
, nsGkAtoms::poly
,
691 nsGkAtoms::polygon
, nullptr};
693 UniquePtr
<Area
> area
;
694 switch (aArea
->FindAttrValueIn(kNameSpaceID_None
, nsGkAtoms::shape
, strings
,
696 case AttrArray::ATTR_VALUE_NO_MATCH
:
697 case AttrArray::ATTR_MISSING
:
700 area
= MakeUnique
<RectArea
>(aArea
);
704 area
= MakeUnique
<CircleArea
>(aArea
);
707 area
= MakeUnique
<DefaultArea
>(aArea
);
711 area
= MakeUnique
<PolyArea
>(aArea
);
715 MOZ_ASSERT_UNREACHABLE("FindAttrValueIn returned an unexpected value.");
719 // Add focus listener to track area focus changes
720 aArea
->AddSystemEventListener(u
"focus"_ns
, this, false, false);
721 aArea
->AddSystemEventListener(u
"blur"_ns
, this, false, false);
723 // This is a nasty hack. It needs to go away: see bug 135040. Once this is
724 // removed, the code added to RestyleManager::RestyleElement,
725 // nsCSSFrameConstructor::ContentRemoved (both hacks there), and
726 // RestyleManager::ProcessRestyledFrames to work around this issue can
728 aArea
->SetPrimaryFrame(mImageFrame
);
731 aArea
->GetAttr(nsGkAtoms::coords
, coords
);
732 area
->ParseCoords(coords
);
733 mAreas
.AppendElement(std::move(area
));
736 HTMLAreaElement
* nsImageMap::GetArea(const CSSIntPoint
& aPt
) const {
737 NS_ASSERTION(mMap
, "Not initialized");
738 for (const auto& area
: mAreas
) {
739 if (area
->IsInside(aPt
.x
, aPt
.y
)) {
747 HTMLAreaElement
* nsImageMap::GetAreaAt(uint32_t aIndex
) const {
748 return mAreas
.ElementAt(aIndex
)->mArea
;
751 void nsImageMap::Draw(nsIFrame
* aFrame
, DrawTarget
& aDrawTarget
,
752 const ColorPattern
& aColor
,
753 const StrokeOptions
& aStrokeOptions
) {
754 for (auto& area
: mAreas
) {
755 area
->Draw(aFrame
, aDrawTarget
, aColor
, aStrokeOptions
);
759 void nsImageMap::MaybeUpdateAreas(nsIContent
* aContent
) {
760 if (aContent
== mMap
|| mConsiderWholeSubtree
) {
765 void nsImageMap::AttributeChanged(dom::Element
* aElement
, int32_t aNameSpaceID
,
766 nsAtom
* aAttribute
, int32_t aModType
,
767 const nsAttrValue
* aOldValue
) {
768 // If the parent of the changing content node is our map then update
769 // the map. But only do this if the node is an HTML <area> or <a>
770 // and the attribute that's changing is "shape" or "coords" -- those
771 // are the only cases we care about.
772 if ((aElement
->NodeInfo()->Equals(nsGkAtoms::area
) ||
773 aElement
->NodeInfo()->Equals(nsGkAtoms::a
)) &&
774 aElement
->IsHTMLElement() && aNameSpaceID
== kNameSpaceID_None
&&
775 (aAttribute
== nsGkAtoms::shape
|| aAttribute
== nsGkAtoms::coords
)) {
776 MaybeUpdateAreas(aElement
->GetParent());
777 } else if (aElement
== mMap
&& aNameSpaceID
== kNameSpaceID_None
&&
778 (aAttribute
== nsGkAtoms::name
|| aAttribute
== nsGkAtoms::id
) &&
780 // ID or name has changed. Let ImageFrame recreate ImageMap.
781 mImageFrame
->DisconnectMap();
785 void nsImageMap::ContentAppended(nsIContent
* aFirstNewContent
) {
786 MaybeUpdateAreas(aFirstNewContent
->GetParent());
789 void nsImageMap::ContentInserted(nsIContent
* aChild
) {
790 MaybeUpdateAreas(aChild
->GetParent());
793 static UniquePtr
<Area
> TakeArea(nsImageMap::AreaList
& aAreas
,
794 HTMLAreaElement
* aArea
) {
795 UniquePtr
<Area
> result
;
797 for (UniquePtr
<Area
>& area
: aAreas
) {
798 if (area
->mArea
== aArea
) {
799 result
= std::move(area
);
806 aAreas
.RemoveElementAt(index
);
812 void nsImageMap::ContentRemoved(nsIContent
* aChild
,
813 nsIContent
* aPreviousSibling
) {
814 if (aChild
->GetParent() != mMap
&& !mConsiderWholeSubtree
) {
818 auto* areaElement
= HTMLAreaElement::FromNode(aChild
);
823 UniquePtr
<Area
> area
= TakeArea(mAreas
, areaElement
);
828 AreaRemoved(area
->mArea
);
831 if (nsAccessibilityService
* accService
= GetAccService()) {
832 accService
->UpdateImageMap(mImageFrame
);
837 void nsImageMap::ParentChainChanged(nsIContent
* aContent
) {
838 NS_ASSERTION(aContent
== mMap
, "Unexpected ParentChainChanged notification!");
840 mImageFrame
->DisconnectMap();
844 nsresult
nsImageMap::HandleEvent(Event
* aEvent
) {
845 nsAutoString eventType
;
846 aEvent
->GetType(eventType
);
847 bool focus
= eventType
.EqualsLiteral("focus");
848 MOZ_ASSERT(focus
== !eventType
.EqualsLiteral("blur"),
849 "Unexpected event type");
851 // Set which one of our areas changed focus
852 nsCOMPtr
<nsIContent
> targetContent
= do_QueryInterface(aEvent
->GetTarget());
853 if (!targetContent
) {
857 for (auto& area
: mAreas
) {
858 if (area
->mArea
== targetContent
) {
859 // Set or Remove internal focus
860 area
->HasFocus(focus
);
861 // Now invalidate the rect
863 mImageFrame
->InvalidateFrame();
871 void nsImageMap::Destroy() {
873 mImageFrame
= nullptr;
874 mMap
->RemoveMutationObserver(this);