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: */
|
2013-02-09 00:24:47 +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/. */
|
|
|
|
|
|
|
|
#include "SVGElementFactory.h"
|
|
|
|
#include "nsGkAtoms.h"
|
2013-03-12 02:05:58 +04:00
|
|
|
#include "nsIContent.h"
|
2014-06-20 06:01:40 +04:00
|
|
|
#include "mozilla/dom/NodeInfo.h"
|
2013-12-03 19:10:39 +04:00
|
|
|
#include "mozilla/dom/Element.h"
|
2013-03-12 02:05:58 +04:00
|
|
|
#include "mozilla/dom/FromParser.h"
|
2018-03-02 03:49:32 +03:00
|
|
|
#include "mozilla/StaticPtr.h"
|
|
|
|
#include "nsDataHashtable.h"
|
|
|
|
#include "nsHashKeys.h"
|
2013-02-09 00:24:47 +04:00
|
|
|
|
|
|
|
using namespace mozilla;
|
|
|
|
using namespace mozilla::dom;
|
|
|
|
|
2018-03-02 04:07:16 +03:00
|
|
|
// Hash table that maps nsAtom* SVG tags to a SVGContentCreatorFunction.
|
|
|
|
using TagAtomTable =
|
|
|
|
nsDataHashtable<nsPtrHashKey<nsAtom>, SVGContentCreatorFunction>;
|
2018-03-02 03:49:32 +03:00
|
|
|
StaticAutoPtr<TagAtomTable> sTagAtomTable;
|
2013-02-09 00:24:47 +04:00
|
|
|
|
2017-07-04 11:00:03 +03:00
|
|
|
#define SVG_TAG(_tag, _classname) \
|
|
|
|
nsresult NS_NewSVG##_classname##Element( \
|
|
|
|
nsIContent** aResult, \
|
|
|
|
already_AddRefed<mozilla::dom::NodeInfo>&& aNodeInfo); \
|
|
|
|
\
|
|
|
|
nsresult NS_NewSVG##_classname##Element( \
|
|
|
|
nsIContent** aResult, \
|
|
|
|
already_AddRefed<mozilla::dom::NodeInfo>&& aNodeInfo, \
|
|
|
|
FromParser aFromParser) { \
|
2018-05-30 22:15:35 +03:00
|
|
|
return NS_NewSVG##_classname##Element(aResult, std::move(aNodeInfo)); \
|
2017-07-04 11:00:03 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
#define SVG_FROM_PARSER_TAG(_tag, _classname)
|
2013-02-09 00:24:47 +04:00
|
|
|
|
|
|
|
#include "SVGTagList.h"
|
|
|
|
#undef SVG_TAG
|
|
|
|
#undef SVG_FROM_PARSER_TAG
|
|
|
|
|
2013-12-03 18:40:11 +04:00
|
|
|
nsresult NS_NewSVGElement(Element** aResult,
|
2014-06-20 06:01:40 +04:00
|
|
|
already_AddRefed<mozilla::dom::NodeInfo>&& aNodeInfo);
|
2013-02-09 00:24:47 +04:00
|
|
|
|
|
|
|
enum SVGTag {
|
|
|
|
#define SVG_TAG(_tag, _classname) eSVGTag_##_tag,
|
|
|
|
#define SVG_FROM_PARSER_TAG(_tag, _classname) eSVGTag_##_tag,
|
|
|
|
#include "SVGTagList.h"
|
|
|
|
#undef SVG_TAG
|
|
|
|
#undef SVG_FROM_PARSER_TAG
|
|
|
|
eSVGTag_Count
|
|
|
|
};
|
|
|
|
|
|
|
|
void SVGElementFactory::Init() {
|
2018-03-02 03:49:32 +03:00
|
|
|
sTagAtomTable = new TagAtomTable(64);
|
2013-02-09 00:24:47 +04:00
|
|
|
|
|
|
|
#define SVG_TAG(_tag, _classname) \
|
2018-03-02 04:07:16 +03:00
|
|
|
sTagAtomTable->Put(nsGkAtoms::_tag, NS_NewSVG##_classname##Element);
|
2013-02-09 00:24:47 +04:00
|
|
|
#define SVG_FROM_PARSER_TAG(_tag, _classname) \
|
2018-03-02 04:07:16 +03:00
|
|
|
sTagAtomTable->Put(nsGkAtoms::_tag, NS_NewSVG##_classname##Element);
|
2013-02-09 00:24:47 +04:00
|
|
|
#include "SVGTagList.h"
|
|
|
|
#undef SVG_TAG
|
|
|
|
#undef SVG_FROM_PARSER_TAG
|
|
|
|
}
|
|
|
|
|
|
|
|
void SVGElementFactory::Shutdown() { sTagAtomTable = nullptr; }
|
|
|
|
|
2014-06-20 06:01:40 +04:00
|
|
|
nsresult NS_NewSVGElement(Element** aResult,
|
|
|
|
already_AddRefed<mozilla::dom::NodeInfo>&& aNodeInfo,
|
2013-02-09 00:24:47 +04:00
|
|
|
FromParser aFromParser) {
|
|
|
|
NS_ASSERTION(sTagAtomTable, "no lookup table, needs SVGElementFactory::Init");
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<mozilla::dom::NodeInfo> ni = aNodeInfo;
|
2017-10-03 01:05:19 +03:00
|
|
|
nsAtom* name = ni->NameAtom();
|
2013-02-09 00:24:47 +04:00
|
|
|
|
2014-03-15 23:00:15 +04:00
|
|
|
NS_ASSERTION(
|
|
|
|
ni->NamespaceEquals(kNameSpaceID_SVG),
|
2013-02-09 00:24:47 +04:00
|
|
|
"Trying to create SVG elements that aren't in the SVG namespace");
|
|
|
|
|
2018-03-02 04:07:16 +03:00
|
|
|
SVGContentCreatorFunction cb = sTagAtomTable->Get(name);
|
|
|
|
if (cb) {
|
2013-12-03 18:40:11 +04:00
|
|
|
nsCOMPtr<nsIContent> content;
|
2014-03-15 23:00:15 +04:00
|
|
|
nsresult rv = cb(getter_AddRefs(content), ni.forget(), aFromParser);
|
|
|
|
*aResult = content.forget().take()->AsElement();
|
2013-12-03 18:40:11 +04:00
|
|
|
return rv;
|
2013-02-09 00:24:47 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// if we don't know what to create, just create a standard svg element:
|
2014-03-15 23:00:15 +04:00
|
|
|
return NS_NewSVGElement(aResult, ni.forget());
|
2013-02-09 00:24:47 +04:00
|
|
|
}
|
2017-07-04 11:00:03 +03:00
|
|
|
|
|
|
|
nsresult NS_NewSVGUnknownElement(
|
|
|
|
nsIContent** aResult, already_AddRefed<mozilla::dom::NodeInfo>&& aNodeInfo,
|
|
|
|
FromParser aFromParser) {
|
|
|
|
RefPtr<mozilla::dom::NodeInfo> ni = aNodeInfo;
|
|
|
|
nsCOMPtr<Element> element;
|
|
|
|
nsresult rv = NS_NewSVGElement(getter_AddRefs(element), ni.forget());
|
|
|
|
element.forget(aResult);
|
|
|
|
return rv;
|
|
|
|
}
|