layout/generic/nsImageMap.cpp

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
michael@0 2 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 3 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 5
michael@0 6 /* code for HTML client-side image maps */
michael@0 7
michael@0 8 #include "nsImageMap.h"
michael@0 9
michael@0 10 #include "mozilla/dom/Element.h"
michael@0 11 #include "mozilla/dom/Event.h" // for nsIDOMEvent::InternalDOMEvent()
michael@0 12 #include "nsString.h"
michael@0 13 #include "nsReadableUtils.h"
michael@0 14 #include "nsRenderingContext.h"
michael@0 15 #include "nsPresContext.h"
michael@0 16 #include "nsNameSpaceManager.h"
michael@0 17 #include "nsGkAtoms.h"
michael@0 18 #include "nsImageFrame.h"
michael@0 19 #include "nsCoord.h"
michael@0 20 #include "nsIScriptError.h"
michael@0 21 #include "nsIStringBundle.h"
michael@0 22 #include "nsContentUtils.h"
michael@0 23
michael@0 24 #ifdef ACCESSIBILITY
michael@0 25 #include "nsAccessibilityService.h"
michael@0 26 #endif
michael@0 27
michael@0 28 using namespace mozilla;
michael@0 29
michael@0 30 class Area {
michael@0 31 public:
michael@0 32 Area(nsIContent* aArea);
michael@0 33 virtual ~Area();
michael@0 34
michael@0 35 virtual void ParseCoords(const nsAString& aSpec);
michael@0 36
michael@0 37 virtual bool IsInside(nscoord x, nscoord y) const = 0;
michael@0 38 virtual void Draw(nsIFrame* aFrame, nsRenderingContext& aRC) = 0;
michael@0 39 virtual void GetRect(nsIFrame* aFrame, nsRect& aRect) = 0;
michael@0 40
michael@0 41 void HasFocus(bool aHasFocus);
michael@0 42
michael@0 43 nsCOMPtr<nsIContent> mArea;
michael@0 44 nscoord* mCoords;
michael@0 45 int32_t mNumCoords;
michael@0 46 bool mHasFocus;
michael@0 47 };
michael@0 48
michael@0 49 Area::Area(nsIContent* aArea)
michael@0 50 : mArea(aArea)
michael@0 51 {
michael@0 52 MOZ_COUNT_CTOR(Area);
michael@0 53 NS_PRECONDITION(mArea, "How did that happen?");
michael@0 54 mCoords = nullptr;
michael@0 55 mNumCoords = 0;
michael@0 56 mHasFocus = false;
michael@0 57 }
michael@0 58
michael@0 59 Area::~Area()
michael@0 60 {
michael@0 61 MOZ_COUNT_DTOR(Area);
michael@0 62 delete [] mCoords;
michael@0 63 }
michael@0 64
michael@0 65 #include <stdlib.h>
michael@0 66
michael@0 67 inline bool
michael@0 68 is_space(char c)
michael@0 69 {
michael@0 70 return (c == ' ' ||
michael@0 71 c == '\f' ||
michael@0 72 c == '\n' ||
michael@0 73 c == '\r' ||
michael@0 74 c == '\t' ||
michael@0 75 c == '\v');
michael@0 76 }
michael@0 77
michael@0 78 static void logMessage(nsIContent* aContent,
michael@0 79 const nsAString& aCoordsSpec,
michael@0 80 int32_t aFlags,
michael@0 81 const char* aMessageName) {
michael@0 82 nsIDocument* doc = aContent->OwnerDoc();
michael@0 83
michael@0 84 nsContentUtils::ReportToConsole(
michael@0 85 aFlags, NS_LITERAL_CSTRING("ImageMap"), doc,
michael@0 86 nsContentUtils::eLAYOUT_PROPERTIES,
michael@0 87 aMessageName,
michael@0 88 nullptr, /* params */
michael@0 89 0, /* params length */
michael@0 90 nullptr,
michael@0 91 PromiseFlatString(NS_LITERAL_STRING("coords=\"") +
michael@0 92 aCoordsSpec +
michael@0 93 NS_LITERAL_STRING("\""))); /* source line */
michael@0 94 }
michael@0 95
michael@0 96 void Area::ParseCoords(const nsAString& aSpec)
michael@0 97 {
michael@0 98 char* cp = ToNewCString(aSpec);
michael@0 99 if (cp) {
michael@0 100 char *tptr;
michael@0 101 char *n_str;
michael@0 102 int32_t i, cnt;
michael@0 103 int32_t *value_list;
michael@0 104
michael@0 105 /*
michael@0 106 * Nothing in an empty list
michael@0 107 */
michael@0 108 mNumCoords = 0;
michael@0 109 mCoords = nullptr;
michael@0 110 if (*cp == '\0')
michael@0 111 {
michael@0 112 nsMemory::Free(cp);
michael@0 113 return;
michael@0 114 }
michael@0 115
michael@0 116 /*
michael@0 117 * Skip beginning whitespace, all whitespace is empty list.
michael@0 118 */
michael@0 119 n_str = cp;
michael@0 120 while (is_space(*n_str))
michael@0 121 {
michael@0 122 n_str++;
michael@0 123 }
michael@0 124 if (*n_str == '\0')
michael@0 125 {
michael@0 126 nsMemory::Free(cp);
michael@0 127 return;
michael@0 128 }
michael@0 129
michael@0 130 /*
michael@0 131 * Make a pass where any two numbers separated by just whitespace
michael@0 132 * are given a comma separator. Count entries while passing.
michael@0 133 */
michael@0 134 cnt = 0;
michael@0 135 while (*n_str != '\0')
michael@0 136 {
michael@0 137 bool has_comma;
michael@0 138
michael@0 139 /*
michael@0 140 * Skip to a separator
michael@0 141 */
michael@0 142 tptr = n_str;
michael@0 143 while (!is_space(*tptr) && *tptr != ',' && *tptr != '\0')
michael@0 144 {
michael@0 145 tptr++;
michael@0 146 }
michael@0 147 n_str = tptr;
michael@0 148
michael@0 149 /*
michael@0 150 * If no more entries, break out here
michael@0 151 */
michael@0 152 if (*n_str == '\0')
michael@0 153 {
michael@0 154 break;
michael@0 155 }
michael@0 156
michael@0 157 /*
michael@0 158 * Skip to the end of the separator, noting if we have a
michael@0 159 * comma.
michael@0 160 */
michael@0 161 has_comma = false;
michael@0 162 while (is_space(*tptr) || *tptr == ',')
michael@0 163 {
michael@0 164 if (*tptr == ',')
michael@0 165 {
michael@0 166 if (!has_comma)
michael@0 167 {
michael@0 168 has_comma = true;
michael@0 169 }
michael@0 170 else
michael@0 171 {
michael@0 172 break;
michael@0 173 }
michael@0 174 }
michael@0 175 tptr++;
michael@0 176 }
michael@0 177 /*
michael@0 178 * If this was trailing whitespace we skipped, we are done.
michael@0 179 */
michael@0 180 if ((*tptr == '\0') && !has_comma)
michael@0 181 {
michael@0 182 break;
michael@0 183 }
michael@0 184 /*
michael@0 185 * Else if the separator is all whitespace, and this is not the
michael@0 186 * end of the string, add a comma to the separator.
michael@0 187 */
michael@0 188 else if (!has_comma)
michael@0 189 {
michael@0 190 *n_str = ',';
michael@0 191 }
michael@0 192
michael@0 193 /*
michael@0 194 * count the entry skipped.
michael@0 195 */
michael@0 196 cnt++;
michael@0 197
michael@0 198 n_str = tptr;
michael@0 199 }
michael@0 200 /*
michael@0 201 * count the last entry in the list.
michael@0 202 */
michael@0 203 cnt++;
michael@0 204
michael@0 205 /*
michael@0 206 * Allocate space for the coordinate array.
michael@0 207 */
michael@0 208 value_list = new nscoord[cnt];
michael@0 209 if (!value_list)
michael@0 210 {
michael@0 211 nsMemory::Free(cp);
michael@0 212 return;
michael@0 213 }
michael@0 214
michael@0 215 /*
michael@0 216 * Second pass to copy integer values into list.
michael@0 217 */
michael@0 218 tptr = cp;
michael@0 219 for (i=0; i<cnt; i++)
michael@0 220 {
michael@0 221 char *ptr;
michael@0 222
michael@0 223 ptr = strchr(tptr, ',');
michael@0 224 if (ptr)
michael@0 225 {
michael@0 226 *ptr = '\0';
michael@0 227 }
michael@0 228 /*
michael@0 229 * Strip whitespace in front of number because I don't
michael@0 230 * trust atoi to do it on all platforms.
michael@0 231 */
michael@0 232 while (is_space(*tptr))
michael@0 233 {
michael@0 234 tptr++;
michael@0 235 }
michael@0 236 if (*tptr == '\0')
michael@0 237 {
michael@0 238 value_list[i] = 0;
michael@0 239 }
michael@0 240 else
michael@0 241 {
michael@0 242 value_list[i] = (nscoord) ::atoi(tptr);
michael@0 243 }
michael@0 244 if (ptr)
michael@0 245 {
michael@0 246 *ptr = ',';
michael@0 247 tptr = ptr + 1;
michael@0 248 }
michael@0 249 }
michael@0 250
michael@0 251 mNumCoords = cnt;
michael@0 252 mCoords = value_list;
michael@0 253
michael@0 254 nsMemory::Free(cp);
michael@0 255 }
michael@0 256 }
michael@0 257
michael@0 258 void Area::HasFocus(bool aHasFocus)
michael@0 259 {
michael@0 260 mHasFocus = aHasFocus;
michael@0 261 }
michael@0 262
michael@0 263 //----------------------------------------------------------------------
michael@0 264
michael@0 265 class DefaultArea : public Area {
michael@0 266 public:
michael@0 267 DefaultArea(nsIContent* aArea);
michael@0 268
michael@0 269 virtual bool IsInside(nscoord x, nscoord y) const MOZ_OVERRIDE;
michael@0 270 virtual void Draw(nsIFrame* aFrame, nsRenderingContext& aRC) MOZ_OVERRIDE;
michael@0 271 virtual void GetRect(nsIFrame* aFrame, nsRect& aRect) MOZ_OVERRIDE;
michael@0 272 };
michael@0 273
michael@0 274 DefaultArea::DefaultArea(nsIContent* aArea)
michael@0 275 : Area(aArea)
michael@0 276 {
michael@0 277 }
michael@0 278
michael@0 279 bool DefaultArea::IsInside(nscoord x, nscoord y) const
michael@0 280 {
michael@0 281 return true;
michael@0 282 }
michael@0 283
michael@0 284 void DefaultArea::Draw(nsIFrame* aFrame, nsRenderingContext& aRC)
michael@0 285 {
michael@0 286 if (mHasFocus) {
michael@0 287 nsRect r = aFrame->GetRect();
michael@0 288 r.MoveTo(0, 0);
michael@0 289 nscoord x1 = r.x;
michael@0 290 nscoord y1 = r.y;
michael@0 291 const nscoord kOnePixel = nsPresContext::CSSPixelsToAppUnits(1);
michael@0 292 nscoord x2 = r.XMost() - kOnePixel;
michael@0 293 nscoord y2 = r.YMost() - kOnePixel;
michael@0 294 // XXX aRC.DrawRect(r) result is ugly, that's why we use DrawLine.
michael@0 295 aRC.DrawLine(x1, y1, x1, y2);
michael@0 296 aRC.DrawLine(x1, y2, x2, y2);
michael@0 297 aRC.DrawLine(x1, y1, x2, y1);
michael@0 298 aRC.DrawLine(x2, y1, x2, y2);
michael@0 299 }
michael@0 300 }
michael@0 301
michael@0 302 void DefaultArea::GetRect(nsIFrame* aFrame, nsRect& aRect)
michael@0 303 {
michael@0 304 aRect = aFrame->GetRect();
michael@0 305 aRect.MoveTo(0, 0);
michael@0 306 }
michael@0 307
michael@0 308 //----------------------------------------------------------------------
michael@0 309
michael@0 310 class RectArea : public Area {
michael@0 311 public:
michael@0 312 RectArea(nsIContent* aArea);
michael@0 313
michael@0 314 virtual void ParseCoords(const nsAString& aSpec) MOZ_OVERRIDE;
michael@0 315 virtual bool IsInside(nscoord x, nscoord y) const MOZ_OVERRIDE;
michael@0 316 virtual void Draw(nsIFrame* aFrame, nsRenderingContext& aRC) MOZ_OVERRIDE;
michael@0 317 virtual void GetRect(nsIFrame* aFrame, nsRect& aRect) MOZ_OVERRIDE;
michael@0 318 };
michael@0 319
michael@0 320 RectArea::RectArea(nsIContent* aArea)
michael@0 321 : Area(aArea)
michael@0 322 {
michael@0 323 }
michael@0 324
michael@0 325 void RectArea::ParseCoords(const nsAString& aSpec)
michael@0 326 {
michael@0 327 Area::ParseCoords(aSpec);
michael@0 328
michael@0 329 bool saneRect = true;
michael@0 330 int32_t flag = nsIScriptError::warningFlag;
michael@0 331 if (mNumCoords >= 4) {
michael@0 332 if (mCoords[0] > mCoords[2]) {
michael@0 333 // x-coords in reversed order
michael@0 334 nscoord x = mCoords[2];
michael@0 335 mCoords[2] = mCoords[0];
michael@0 336 mCoords[0] = x;
michael@0 337 saneRect = false;
michael@0 338 }
michael@0 339
michael@0 340 if (mCoords[1] > mCoords[3]) {
michael@0 341 // y-coords in reversed order
michael@0 342 nscoord y = mCoords[3];
michael@0 343 mCoords[3] = mCoords[1];
michael@0 344 mCoords[1] = y;
michael@0 345 saneRect = false;
michael@0 346 }
michael@0 347
michael@0 348 if (mNumCoords > 4) {
michael@0 349 // Someone missed the concept of a rect here
michael@0 350 saneRect = false;
michael@0 351 }
michael@0 352 } else {
michael@0 353 saneRect = false;
michael@0 354 flag = nsIScriptError::errorFlag;
michael@0 355 }
michael@0 356
michael@0 357 if (!saneRect) {
michael@0 358 logMessage(mArea, aSpec, flag, "ImageMapRectBoundsError");
michael@0 359 }
michael@0 360 }
michael@0 361
michael@0 362 bool RectArea::IsInside(nscoord x, nscoord y) const
michael@0 363 {
michael@0 364 if (mNumCoords >= 4) { // Note: > is for nav compatibility
michael@0 365 nscoord x1 = mCoords[0];
michael@0 366 nscoord y1 = mCoords[1];
michael@0 367 nscoord x2 = mCoords[2];
michael@0 368 nscoord y2 = mCoords[3];
michael@0 369 NS_ASSERTION(x1 <= x2 && y1 <= y2,
michael@0 370 "Someone screwed up RectArea::ParseCoords");
michael@0 371 if ((x >= x1) && (x <= x2) && (y >= y1) && (y <= y2)) {
michael@0 372 return true;
michael@0 373 }
michael@0 374 }
michael@0 375 return false;
michael@0 376 }
michael@0 377
michael@0 378 void RectArea::Draw(nsIFrame* aFrame, nsRenderingContext& aRC)
michael@0 379 {
michael@0 380 if (mHasFocus) {
michael@0 381 if (mNumCoords >= 4) {
michael@0 382 nscoord x1 = nsPresContext::CSSPixelsToAppUnits(mCoords[0]);
michael@0 383 nscoord y1 = nsPresContext::CSSPixelsToAppUnits(mCoords[1]);
michael@0 384 nscoord x2 = nsPresContext::CSSPixelsToAppUnits(mCoords[2]);
michael@0 385 nscoord y2 = nsPresContext::CSSPixelsToAppUnits(mCoords[3]);
michael@0 386 NS_ASSERTION(x1 <= x2 && y1 <= y2,
michael@0 387 "Someone screwed up RectArea::ParseCoords");
michael@0 388 aRC.DrawLine(x1, y1, x1, y2);
michael@0 389 aRC.DrawLine(x1, y2, x2, y2);
michael@0 390 aRC.DrawLine(x1, y1, x2, y1);
michael@0 391 aRC.DrawLine(x2, y1, x2, y2);
michael@0 392 }
michael@0 393 }
michael@0 394 }
michael@0 395
michael@0 396 void RectArea::GetRect(nsIFrame* aFrame, nsRect& aRect)
michael@0 397 {
michael@0 398 if (mNumCoords >= 4) {
michael@0 399 nscoord x1 = nsPresContext::CSSPixelsToAppUnits(mCoords[0]);
michael@0 400 nscoord y1 = nsPresContext::CSSPixelsToAppUnits(mCoords[1]);
michael@0 401 nscoord x2 = nsPresContext::CSSPixelsToAppUnits(mCoords[2]);
michael@0 402 nscoord y2 = nsPresContext::CSSPixelsToAppUnits(mCoords[3]);
michael@0 403 NS_ASSERTION(x1 <= x2 && y1 <= y2,
michael@0 404 "Someone screwed up RectArea::ParseCoords");
michael@0 405
michael@0 406 aRect.SetRect(x1, y1, x2, y2);
michael@0 407 }
michael@0 408 }
michael@0 409
michael@0 410 //----------------------------------------------------------------------
michael@0 411
michael@0 412 class PolyArea : public Area {
michael@0 413 public:
michael@0 414 PolyArea(nsIContent* aArea);
michael@0 415
michael@0 416 virtual void ParseCoords(const nsAString& aSpec) MOZ_OVERRIDE;
michael@0 417 virtual bool IsInside(nscoord x, nscoord y) const MOZ_OVERRIDE;
michael@0 418 virtual void Draw(nsIFrame* aFrame, nsRenderingContext& aRC) MOZ_OVERRIDE;
michael@0 419 virtual void GetRect(nsIFrame* aFrame, nsRect& aRect) MOZ_OVERRIDE;
michael@0 420 };
michael@0 421
michael@0 422 PolyArea::PolyArea(nsIContent* aArea)
michael@0 423 : Area(aArea)
michael@0 424 {
michael@0 425 }
michael@0 426
michael@0 427 void PolyArea::ParseCoords(const nsAString& aSpec)
michael@0 428 {
michael@0 429 Area::ParseCoords(aSpec);
michael@0 430
michael@0 431 if (mNumCoords >= 2) {
michael@0 432 if (mNumCoords & 1U) {
michael@0 433 logMessage(mArea,
michael@0 434 aSpec,
michael@0 435 nsIScriptError::warningFlag,
michael@0 436 "ImageMapPolyOddNumberOfCoords");
michael@0 437 }
michael@0 438 } else {
michael@0 439 logMessage(mArea,
michael@0 440 aSpec,
michael@0 441 nsIScriptError::errorFlag,
michael@0 442 "ImageMapPolyWrongNumberOfCoords");
michael@0 443 }
michael@0 444 }
michael@0 445
michael@0 446 bool PolyArea::IsInside(nscoord x, nscoord y) const
michael@0 447 {
michael@0 448 if (mNumCoords >= 6) {
michael@0 449 int32_t intersects = 0;
michael@0 450 nscoord wherex = x;
michael@0 451 nscoord wherey = y;
michael@0 452 int32_t totalv = mNumCoords / 2;
michael@0 453 int32_t totalc = totalv * 2;
michael@0 454 nscoord xval = mCoords[totalc - 2];
michael@0 455 nscoord yval = mCoords[totalc - 1];
michael@0 456 int32_t end = totalc;
michael@0 457 int32_t pointer = 1;
michael@0 458
michael@0 459 if ((yval >= wherey) != (mCoords[pointer] >= wherey)) {
michael@0 460 if ((xval >= wherex) == (mCoords[0] >= wherex)) {
michael@0 461 intersects += (xval >= wherex) ? 1 : 0;
michael@0 462 } else {
michael@0 463 intersects += ((xval - (yval - wherey) *
michael@0 464 (mCoords[0] - xval) /
michael@0 465 (mCoords[pointer] - yval)) >= wherex) ? 1 : 0;
michael@0 466 }
michael@0 467 }
michael@0 468
michael@0 469 // XXX I wonder what this is doing; this is a translation of ptinpoly.c
michael@0 470 while (pointer < end) {
michael@0 471 yval = mCoords[pointer];
michael@0 472 pointer += 2;
michael@0 473 if (yval >= wherey) {
michael@0 474 while((pointer < end) && (mCoords[pointer] >= wherey))
michael@0 475 pointer+=2;
michael@0 476 if (pointer >= end)
michael@0 477 break;
michael@0 478 if ((mCoords[pointer-3] >= wherex) ==
michael@0 479 (mCoords[pointer-1] >= wherex)) {
michael@0 480 intersects += (mCoords[pointer-3] >= wherex) ? 1 : 0;
michael@0 481 } else {
michael@0 482 intersects +=
michael@0 483 ((mCoords[pointer-3] - (mCoords[pointer-2] - wherey) *
michael@0 484 (mCoords[pointer-1] - mCoords[pointer-3]) /
michael@0 485 (mCoords[pointer] - mCoords[pointer - 2])) >= wherex) ? 1:0;
michael@0 486 }
michael@0 487 } else {
michael@0 488 while((pointer < end) && (mCoords[pointer] < wherey))
michael@0 489 pointer+=2;
michael@0 490 if (pointer >= end)
michael@0 491 break;
michael@0 492 if ((mCoords[pointer-3] >= wherex) ==
michael@0 493 (mCoords[pointer-1] >= wherex)) {
michael@0 494 intersects += (mCoords[pointer-3] >= wherex) ? 1:0;
michael@0 495 } else {
michael@0 496 intersects +=
michael@0 497 ((mCoords[pointer-3] - (mCoords[pointer-2] - wherey) *
michael@0 498 (mCoords[pointer-1] - mCoords[pointer-3]) /
michael@0 499 (mCoords[pointer] - mCoords[pointer - 2])) >= wherex) ? 1:0;
michael@0 500 }
michael@0 501 }
michael@0 502 }
michael@0 503 if ((intersects & 1) != 0) {
michael@0 504 return true;
michael@0 505 }
michael@0 506 }
michael@0 507 return false;
michael@0 508 }
michael@0 509
michael@0 510 void PolyArea::Draw(nsIFrame* aFrame, nsRenderingContext& aRC)
michael@0 511 {
michael@0 512 if (mHasFocus) {
michael@0 513 if (mNumCoords >= 6) {
michael@0 514 nscoord x0 = nsPresContext::CSSPixelsToAppUnits(mCoords[0]);
michael@0 515 nscoord y0 = nsPresContext::CSSPixelsToAppUnits(mCoords[1]);
michael@0 516 nscoord x1, y1;
michael@0 517 for (int32_t i = 2; i < mNumCoords; i += 2) {
michael@0 518 x1 = nsPresContext::CSSPixelsToAppUnits(mCoords[i]);
michael@0 519 y1 = nsPresContext::CSSPixelsToAppUnits(mCoords[i+1]);
michael@0 520 aRC.DrawLine(x0, y0, x1, y1);
michael@0 521 x0 = x1;
michael@0 522 y0 = y1;
michael@0 523 }
michael@0 524 x1 = nsPresContext::CSSPixelsToAppUnits(mCoords[0]);
michael@0 525 y1 = nsPresContext::CSSPixelsToAppUnits(mCoords[1]);
michael@0 526 aRC.DrawLine(x0, y0, x1, y1);
michael@0 527 }
michael@0 528 }
michael@0 529 }
michael@0 530
michael@0 531 void PolyArea::GetRect(nsIFrame* aFrame, nsRect& aRect)
michael@0 532 {
michael@0 533 if (mNumCoords >= 6) {
michael@0 534 nscoord x1, x2, y1, y2, xtmp, ytmp;
michael@0 535 x1 = x2 = nsPresContext::CSSPixelsToAppUnits(mCoords[0]);
michael@0 536 y1 = y2 = nsPresContext::CSSPixelsToAppUnits(mCoords[1]);
michael@0 537 for (int32_t i = 2; i < mNumCoords; i += 2) {
michael@0 538 xtmp = nsPresContext::CSSPixelsToAppUnits(mCoords[i]);
michael@0 539 ytmp = nsPresContext::CSSPixelsToAppUnits(mCoords[i+1]);
michael@0 540 x1 = x1 < xtmp ? x1 : xtmp;
michael@0 541 y1 = y1 < ytmp ? y1 : ytmp;
michael@0 542 x2 = x2 > xtmp ? x2 : xtmp;
michael@0 543 y2 = y2 > ytmp ? y2 : ytmp;
michael@0 544 }
michael@0 545
michael@0 546 aRect.SetRect(x1, y1, x2, y2);
michael@0 547 }
michael@0 548 }
michael@0 549
michael@0 550 //----------------------------------------------------------------------
michael@0 551
michael@0 552 class CircleArea : public Area {
michael@0 553 public:
michael@0 554 CircleArea(nsIContent* aArea);
michael@0 555
michael@0 556 virtual void ParseCoords(const nsAString& aSpec) MOZ_OVERRIDE;
michael@0 557 virtual bool IsInside(nscoord x, nscoord y) const MOZ_OVERRIDE;
michael@0 558 virtual void Draw(nsIFrame* aFrame, nsRenderingContext& aRC) MOZ_OVERRIDE;
michael@0 559 virtual void GetRect(nsIFrame* aFrame, nsRect& aRect) MOZ_OVERRIDE;
michael@0 560 };
michael@0 561
michael@0 562 CircleArea::CircleArea(nsIContent* aArea)
michael@0 563 : Area(aArea)
michael@0 564 {
michael@0 565 }
michael@0 566
michael@0 567 void CircleArea::ParseCoords(const nsAString& aSpec)
michael@0 568 {
michael@0 569 Area::ParseCoords(aSpec);
michael@0 570
michael@0 571 bool wrongNumberOfCoords = false;
michael@0 572 int32_t flag = nsIScriptError::warningFlag;
michael@0 573 if (mNumCoords >= 3) {
michael@0 574 if (mCoords[2] < 0) {
michael@0 575 logMessage(mArea,
michael@0 576 aSpec,
michael@0 577 nsIScriptError::errorFlag,
michael@0 578 "ImageMapCircleNegativeRadius");
michael@0 579 }
michael@0 580
michael@0 581 if (mNumCoords > 3) {
michael@0 582 wrongNumberOfCoords = true;
michael@0 583 }
michael@0 584 } else {
michael@0 585 wrongNumberOfCoords = true;
michael@0 586 flag = nsIScriptError::errorFlag;
michael@0 587 }
michael@0 588
michael@0 589 if (wrongNumberOfCoords) {
michael@0 590 logMessage(mArea,
michael@0 591 aSpec,
michael@0 592 flag,
michael@0 593 "ImageMapCircleWrongNumberOfCoords");
michael@0 594 }
michael@0 595 }
michael@0 596
michael@0 597 bool CircleArea::IsInside(nscoord x, nscoord y) const
michael@0 598 {
michael@0 599 // Note: > is for nav compatibility
michael@0 600 if (mNumCoords >= 3) {
michael@0 601 nscoord x1 = mCoords[0];
michael@0 602 nscoord y1 = mCoords[1];
michael@0 603 nscoord radius = mCoords[2];
michael@0 604 if (radius < 0) {
michael@0 605 return false;
michael@0 606 }
michael@0 607 nscoord dx = x1 - x;
michael@0 608 nscoord dy = y1 - y;
michael@0 609 nscoord dist = (dx * dx) + (dy * dy);
michael@0 610 if (dist <= (radius * radius)) {
michael@0 611 return true;
michael@0 612 }
michael@0 613 }
michael@0 614 return false;
michael@0 615 }
michael@0 616
michael@0 617 void CircleArea::Draw(nsIFrame* aFrame, nsRenderingContext& aRC)
michael@0 618 {
michael@0 619 if (mHasFocus) {
michael@0 620 if (mNumCoords >= 3) {
michael@0 621 nscoord x1 = nsPresContext::CSSPixelsToAppUnits(mCoords[0]);
michael@0 622 nscoord y1 = nsPresContext::CSSPixelsToAppUnits(mCoords[1]);
michael@0 623 nscoord radius = nsPresContext::CSSPixelsToAppUnits(mCoords[2]);
michael@0 624 if (radius < 0) {
michael@0 625 return;
michael@0 626 }
michael@0 627 nscoord x = x1 - radius;
michael@0 628 nscoord y = y1 - radius;
michael@0 629 nscoord w = 2 * radius;
michael@0 630 aRC.DrawEllipse(x, y, w, w);
michael@0 631 }
michael@0 632 }
michael@0 633 }
michael@0 634
michael@0 635 void CircleArea::GetRect(nsIFrame* aFrame, nsRect& aRect)
michael@0 636 {
michael@0 637 if (mNumCoords >= 3) {
michael@0 638 nscoord x1 = nsPresContext::CSSPixelsToAppUnits(mCoords[0]);
michael@0 639 nscoord y1 = nsPresContext::CSSPixelsToAppUnits(mCoords[1]);
michael@0 640 nscoord radius = nsPresContext::CSSPixelsToAppUnits(mCoords[2]);
michael@0 641 if (radius < 0) {
michael@0 642 return;
michael@0 643 }
michael@0 644
michael@0 645 aRect.SetRect(x1 - radius, y1 - radius, x1 + radius, y1 + radius);
michael@0 646 }
michael@0 647 }
michael@0 648
michael@0 649 //----------------------------------------------------------------------
michael@0 650
michael@0 651
michael@0 652 nsImageMap::nsImageMap() :
michael@0 653 mImageFrame(nullptr),
michael@0 654 mContainsBlockContents(false)
michael@0 655 {
michael@0 656 }
michael@0 657
michael@0 658 nsImageMap::~nsImageMap()
michael@0 659 {
michael@0 660 NS_ASSERTION(mAreas.Length() == 0, "Destroy was not called");
michael@0 661 }
michael@0 662
michael@0 663 NS_IMPL_ISUPPORTS(nsImageMap,
michael@0 664 nsIMutationObserver,
michael@0 665 nsIDOMEventListener)
michael@0 666
michael@0 667 nsresult
michael@0 668 nsImageMap::GetBoundsForAreaContent(nsIContent *aContent,
michael@0 669 nsRect& aBounds)
michael@0 670 {
michael@0 671 NS_ENSURE_TRUE(aContent && mImageFrame, NS_ERROR_INVALID_ARG);
michael@0 672
michael@0 673 // Find the Area struct associated with this content node, and return bounds
michael@0 674 uint32_t i, n = mAreas.Length();
michael@0 675 for (i = 0; i < n; i++) {
michael@0 676 Area* area = mAreas.ElementAt(i);
michael@0 677 if (area->mArea == aContent) {
michael@0 678 aBounds = nsRect();
michael@0 679 area->GetRect(mImageFrame, aBounds);
michael@0 680 return NS_OK;
michael@0 681 }
michael@0 682 }
michael@0 683 return NS_ERROR_FAILURE;
michael@0 684 }
michael@0 685
michael@0 686 void
michael@0 687 nsImageMap::FreeAreas()
michael@0 688 {
michael@0 689 uint32_t i, n = mAreas.Length();
michael@0 690 for (i = 0; i < n; i++) {
michael@0 691 Area* area = mAreas.ElementAt(i);
michael@0 692 if (area->mArea->IsInDoc()) {
michael@0 693 NS_ASSERTION(area->mArea->GetPrimaryFrame() == mImageFrame,
michael@0 694 "Unexpected primary frame");
michael@0 695
michael@0 696 area->mArea->SetPrimaryFrame(nullptr);
michael@0 697 }
michael@0 698
michael@0 699 area->mArea->RemoveSystemEventListener(NS_LITERAL_STRING("focus"), this,
michael@0 700 false);
michael@0 701 area->mArea->RemoveSystemEventListener(NS_LITERAL_STRING("blur"), this,
michael@0 702 false);
michael@0 703 delete area;
michael@0 704 }
michael@0 705 mAreas.Clear();
michael@0 706 }
michael@0 707
michael@0 708 nsresult
michael@0 709 nsImageMap::Init(nsImageFrame* aImageFrame, nsIContent* aMap)
michael@0 710 {
michael@0 711 NS_PRECONDITION(aMap, "null ptr");
michael@0 712 if (!aMap) {
michael@0 713 return NS_ERROR_NULL_POINTER;
michael@0 714 }
michael@0 715 mImageFrame = aImageFrame;
michael@0 716
michael@0 717 mMap = aMap;
michael@0 718 mMap->AddMutationObserver(this);
michael@0 719
michael@0 720 // "Compile" the areas in the map into faster access versions
michael@0 721 return UpdateAreas();
michael@0 722 }
michael@0 723
michael@0 724
michael@0 725 nsresult
michael@0 726 nsImageMap::SearchForAreas(nsIContent* aParent, bool& aFoundArea,
michael@0 727 bool& aFoundAnchor)
michael@0 728 {
michael@0 729 nsresult rv = NS_OK;
michael@0 730 uint32_t i, n = aParent->GetChildCount();
michael@0 731
michael@0 732 // Look for <area> or <a> elements. We'll use whichever type we find first.
michael@0 733 for (i = 0; i < n; i++) {
michael@0 734 nsIContent *child = aParent->GetChildAt(i);
michael@0 735
michael@0 736 if (child->IsHTML()) {
michael@0 737 // If we haven't determined that the map element contains an
michael@0 738 // <a> element yet, then look for <area>.
michael@0 739 if (!aFoundAnchor && child->Tag() == nsGkAtoms::area) {
michael@0 740 aFoundArea = true;
michael@0 741 rv = AddArea(child);
michael@0 742 NS_ENSURE_SUCCESS(rv, rv);
michael@0 743
michael@0 744 // Continue to next child. This stops mContainsBlockContents from
michael@0 745 // getting set. It also makes us ignore children of <area>s which
michael@0 746 // is consistent with how we react to dynamic insertion of such
michael@0 747 // children.
michael@0 748 continue;
michael@0 749 }
michael@0 750 // If we haven't determined that the map element contains an
michael@0 751 // <area> element yet, then look for <a>.
michael@0 752 if (!aFoundArea && child->Tag() == nsGkAtoms::a) {
michael@0 753 aFoundAnchor = true;
michael@0 754 rv = AddArea(child);
michael@0 755 NS_ENSURE_SUCCESS(rv, rv);
michael@0 756 }
michael@0 757 }
michael@0 758
michael@0 759 if (child->IsElement()) {
michael@0 760 mContainsBlockContents = true;
michael@0 761 rv = SearchForAreas(child, aFoundArea, aFoundAnchor);
michael@0 762 NS_ENSURE_SUCCESS(rv, rv);
michael@0 763 }
michael@0 764 }
michael@0 765
michael@0 766 return NS_OK;
michael@0 767 }
michael@0 768
michael@0 769 nsresult
michael@0 770 nsImageMap::UpdateAreas()
michael@0 771 {
michael@0 772 // Get rid of old area data
michael@0 773 FreeAreas();
michael@0 774
michael@0 775 bool foundArea = false;
michael@0 776 bool foundAnchor = false;
michael@0 777 mContainsBlockContents = false;
michael@0 778
michael@0 779 nsresult rv = SearchForAreas(mMap, foundArea, foundAnchor);
michael@0 780 #ifdef ACCESSIBILITY
michael@0 781 if (NS_SUCCEEDED(rv)) {
michael@0 782 nsAccessibilityService* accService = GetAccService();
michael@0 783 if (accService) {
michael@0 784 accService->UpdateImageMap(mImageFrame);
michael@0 785 }
michael@0 786 }
michael@0 787 #endif
michael@0 788 return rv;
michael@0 789 }
michael@0 790
michael@0 791 nsresult
michael@0 792 nsImageMap::AddArea(nsIContent* aArea)
michael@0 793 {
michael@0 794 static nsIContent::AttrValuesArray strings[] =
michael@0 795 {&nsGkAtoms::rect, &nsGkAtoms::rectangle,
michael@0 796 &nsGkAtoms::circle, &nsGkAtoms::circ,
michael@0 797 &nsGkAtoms::_default,
michael@0 798 &nsGkAtoms::poly, &nsGkAtoms::polygon,
michael@0 799 nullptr};
michael@0 800
michael@0 801 Area* area;
michael@0 802 switch (aArea->FindAttrValueIn(kNameSpaceID_None, nsGkAtoms::shape,
michael@0 803 strings, eIgnoreCase)) {
michael@0 804 case nsIContent::ATTR_VALUE_NO_MATCH:
michael@0 805 case nsIContent::ATTR_MISSING:
michael@0 806 case 0:
michael@0 807 case 1:
michael@0 808 area = new RectArea(aArea);
michael@0 809 break;
michael@0 810 case 2:
michael@0 811 case 3:
michael@0 812 area = new CircleArea(aArea);
michael@0 813 break;
michael@0 814 case 4:
michael@0 815 area = new DefaultArea(aArea);
michael@0 816 break;
michael@0 817 case 5:
michael@0 818 case 6:
michael@0 819 area = new PolyArea(aArea);
michael@0 820 break;
michael@0 821 default:
michael@0 822 area = nullptr;
michael@0 823 NS_NOTREACHED("FindAttrValueIn returned an unexpected value.");
michael@0 824 break;
michael@0 825 }
michael@0 826 if (!area)
michael@0 827 return NS_ERROR_OUT_OF_MEMORY;
michael@0 828
michael@0 829 //Add focus listener to track area focus changes
michael@0 830 aArea->AddSystemEventListener(NS_LITERAL_STRING("focus"), this, false,
michael@0 831 false);
michael@0 832 aArea->AddSystemEventListener(NS_LITERAL_STRING("blur"), this, false,
michael@0 833 false);
michael@0 834
michael@0 835 // This is a nasty hack. It needs to go away: see bug 135040. Once this is
michael@0 836 // removed, the code added to RestyleManager::RestyleElement,
michael@0 837 // nsCSSFrameConstructor::ContentRemoved (both hacks there), and
michael@0 838 // RestyleManager::ProcessRestyledFrames to work around this issue can
michael@0 839 // be removed.
michael@0 840 aArea->SetPrimaryFrame(mImageFrame);
michael@0 841
michael@0 842 nsAutoString coords;
michael@0 843 aArea->GetAttr(kNameSpaceID_None, nsGkAtoms::coords, coords);
michael@0 844 area->ParseCoords(coords);
michael@0 845 mAreas.AppendElement(area);
michael@0 846 return NS_OK;
michael@0 847 }
michael@0 848
michael@0 849 nsIContent*
michael@0 850 nsImageMap::GetArea(nscoord aX, nscoord aY) const
michael@0 851 {
michael@0 852 NS_ASSERTION(mMap, "Not initialized");
michael@0 853 uint32_t i, n = mAreas.Length();
michael@0 854 for (i = 0; i < n; i++) {
michael@0 855 Area* area = mAreas.ElementAt(i);
michael@0 856 if (area->IsInside(aX, aY)) {
michael@0 857 return area->mArea;
michael@0 858 }
michael@0 859 }
michael@0 860
michael@0 861 return nullptr;
michael@0 862 }
michael@0 863
michael@0 864 nsIContent*
michael@0 865 nsImageMap::GetAreaAt(uint32_t aIndex) const
michael@0 866 {
michael@0 867 return mAreas.ElementAt(aIndex)->mArea;
michael@0 868 }
michael@0 869
michael@0 870 void
michael@0 871 nsImageMap::Draw(nsIFrame* aFrame, nsRenderingContext& aRC)
michael@0 872 {
michael@0 873 uint32_t i, n = mAreas.Length();
michael@0 874 for (i = 0; i < n; i++) {
michael@0 875 Area* area = mAreas.ElementAt(i);
michael@0 876 area->Draw(aFrame, aRC);
michael@0 877 }
michael@0 878 }
michael@0 879
michael@0 880 void
michael@0 881 nsImageMap::MaybeUpdateAreas(nsIContent *aContent)
michael@0 882 {
michael@0 883 if (aContent == mMap || mContainsBlockContents) {
michael@0 884 UpdateAreas();
michael@0 885 }
michael@0 886 }
michael@0 887
michael@0 888 void
michael@0 889 nsImageMap::AttributeChanged(nsIDocument* aDocument,
michael@0 890 dom::Element* aElement,
michael@0 891 int32_t aNameSpaceID,
michael@0 892 nsIAtom* aAttribute,
michael@0 893 int32_t aModType)
michael@0 894 {
michael@0 895 // If the parent of the changing content node is our map then update
michael@0 896 // the map. But only do this if the node is an HTML <area> or <a>
michael@0 897 // and the attribute that's changing is "shape" or "coords" -- those
michael@0 898 // are the only cases we care about.
michael@0 899 if ((aElement->NodeInfo()->Equals(nsGkAtoms::area) ||
michael@0 900 aElement->NodeInfo()->Equals(nsGkAtoms::a)) &&
michael@0 901 aElement->IsHTML() &&
michael@0 902 aNameSpaceID == kNameSpaceID_None &&
michael@0 903 (aAttribute == nsGkAtoms::shape ||
michael@0 904 aAttribute == nsGkAtoms::coords)) {
michael@0 905 MaybeUpdateAreas(aElement->GetParent());
michael@0 906 } else if (aElement == mMap &&
michael@0 907 aNameSpaceID == kNameSpaceID_None &&
michael@0 908 (aAttribute == nsGkAtoms::name ||
michael@0 909 aAttribute == nsGkAtoms::id) &&
michael@0 910 mImageFrame) {
michael@0 911 // ID or name has changed. Let ImageFrame recreate ImageMap.
michael@0 912 mImageFrame->DisconnectMap();
michael@0 913 }
michael@0 914 }
michael@0 915
michael@0 916 void
michael@0 917 nsImageMap::ContentAppended(nsIDocument *aDocument,
michael@0 918 nsIContent* aContainer,
michael@0 919 nsIContent* aFirstNewContent,
michael@0 920 int32_t /* unused */)
michael@0 921 {
michael@0 922 MaybeUpdateAreas(aContainer);
michael@0 923 }
michael@0 924
michael@0 925 void
michael@0 926 nsImageMap::ContentInserted(nsIDocument *aDocument,
michael@0 927 nsIContent* aContainer,
michael@0 928 nsIContent* aChild,
michael@0 929 int32_t /* unused */)
michael@0 930 {
michael@0 931 MaybeUpdateAreas(aContainer);
michael@0 932 }
michael@0 933
michael@0 934 void
michael@0 935 nsImageMap::ContentRemoved(nsIDocument *aDocument,
michael@0 936 nsIContent* aContainer,
michael@0 937 nsIContent* aChild,
michael@0 938 int32_t aIndexInContainer,
michael@0 939 nsIContent* aPreviousSibling)
michael@0 940 {
michael@0 941 MaybeUpdateAreas(aContainer);
michael@0 942 }
michael@0 943
michael@0 944 void
michael@0 945 nsImageMap::ParentChainChanged(nsIContent* aContent)
michael@0 946 {
michael@0 947 NS_ASSERTION(aContent == mMap,
michael@0 948 "Unexpected ParentChainChanged notification!");
michael@0 949 if (mImageFrame) {
michael@0 950 mImageFrame->DisconnectMap();
michael@0 951 }
michael@0 952 }
michael@0 953
michael@0 954 nsresult
michael@0 955 nsImageMap::HandleEvent(nsIDOMEvent* aEvent)
michael@0 956 {
michael@0 957 nsAutoString eventType;
michael@0 958 aEvent->GetType(eventType);
michael@0 959 bool focus = eventType.EqualsLiteral("focus");
michael@0 960 NS_ABORT_IF_FALSE(focus == !eventType.EqualsLiteral("blur"),
michael@0 961 "Unexpected event type");
michael@0 962
michael@0 963 //Set which one of our areas changed focus
michael@0 964 nsCOMPtr<nsIContent> targetContent = do_QueryInterface(
michael@0 965 aEvent->InternalDOMEvent()->GetTarget());
michael@0 966 if (!targetContent) {
michael@0 967 return NS_OK;
michael@0 968 }
michael@0 969 uint32_t i, n = mAreas.Length();
michael@0 970 for (i = 0; i < n; i++) {
michael@0 971 Area* area = mAreas.ElementAt(i);
michael@0 972 if (area->mArea == targetContent) {
michael@0 973 //Set or Remove internal focus
michael@0 974 area->HasFocus(focus);
michael@0 975 //Now invalidate the rect
michael@0 976 if (mImageFrame) {
michael@0 977 mImageFrame->InvalidateFrame();
michael@0 978 }
michael@0 979 break;
michael@0 980 }
michael@0 981 }
michael@0 982 return NS_OK;
michael@0 983 }
michael@0 984
michael@0 985 void
michael@0 986 nsImageMap::Destroy(void)
michael@0 987 {
michael@0 988 FreeAreas();
michael@0 989 mImageFrame = nullptr;
michael@0 990 mMap->RemoveMutationObserver(this);
michael@0 991 }

mercurial