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 
14 using namespace mozilla;
15 using namespace mozilla::dom;
16 
17 // Hash table that maps nsIAtom* SVG tags to an offset index
18 // within the array sContentCreatorCallbacks (offset by TABLE_VALUE_OFFSET)
19 static PLHashTable* sTagAtomTable = nullptr;
20 
21 // We don't want to store 0 in the hash table as a return value of 0 from
22 // PL_HashTableLookupConst indicates that the value is not found
23 #define TABLE_VALUE_OFFSET 1
24 
25 #define SVG_TAG(_tag, _classname) \
26 nsresult \
27 NS_NewSVG##_classname##Element(nsIContent** aResult, \
28                                already_AddRefed<mozilla::dom::NodeInfo>&& aNodeInfo); \
29 \
30 static inline nsresult \
31 Create##_classname##Element(nsIContent** aResult, \
32                             already_AddRefed<mozilla::dom::NodeInfo>&& aNodeInfo, \
33                             FromParser aFromParser) \
34 { \
35   return NS_NewSVG##_classname##Element(aResult, mozilla::Move(aNodeInfo)); \
36 }
37 
38 #define SVG_FROM_PARSER_TAG(_tag, _classname) \
39 nsresult \
40 NS_NewSVG##_classname##Element(nsIContent** aResult, \
41                                already_AddRefed<mozilla::dom::NodeInfo>&& aNodeInfo, \
42                                FromParser aFromParser);
43 #include "SVGTagList.h"
44 #undef SVG_TAG
45 #undef SVG_FROM_PARSER_TAG
46 
47 nsresult
48 NS_NewSVGElement(Element** aResult,
49                  already_AddRefed<mozilla::dom::NodeInfo>&& aNodeInfo);
50 
51 typedef nsresult
52   (*contentCreatorCallback)(nsIContent** aResult,
53                             already_AddRefed<mozilla::dom::NodeInfo>&& aNodeInfo,
54                             FromParser aFromParser);
55 
56 static const contentCreatorCallback sContentCreatorCallbacks[] = {
57 #define SVG_TAG(_tag, _classname) Create##_classname##Element,
58 #define SVG_FROM_PARSER_TAG(_tag, _classname)  NS_NewSVG##_classname##Element,
59 #include "SVGTagList.h"
60 #undef SVG_TAG
61 #undef SVG_FROM_PARSER_TAG
62 };
63 
64 enum SVGTag {
65 #define SVG_TAG(_tag, _classname) eSVGTag_##_tag,
66 #define SVG_FROM_PARSER_TAG(_tag, _classname) eSVGTag_##_tag,
67 #include "SVGTagList.h"
68 #undef SVG_TAG
69 #undef SVG_FROM_PARSER_TAG
70   eSVGTag_Count
71 };
72 
73 // nsIAtom* -> id hash
74 static PLHashNumber
SVGTagsHashCodeAtom(const void * key)75 SVGTagsHashCodeAtom(const void* key)
76 {
77   return NS_PTR_TO_INT32(key) >> 2;
78 }
79 
80 void
Init()81 SVGElementFactory::Init()
82 {
83   sTagAtomTable = PL_NewHashTable(64, SVGTagsHashCodeAtom,
84                                   PL_CompareValues, PL_CompareValues,
85                                   nullptr, nullptr);
86 
87 #define SVG_TAG(_tag, _classname) \
88   PL_HashTableAdd(sTagAtomTable, nsGkAtoms::_tag,\
89                   NS_INT32_TO_PTR(eSVGTag_##_tag + TABLE_VALUE_OFFSET));
90 #define SVG_FROM_PARSER_TAG(_tag, _classname) \
91   PL_HashTableAdd(sTagAtomTable, nsGkAtoms::_tag,\
92                   NS_INT32_TO_PTR(eSVGTag_##_tag + TABLE_VALUE_OFFSET));
93 #include "SVGTagList.h"
94 #undef SVG_TAG
95 #undef SVG_FROM_PARSER_TAG
96 }
97 
98 void
Shutdown()99 SVGElementFactory::Shutdown()
100 {
101   if (sTagAtomTable) {
102     PL_HashTableDestroy(sTagAtomTable);
103     sTagAtomTable = nullptr;
104   }
105 }
106 
107 nsresult
NS_NewSVGElement(Element ** aResult,already_AddRefed<mozilla::dom::NodeInfo> && aNodeInfo,FromParser aFromParser)108 NS_NewSVGElement(Element** aResult, already_AddRefed<mozilla::dom::NodeInfo>&& aNodeInfo,
109                  FromParser aFromParser)
110 {
111   NS_ASSERTION(sTagAtomTable, "no lookup table, needs SVGElementFactory::Init");
112 
113   RefPtr<mozilla::dom::NodeInfo> ni = aNodeInfo;
114   nsIAtom* name = ni->NameAtom();
115 
116   NS_ASSERTION(ni->NamespaceEquals(kNameSpaceID_SVG),
117                "Trying to create SVG elements that aren't in the SVG namespace");
118 
119   void* tag = PL_HashTableLookupConst(sTagAtomTable, name);
120   if (tag) {
121     int32_t index = NS_PTR_TO_INT32(tag) - TABLE_VALUE_OFFSET;
122     if (index < 0 || index >= eSVGTag_Count) {
123       NS_WARNING("About to index out of array bounds - crashing instead");
124       MOZ_CRASH();
125     }
126 
127     contentCreatorCallback cb = sContentCreatorCallbacks[index];
128 
129     nsCOMPtr<nsIContent> content;
130     nsresult rv = cb(getter_AddRefs(content), ni.forget(), aFromParser);
131     *aResult = content.forget().take()->AsElement();
132     return rv;
133   }
134 
135   // if we don't know what to create, just create a standard svg element:
136   return NS_NewSVGElement(aResult, ni.forget());
137 }
138