summaryrefslogtreecommitdiffstats
path: root/src/xml/href-attribute-helper.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/xml/href-attribute-helper.cpp')
-rw-r--r--src/xml/href-attribute-helper.cpp46
1 files changed, 46 insertions, 0 deletions
diff --git a/src/xml/href-attribute-helper.cpp b/src/xml/href-attribute-helper.cpp
new file mode 100644
index 0000000..55f2b93
--- /dev/null
+++ b/src/xml/href-attribute-helper.cpp
@@ -0,0 +1,46 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Authors:
+ * Thomas Holder
+ *
+ * Copyright (C) 2022 Authors
+ *
+ * Released under GNU GPL v2+, read the file 'COPYING' for more information.
+ */
+
+#include "href-attribute-helper.h"
+
+namespace Inkscape {
+
+/**
+ * Get the 'href' or 'xlink:href' (fallback) attribute from an XML node.
+ *
+ * @return The observed key and its value
+ */
+std::pair<char const *, char const *> getHrefAttribute(XML::Node const &node)
+{
+ if (auto value = node.attribute("href")) {
+ return {"href", value};
+ }
+
+ if (auto value = node.attribute("xlink:href")) {
+ return {"xlink:href", value};
+ }
+
+ return {"xlink:href", nullptr};
+}
+
+/**
+ * If the 'href' attribute already exists for the given node, then set a new
+ * value for it. Otherwise set the value for 'xlink:href'.
+ */
+void setHrefAttribute(XML::Node &node, Util::const_char_ptr value)
+{
+ if (node.attribute("href")) {
+ node.setAttribute("href", value);
+ } else {
+ node.setAttribute("xlink:href", value);
+ }
+}
+
+} // namespace Inkscape