summaryrefslogtreecommitdiffstats
path: root/src/attribute-rel-css.cpp
blob: f476d5083012b9dbe60722745b0686ab9c79ee5a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
// SPDX-License-Identifier: GPL-2.0-or-later
/** @file
 * TODO: insert short description here
 *//*
 * Authors: see git history
 *
 * Copyright (C) 2018 Authors
 * Released under GNU GPL v2+, read the file 'COPYING' for more information.
 */
/*
 * attribute-rel-css.cpp
 *
 *  Created on: Jul 25, 2011
 *      Author: abhishek
 */

/** \class SPAttributeRelCSS
 *
 * SPAttributeRelCSS class stores the mapping of element->style_properties
 * relationship and provides a static function to access that
 * mapping indirectly(only reading).
 */

#include <fstream>
#include <sstream>
#include <iostream>

#include "attribute-rel-css.h"

#include "io/resource.h"
#include "path-prefix.h"
#include "preferences.h"

SPAttributeRelCSS * SPAttributeRelCSS::instance = nullptr;
bool SPAttributeRelCSS::foundFileProp = false;
bool SPAttributeRelCSS::foundFileDefault = false;

/*
 * This function checks whether an element -> CSS property pair 
 * is allowed or not
 */
bool SPAttributeRelCSS::findIfValid(Glib::ustring property, Glib::ustring element)
{
    if (SPAttributeRelCSS::instance == nullptr) {
        SPAttributeRelCSS::instance = new SPAttributeRelCSS();
    }
    
    // Always valid if data file not found!
    if( !foundFileProp ) return true;

    // Strip of "svg:" from the element's name
    Glib::ustring temp = element;
    if ( temp.find("svg:") != std::string::npos ) {
        temp.erase( temp.find("svg:"), 4 );
    }

    // Don't check for properties with -, role, aria etc. to allow for more accessibility
    // FixMe: Name space list should be created when file read in.
    // clang-format off
    if (property[0] == '-'
        || property.substr(0,4) == "role"
        || property.substr(0,4) == "aria"
        || property.substr(0,5) == "xmlns"
        || property.substr(0,9) == "inkscape:"
        || property.substr(0,9) == "sodipodi:"
        || property.substr(0,4) == "rdf:"
        || property.substr(0,3) == "cc:"
        || property.substr(0,4) == "ns1:"  // JessyInk
        || (SPAttributeRelCSS::instance->propertiesOfElements[temp].find(property)
            != SPAttributeRelCSS::instance->propertiesOfElements[temp].end()) ) {
    // clang-format on
        return true;
    } else {
        //g_warning( "Invalid attribute: %s used on <%s>", property.c_str(), element.c_str() );
        return false;
    }
}

/*
 * This function checks whether an CSS property -> default value 
 * pair is allowed or not
 */
bool SPAttributeRelCSS::findIfDefault(Glib::ustring property, Glib::ustring value)
{
    if (SPAttributeRelCSS::instance == nullptr) {
        SPAttributeRelCSS::instance = new SPAttributeRelCSS();
    }

    // Always false if data file not found!
    if( !foundFileDefault ) return false;

    if( instance->defaultValuesOfProps[property] == value) {
        return true;
    } else {
        return false;
    }
}

/*
 * Check if property can be inherited.
 */
bool SPAttributeRelCSS::findIfInherit(Glib::ustring property)
{
    if (SPAttributeRelCSS::instance == nullptr) {
        SPAttributeRelCSS::instance = new SPAttributeRelCSS();
    }

    // Always false if data file not found!
    if( !foundFileDefault ) return false;

    return instance->inheritProps[property];
}

/*
 * Check if attribute is a property.
 */
bool SPAttributeRelCSS::findIfProperty(Glib::ustring property)
{
    if (SPAttributeRelCSS::instance == nullptr) {
        SPAttributeRelCSS::instance = new SPAttributeRelCSS();
    }

    // Always true if data file not found!
    if( !foundFileProp ) return true;

    return ( instance->defaultValuesOfProps.find( property )
             != instance->defaultValuesOfProps.end() );
}

SPAttributeRelCSS::SPAttributeRelCSS()
{
    // Read data from standard path

    using namespace Inkscape::IO::Resource;
    auto filepath = get_path_string(SYSTEM, ATTRIBUTES, "cssprops");

    // Try and load data from filepath
    if (readDataFromFileIn(filepath, SPAttributeRelCSS::prop_element_pair)) {
        foundFileProp = true;
    }
    
    // Read data from standard path
    filepath = get_path_string(SYSTEM, ATTRIBUTES, "css_defaults");
    
    // Try and load data from filepath
    if (readDataFromFileIn(filepath, SPAttributeRelCSS::prop_defValue_pair)) {
        foundFileDefault = true;
    }

}

bool SPAttributeRelCSS::readDataFromFileIn(Glib::ustring fileName, storageType type)
{
    std::fstream file;
    file.open(fileName.c_str(), std::ios::in);
    
    if (!file.is_open()) {
        // Display warning for file not open
        g_warning("Could not open the data file for CSS attribute-element map construction: %s", fileName.c_str());
        file.close();
        return false;
    }

    while (!file.eof()) {
        std::stringstream ss;
        std::string s;

        std::getline(file,s,'"');
        std::getline(file,s,'"');
        if (s.size() > 0 && s[0] != '\n') {
            std::string prop = s;
            getline(file,s);
            ss << s;
            
            // Load data to structure that holds element -> set of CSS props
            if (type == SPAttributeRelCSS::prop_element_pair) {
                while (std::getline(ss,s,'"')) {
                    std::string element;
                    std::getline(ss,s,'"');
                    element = s;                               
                    propertiesOfElements[element].insert(prop);
                }
            // Load data to structure that holds CSS prop -> default value    
            } else if (type == SPAttributeRelCSS::prop_defValue_pair) {
                std::string value;
                std::getline(ss,s,'"');
                std::getline(ss,s,'"');
                value = s;
                defaultValuesOfProps[prop] = value;
                std::getline(ss,s,'"');
                std::getline(ss,s,'"');
                gboolean inherit = false;
                if ( s.find( "yes" ) != std::string::npos ) {
                    inherit = true;
                }
                inheritProps[prop] = inherit;
            }
        }
    }
    
    file.close();
    return true;
}

/*
  Local Variables:
  mode:c++
  c-file-style:"stroustrup"
  c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
  indent-tabs-mode:nil
  fill-column:99
  End:
*/
// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :