Update configs. IGNORE BROKEN CHANGESETS CLOSED TREE NO BUG a=release ba=release
[gecko.git] / layout / generic / nsImageMap.cpp
blobe6c5cfd1576063161b01ca234b75b3ffcc212a1d
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"
16 #include "nsString.h"
17 #include "nsReadableUtils.h"
18 #include "nsPresContext.h"
19 #include "nsNameSpaceManager.h"
20 #include "nsGkAtoms.h"
21 #include "nsImageFrame.h"
22 #include "nsCoord.h"
23 #include "nsIContentInlines.h"
24 #include "nsIScriptError.h"
25 #include "nsContentUtils.h"
26 #include "nsLayoutUtils.h"
28 #ifdef ACCESSIBILITY
29 # include "nsAccessibilityService.h"
30 #endif
32 using namespace mozilla;
33 using namespace mozilla::gfx;
34 using namespace mozilla::dom;
36 class Area {
37 public:
38 explicit Area(HTMLAreaElement* aArea);
39 virtual ~Area();
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;
53 int32_t mNumCoords;
54 bool mHasFocus;
57 Area::Area(HTMLAreaElement* aArea) : mArea(aArea) {
58 MOZ_COUNT_CTOR(Area);
59 MOZ_ASSERT(mArea, "How did that happen?");
60 mNumCoords = 0;
61 mHasFocus = false;
64 Area::~Area() { MOZ_COUNT_DTOR(Area); }
66 #include <stdlib.h>
68 inline bool is_space(char c) {
69 return (c == ' ' || c == '\f' || c == '\n' || c == '\r' || c == '\t' ||
70 c == '\v');
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);
82 if (cp) {
83 char* tptr;
84 char* n_str;
85 int32_t i, cnt;
88 * Nothing in an empty list
90 mNumCoords = 0;
91 mCoords = nullptr;
92 if (*cp == '\0') {
93 free(cp);
94 return;
98 * Skip beginning whitespace, all whitespace is empty list.
100 n_str = cp;
101 while (is_space(*n_str)) {
102 n_str++;
104 if (*n_str == '\0') {
105 free(cp);
106 return;
110 * Make a pass where any two numbers separated by just whitespace
111 * are given a comma separator. Count entries while passing.
113 cnt = 0;
114 while (*n_str != '\0') {
115 bool has_comma;
118 * Skip to a separator
120 tptr = n_str;
121 while (!is_space(*tptr) && *tptr != ',' && *tptr != '\0') {
122 tptr++;
124 n_str = tptr;
127 * If no more entries, break out here
129 if (*n_str == '\0') {
130 break;
134 * Skip to the end of the separator, noting if we have a
135 * comma.
137 has_comma = false;
138 while (is_space(*tptr) || *tptr == ',') {
139 if (*tptr == ',') {
140 if (!has_comma) {
141 has_comma = true;
142 } else {
143 break;
146 tptr++;
149 * If this was trailing whitespace we skipped, we are done.
151 if ((*tptr == '\0') && !has_comma) {
152 break;
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) {
159 *n_str = ',';
163 * count the entry skipped.
165 cnt++;
167 n_str = tptr;
170 * count the last entry in the list.
172 cnt++;
175 * Allocate space for the coordinate array.
177 UniquePtr<nscoord[]> value_list = MakeUnique<nscoord[]>(cnt);
178 if (!value_list) {
179 free(cp);
180 return;
184 * Second pass to copy integer values into list.
186 tptr = cp;
187 for (i = 0; i < cnt; i++) {
188 char* ptr;
190 ptr = strchr(tptr, ',');
191 if (ptr) {
192 *ptr = '\0';
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)) {
199 tptr++;
201 if (*tptr == '\0') {
202 value_list[i] = 0;
203 } else {
204 value_list[i] = (nscoord)::atoi(tptr);
206 if (ptr) {
207 *ptr = ',';
208 tptr = ptr + 1;
212 mNumCoords = cnt;
213 mCoords = std::move(value_list);
215 free(cp);
219 void Area::HasFocus(bool aHasFocus) { mHasFocus = aHasFocus; }
221 //----------------------------------------------------------------------
223 class DefaultArea final : public Area {
224 public:
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) {
241 if (mHasFocus) {
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();
254 aRect.MoveTo(0, 0);
257 //----------------------------------------------------------------------
259 class RectArea final : public Area {
260 public:
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];
283 mCoords[0] = x;
284 saneRect = false;
287 if (mCoords[1] > mCoords[3]) {
288 // y-coords in reversed order
289 nscoord y = mCoords[3];
290 mCoords[3] = mCoords[1];
291 mCoords[1] = y;
292 saneRect = false;
295 if (mNumCoords > 4) {
296 // Someone missed the concept of a rect here
297 saneRect = false;
299 } else {
300 saneRect = false;
301 flag = nsIScriptError::errorFlag;
304 if (!saneRect) {
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)) {
318 return true;
321 return false;
324 void RectArea::Draw(nsIFrame* aFrame, DrawTarget& aDrawTarget,
325 const ColorPattern& aColor,
326 const StrokeOptions& aStrokeOptions) {
327 if (mHasFocus) {
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 {
359 public:
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");
380 } else {
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;
389 nscoord wherex = x;
390 nscoord wherey = y;
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;
396 int32_t pointer = 1;
398 if ((yval >= wherey) != (mCoords[pointer] >= wherey)) {
399 if ((xval >= wherex) == (mCoords[0] >= wherex)) {
400 intersects += (xval >= wherex) ? 1 : 0;
401 } else {
402 intersects += ((xval - (yval - wherey) * (mCoords[0] - xval) /
403 (mCoords[pointer] - yval)) >= wherex)
405 : 0;
409 // XXX I wonder what this is doing; this is a translation of ptinpoly.c
410 while (pointer < end) {
411 yval = mCoords[pointer];
412 pointer += 2;
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;
419 } else {
420 intersects +=
421 ((mCoords[pointer - 3] -
422 (mCoords[pointer - 2] - wherey) *
423 (mCoords[pointer - 1] - mCoords[pointer - 3]) /
424 (mCoords[pointer] - mCoords[pointer - 2])) >= wherex)
426 : 0;
428 } else {
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;
434 } else {
435 intersects +=
436 ((mCoords[pointer - 3] -
437 (mCoords[pointer - 2] - wherey) *
438 (mCoords[pointer - 1] - mCoords[pointer - 3]) /
439 (mCoords[pointer] - mCoords[pointer - 2])) >= wherex)
441 : 0;
445 if ((intersects & 1) != 0) {
446 return true;
449 return false;
452 void PolyArea::Draw(nsIFrame* aFrame, DrawTarget& aDrawTarget,
453 const ColorPattern& aColor,
454 const StrokeOptions& aStrokeOptions) {
455 if (mHasFocus) {
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]);
469 p1snapped = p1;
470 p2snapped = p2;
471 SnapLineToDevicePixelsForStroking(p1snapped, p2snapped, aDrawTarget,
472 aStrokeOptions.mLineWidth);
473 aDrawTarget.StrokeLine(p1snapped, p2snapped, aColor, aStrokeOptions);
474 p1 = p2;
476 p2.x = pc->CSSPixelsToDevPixels(mCoords[0]);
477 p2.y = pc->CSSPixelsToDevPixels(mCoords[1]);
478 p1snapped = p1;
479 p2snapped = p2;
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 {
508 public:
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;
535 } else {
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];
551 if (radius < 0) {
552 return false;
554 nscoord dx = x1 - x;
555 nscoord dy = y1 - y;
556 nscoord dist = (dx * dx) + (dy * dy);
557 if (dist <= (radius * radius)) {
558 return true;
561 return false;
564 void CircleArea::Draw(nsIFrame* aFrame, DrawTarget& aDrawTarget,
565 const ColorPattern& aColor,
566 const StrokeOptions& aStrokeOptions) {
567 if (mHasFocus) {
568 if (mNumCoords >= 3) {
569 Point center(aFrame->PresContext()->CSSPixelsToDevPixels(mCoords[0]),
570 aFrame->PresContext()->CSSPixelsToDevPixels(mCoords[1]));
571 Float diameter =
572 2 * aFrame->PresContext()->CSSPixelsToDevPixels(mCoords[2]);
573 if (diameter <= 0) {
574 return;
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]);
589 if (radius < 0) {
590 return;
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,
608 nsRect& aBounds) {
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) {
614 aBounds = nsRect();
615 area->GetRect(mImageFrame, aBounds);
616 return NS_OK;
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);
636 mAreas.Clear();
639 void nsImageMap::Init(nsImageFrame* aImageFrame, nsIContent* aMap) {
640 MOZ_ASSERT(aMap);
641 MOZ_ASSERT(aImageFrame);
643 mImageFrame = aImageFrame;
644 mMap = aMap;
645 mMap->AddMutationObserver(this);
647 // "Compile" the areas in the map into faster access versions
648 UpdateAreas();
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)) {
656 AddArea(area);
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
661 // children.
662 continue;
665 if (child->IsElement()) {
666 mConsiderWholeSubtree = true;
667 SearchForAreas(child);
672 void nsImageMap::UpdateAreas() {
673 // Get rid of old area data
674 FreeAreas();
676 mConsiderWholeSubtree = false;
677 SearchForAreas(mMap);
679 #ifdef ACCESSIBILITY
680 if (nsAccessibilityService* accService = GetAccService()) {
681 accService->UpdateImageMap(mImageFrame);
683 #endif
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,
695 eIgnoreCase)) {
696 case AttrArray::ATTR_VALUE_NO_MATCH:
697 case AttrArray::ATTR_MISSING:
698 case 0:
699 case 1:
700 area = MakeUnique<RectArea>(aArea);
701 break;
702 case 2:
703 case 3:
704 area = MakeUnique<CircleArea>(aArea);
705 break;
706 case 4:
707 area = MakeUnique<DefaultArea>(aArea);
708 break;
709 case 5:
710 case 6:
711 area = MakeUnique<PolyArea>(aArea);
712 break;
713 default:
714 area = nullptr;
715 MOZ_ASSERT_UNREACHABLE("FindAttrValueIn returned an unexpected value.");
716 break;
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
727 // be removed.
728 aArea->SetPrimaryFrame(mImageFrame);
730 nsAutoString coords;
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)) {
740 return area->mArea;
744 return nullptr;
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) {
761 UpdateAreas();
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) &&
779 mImageFrame) {
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;
796 size_t index = 0;
797 for (UniquePtr<Area>& area : aAreas) {
798 if (area->mArea == aArea) {
799 result = std::move(area);
800 break;
802 index++;
805 if (result) {
806 aAreas.RemoveElementAt(index);
809 return result;
812 void nsImageMap::ContentRemoved(nsIContent* aChild,
813 nsIContent* aPreviousSibling) {
814 if (aChild->GetParent() != mMap && !mConsiderWholeSubtree) {
815 return;
818 auto* areaElement = HTMLAreaElement::FromNode(aChild);
819 if (!areaElement) {
820 return;
823 UniquePtr<Area> area = TakeArea(mAreas, areaElement);
824 if (!area) {
825 return;
828 AreaRemoved(area->mArea);
830 #ifdef ACCESSIBILITY
831 if (nsAccessibilityService* accService = GetAccService()) {
832 accService->UpdateImageMap(mImageFrame);
834 #endif
837 void nsImageMap::ParentChainChanged(nsIContent* aContent) {
838 NS_ASSERTION(aContent == mMap, "Unexpected ParentChainChanged notification!");
839 if (mImageFrame) {
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) {
854 return NS_OK;
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
862 if (mImageFrame) {
863 mImageFrame->InvalidateFrame();
865 break;
868 return NS_OK;
871 void nsImageMap::Destroy() {
872 FreeAreas();
873 mImageFrame = nullptr;
874 mMap->RemoveMutationObserver(this);