SVGViewSpec objects should mark relevant SVG elements
[WebKit-https.git] / Source / WebCore / svg / SVGViewSpec.cpp
1 /*
2  * Copyright (C) 2007, 2010 Rob Buis <buis@kde.org>
3  * Copyright (C) 2018 Apple Inc. All rights reserved.
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Library General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Library General Public License for more details.
14  *
15  * You should have received a copy of the GNU Library General Public License
16  * along with this library; see the file COPYING.LIB.  If not, write to
17  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18  * Boston, MA 02110-1301, USA.
19  */
20
21 #include "config.h"
22 #include "SVGViewSpec.h"
23
24 #include "Document.h"
25 #include "SVGAnimatedTransformList.h"
26 #include "SVGElement.h"
27 #include "SVGFitToViewBox.h"
28 #include "SVGNames.h"
29 #include "SVGParserUtilities.h"
30 #include "SVGTransformList.h"
31 #include "SVGTransformable.h"
32
33 namespace WebCore {
34
35 SVGViewSpec::SVGViewSpec(SVGElement& contextElement)
36     : SVGFitToViewBox(&contextElement, PropertyIsReadOnly)
37     , m_contextElement(makeWeakPtr(contextElement))
38     , m_attributeOwnerProxy(*this, contextElement)
39 {
40     registerAttributes();
41 }
42
43 void SVGViewSpec::registerAttributes()
44 {
45     auto& registry = attributeRegistry();
46     if (!registry.isEmpty())
47         return;
48     registry.registerAttribute<SVGNames::transformAttr, &SVGViewSpec::m_transform>();
49 }
50
51 SVGElement* SVGViewSpec::viewTarget() const
52 {
53     if (!m_contextElement)
54         return nullptr;
55     auto* element = m_contextElement->treeScope().getElementById(m_viewTargetString);
56     if (!is<SVGElement>(element))
57         return nullptr;
58     return downcast<SVGElement>(element);
59 }
60
61 RefPtr<SVGTransformList> SVGViewSpec::transform()
62 {
63     if (!m_contextElement)
64         return nullptr;
65     // Return the animVal here, as its readonly by default - which is exactly what we want here.
66     return m_transform.animatedProperty(m_attributeOwnerProxy)->animVal();
67 }
68
69 void SVGViewSpec::reset()
70 {
71     m_viewTargetString = emptyString();
72     m_transform.resetValue();
73     SVGFitToViewBox::reset();
74     SVGZoomAndPan::reset();
75 }
76
77 static const UChar svgViewSpec[] = {'s', 'v', 'g', 'V', 'i', 'e', 'w'};
78 static const UChar viewBoxSpec[] = {'v', 'i', 'e', 'w', 'B', 'o', 'x'};
79 static const UChar preserveAspectRatioSpec[] = {'p', 'r', 'e', 's', 'e', 'r', 'v', 'e', 'A', 's', 'p', 'e', 'c', 't', 'R', 'a', 't', 'i', 'o'};
80 static const UChar transformSpec[] = {'t', 'r', 'a', 'n', 's', 'f', 'o', 'r', 'm'};
81 static const UChar zoomAndPanSpec[] = {'z', 'o', 'o', 'm', 'A', 'n', 'd', 'P', 'a', 'n'};
82 static const UChar viewTargetSpec[] =  {'v', 'i', 'e', 'w', 'T', 'a', 'r', 'g', 'e', 't'};
83
84 bool SVGViewSpec::parseViewSpec(const String& viewSpec)
85 {
86     auto upconvertedCharacters = StringView(viewSpec).upconvertedCharacters();
87     const UChar* currViewSpec = upconvertedCharacters;
88     const UChar* end = currViewSpec + viewSpec.length();
89
90     if (currViewSpec >= end || !m_contextElement)
91         return false;
92
93     if (!skipString(currViewSpec, end, svgViewSpec, WTF_ARRAY_LENGTH(svgViewSpec)))
94         return false;
95
96     if (currViewSpec >= end || *currViewSpec != '(')
97         return false;
98     currViewSpec++;
99
100     while (currViewSpec < end && *currViewSpec != ')') {
101         if (*currViewSpec == 'v') {
102             if (skipString(currViewSpec, end, viewBoxSpec, WTF_ARRAY_LENGTH(viewBoxSpec))) {
103                 if (currViewSpec >= end || *currViewSpec != '(')
104                     return false;
105                 currViewSpec++;
106                 FloatRect viewBox;
107                 if (!SVGFitToViewBox::parseViewBox(currViewSpec, end, viewBox, false))
108                     return false;
109                 setViewBox(viewBox);
110                 if (currViewSpec >= end || *currViewSpec != ')')
111                     return false;
112                 currViewSpec++;
113             } else if (skipString(currViewSpec, end, viewTargetSpec, WTF_ARRAY_LENGTH(viewTargetSpec))) {
114                 if (currViewSpec >= end || *currViewSpec != '(')
115                     return false;
116                 const UChar* viewTargetStart = ++currViewSpec;
117                 while (currViewSpec < end && *currViewSpec != ')')
118                     currViewSpec++;
119                 if (currViewSpec >= end)
120                     return false;
121                 m_viewTargetString = String(viewTargetStart, currViewSpec - viewTargetStart);
122                 currViewSpec++;
123             } else
124                 return false;
125         } else if (*currViewSpec == 'z') {
126             if (!skipString(currViewSpec, end, zoomAndPanSpec, WTF_ARRAY_LENGTH(zoomAndPanSpec)))
127                 return false;
128             if (currViewSpec >= end || *currViewSpec != '(')
129                 return false;
130             currViewSpec++;
131             if (!SVGZoomAndPan::parseZoomAndPan(currViewSpec, end))
132                 return false;
133             if (currViewSpec >= end || *currViewSpec != ')')
134                 return false;
135             currViewSpec++;
136         } else if (*currViewSpec == 'p') {
137             if (!skipString(currViewSpec, end, preserveAspectRatioSpec, WTF_ARRAY_LENGTH(preserveAspectRatioSpec)))
138                 return false;
139             if (currViewSpec >= end || *currViewSpec != '(')
140                 return false;
141             currViewSpec++;
142             SVGPreserveAspectRatioValue preserveAspectRatio;
143             if (!preserveAspectRatio.parse(currViewSpec, end, false))
144                 return false;
145             setPreserveAspectRatio(preserveAspectRatio);
146             if (currViewSpec >= end || *currViewSpec != ')')
147                 return false;
148             currViewSpec++;
149         } else if (*currViewSpec == 't') {
150             if (!skipString(currViewSpec, end, transformSpec, WTF_ARRAY_LENGTH(transformSpec)))
151                 return false;
152             if (currViewSpec >= end || *currViewSpec != '(')
153                 return false;
154             currViewSpec++;
155             SVGTransformable::parseTransformAttribute(m_transform.value(), currViewSpec, end, SVGTransformable::DoNotClearList);
156             if (currViewSpec >= end || *currViewSpec != ')')
157                 return false;
158             currViewSpec++;
159         } else
160             return false;
161
162         if (currViewSpec < end && *currViewSpec == ';')
163             currViewSpec++;
164     }
165     
166     if (currViewSpec >= end || *currViewSpec != ')')
167         return false;
168
169     return true;
170 }
171
172 }