gecko-dev/dom/svg/SVGMPathElement.cpp

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

249 строки
8.8 KiB
C++
Исходник Обычный вид История

/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
2012-05-21 15:12:37 +04:00
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "mozilla/dom/SVGMPathElement.h"
#include "nsDebug.h"
#include "mozilla/ArrayUtils.h"
#include "mozilla/dom/SVGAnimateMotionElement.h"
#include "mozilla/dom/SVGPathElement.h"
#include "nsContentUtils.h"
#include "nsIReferrerInfo.h"
#include "mozilla/dom/SVGMPathElementBinding.h"
#include "nsIURI.h"
NS_IMPL_NS_NEW_SVG_ELEMENT(MPath)
namespace mozilla {
namespace dom {
Bug 1117172 part 3. Change the wrappercached WrapObject methods to allow passing in aGivenProto. r=peterv The only manual changes here are to BindingUtils.h, BindingUtils.cpp, Codegen.py, Element.cpp, IDBFileRequest.cpp, IDBObjectStore.cpp, dom/workers/Navigator.cpp, WorkerPrivate.cpp, DeviceStorageRequestChild.cpp, Notification.cpp, nsGlobalWindow.cpp, MessagePort.cpp, nsJSEnvironment.cpp, Sandbox.cpp, XPCConvert.cpp, ExportHelpers.cpp, and DataStoreService.cpp. The rest of this diff was generated by running the following commands: find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapObjectInternal\(JSContext *\* *(?:aCx|cx|aContext|aCtx|js))\)/\1, JS::Handle<JSObject*> aGivenProto)/g' find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapObjectInternal\((?:aCx|cx|aContext|aCtx|js))\)/\1, aGivenProto)/g' find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapNode\(JSContext *\* *(?:aCx|cx|aContext|aCtx|js))\)/\1, JS::Handle<JSObject*> aGivenProto)/g' find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapNode\((?:aCx|cx|aContext|aCtx|js))\)/\1, aGivenProto)/g' find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapObject\(JSContext *\* *(?:aCx|cx|aContext|aCtx|js))\)/\1, JS::Handle<JSObject*> aGivenProto)/g' find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(Binding(?:_workers)?::Wrap\((?:aCx|cx|aContext|aCtx|js), [^,)]+)\)/\1, aGivenProto)/g'
2015-03-19 17:13:33 +03:00
JSObject* SVGMPathElement::WrapNode(JSContext* aCx,
JS::Handle<JSObject*> aGivenProto) {
return SVGMPathElement_Binding::Wrap(aCx, this, aGivenProto);
}
SVGElement::StringInfo SVGMPathElement::sStringInfo[2] = {
{nsGkAtoms::href, kNameSpaceID_None, false},
{nsGkAtoms::href, kNameSpaceID_XLink, false}};
// Cycle collection magic -- based on SVGUseElement
NS_IMPL_CYCLE_COLLECTION_CLASS(SVGMPathElement)
NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN_INHERITED(SVGMPathElement,
SVGMPathElementBase)
tmp->UnlinkHrefTarget(false);
NS_IMPL_CYCLE_COLLECTION_UNLINK_END
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN_INHERITED(SVGMPathElement,
SVGMPathElementBase)
tmp->mPathTracker.Traverse(&cb);
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
//----------------------------------------------------------------------
// nsISupports methods
NS_IMPL_ISUPPORTS_CYCLE_COLLECTION_INHERITED(SVGMPathElement,
SVGMPathElementBase,
nsIMutationObserver)
// Constructor
SVGMPathElement::SVGMPathElement(
already_AddRefed<mozilla::dom::NodeInfo>&& aNodeInfo)
: SVGMPathElementBase(std::move(aNodeInfo)), mPathTracker(this) {}
SVGMPathElement::~SVGMPathElement() { UnlinkHrefTarget(false); }
//----------------------------------------------------------------------
// nsINode methods
NS_IMPL_ELEMENT_CLONE_WITH_INIT(SVGMPathElement)
already_AddRefed<DOMSVGAnimatedString> SVGMPathElement::Href() {
return mStringAttributes[HREF].IsExplicitlySet()
? mStringAttributes[HREF].ToDOMAnimatedString(this)
: mStringAttributes[XLINK_HREF].ToDOMAnimatedString(this);
}
//----------------------------------------------------------------------
// nsIContent methods
Bug 1555216 - Change the signature of BindToTree to be (BindContext&, nsINode& aParentNode). r=bzbarsky BindContext was going to have way more information at first, but then I realized that most of the things I wanted to know were basically a flag away using the parent node. Still I think it's worth it, now experimenting with BindToTree will only mean adding a field to a struct that's included from a couple cpp files, instead of a massive pain. I also think this is clearer, and doing this highlights quite a few inconsistencies in our code which I've left untouched, but commented with FIXMEs. Steps are: $ for file in $(rg 'nsresult BindToTree\(' | cut -d : -f 1 | sort | uniq); do sed -i 's#nsresult BindToTree(Document\* aDocument, nsIContent\* aParent,#nsresult BindToTree(BindContext\&, nsINode\& aParent)#g' $file; done $ for file in $(rg 'nsresult BindToTree\(' | cut -d : -f 1 | sort | uniq); do sed -i 's# nsIContent\* aBindingParent) override#override#g' $file; done $ for file in $(rg '::BindToTree\(' | cut -d : -f 1 | sort | uniq); do sed -i 's#::BindToTree(Document\* aDocument, nsIContent\* aParent,#::BindToTree(BindContext\& aContext, nsINode\& aParent)#g' $file; done $ for file in $(rg '::BindToTree\(' | cut -d : -f 1 | sort | uniq); do sed -i 's#nsIContent\* aBindingParent)##g' $file; done $ for file in $(rg '::BindToTree\(' | cut -d : -f 1 | sort | uniq); do sed -i 's#::BindToTree(aDocument, aParent, aBindingParent)#::BindToTree(aContext, aParent)#g' $file; done $ ./mach clang-format Then manual fixups. Depends on D32948 Differential Revision: https://phabricator.services.mozilla.com/D32949
2019-05-29 07:27:04 +03:00
nsresult SVGMPathElement::BindToTree(BindContext& aContext, nsINode& aParent) {
MOZ_ASSERT(!mPathTracker.get(),
"Shouldn't have href-target yet (or it should've been cleared)");
Bug 1555216 - Change the signature of BindToTree to be (BindContext&, nsINode& aParentNode). r=bzbarsky BindContext was going to have way more information at first, but then I realized that most of the things I wanted to know were basically a flag away using the parent node. Still I think it's worth it, now experimenting with BindToTree will only mean adding a field to a struct that's included from a couple cpp files, instead of a massive pain. I also think this is clearer, and doing this highlights quite a few inconsistencies in our code which I've left untouched, but commented with FIXMEs. Steps are: $ for file in $(rg 'nsresult BindToTree\(' | cut -d : -f 1 | sort | uniq); do sed -i 's#nsresult BindToTree(Document\* aDocument, nsIContent\* aParent,#nsresult BindToTree(BindContext\&, nsINode\& aParent)#g' $file; done $ for file in $(rg 'nsresult BindToTree\(' | cut -d : -f 1 | sort | uniq); do sed -i 's# nsIContent\* aBindingParent) override#override#g' $file; done $ for file in $(rg '::BindToTree\(' | cut -d : -f 1 | sort | uniq); do sed -i 's#::BindToTree(Document\* aDocument, nsIContent\* aParent,#::BindToTree(BindContext\& aContext, nsINode\& aParent)#g' $file; done $ for file in $(rg '::BindToTree\(' | cut -d : -f 1 | sort | uniq); do sed -i 's#nsIContent\* aBindingParent)##g' $file; done $ for file in $(rg '::BindToTree\(' | cut -d : -f 1 | sort | uniq); do sed -i 's#::BindToTree(aDocument, aParent, aBindingParent)#::BindToTree(aContext, aParent)#g' $file; done $ ./mach clang-format Then manual fixups. Depends on D32948 Differential Revision: https://phabricator.services.mozilla.com/D32949
2019-05-29 07:27:04 +03:00
nsresult rv = SVGMPathElementBase::BindToTree(aContext, aParent);
NS_ENSURE_SUCCESS(rv, rv);
if (IsInComposedDoc()) {
const nsAttrValue* hrefAttrValue =
HasAttr(kNameSpaceID_None, nsGkAtoms::href)
? mAttrs.GetAttr(nsGkAtoms::href, kNameSpaceID_None)
: mAttrs.GetAttr(nsGkAtoms::href, kNameSpaceID_XLink);
if (hrefAttrValue) {
Bug 1555216 - Change the signature of BindToTree to be (BindContext&, nsINode& aParentNode). r=bzbarsky BindContext was going to have way more information at first, but then I realized that most of the things I wanted to know were basically a flag away using the parent node. Still I think it's worth it, now experimenting with BindToTree will only mean adding a field to a struct that's included from a couple cpp files, instead of a massive pain. I also think this is clearer, and doing this highlights quite a few inconsistencies in our code which I've left untouched, but commented with FIXMEs. Steps are: $ for file in $(rg 'nsresult BindToTree\(' | cut -d : -f 1 | sort | uniq); do sed -i 's#nsresult BindToTree(Document\* aDocument, nsIContent\* aParent,#nsresult BindToTree(BindContext\&, nsINode\& aParent)#g' $file; done $ for file in $(rg 'nsresult BindToTree\(' | cut -d : -f 1 | sort | uniq); do sed -i 's# nsIContent\* aBindingParent) override#override#g' $file; done $ for file in $(rg '::BindToTree\(' | cut -d : -f 1 | sort | uniq); do sed -i 's#::BindToTree(Document\* aDocument, nsIContent\* aParent,#::BindToTree(BindContext\& aContext, nsINode\& aParent)#g' $file; done $ for file in $(rg '::BindToTree\(' | cut -d : -f 1 | sort | uniq); do sed -i 's#nsIContent\* aBindingParent)##g' $file; done $ for file in $(rg '::BindToTree\(' | cut -d : -f 1 | sort | uniq); do sed -i 's#::BindToTree(aDocument, aParent, aBindingParent)#::BindToTree(aContext, aParent)#g' $file; done $ ./mach clang-format Then manual fixups. Depends on D32948 Differential Revision: https://phabricator.services.mozilla.com/D32949
2019-05-29 07:27:04 +03:00
UpdateHrefTarget(nsIContent::FromNode(aParent),
hrefAttrValue->GetStringValue());
}
}
return NS_OK;
}
void SVGMPathElement::UnbindFromTree(bool aNullParent) {
UnlinkHrefTarget(true);
SVGMPathElementBase::UnbindFromTree(aNullParent);
}
bool SVGMPathElement::ParseAttribute(int32_t aNamespaceID, nsAtom* aAttribute,
const nsAString& aValue,
nsIPrincipal* aMaybeScriptedPrincipal,
nsAttrValue& aResult) {
bool returnVal = SVGMPathElementBase::ParseAttribute(
aNamespaceID, aAttribute, aValue, aMaybeScriptedPrincipal, aResult);
if ((aNamespaceID == kNameSpaceID_XLink ||
aNamespaceID == kNameSpaceID_None) &&
aAttribute == nsGkAtoms::href && IsInComposedDoc()) {
// Note: If we fail the IsInDoc call, it's ok -- we'll update the target
// on next BindToTree call.
// Note: "href" takes priority over xlink:href. So if "xlink:href" is being
// set here, we only let that update our target if "href" is *unset*.
if (aNamespaceID != kNameSpaceID_XLink ||
!mStringAttributes[HREF].IsExplicitlySet()) {
UpdateHrefTarget(GetParent(), aValue);
}
}
return returnVal;
}
nsresult SVGMPathElement::AfterSetAttr(int32_t aNamespaceID, nsAtom* aName,
const nsAttrValue* aValue,
const nsAttrValue* aOldValue,
nsIPrincipal* aMaybeScriptedPrincipal,
bool aNotify) {
if (!aValue && aName == nsGkAtoms::href) {
// href attr being removed.
if (aNamespaceID == kNameSpaceID_None) {
UnlinkHrefTarget(true);
// After unsetting href, we may still have xlink:href, so we should
// try to add it back.
const nsAttrValue* xlinkHref =
mAttrs.GetAttr(nsGkAtoms::href, kNameSpaceID_XLink);
if (xlinkHref) {
UpdateHrefTarget(GetParent(), xlinkHref->GetStringValue());
}
} else if (aNamespaceID == kNameSpaceID_XLink &&
!HasAttr(kNameSpaceID_None, nsGkAtoms::href)) {
UnlinkHrefTarget(true);
} // else: we unset some random-namespace href attribute, or unset
// xlink:href but still have href attribute, so keep the target linking
// to href.
}
return SVGMPathElementBase::AfterSetAttr(
aNamespaceID, aName, aValue, aOldValue, aMaybeScriptedPrincipal, aNotify);
}
//----------------------------------------------------------------------
// SVGElement methods
SVGElement::StringAttributesInfo SVGMPathElement::GetStringInfo() {
return StringAttributesInfo(mStringAttributes, sStringInfo,
ArrayLength(sStringInfo));
}
//----------------------------------------------------------------------
// nsIMutationObserver methods
void SVGMPathElement::AttributeChanged(Element* aElement, int32_t aNameSpaceID,
nsAtom* aAttribute, int32_t aModType,
const nsAttrValue* aOldValue) {
if (aNameSpaceID == kNameSpaceID_None) {
if (aAttribute == nsGkAtoms::d) {
NotifyParentOfMpathChange(GetParent());
}
}
}
//----------------------------------------------------------------------
// Public helper methods
SVGPathElement* SVGMPathElement::GetReferencedPath() {
if (!HasAttr(kNameSpaceID_XLink, nsGkAtoms::href) &&
!HasAttr(kNameSpaceID_None, nsGkAtoms::href)) {
MOZ_ASSERT(!mPathTracker.get(),
"We shouldn't have a href target "
"if we don't have an xlink:href or href attribute");
return nullptr;
}
nsIContent* genericTarget = mPathTracker.get();
if (genericTarget && genericTarget->IsSVGElement(nsGkAtoms::path)) {
return static_cast<SVGPathElement*>(genericTarget);
}
return nullptr;
}
//----------------------------------------------------------------------
// Protected helper methods
void SVGMPathElement::UpdateHrefTarget(nsIContent* aParent,
const nsAString& aHrefStr) {
nsCOMPtr<nsIURI> targetURI;
nsContentUtils::NewURIWithDocumentCharset(getter_AddRefs(targetURI), aHrefStr,
OwnerDoc(), GetBaseURI());
// Stop observing old target (if any)
if (mPathTracker.get()) {
mPathTracker.get()->RemoveMutationObserver(this);
}
if (aParent) {
// Pass in |aParent| instead of |this| -- first argument is only used
// for a call to GetComposedDoc(), and |this| might not have a current
// document yet (if our caller is BindToTree).
nsCOMPtr<nsIReferrerInfo> referrerInfo =
ReferrerInfo::CreateForSVGResources(OwnerDoc());
mPathTracker.ResetToURIFragmentID(aParent, targetURI, referrerInfo);
} else {
// if we don't have a parent, then there's no animateMotion element
// depending on our target, so there's no point tracking it right now.
mPathTracker.Unlink();
}
// Start observing new target (if any)
if (mPathTracker.get()) {
mPathTracker.get()->AddMutationObserver(this);
}
NotifyParentOfMpathChange(aParent);
}
void SVGMPathElement::UnlinkHrefTarget(bool aNotifyParent) {
// Stop observing old target (if any)
if (mPathTracker.get()) {
mPathTracker.get()->RemoveMutationObserver(this);
}
mPathTracker.Unlink();
if (aNotifyParent) {
NotifyParentOfMpathChange(GetParent());
}
}
void SVGMPathElement::NotifyParentOfMpathChange(nsIContent* aParent) {
if (aParent && aParent->IsSVGElement(nsGkAtoms::animateMotion)) {
SVGAnimateMotionElement* animateMotionParent =
static_cast<SVGAnimateMotionElement*>(aParent);
animateMotionParent->MpathChanged();
AnimationNeedsResample();
}
}
} // namespace dom
} // namespace mozilla