2015-05-03 22:32:37 +03:00
|
|
|
/* -*- 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/. */
|
2012-05-17 14:02:41 +04:00
|
|
|
|
|
|
|
#include "SVGFragmentIdentifier.h"
|
2013-08-14 10:56:21 +04:00
|
|
|
|
2013-01-10 03:02:45 +04:00
|
|
|
#include "mozilla/dom/SVGSVGElement.h"
|
2013-01-08 07:22:41 +04:00
|
|
|
#include "mozilla/dom/SVGViewElement.h"
|
2013-08-14 10:56:21 +04:00
|
|
|
#include "nsContentUtils.h" // for nsCharSeparatedTokenizerTemplate
|
2013-04-15 02:56:34 +04:00
|
|
|
#include "nsSVGAnimatedTransformList.h"
|
2013-08-21 23:28:26 +04:00
|
|
|
#include "nsCharSeparatedTokenizer.h"
|
2012-05-17 14:02:41 +04:00
|
|
|
|
|
|
|
using namespace mozilla;
|
|
|
|
|
|
|
|
static bool
|
2013-05-05 11:20:25 +04:00
|
|
|
IsMatchingParameter(const nsAString& aString, const nsAString& aParameterName)
|
2012-05-17 14:02:41 +04:00
|
|
|
{
|
2012-05-18 14:41:30 +04:00
|
|
|
// The first two tests ensure aString.Length() > aParameterName.Length()
|
|
|
|
// so it's then safe to do the third test
|
2012-05-17 14:02:41 +04:00
|
|
|
return StringBeginsWith(aString, aParameterName) &&
|
2012-05-18 14:41:30 +04:00
|
|
|
aString.Last() == ')' &&
|
|
|
|
aString.CharAt(aParameterName.Length()) == '(';
|
2012-05-17 14:02:41 +04:00
|
|
|
}
|
|
|
|
|
2012-08-25 21:02:34 +04:00
|
|
|
inline bool
|
2014-01-04 19:02:17 +04:00
|
|
|
IgnoreWhitespace(char16_t aChar)
|
2012-08-25 21:02:34 +04:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-01-08 07:22:41 +04:00
|
|
|
static dom::SVGViewElement*
|
2013-05-05 11:20:25 +04:00
|
|
|
GetViewElement(nsIDocument* aDocument, const nsAString& aId)
|
2012-05-17 14:02:41 +04:00
|
|
|
{
|
|
|
|
dom::Element* element = aDocument->GetElementById(aId);
|
2015-03-03 14:08:59 +03:00
|
|
|
return (element && element->IsSVGElement(nsGkAtoms::view)) ?
|
2013-01-08 07:22:41 +04:00
|
|
|
static_cast<dom::SVGViewElement*>(element) : nullptr;
|
2012-05-17 14:02:41 +04:00
|
|
|
}
|
|
|
|
|
2013-01-10 03:02:45 +04:00
|
|
|
void
|
2013-05-05 11:20:25 +04:00
|
|
|
SVGFragmentIdentifier::SaveOldPreserveAspectRatio(dom::SVGSVGElement* root)
|
2012-05-17 14:02:41 +04:00
|
|
|
{
|
2012-08-15 15:50:48 +04:00
|
|
|
if (root->mPreserveAspectRatio.IsExplicitlySet()) {
|
2012-05-17 14:02:41 +04:00
|
|
|
root->SetPreserveAspectRatioProperty(root->mPreserveAspectRatio.GetBaseValue());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2013-05-05 11:20:25 +04:00
|
|
|
SVGFragmentIdentifier::RestoreOldPreserveAspectRatio(dom::SVGSVGElement* root)
|
2012-05-17 14:02:41 +04:00
|
|
|
{
|
2013-05-05 11:20:25 +04:00
|
|
|
const SVGPreserveAspectRatio* oldPARPtr = root->GetPreserveAspectRatioProperty();
|
2012-05-17 14:02:41 +04:00
|
|
|
if (oldPARPtr) {
|
|
|
|
root->mPreserveAspectRatio.SetBaseValue(*oldPARPtr, root);
|
2012-08-15 15:50:48 +04:00
|
|
|
} else if (root->mPreserveAspectRatio.IsExplicitlySet()) {
|
2012-10-16 15:51:00 +04:00
|
|
|
mozilla::ErrorResult error;
|
|
|
|
root->RemoveAttribute(NS_LITERAL_STRING("preserveAspectRatio"), error);
|
2012-05-17 14:02:41 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2013-05-05 11:20:25 +04:00
|
|
|
SVGFragmentIdentifier::SaveOldViewBox(dom::SVGSVGElement* root)
|
2012-05-17 14:02:41 +04:00
|
|
|
{
|
2012-08-15 15:50:48 +04:00
|
|
|
if (root->mViewBox.IsExplicitlySet()) {
|
2012-05-17 14:02:41 +04:00
|
|
|
root->SetViewBoxProperty(root->mViewBox.GetBaseValue());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2013-05-05 11:20:25 +04:00
|
|
|
SVGFragmentIdentifier::RestoreOldViewBox(dom::SVGSVGElement* root)
|
2012-05-17 14:02:41 +04:00
|
|
|
{
|
2013-05-05 11:20:25 +04:00
|
|
|
const nsSVGViewBoxRect* oldViewBoxPtr = root->GetViewBoxProperty();
|
2012-05-17 14:02:41 +04:00
|
|
|
if (oldViewBoxPtr) {
|
|
|
|
root->mViewBox.SetBaseValue(*oldViewBoxPtr, root);
|
2012-08-15 15:50:48 +04:00
|
|
|
} else if (root->mViewBox.IsExplicitlySet()) {
|
2012-10-16 15:51:00 +04:00
|
|
|
mozilla::ErrorResult error;
|
|
|
|
root->RemoveAttribute(NS_LITERAL_STRING("viewBox"), error);
|
2012-05-17 14:02:41 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2013-05-05 11:20:25 +04:00
|
|
|
SVGFragmentIdentifier::SaveOldZoomAndPan(dom::SVGSVGElement* root)
|
2012-05-17 14:02:41 +04:00
|
|
|
{
|
2013-01-10 03:02:45 +04:00
|
|
|
if (root->mEnumAttributes[dom::SVGSVGElement::ZOOMANDPAN].IsExplicitlySet()) {
|
|
|
|
root->SetZoomAndPanProperty(root->mEnumAttributes[dom::SVGSVGElement::ZOOMANDPAN].GetBaseValue());
|
2012-05-17 14:02:41 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-10 09:24:37 +04:00
|
|
|
void
|
2013-05-05 11:20:25 +04:00
|
|
|
SVGFragmentIdentifier::RestoreOldZoomAndPan(dom::SVGSVGElement* root)
|
2012-05-17 14:02:41 +04:00
|
|
|
{
|
2012-08-22 19:56:38 +04:00
|
|
|
uint16_t oldZoomAndPan = root->GetZoomAndPanProperty();
|
2013-01-10 09:24:37 +04:00
|
|
|
if (oldZoomAndPan != SVG_ZOOMANDPAN_UNKNOWN) {
|
2013-01-10 03:02:45 +04:00
|
|
|
root->mEnumAttributes[dom::SVGSVGElement::ZOOMANDPAN].SetBaseValue(oldZoomAndPan, root);
|
|
|
|
} else if (root->mEnumAttributes[dom::SVGSVGElement::ZOOMANDPAN].IsExplicitlySet()) {
|
2012-10-16 15:51:00 +04:00
|
|
|
mozilla::ErrorResult error;
|
|
|
|
root->RemoveAttribute(NS_LITERAL_STRING("zoomAndPan"), error);
|
2012-05-17 14:02:41 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-22 13:13:24 +04:00
|
|
|
void
|
2013-05-05 11:20:25 +04:00
|
|
|
SVGFragmentIdentifier::SaveOldTransform(dom::SVGSVGElement* root)
|
2012-10-22 13:13:24 +04:00
|
|
|
{
|
2013-05-05 11:20:25 +04:00
|
|
|
nsSVGAnimatedTransformList* transformList = root->GetAnimatedTransformList();
|
|
|
|
|
|
|
|
if (transformList && transformList->IsExplicitlySet()) {
|
|
|
|
root->SetTransformProperty(transformList->GetBaseValue());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
SVGFragmentIdentifier::RestoreOldTransform(dom::SVGSVGElement* root)
|
|
|
|
{
|
|
|
|
const SVGTransformList* oldTransformPtr = root->GetTransformProperty();
|
|
|
|
if (oldTransformPtr) {
|
|
|
|
root->GetAnimatedTransformList(nsSVGElement::DO_ALLOCATE)->SetBaseValue(*oldTransformPtr);
|
|
|
|
} else {
|
|
|
|
nsSVGAnimatedTransformList* transformList = root->GetAnimatedTransformList();
|
|
|
|
if (transformList && transformList->IsExplicitlySet()) {
|
|
|
|
mozilla::ErrorResult error;
|
|
|
|
root->RemoveAttribute(NS_LITERAL_STRING("transform"), error);
|
|
|
|
}
|
|
|
|
}
|
2012-10-22 13:13:24 +04:00
|
|
|
}
|
|
|
|
|
2012-05-17 14:02:41 +04:00
|
|
|
bool
|
2013-05-05 11:20:25 +04:00
|
|
|
SVGFragmentIdentifier::ProcessSVGViewSpec(const nsAString& aViewSpec,
|
|
|
|
dom::SVGSVGElement* root)
|
2012-05-17 14:02:41 +04:00
|
|
|
{
|
|
|
|
if (!IsMatchingParameter(aViewSpec, NS_LITERAL_STRING("svgView"))) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-08-25 21:02:34 +04:00
|
|
|
// SVGViewAttributes may occur in any order, but each type may only occur
|
|
|
|
// at most one time in a correctly formed SVGViewSpec.
|
|
|
|
// If we encounter any attribute more than once or get any syntax errors
|
|
|
|
// we're going to return false and cancel any changes.
|
|
|
|
|
|
|
|
bool viewBoxFound = false;
|
|
|
|
bool preserveAspectRatioFound = false;
|
2012-10-22 13:13:24 +04:00
|
|
|
bool transformFound = false;
|
2012-08-25 21:02:34 +04:00
|
|
|
bool zoomAndPanFound = false;
|
2012-05-17 14:02:41 +04:00
|
|
|
|
|
|
|
// Each token is a SVGViewAttribute
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t bracketPos = aViewSpec.FindChar('(');
|
2012-08-25 21:02:34 +04:00
|
|
|
uint32_t lengthOfViewSpec = aViewSpec.Length() - bracketPos - 2;
|
|
|
|
nsCharSeparatedTokenizerTemplate<IgnoreWhitespace> tokenizer(
|
|
|
|
Substring(aViewSpec, bracketPos + 1, lengthOfViewSpec), ';');
|
2012-05-17 14:02:41 +04:00
|
|
|
|
2012-05-31 14:39:12 +04:00
|
|
|
if (!tokenizer.hasMoreTokens()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
do {
|
2012-05-17 14:02:41 +04:00
|
|
|
|
|
|
|
nsAutoString token(tokenizer.nextToken());
|
|
|
|
|
|
|
|
bracketPos = token.FindChar('(');
|
|
|
|
if (bracketPos < 1 || token.Last() != ')') {
|
|
|
|
// invalid SVGViewAttribute syntax
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
const nsAString ¶ms =
|
|
|
|
Substring(token, bracketPos + 1, token.Length() - bracketPos - 2);
|
|
|
|
|
|
|
|
if (IsMatchingParameter(token, NS_LITERAL_STRING("viewBox"))) {
|
2012-08-25 21:02:34 +04:00
|
|
|
if (viewBoxFound ||
|
|
|
|
NS_FAILED(root->mViewBox.SetBaseValueString(
|
|
|
|
params, root, true))) {
|
2012-05-17 14:02:41 +04:00
|
|
|
return false;
|
|
|
|
}
|
2012-08-25 21:02:34 +04:00
|
|
|
viewBoxFound = true;
|
2012-05-17 14:02:41 +04:00
|
|
|
} else if (IsMatchingParameter(token, NS_LITERAL_STRING("preserveAspectRatio"))) {
|
2012-08-25 21:02:34 +04:00
|
|
|
if (preserveAspectRatioFound ||
|
|
|
|
NS_FAILED(root->mPreserveAspectRatio.SetBaseValueString(
|
|
|
|
params, root, true))) {
|
2012-05-17 14:02:41 +04:00
|
|
|
return false;
|
|
|
|
}
|
2012-08-25 21:02:34 +04:00
|
|
|
preserveAspectRatioFound = true;
|
2012-10-22 13:13:24 +04:00
|
|
|
} else if (IsMatchingParameter(token, NS_LITERAL_STRING("transform"))) {
|
|
|
|
if (transformFound ||
|
2013-05-05 11:20:25 +04:00
|
|
|
NS_FAILED(root->GetAnimatedTransformList(nsSVGElement::DO_ALLOCATE)->
|
|
|
|
SetBaseValueString(params))) {
|
2012-10-22 13:13:24 +04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
transformFound = true;
|
2012-05-17 14:02:41 +04:00
|
|
|
} else if (IsMatchingParameter(token, NS_LITERAL_STRING("zoomAndPan"))) {
|
2012-08-25 21:02:34 +04:00
|
|
|
if (zoomAndPanFound) {
|
2012-05-17 14:02:41 +04:00
|
|
|
return false;
|
|
|
|
}
|
2013-05-05 11:20:25 +04:00
|
|
|
nsIAtom* valAtom = NS_GetStaticAtom(params);
|
2012-08-25 21:02:34 +04:00
|
|
|
if (!valAtom) {
|
|
|
|
return false;
|
|
|
|
}
|
2013-05-05 11:20:25 +04:00
|
|
|
const nsSVGEnumMapping* mapping = dom::SVGSVGElement::sZoomAndPanMap;
|
2012-08-25 21:02:34 +04:00
|
|
|
while (mapping->mKey) {
|
|
|
|
if (valAtom == *(mapping->mKey)) {
|
|
|
|
// If we've got a valid zoomAndPan value, then set it on our root element.
|
2013-01-10 03:02:45 +04:00
|
|
|
if (NS_FAILED(root->mEnumAttributes[dom::SVGSVGElement::ZOOMANDPAN].SetBaseValue(
|
2012-08-25 21:02:34 +04:00
|
|
|
mapping->mVal, root))) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
mapping++;
|
|
|
|
}
|
|
|
|
if (!mapping->mKey) {
|
|
|
|
// Unrecognised zoomAndPan value
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
zoomAndPanFound = true;
|
2012-05-17 14:02:41 +04:00
|
|
|
} else {
|
2012-10-22 13:13:24 +04:00
|
|
|
// We don't support viewTarget currently
|
2012-05-17 14:02:41 +04:00
|
|
|
return false;
|
|
|
|
}
|
2012-05-31 14:39:12 +04:00
|
|
|
} while (tokenizer.hasMoreTokens());
|
2012-05-17 14:02:41 +04:00
|
|
|
|
2012-08-25 21:02:34 +04:00
|
|
|
if (root->mUseCurrentView) {
|
|
|
|
// A previous SVGViewSpec may have overridden some attributes.
|
|
|
|
// If they are no longer overridden we need to restore the old values.
|
2012-10-22 13:13:24 +04:00
|
|
|
if (!transformFound) {
|
2013-05-05 11:20:25 +04:00
|
|
|
RestoreOldTransform(root);
|
2012-10-22 13:13:24 +04:00
|
|
|
}
|
2012-08-25 21:02:34 +04:00
|
|
|
if (!viewBoxFound) {
|
|
|
|
RestoreOldViewBox(root);
|
|
|
|
}
|
|
|
|
if (!preserveAspectRatioFound) {
|
|
|
|
RestoreOldPreserveAspectRatio(root);
|
|
|
|
}
|
|
|
|
if (!zoomAndPanFound) {
|
|
|
|
RestoreOldZoomAndPan(root);
|
2012-05-17 14:02:41 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2013-05-05 11:20:25 +04:00
|
|
|
SVGFragmentIdentifier::ProcessFragmentIdentifier(nsIDocument* aDocument,
|
|
|
|
const nsAString& aAnchorName)
|
2012-05-17 14:02:41 +04:00
|
|
|
{
|
2015-03-03 14:08:59 +03:00
|
|
|
MOZ_ASSERT(aDocument->GetRootElement()->IsSVGElement(nsGkAtoms::svg),
|
2015-02-10 01:34:50 +03:00
|
|
|
"expecting an SVG root element");
|
2012-05-17 14:02:41 +04:00
|
|
|
|
2013-05-05 11:20:25 +04:00
|
|
|
dom::SVGSVGElement* rootElement =
|
2013-01-10 03:02:45 +04:00
|
|
|
static_cast<dom::SVGSVGElement*>(aDocument->GetRootElement());
|
2012-05-17 14:02:41 +04:00
|
|
|
|
2012-08-15 15:50:48 +04:00
|
|
|
if (!rootElement->mUseCurrentView) {
|
|
|
|
SaveOldViewBox(rootElement);
|
|
|
|
SaveOldPreserveAspectRatio(rootElement);
|
|
|
|
SaveOldZoomAndPan(rootElement);
|
|
|
|
}
|
|
|
|
|
2013-05-05 11:20:25 +04:00
|
|
|
const dom::SVGViewElement* viewElement = GetViewElement(aDocument, aAnchorName);
|
2012-05-17 14:02:41 +04:00
|
|
|
|
|
|
|
if (viewElement) {
|
2012-09-09 15:44:03 +04:00
|
|
|
if (!rootElement->mCurrentViewID) {
|
|
|
|
rootElement->mCurrentViewID = new nsString();
|
2012-05-17 14:02:41 +04:00
|
|
|
}
|
2012-09-09 15:44:03 +04:00
|
|
|
*rootElement->mCurrentViewID = aAnchorName;
|
2012-05-31 14:39:12 +04:00
|
|
|
rootElement->mUseCurrentView = true;
|
2012-09-09 15:44:03 +04:00
|
|
|
rootElement->InvalidateTransformNotifyFrame();
|
2015-03-08 20:34:47 +03:00
|
|
|
// not an svgView()-style fragment identifier, return false so the caller
|
|
|
|
// continues processing to match any :target pseudo elements
|
|
|
|
return false;
|
2012-05-17 14:02:41 +04:00
|
|
|
}
|
|
|
|
|
2012-09-09 15:44:03 +04:00
|
|
|
bool wasOverridden = !!rootElement->mCurrentViewID;
|
|
|
|
rootElement->mCurrentViewID = nullptr;
|
|
|
|
|
2012-05-31 14:39:12 +04:00
|
|
|
rootElement->mUseCurrentView = ProcessSVGViewSpec(aAnchorName, rootElement);
|
|
|
|
if (rootElement->mUseCurrentView) {
|
2012-05-17 14:02:41 +04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
RestoreOldViewBox(rootElement);
|
2012-08-15 15:50:48 +04:00
|
|
|
rootElement->ClearViewBoxProperty();
|
2012-05-17 14:02:41 +04:00
|
|
|
RestoreOldPreserveAspectRatio(rootElement);
|
2012-08-15 15:50:48 +04:00
|
|
|
rootElement->ClearPreserveAspectRatioProperty();
|
2012-05-17 14:02:41 +04:00
|
|
|
RestoreOldZoomAndPan(rootElement);
|
2012-08-15 15:50:48 +04:00
|
|
|
rootElement->ClearZoomAndPanProperty();
|
2013-05-05 11:20:25 +04:00
|
|
|
RestoreOldTransform(rootElement);
|
|
|
|
rootElement->ClearTransformProperty();
|
2012-09-09 15:44:03 +04:00
|
|
|
if (wasOverridden) {
|
|
|
|
rootElement->InvalidateTransformNotifyFrame();
|
|
|
|
}
|
2012-05-17 14:02:41 +04:00
|
|
|
return false;
|
|
|
|
}
|