Bug 1700051: part 46) Const-qualify `mozInlineSpellStatus::mAnchorRange`. r=smaug
[gecko.git] / dom / svg / SVGElementFactory.cpp
blob61cd2f65dd541bd60616c7fa8cae4318f226c19a
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/. */
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 "nsTHashMap.h"
15 #include "nsHashKeys.h"
17 using namespace mozilla;
18 using namespace mozilla::dom;
20 // Hash table that maps nsAtom* SVG tags to a SVGContentCreatorFunction.
21 using TagAtomTable =
22 nsTHashMap<nsPtrHashKey<nsAtom>, SVGContentCreatorFunction>;
23 StaticAutoPtr<TagAtomTable> sTagAtomTable;
25 #define SVG_TAG(_tag, _classname) \
26 nsresult NS_NewSVG##_classname##Element( \
27 nsIContent** aResult, \
28 already_AddRefed<mozilla::dom::NodeInfo>&& aNodeInfo); \
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)); \
37 #define SVG_FROM_PARSER_TAG(_tag, _classname)
39 #include "SVGTagList.h"
40 #undef SVG_TAG
41 #undef SVG_FROM_PARSER_TAG
43 nsresult NS_NewSVGElement(Element** aResult,
44 already_AddRefed<mozilla::dom::NodeInfo>&& aNodeInfo);
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
55 void SVGElementFactory::Init() {
56 sTagAtomTable = new TagAtomTable(64);
58 #define SVG_TAG(_tag, _classname) \
59 sTagAtomTable->InsertOrUpdate( \
60 nsGkAtoms::_tag, \
61 SVGContentCreatorFunction(NS_NewSVG##_classname##Element));
62 #define SVG_FROM_PARSER_TAG(_tag, _classname) \
63 sTagAtomTable->InsertOrUpdate( \
64 nsGkAtoms::_tag, \
65 SVGContentCreatorFunction(NS_NewSVG##_classname##Element));
66 #include "SVGTagList.h"
67 #undef SVG_TAG
68 #undef SVG_FROM_PARSER_TAG
71 void SVGElementFactory::Shutdown() { sTagAtomTable = nullptr; }
73 nsresult NS_NewSVGElement(Element** aResult,
74 already_AddRefed<mozilla::dom::NodeInfo>&& aNodeInfo,
75 FromParser aFromParser) {
76 NS_ASSERTION(sTagAtomTable, "no lookup table, needs SVGElementFactory::Init");
78 RefPtr<mozilla::dom::NodeInfo> ni = aNodeInfo;
79 nsAtom* name = ni->NameAtom();
81 NS_ASSERTION(
82 ni->NamespaceEquals(kNameSpaceID_SVG),
83 "Trying to create SVG elements that aren't in the SVG namespace");
85 SVGContentCreatorFunction cb = sTagAtomTable->Get(name);
86 if (cb) {
87 nsCOMPtr<nsIContent> content;
88 nsresult rv = cb(getter_AddRefs(content), ni.forget(), aFromParser);
89 *aResult = content.forget().take()->AsElement();
90 return rv;
93 // if we don't know what to create, just create a standard svg element:
94 return NS_NewSVGElement(aResult, ni.forget());
97 nsresult NS_NewSVGUnknownElement(
98 nsIContent** aResult, already_AddRefed<mozilla::dom::NodeInfo>&& aNodeInfo,
99 FromParser aFromParser) {
100 RefPtr<mozilla::dom::NodeInfo> ni = aNodeInfo;
101 nsCOMPtr<Element> element;
102 nsresult rv = NS_NewSVGElement(getter_AddRefs(element), ni.forget());
103 element.forget(aResult);
104 return rv;