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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
|
/***************************************************************************
stylesheetparser.cpp - description
-------------------
begin : gio ago 19 2004
copyright : (C) 2004 by gulmini luciano
email : gulmini.luciano@student.unife.it
***************************************************************************/
/***************************************************************************
* *
* This program 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 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#include "stylesheetparser.h"
#include <klocale.h>
#include <kdebug.h>
static const TQString msg1(i18n("has not been closed")),
msg2(i18n("needs an opening parenthesis "));
stylesheetParser::stylesheetParser(const TQString& s){
m_styleSheet = s;
m_styleSheet=m_styleSheet.right(m_styleSheet.length()-whiteSpaces(0));
m_stopProcessing = false;
m_orderNumber = 0;
}
int stylesheetParser::whiteSpaces(int d){
int ws=0;
for( unsigned int i=d;i<m_styleSheet.length();i++){
TQString temp;
if(m_styleSheet[i] == ' ' || m_styleSheet[i] == '\n' || m_styleSheet[i] == '\t' )
ws++;
else break;
}
return ws;
}
void stylesheetParser::parse(){
if(!m_stopProcessing){
if(!m_styleSheet.isEmpty()){
if(m_styleSheet.startsWith("/*"))
parseComment();
else if(m_styleSheet.startsWith("@page"))
parseAtRules1();
else if(m_styleSheet.startsWith("@media"))
parseAtRules1();
else if(m_styleSheet.startsWith("@import"))
parseAtRules2();
else if(m_styleSheet.startsWith("@charset"))
parseAtRules2();
else parseSelector();
}
else return;
}
}
void stylesheetParser::parseComment(){
bool stopProcessingComment=false;
unsigned int k;
for(k=2;k<m_styleSheet.length()-1;k++){
TQString temp;
temp.append(m_styleSheet[k]).append(m_styleSheet[k+1]);
if(temp=="*/") {
k+=2;
stopProcessingComment=true;
break;
}
}
if(stopProcessingComment){
int ws=whiteSpaces(k);
TQPair<TQString,unsigned int> tmp(m_styleSheet.left(k+ws),++m_orderNumber);
m_stylesheetStructure["/*"+TQString::number(m_orderNumber,10)]=tmp;
m_styleSheet=m_styleSheet.right(m_styleSheet.length()-k-ws);
parse();
}
else {
m_stopProcessing = true;
emit errorOccurred(i18n("The comment") + " :\n" +m_styleSheet.mid(0,20) + "...\n "+ msg1);
return;
}
}
unsigned int stylesheetParser::numberOfParenthesisInAParenthesisBlock(parenthesisKind p, const TQString& b){
TQChar searchFor = '{';
if (p == closed)
searchFor = '}';
int num = 0;
int len = b.length();
bool ignore = false;
for (int i = 0; i < len; i++)
{
if (b[i] == '/' && (i + 1 < len) && b[i + 1] == '*')
ignore = true;
if (b[i] == '*' && (i + 1 < len) && b[i + 1] == '/')
ignore = false;
if (!ignore && b[i] == searchFor)
num++;
}
return num;
}
int findParanthesis(const TQString& string, const TQChar &ch, int startPos = 0)
{
int pos = -1;
int len = string.length();
bool ignore = false;
for (int i = startPos; i < len; i++)
{
if (string[i] == '/' && (i + 1 < len) && string[i + 1] == '*')
ignore = true;
if (string[i] == '*' && (i + 1 < len) && string[i + 1] == '/')
ignore = false;
if (!ignore && string[i] == ch)
{
pos = i;
break;
}
}
return pos;
}
void stylesheetParser::parseSelector(){
int closingParenthesisPos = findParanthesis(m_styleSheet, '}');
if(closingParenthesisPos==-1) {
m_stopProcessing = true;
emit errorOccurred(i18n("The selector") + " :\n" +m_styleSheet.mid(0,20) + "...\n "+ msg1);
return;
}
TQString temp(m_styleSheet.left(closingParenthesisPos+1));
if(numberOfParenthesisInAParenthesisBlock(closed,temp) < numberOfParenthesisInAParenthesisBlock(opened,temp)){
m_stopProcessing = true;
emit errorOccurred(i18n("The selector") + " :\n" +m_styleSheet.mid(0,20) + "...\n "+ msg1);
return;
}
if(numberOfParenthesisInAParenthesisBlock(closed,temp) > numberOfParenthesisInAParenthesisBlock(opened,temp)){
m_stopProcessing = true;
emit errorOccurred(i18n("The selector") + " :\n" +m_styleSheet.mid(0,20) + "...\n "+ msg2);
return;
}
int closingParentheses = 1,
openingParentheses = 0;
while(true){
openingParentheses = numberOfParenthesisInAParenthesisBlock(closed,m_styleSheet.left(closingParenthesisPos+1)); //m_styleSheet.left(closingParenthesisPos+1).contains("{");
if(openingParentheses==closingParentheses){
TQString selectorName=m_styleSheet.left(findParanthesis(m_styleSheet, '{')/*m_styleSheet.find("{")*/).stripWhiteSpace(),
selectorValue=m_styleSheet.mid(findParanthesis(m_styleSheet, '{')/*m_styleSheet.find("{")*/+1, closingParenthesisPos - m_styleSheet.find("{") -1);
selectorName.remove("\n").remove("\t");
selectorValue.remove("\n").remove("\t");
TQPair<TQString,unsigned int> tmp(selectorValue,++m_orderNumber);
if (m_stylesheetStructure.contains(selectorName))
{
uint i = 2;
TQString s = selectorName + TQString("-v%1").arg(i);
while (m_stylesheetStructure.contains(s))
{
i++;
s = selectorName + TQString("-v%1").arg(i);
}
selectorName = s;
}
m_stylesheetStructure[selectorName]=tmp;
break;
}
else {
closingParenthesisPos = findParanthesis(m_styleSheet, '{',closingParenthesisPos+1)/*m_styleSheet.find("}",closingParenthesisPos+1)*/;
closingParentheses++;
}
}
int ws=whiteSpaces(closingParenthesisPos+1);
m_styleSheet=m_styleSheet.right(m_styleSheet.length()-closingParenthesisPos-1-ws);
parse();
}
void stylesheetParser::parseAtRules1(){
//TODO this needs to be fixed : in case the at rule is not properly closed the parser hangs
if(m_styleSheet.find("{") == -1) {
m_stopProcessing = true;
emit errorOccurred(m_styleSheet.mid(0,20) + "...\n " + msg2);
return;
}
int closingParenthesisPos = m_styleSheet.find("}"),
closingParentheses = 0;
if(closingParenthesisPos==-1) return;
else closingParentheses = 1;
int openingParentheses=0;
while(true){
openingParentheses = m_styleSheet.left(closingParenthesisPos+1).contains("{");
if(openingParentheses==closingParentheses)
break;
else {
closingParenthesisPos = m_styleSheet.find("}",closingParenthesisPos+1);
if( closingParenthesisPos!= -1 )
closingParentheses++;
else {
m_stopProcessing = true;
emit errorOccurred(m_styleSheet.mid(0,20) + "...\n " + msg1);
return;
}
}
}
int ws=whiteSpaces(closingParenthesisPos+1);
TQPair<TQString,unsigned int> tmp(m_styleSheet.left(closingParenthesisPos+1+ws),++m_orderNumber);
m_stylesheetStructure["@rule"+TQString::number(m_orderNumber,10)]=tmp;
m_styleSheet=m_styleSheet.right(m_styleSheet.length()-closingParenthesisPos-1-ws);
parse();
}
void stylesheetParser::parseAtRules2(){
//TODO this needs to be fixed : in case the at rule is not properly closed the parser hangs
int semicolonPos = m_styleSheet.find(";");
if(semicolonPos==-1) {
m_stopProcessing = true;
emit errorOccurred(m_styleSheet.mid(0,20) + "...\n " + msg1);
return;
}
int ws=whiteSpaces(semicolonPos+1);
TQPair<TQString,unsigned int> tmp(m_styleSheet.left(semicolonPos+1+ws),++m_orderNumber);
m_stylesheetStructure["@rule"+TQString::number(m_orderNumber,10)]=tmp;
m_styleSheet=m_styleSheet.right(m_styleSheet.length()-semicolonPos-1-ws);
parse();
}
#include "stylesheetparser.moc"
|