cb41611415b5ecd0af118fa6662ace6086e03d66
1 #ifdef HAVE_CONFIG_H
2 # include "config.h"
3 #endif
5 #include <map>
6 #include <cstring>
7 #include <string>
8 #include <glib.h> // g_assert()
10 #include "xml/node-iterators.h"
11 #include "algorithms/find-if-before.h"
13 namespace Inkscape {
14 namespace XML {
16 /* the id_permitted stuff is a temporary-ish hack */
18 namespace {
20 bool id_permitted_internal(GQuark qname) {
21 char const *qname_s=g_quark_to_string(qname);
22 return !strncmp("svg:", qname_s, 4) || !strncmp("sodipodi:", qname_s, 9) ||
23 !strncmp("inkscape:", qname_s, 9);
24 }
27 bool id_permitted_internal_memoized(GQuark qname) {
28 typedef std::map<GQuark, bool> IdPermittedMap;
29 IdPermittedMap id_permitted_names;
31 IdPermittedMap::iterator found;
32 found = id_permitted_names.find(qname);
33 if ( found != id_permitted_names.end() ) {
34 return found->second;
35 } else {
36 bool permitted=id_permitted_internal(qname);
37 id_permitted_names[qname] = permitted;
38 return permitted;
39 }
40 }
42 }
44 bool id_permitted(Node const *node) {
45 g_return_val_if_fail(node != NULL, false);
47 if ( node->type() != ELEMENT_NODE ) {
48 return false;
49 }
51 return id_permitted_internal_memoized((GQuark)node->code());
52 }
54 struct node_matches {
55 node_matches(Node const &n) : node(n) {}
56 bool operator()(Node const &other) { return &other == &node; }
57 Node const &node;
58 };
60 /** Returns the sibling before \a node in \a node's parent's children,
61 * or NULL if \a node is the first of those children (or if child is
62 * NULL or has no parent).
63 *
64 * Useful in combination with Node::addChild, when you want to insert
65 * a new child _before_ a given existing child.
66 *
67 * Note: Involves a linear search (unlike next_node).
68 *
69 * \pre Links are correct, i.e. \a node isin its parent's children.
70 *
71 * \post (ret == NULL
72 * ? node == NULL || node->parent() == NULL || node->parent()->firstChild() == node
73 * : ret->next() == node).
74 */
75 Node *previous_node(Node *node) {
76 using Inkscape::Algorithms::find_if_before;
78 if ( !node || !node->parent() ) {
79 return NULL;
80 }
82 Node *previous=find_if_before<NodeSiblingIterator>(
83 node->parent()->firstChild(), NULL, node_matches(*node)
84 );
86 g_assert(previous == NULL
87 ? node->parent()->firstChild() == node
88 : previous->next() == node);
90 return previous;
91 }
93 }
94 }
97 /*
98 Local Variables:
99 mode:c++
100 c-file-style:"stroustrup"
101 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
102 indent-tabs-mode:nil
103 fill-column:99
104 End:
105 */
106 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :