-
Notifications
You must be signed in to change notification settings - Fork 28
/
ClassInfo.cpp
184 lines (168 loc) · 7.12 KB
/
ClassInfo.cpp
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
/************************************************************************
**
** Copyright (C) 2023-2024 Kevin B. Hendricks, Stratford, ON Canada
**
** This file is part of PageEdit.
**
** PageEdit is free software: you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation, either version 3 of the License, or
** (at your option) any later version.
**
** PageEdit is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with PageEdit. If not, see <http://www.gnu.org/licenses/>.
**
*************************************************************************/
#include <QRegularExpression>
#include <QDebug>
#include "Utility.h"
#include "ClassInfo.h"
const int TAB_SPACES_WIDTH = 4;
static const QString DELIMITERS = "}{;";
// Note: CSSProperties and CSSSelectors are simple struct that this code
// created with new and so need to be manually cleaned up to prevent
// large memory leaks
ClassInfo::ClassInfo(const QString &text)
: m_OriginalText(text)
{
parseCSSSelectors(text, 0, 0);
}
// Need to manually clean up the Selector List
ClassInfo::~ClassInfo()
{
foreach(CSSSelector * sp, m_CSSSelectors) {
if (sp) delete sp;
}
}
QList<ClassInfo::CSSSelector *> ClassInfo::getClassSelectors()
{
QList<ClassInfo::CSSSelector *> selectors;
foreach(ClassInfo::CSSSelector * cssSelector, m_CSSSelectors) {
if (cssSelector->classNames.count() > 0) {
selectors.append(cssSelector);
}
}
return selectors;
}
void ClassInfo::parseCSSSelectors(const QString &text, const int &offsetLines, const int &offsetPos)
{
QRegularExpression strip_attributes_regex("\\[[^\\]]*\\]");
QRegularExpression strip_ids_regex("#[^\\s\\.]+");
// QRegularExpression strip_non_name_chars_regex("[^A-Za-z0-9_\\-\\.:]+");
QRegularExpression strip_non_name_chars_regex("[^\\w_\\-\\.:]+", QRegularExpression::UseUnicodePropertiesOption);
QString search_text = replaceBlockComments(text);
// CSS selectors can be in a myriad of formats... the class based selectors could be:
// .c1 / e1.c1 / e1.c1.c2 / e1[class~=c1] / e1#id1.c1 / e1.c1#id1 / .c1, .c2 / ...
// Then the element based selectors could be:
// e1 / e1 > e2 / e1 e2 / e1 + e2 / e1[attribs...] / e1#id1 / e1, e2 / ...
// Really needs a parser to do this properly, this will only handle the 90% scenarios.
// Note: selector groups can be sepaparated by line feeds so you can not stop
// at the beginning of line when searching for the start of a selector
int pos = 0;
int open_brace_pos = -1;
int close_brace_pos = -1;
while (true) {
open_brace_pos = search_text.indexOf(QChar('{'), pos);
if (open_brace_pos < 0) {
break;
}
// Now search backwards until we get a line (or more) containing text .
bool have_text = false;
pos = open_brace_pos - 1;
while ((pos >= 0) && (!DELIMITERS.contains(search_text.at(pos)) || !have_text)) {
if (search_text.at(pos).isLetter()) {
have_text = true;
}
pos--;
}
pos++;
if (!have_text) {
// Really badly formed CSS document - try to skip ahead
pos = open_brace_pos + 1;
continue;
}
close_brace_pos = search_text.indexOf(QChar('}'), open_brace_pos + 1);
if (close_brace_pos < 0) {
// Another badly formed scenario - no point in looking further
break;
}
// Skip past leading whitespace/newline.
while (search_text.at(pos).isSpace()) {
pos++;
}
int line = search_text.left(pos + 1).count(QChar('\n')) + 1;
QString selector_text = search_text.mid(pos, open_brace_pos - pos).trimmed();
// Handle case of a selector group containing multiple declarations
QStringList matches = selector_text.split(QChar(','), Qt::SkipEmptyParts);
foreach(QString match, matches) {
CSSSelector *selector = new CSSSelector();
selector->text = selector_text;
selector->groupText = match.trimmed();
selector->position = pos + offsetPos;
selector->line = line + offsetLines;
selector->isGroup = matches.length() > 1;
selector->openingBracePos = open_brace_pos + offsetPos;
selector->closingBracePos = close_brace_pos + offsetPos;
// Need to parse our selector text to determine what sort of selector it contains.
// First strip out any attributes and then identifiers
match.replace(strip_attributes_regex, "");
match.replace(strip_ids_regex, "");
// Also replace any other characters like > or + not of interest
match.replace(strip_non_name_chars_regex, QChar(' '));
// Now break it down into the element components
QStringList elements = match.trimmed().split(QChar(' '), Qt::SkipEmptyParts);
foreach(QString element, elements) {
if (element.contains(QChar('.'))) {
QStringList parts = element.split('.');
if (!parts.at(0).isEmpty()) {
selector->elementNames.append(parts.at(0));
}
for (int i = 1; i < parts.length(); i++) {
selector->classNames.append(parts.at(i));
}
} else {
selector->elementNames.append(element);
}
}
m_CSSSelectors.append(selector);
}
pos = open_brace_pos + 1;
}
}
QString ClassInfo::replaceBlockComments(const QString &text)
{
// We take a copy of the text and remove all block comments from it.
// However we must be careful to replace with spaces/keep line feeds
// so that do not corrupt the position information used by the parser.
QString new_text(text);
QRegularExpression comment_search("/\\*.*\\*/", QRegularExpression::InvertedGreedinessOption|QRegularExpression::DotMatchesEverythingOption);
int start = 0;
int comment_index;
while (true) {
int comment_len = 0;
comment_index = -1;
QRegularExpressionMatch match = comment_search.match(new_text, start);
if (match.hasMatch()) {
comment_index = match.capturedStart();
comment_len = match.capturedLength();
}
if (comment_index < 0) {
break;
}
QString match_text = new_text.mid(comment_index, comment_len);
match_text.replace(QRegularExpression("[^\r\n]"), QChar(' '));
new_text.remove(comment_index, match_text.length());
new_text.insert(comment_index, match_text);
// Prepare for the next comment.
start = comment_index + comment_len;
if (start >= new_text.length() - 2) {
break;
}
}
return new_text;
}