Bug 1468402 - Part 3: Add test for subgrids in the grid list. r=pbro
[gecko.git] / layout / generic / nsImageMap.cpp
blobcd4023e2b48f1dca7b17f0f360d2a50d094c08e9
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 "nsIStringBundle.h"
26 #include "nsContentUtils.h"
27 #include "ImageLayers.h"
29 #ifdef ACCESSIBILITY
30 # include "nsAccessibilityService.h"
31 #endif
33 using namespace mozilla;
34 using namespace mozilla::gfx;
35 using namespace mozilla::dom;
37 class Area {
38 public:
39 explicit Area(HTMLAreaElement* aArea);
40 virtual ~Area();
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;
54 int32_t mNumCoords;
55 bool mHasFocus;
58 Area::Area(HTMLAreaElement* aArea) : mArea(aArea) {
59 MOZ_COUNT_CTOR(Area);
60 MOZ_ASSERT(mArea, "How did that happen?");
61 mNumCoords = 0;
62 mHasFocus = false;
65 Area::~Area() { MOZ_COUNT_DTOR(Area); }
67 #include <stdlib.h>
69 inline bool is_space(char c) {
70 return (c == ' ' || c == '\f' || c == '\n' || c == '\r' || c == '\t' ||
71 c == '\v');
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 */
80 nullptr,
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);
87 if (cp) {
88 char* tptr;
89 char* n_str;
90 int32_t i, cnt;
93 * Nothing in an empty list
95 mNumCoords = 0;
96 mCoords = nullptr;
97 if (*cp == '\0') {
98 free(cp);
99 return;
103 * Skip beginning whitespace, all whitespace is empty list.
105 n_str = cp;
106 while (is_space(*n_str)) {
107 n_str++;
109 if (*n_str == '\0') {
110 free(cp);
111 return;
115 * Make a pass where any two numbers separated by just whitespace
116 * are given a comma separator. Count entries while passing.
118 cnt = 0;
119 while (*n_str != '\0') {
120 bool has_comma;
123 * Skip to a separator
125 tptr = n_str;
126 while (!is_space(*tptr) && *tptr != ',' && *tptr != '\0') {
127 tptr++;
129 n_str = tptr;
132 * If no more entries, break out here
134 if (*n_str == '\0') {
135 break;
139 * Skip to the end of the separator, noting if we have a
140 * comma.
142 has_comma = false;
143 while (is_space(*tptr) || *tptr == ',') {
144 if (*tptr == ',') {
145 if (!has_comma) {
146 has_comma = true;
147 } else {
148 break;
151 tptr++;
154 * If this was trailing whitespace we skipped, we are done.
156 if ((*tptr == '\0') && !has_comma) {
157 break;
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) {
164 *n_str = ',';
168 * count the entry skipped.
170 cnt++;
172 n_str = tptr;
175 * count the last entry in the list.
177 cnt++;
180 * Allocate space for the coordinate array.
182 UniquePtr<nscoord[]> value_list = MakeUnique<nscoord[]>(cnt);
183 if (!value_list) {
184 free(cp);
185 return;
189 * Second pass to copy integer values into list.
191 tptr = cp;
192 for (i = 0; i < cnt; i++) {
193 char* ptr;
195 ptr = strchr(tptr, ',');
196 if (ptr) {
197 *ptr = '\0';
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)) {
204 tptr++;
206 if (*tptr == '\0') {
207 value_list[i] = 0;
208 } else {
209 value_list[i] = (nscoord)::atoi(tptr);
211 if (ptr) {
212 *ptr = ',';
213 tptr = ptr + 1;
217 mNumCoords = cnt;
218 mCoords = std::move(value_list);
220 free(cp);
224 void Area::HasFocus(bool aHasFocus) { mHasFocus = aHasFocus; }
226 //----------------------------------------------------------------------
228 class DefaultArea final : public Area {
229 public:
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) {
246 if (mHasFocus) {
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();
259 aRect.MoveTo(0, 0);
262 //----------------------------------------------------------------------
264 class RectArea final : public Area {
265 public:
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];
288 mCoords[0] = x;
289 saneRect = false;
292 if (mCoords[1] > mCoords[3]) {
293 // y-coords in reversed order
294 nscoord y = mCoords[3];
295 mCoords[3] = mCoords[1];
296 mCoords[1] = y;
297 saneRect = false;
300 if (mNumCoords > 4) {
301 // Someone missed the concept of a rect here
302 saneRect = false;
304 } else {
305 saneRect = false;
306 flag = nsIScriptError::errorFlag;
309 if (!saneRect) {
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)) {
323 return true;
326 return false;
329 void RectArea::Draw(nsIFrame* aFrame, DrawTarget& aDrawTarget,
330 const ColorPattern& aColor,
331 const StrokeOptions& aStrokeOptions) {
332 if (mHasFocus) {
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 {
364 public:
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");
385 } else {
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;
394 nscoord wherex = x;
395 nscoord wherey = y;
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;
401 int32_t pointer = 1;
403 if ((yval >= wherey) != (mCoords[pointer] >= wherey)) {
404 if ((xval >= wherex) == (mCoords[0] >= wherex)) {
405 intersects += (xval >= wherex) ? 1 : 0;
406 } else {
407 intersects += ((xval - (yval - wherey) * (mCoords[0] - xval) /
408 (mCoords[pointer] - yval)) >= wherex)
410 : 0;
414 // XXX I wonder what this is doing; this is a translation of ptinpoly.c
415 while (pointer < end) {
416 yval = mCoords[pointer];
417 pointer += 2;
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;
424 } else {
425 intersects +=
426 ((mCoords[pointer - 3] -
427 (mCoords[pointer - 2] - wherey) *
428 (mCoords[pointer - 1] - mCoords[pointer - 3]) /
429 (mCoords[pointer] - mCoords[pointer - 2])) >= wherex)
431 : 0;
433 } else {
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;
439 } else {
440 intersects +=
441 ((mCoords[pointer - 3] -
442 (mCoords[pointer - 2] - wherey) *
443 (mCoords[pointer - 1] - mCoords[pointer - 3]) /
444 (mCoords[pointer] - mCoords[pointer - 2])) >= wherex)
446 : 0;
450 if ((intersects & 1) != 0) {
451 return true;
454 return false;
457 void PolyArea::Draw(nsIFrame* aFrame, DrawTarget& aDrawTarget,
458 const ColorPattern& aColor,
459 const StrokeOptions& aStrokeOptions) {
460 if (mHasFocus) {
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]);
474 p1snapped = p1;
475 p2snapped = p2;
476 SnapLineToDevicePixelsForStroking(p1snapped, p2snapped, aDrawTarget,
477 aStrokeOptions.mLineWidth);
478 aDrawTarget.StrokeLine(p1snapped, p2snapped, aColor, aStrokeOptions);
479 p1 = p2;
481 p2.x = pc->CSSPixelsToDevPixels(mCoords[0]);
482 p2.y = pc->CSSPixelsToDevPixels(mCoords[1]);
483 p1snapped = p1;
484 p2snapped = p2;
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 {
513 public:
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;
540 } else {
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];
556 if (radius < 0) {
557 return false;
559 nscoord dx = x1 - x;
560 nscoord dy = y1 - y;
561 nscoord dist = (dx * dx) + (dy * dy);
562 if (dist <= (radius * radius)) {
563 return true;
566 return false;
569 void CircleArea::Draw(nsIFrame* aFrame, DrawTarget& aDrawTarget,
570 const ColorPattern& aColor,
571 const StrokeOptions& aStrokeOptions) {
572 if (mHasFocus) {
573 if (mNumCoords >= 3) {
574 Point center(aFrame->PresContext()->CSSPixelsToDevPixels(mCoords[0]),
575 aFrame->PresContext()->CSSPixelsToDevPixels(mCoords[1]));
576 Float diameter =
577 2 * aFrame->PresContext()->CSSPixelsToDevPixels(mCoords[2]);
578 if (diameter <= 0) {
579 return;
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]);
594 if (radius < 0) {
595 return;
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,
613 nsRect& aBounds) {
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) {
619 aBounds = nsRect();
620 area->GetRect(mImageFrame, aBounds);
621 return NS_OK;
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);
641 mAreas.Clear();
644 void nsImageMap::Init(nsImageFrame* aImageFrame, nsIContent* aMap) {
645 MOZ_ASSERT(aMap);
646 MOZ_ASSERT(aImageFrame);
648 mImageFrame = aImageFrame;
649 mMap = aMap;
650 mMap->AddMutationObserver(this);
652 // "Compile" the areas in the map into faster access versions
653 UpdateAreas();
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)) {
661 AddArea(area);
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
666 // children.
667 continue;
670 if (child->IsElement()) {
671 mConsiderWholeSubtree = true;
672 SearchForAreas(child);
677 void nsImageMap::UpdateAreas() {
678 // Get rid of old area data
679 FreeAreas();
681 mConsiderWholeSubtree = false;
682 SearchForAreas(mMap);
684 #ifdef ACCESSIBILITY
685 if (nsAccessibilityService* accService = GetAccService()) {
686 accService->UpdateImageMap(mImageFrame);
688 #endif
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,
700 eIgnoreCase)) {
701 case Element::ATTR_VALUE_NO_MATCH:
702 case Element::ATTR_MISSING:
703 case 0:
704 case 1:
705 area = MakeUnique<RectArea>(aArea);
706 break;
707 case 2:
708 case 3:
709 area = MakeUnique<CircleArea>(aArea);
710 break;
711 case 4:
712 area = MakeUnique<DefaultArea>(aArea);
713 break;
714 case 5:
715 case 6:
716 area = MakeUnique<PolyArea>(aArea);
717 break;
718 default:
719 area = nullptr;
720 MOZ_ASSERT_UNREACHABLE("FindAttrValueIn returned an unexpected value.");
721 break;
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
732 // be removed.
733 aArea->SetPrimaryFrame(mImageFrame);
735 nsAutoString coords;
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)) {
745 return area->mArea;
749 return nullptr;
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) {
766 UpdateAreas();
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) &&
784 mImageFrame) {
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;
801 size_t index = 0;
802 for (UniquePtr<Area>& area : aAreas) {
803 if (area->mArea == aArea) {
804 result = std::move(area);
805 break;
807 index++;
810 if (result) {
811 aAreas.RemoveElementAt(index);
814 return result;
817 void nsImageMap::ContentRemoved(nsIContent* aChild,
818 nsIContent* aPreviousSibling) {
819 if (aChild->GetParent() != mMap && !mConsiderWholeSubtree) {
820 return;
823 auto* areaElement = HTMLAreaElement::FromNode(aChild);
824 if (!areaElement) {
825 return;
828 UniquePtr<Area> area = TakeArea(mAreas, areaElement);
829 if (!area) {
830 return;
833 AreaRemoved(area->mArea);
835 #ifdef ACCESSIBILITY
836 if (nsAccessibilityService* accService = GetAccService()) {
837 accService->UpdateImageMap(mImageFrame);
839 #endif
842 void nsImageMap::ParentChainChanged(nsIContent* aContent) {
843 NS_ASSERTION(aContent == mMap, "Unexpected ParentChainChanged notification!");
844 if (mImageFrame) {
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) {
859 return NS_OK;
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
867 if (mImageFrame) {
868 mImageFrame->InvalidateFrame();
870 break;
873 return NS_OK;
876 void nsImageMap::Destroy() {
877 FreeAreas();
878 mImageFrame = nullptr;
879 mMap->RemoveMutationObserver(this);