1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4  * License, v. 2.0. If a copy of the MPL was not distributed with this
5  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 
7 #include "SVGElementFactory.h"
8 #include "nsGkAtoms.h"
9 #include "nsIContent.h"
10 #include "mozilla/dom/NodeInfo.h"
11 #include "mozilla/dom/Element.h"
12 #include "mozilla/dom/FromParser.h"
13 #include "mozilla/StaticPtr.h"
14 #include "nsDataHashtable.h"
15 #include "nsHashKeys.h"
16 
17 using namespace mozilla;
18 using namespace mozilla::dom;
19 
20 // Hash table that maps nsAtom* SVG tags to a SVGContentCreatorFunction.
21 using TagAtomTable =
22     nsDataHashtable<nsPtrHashKey<nsAtom>, SVGContentCreatorFunction>;
23 StaticAutoPtr<TagAtomTable> sTagAtomTable;
24 
25 #define SVG_TAG(_tag, _classname)                                         \
26   nsresult NS_NewSVG##_classname##Element(                                \
27       nsIContent** aResult,                                               \
28       already_AddRefed<mozilla::dom::NodeInfo>&& aNodeInfo);              \
29                                                                           \
30   nsresult NS_NewSVG##_classname##Element(                                \
31       nsIContent** aResult,                                               \
32       already_AddRefed<mozilla::dom::NodeInfo>&& aNodeInfo,               \
33       FromParser aFromParser) {                                           \
34     return NS_NewSVG##_classname##Element(aResult, std::move(aNodeInfo)); \
35   }
36 
37 #define SVG_FROM_PARSER_TAG(_tag, _classname)
38 
39 #include "SVGTagList.h"
40 #undef SVG_TAG
41 #undef SVG_FROM_PARSER_TAG
42 
43 nsresult NS_NewSVGElement(Element** aResult,
44                           already_AddRefed<mozilla::dom::NodeInfo>&& aNodeInfo);
45 
46 enum SVGTag {
47 #define SVG_TAG(_tag, _classname) eSVGTag_##_tag,
48 #define SVG_FROM_PARSER_TAG(_tag, _classname) eSVGTag_##_tag,
49 #include "SVGTagList.h"
50 #undef SVG_TAG
51 #undef SVG_FROM_PARSER_TAG
52   eSVGTag_Count
53 };
54 
Init()55 void SVGElementFactory::Init() {
56   sTagAtomTable = new TagAtomTable(64);
57 
58 #define SVG_TAG(_tag, _classname) \
59   sTagAtomTable->Put(nsGkAtoms::_tag, NS_NewSVG##_classname##Element);
60 #define SVG_FROM_PARSER_TAG(_tag, _classname) \
61   sTagAtomTable->Put(nsGkAtoms::_tag, NS_NewSVG##_classname##Element);
62 #include "SVGTagList.h"
63 #undef SVG_TAG
64 #undef SVG_FROM_PARSER_TAG
65 }
66 
Shutdown()67 void SVGElementFactory::Shutdown() { sTagAtomTable = nullptr; }
68 
NS_NewSVGElement(Element ** aResult,already_AddRefed<mozilla::dom::NodeInfo> && aNodeInfo,FromParser aFromParser)69 nsresult NS_NewSVGElement(Element** aResult,
70                           already_AddRefed<mozilla::dom::NodeInfo>&& aNodeInfo,
71                           FromParser aFromParser) {
72   NS_ASSERTION(sTagAtomTable, "no lookup table, needs SVGElementFactory::Init");
73 
74   RefPtr<mozilla::dom::NodeInfo> ni = aNodeInfo;
75   nsAtom* name = ni->NameAtom();
76 
77   NS_ASSERTION(
78       ni->NamespaceEquals(kNameSpaceID_SVG),
79       "Trying to create SVG elements that aren't in the SVG namespace");
80 
81   SVGContentCreatorFunction cb = sTagAtomTable->Get(name);
82   if (cb) {
83     nsCOMPtr<nsIContent> content;
84     nsresult rv = cb(getter_AddRefs(content), ni.forget(), aFromParser);
85     *aResult = content.forget().take()->AsElement();
86     return rv;
87   }
88 
89   // if we don't know what to create, just create a standard svg element:
90   return NS_NewSVGElement(aResult, ni.forget());
91 }
92 
NS_NewSVGUnknownElement(nsIContent ** aResult,already_AddRefed<mozilla::dom::NodeInfo> && aNodeInfo,FromParser aFromParser)93 nsresult NS_NewSVGUnknownElement(
94     nsIContent** aResult, already_AddRefed<mozilla::dom::NodeInfo>&& aNodeInfo,
95     FromParser aFromParser) {
96   RefPtr<mozilla::dom::NodeInfo> ni = aNodeInfo;
97   nsCOMPtr<Element> element;
98   nsresult rv = NS_NewSVGElement(getter_AddRefs(element), ni.forget());
99   element.forget(aResult);
100   return rv;
101 }
102