Inkscape
Vector Graphics Editor
Loading...
Searching...
No Matches
attribute-rel-svg.cpp
Go to the documentation of this file.
1// SPDX-License-Identifier: GPL-2.0-or-later
/*
5 * Authors: see git history
6 *
7 * Copyright (C) 2018 Authors
8 * Released under GNU GPL v2+, read the file 'COPYING' for more information.
9 */
10/*
11 * attribute-rel-svg.cpp
12 *
13 * Created on: Jul 25, 2011
14 * Author: abhishek
15 */
16
24#include "attribute-rel-svg.h"
25
26#include <fstream>
27#include <sstream>
28
29#include "io/resource.h"
30#include "path-prefix.h"
31#include "preferences.h"
32
34
35/*
36 * This function returns true if element is an SVG element.
37 */
38bool SPAttributeRelSVG::isSVGElement(Glib::ustring const &element)
39{
40 auto &instance = SPAttributeRelSVG::getInstance();
41
42 // Always valid if data file not found!
43 if (!foundFile) {
44 return true;
45 }
46
47 // Strip off "svg:" from the element's name
48 Glib::ustring temp = element;
49 if (temp.find("svg:") != std::string::npos) {
50 temp.erase(temp.find("svg:"), 4);
51 }
52
53 return (instance.attributesOfElements.find(temp)
54 != instance.attributesOfElements.end());
55}
56
57/*
58 * This functions checks whether an element -> attribute pair is allowed or not
59 */
60bool SPAttributeRelSVG::findIfValid(Glib::ustring const &attribute, Glib::ustring const &element)
61{
62 auto &instance = SPAttributeRelSVG::getInstance();
63
64 // Always valid if data file not found!
65 if (!foundFile) {
66 return true;
67 }
68
69 // Strip off "svg:" from the element's name
70 Glib::ustring temp = element;
71 if (temp.find("svg:") != std::string::npos) {
72 temp.erase(temp.find("svg:"), 4);
73 }
74
75 // Check for attributes with -, role, aria etc. to allow for more accessibility
76 // clang-format off
77 if (attribute[0] == '-'
78 || attribute.substr(0,4) == "role"
79 || attribute.substr(0,4) == "aria"
80 || attribute.substr(0,5) == "xmlns"
81 || attribute.substr(0,9) == "inkscape:"
82 || attribute.substr(0,9) == "sodipodi:"
83 || attribute.substr(0,4) == "rdf:"
84 || attribute.substr(0,3) == "cc:"
85 || attribute.substr(0,4) == "ns1:" // JessyInk
86 || (instance.attributesOfElements[temp].find(attribute)
87 != instance.attributesOfElements[temp].end())) {
88 // clang-format on
89 return true;
90 } else {
91 // g_warning( "Invalid attribute: %s used on <%s>", attribute.c_str(), element.c_str() );
92 return false;
93 }
94}
95
96/*
97 * One timer singleton constructor, to load the element -> attributes data
98 * into memory.
99 */
101{
102 std::fstream f;
103
104 // Read data from standard path
105 using namespace Inkscape::IO::Resource;
106 auto filepath = get_path_string(SYSTEM, ATTRIBUTES, "svgprops");
107
108 f.open(filepath, std::ios::in);
109
110 if (!f.is_open()) {
111 // Display warning for file not open
112 g_warning("Could not open the data file for XML attribute-element map construction: %s", filepath.c_str());
113 f.close();
114 return;
115 }
116
117 foundFile = true;
118
119 while (!f.eof()) {
120 std::stringstream ss;
121 std::string s;
122
123 std::getline(f, s, '"');
124 std::getline(f, s, '"');
125 if (s.size() > 0 && s[0] != '\n') {
126 std::string prop = s;
127 getline(f, s);
128 ss << s;
129
130 while (std::getline(ss, s, '"')) {
131 std::string element;
132 std::getline(ss, s, '"');
133 element = s;
134 attributesOfElements[element].insert(prop);
135 }
136 }
137 }
138
139 f.close();
140}
141
143{
144 static SPAttributeRelSVG theInstance;
145 return theInstance;
146}
147
148/*
149 Local Variables:
150 mode:c++
151 c-file-style:"stroustrup"
152 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
153 indent-tabs-mode:nil
154 fill-column:99
155 End:
156*/
157// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :
TODO: insert short description here.
SPAttributeRelSVG class stores the mapping of element->attribute relationship and provides a static f...
static SPAttributeRelSVG & getInstance()
static bool isSVGElement(Glib::ustring const &element)
static bool findIfValid(Glib::ustring const &attribute, Glib::ustring const &element)
TODO: insert short description here.
Singleton class to access the preferences file in a convenient way.
Inkscape::IO::Resource - simple resource API.