Bug 1382593: Clean a bit nsImageMap. r=heycam

MozReview-Commit-ID: Htm2OoExJ3T

--HG--
extra : rebase_source : 0d4214ab9e0d98421ebd8f7dbaee8d36342cfe85
This commit is contained in:
Emilio Cobos Álvarez 2017-07-03 20:35:14 +02:00
parent 9fed8c4314
commit 0fff6a4e53
3 changed files with 39 additions and 63 deletions

View File

@ -178,17 +178,18 @@ nsImageFrame::AccessibleType()
void
nsImageFrame::DisconnectMap()
{
if (mImageMap) {
mImageMap->Destroy();
mImageMap = nullptr;
if (!mImageMap) {
return;
}
mImageMap->Destroy();
mImageMap = nullptr;
#ifdef ACCESSIBILITY
nsAccessibilityService* accService = GetAccService();
if (accService) {
if (nsAccessibilityService* accService = GetAccService()) {
accService->RecreateAccessible(PresContext()->PresShell(), mContent);
}
#endif
}
}
void
@ -1755,8 +1756,7 @@ nsImageFrame::PaintImage(gfxContext& aRenderingContext, nsPoint aPt,
nsLayoutUtils::GetSamplingFilterForFrame(this), dest, aDirtyRect,
svgContext, flags, &anchorPoint);
nsImageMap* map = GetImageMap();
if (map) {
if (nsImageMap* map = GetImageMap()) {
gfxPoint devPixelOffset =
nsLayoutUtils::PointToGfxPoint(dest.TopLeft(),
PresContext()->AppUnitsPerDevPixel());
@ -1929,8 +1929,7 @@ nsImageMap*
nsImageFrame::GetImageMap()
{
if (!mImageMap) {
nsIContent* map = GetMapElement();
if (map) {
if (nsIContent* map = GetMapElement()) {
mImageMap = new nsImageMap();
mImageMap->Init(this, map);
}
@ -2018,9 +2017,7 @@ nsImageFrame::GetContentForEvent(WidgetEvent* aEvent,
return NS_OK;
}
nsImageMap* map = GetImageMap();
if (nullptr != map) {
if (nsImageMap* map = GetImageMap()) {
nsIntPoint p;
TranslateEventCoords(
nsLayoutUtils::GetEventCoordinatesRelativeTo(aEvent, this), p);
@ -2049,7 +2046,7 @@ nsImageFrame::HandleEvent(nsPresContext* aPresContext,
aEvent->mMessage == eMouseMove) {
nsImageMap* map = GetImageMap();
bool isServerMap = IsServerImageMap();
if ((nullptr != map) || isServerMap) {
if (map || isServerMap) {
nsIntPoint p;
TranslateEventCoords(
nsLayoutUtils::GetEventCoordinatesRelativeTo(aEvent, this), p);
@ -2106,8 +2103,7 @@ nsresult
nsImageFrame::GetCursor(const nsPoint& aPoint,
nsIFrame::Cursor& aCursor)
{
nsImageMap* map = GetImageMap();
if (nullptr != map) {
if (nsImageMap* map = GetImageMap()) {
nsIntPoint p;
TranslateEventCoords(aPoint, p);
nsCOMPtr<nsIContent> area = map->GetArea(p.x, p.y);

View File

@ -713,9 +713,7 @@ nsImageMap::GetBoundsForAreaContent(nsIContent *aContent,
void
nsImageMap::FreeAreas()
{
uint32_t i, n = mAreas.Length();
for (i = 0; i < n; i++) {
Area* area = mAreas.ElementAt(i);
for (auto* area : mAreas) {
if (area->mArea->IsInUncomposedDoc()) {
NS_ASSERTION(area->mArea->GetPrimaryFrame() == mImageFrame,
"Unexpected primary frame");
@ -729,31 +727,28 @@ nsImageMap::FreeAreas()
false);
delete area;
}
mAreas.Clear();
}
nsresult
void
nsImageMap::Init(nsImageFrame* aImageFrame, nsIContent* aMap)
{
NS_PRECONDITION(aMap, "null ptr");
if (!aMap) {
return NS_ERROR_NULL_POINTER;
}
mImageFrame = aImageFrame;
MOZ_ASSERT(aMap);
MOZ_ASSERT(aImageFrame);
mImageFrame = aImageFrame;
mMap = aMap;
mMap->AddMutationObserver(this);
// "Compile" the areas in the map into faster access versions
return UpdateAreas();
UpdateAreas();
}
nsresult
void
nsImageMap::SearchForAreas(nsIContent* aParent, bool& aFoundArea,
bool& aFoundAnchor)
{
nsresult rv = NS_OK;
uint32_t i, n = aParent->GetChildCount();
// Look for <area> or <a> elements. We'll use whichever type we find first.
@ -764,8 +759,7 @@ nsImageMap::SearchForAreas(nsIContent* aParent, bool& aFoundArea,
// <a> element yet, then look for <area>.
if (!aFoundAnchor && child->IsHTMLElement(nsGkAtoms::area)) {
aFoundArea = true;
rv = AddArea(child);
NS_ENSURE_SUCCESS(rv, rv);
AddArea(child);
// Continue to next child. This stops mContainsBlockContents from
// getting set. It also makes us ignore children of <area>s which
@ -778,21 +772,17 @@ nsImageMap::SearchForAreas(nsIContent* aParent, bool& aFoundArea,
// <area> element yet, then look for <a>.
if (!aFoundArea && child->IsHTMLElement(nsGkAtoms::a)) {
aFoundAnchor = true;
rv = AddArea(child);
NS_ENSURE_SUCCESS(rv, rv);
AddArea(child);
}
if (child->IsElement()) {
mContainsBlockContents = true;
rv = SearchForAreas(child, aFoundArea, aFoundAnchor);
NS_ENSURE_SUCCESS(rv, rv);
SearchForAreas(child, aFoundArea, aFoundAnchor);
}
}
return NS_OK;
}
nsresult
void
nsImageMap::UpdateAreas()
{
// Get rid of old area data
@ -802,19 +792,15 @@ nsImageMap::UpdateAreas()
bool foundAnchor = false;
mContainsBlockContents = false;
nsresult rv = SearchForAreas(mMap, foundArea, foundAnchor);
SearchForAreas(mMap, foundArea, foundAnchor);
#ifdef ACCESSIBILITY
if (NS_SUCCEEDED(rv)) {
nsAccessibilityService* accService = GetAccService();
if (accService) {
accService->UpdateImageMap(mImageFrame);
}
if (nsAccessibilityService* accService = GetAccService()) {
accService->UpdateImageMap(mImageFrame);
}
#endif
return rv;
}
nsresult
void
nsImageMap::AddArea(nsIContent* aArea)
{
static nsIContent::AttrValuesArray strings[] =
@ -846,17 +832,13 @@ nsImageMap::AddArea(nsIContent* aArea)
break;
default:
area = nullptr;
NS_NOTREACHED("FindAttrValueIn returned an unexpected value.");
MOZ_ASSERT_UNREACHABLE("FindAttrValueIn returned an unexpected value.");
break;
}
if (!area)
return NS_ERROR_OUT_OF_MEMORY;
//Add focus listener to track area focus changes
aArea->AddSystemEventListener(NS_LITERAL_STRING("focus"), this, false,
false);
aArea->AddSystemEventListener(NS_LITERAL_STRING("blur"), this, false,
false);
aArea->AddSystemEventListener(NS_LITERAL_STRING("focus"), this, false, false);
aArea->AddSystemEventListener(NS_LITERAL_STRING("blur"), this, false, false);
// This is a nasty hack. It needs to go away: see bug 135040. Once this is
// removed, the code added to RestyleManager::RestyleElement,
@ -869,16 +851,13 @@ nsImageMap::AddArea(nsIContent* aArea)
aArea->GetAttr(kNameSpaceID_None, nsGkAtoms::coords, coords);
area->ParseCoords(coords);
mAreas.AppendElement(area);
return NS_OK;
}
nsIContent*
nsImageMap::GetArea(nscoord aX, nscoord aY) const
{
NS_ASSERTION(mMap, "Not initialized");
uint32_t i, n = mAreas.Length();
for (i = 0; i < n; i++) {
Area* area = mAreas.ElementAt(i);
for (auto* area : mAreas) {
if (area->IsInside(aX, aY)) {
return area->mArea;
}
@ -1012,7 +991,7 @@ nsImageMap::HandleEvent(nsIDOMEvent* aEvent)
}
void
nsImageMap::Destroy(void)
nsImageMap::Destroy()
{
FreeAreas();
mImageFrame = nullptr;

View File

@ -31,7 +31,7 @@ class nsImageMap final : public nsStubMutationObserver,
public:
nsImageMap();
nsresult Init(nsImageFrame* aImageFrame, nsIContent* aMap);
void Init(nsImageFrame* aImageFrame, nsIContent* aMap);
/**
* Return the first area element (in content order) for the given aX,aY pixel
@ -80,11 +80,12 @@ protected:
void FreeAreas();
nsresult UpdateAreas();
nsresult SearchForAreas(nsIContent* aParent, bool& aFoundArea,
bool& aFoundAnchor);
void UpdateAreas();
void SearchForAreas(nsIContent* aParent,
bool& aFoundArea,
bool& aFoundAnchor);
nsresult AddArea(nsIContent* aArea);
void AddArea(nsIContent* aArea);
void MaybeUpdateAreas(nsIContent *aContent);