gui
[lbook_fbreader.git] / fbreader / src / formats / html / StyleSheetParser.cpp
blob9b8c2a3b7657250ed647e5c79e545397cf967c98
1 /*
2 * Copyright (C) 2004-2008 Geometer Plus <contact@geometerplus.com>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17 * 02110-1301, USA.
20 #include <cctype>
22 #include <ZLStringUtil.h>
24 #include "StyleSheetParser.h"
26 StyleSheetParser::StyleSheetParser(StyleSheetTable &table) : myTable(table), myReadState(TAG_NAME), myInsideComment(false) {
29 void StyleSheetParser::reset() {
30 myWord.erase();
31 myAttributeName.erase();
32 myReadState = TAG_NAME;
33 myInsideComment = false;
34 myElement = StyleSheetTable::Element();
37 void StyleSheetParser::parse(const char *text, int len) {
38 const char *start = text;
39 const char *end = text + len;
40 for (const char *ptr = start; ptr != end; ++ptr) {
41 if (isspace(*ptr)) {
42 if (start != ptr) {
43 myWord.append(start, ptr - start);
45 processWord(myWord);
46 myWord.erase();
47 start = ptr + 1;
48 } else if (isControlSymbol(*ptr)) {
49 if (start != ptr) {
50 myWord.append(start, ptr - start);
52 processWord(myWord);
53 myWord.erase();
54 processControl(*ptr);
55 start = ptr + 1;
60 bool StyleSheetParser::isControlSymbol(const char symbol) {
61 switch (symbol) {
62 case '{':
63 case '}':
64 case ';':
65 case ':':
66 return true;
67 default:
68 return false;
72 void StyleSheetParser::processControl(const char control) {
73 switch (control) {
74 case '{':
75 myReadState = (myReadState == TAG_NAME) ? ATTRIBUTE_NAME : BROKEN;
76 break;
77 case '}':
78 if (myReadState != BROKEN) {
79 myTable.addElement(myElement);
81 myReadState = TAG_NAME;
82 myElement = StyleSheetTable::Element();
83 break;
84 case ';':
85 myReadState =
86 ((myReadState == ATTRIBUTE_VALUE) ||
87 (myReadState == ATTRIBUTE_NAME)) ? ATTRIBUTE_NAME : BROKEN;
88 break;
89 case ':':
90 myReadState = (myReadState == ATTRIBUTE_NAME) ? ATTRIBUTE_VALUE : BROKEN;
91 break;
95 void StyleSheetParser::processWord(std::string &word) {
96 while (!word.empty()) {
97 int index = word.find(myInsideComment ? "*/" : "/*");
98 if (!myInsideComment) {
99 if (index == -1) {
100 processWordWithoutComments(word);
101 } else if (index > 0) {
102 processWordWithoutComments(word.substr(0, index));
105 if (index == -1) {
106 break;
108 myInsideComment = !myInsideComment;
109 word.erase(0, index + 2);
113 void StyleSheetParser::processWordWithoutComments(const std::string &word) {
114 switch (myReadState) {
115 case TAG_NAME:
117 int index = word.find('.');
118 if (index == -1) {
119 myElement.first.TagName = word;
120 } else {
121 myElement.first.TagName = word.substr(0, index);
122 myElement.first.ClassName = word.substr(index + 1);
124 break;
126 case ATTRIBUTE_NAME:
127 myAttributeName = word;
128 myElement.second[myAttributeName].clear();
129 break;
130 case ATTRIBUTE_VALUE:
131 myElement.second[myAttributeName].push_back(word);
132 break;
133 case BROKEN:
134 break;