1
0
Fork 0
mirror of https://github.com/geometer/FBReaderJ.git synced 2025-10-05 19:42:17 +02:00

CSS: comma-separated selectors

This commit is contained in:
Nikolay Pultsin 2012-05-09 00:56:20 +01:00
parent 7661f69aa6
commit b8e8571704
6 changed files with 55 additions and 36 deletions

View file

@ -27,7 +27,7 @@ DONE Chinese/Tamil UTF16 files
DONE encodings list for native plugins
* CSS:
** why do we use CSS stack?
** parse tag lists like 'h1,h2,h3'
DONE parse tag lists like 'h1,h2,h3'
** "metric" style params
** font size
** underline

View file

@ -30,8 +30,20 @@ StyleSheetTableParser::StyleSheetTableParser(StyleSheetTable &table) : myTable(t
//ZLLogger::Instance().registerClass("CSS");
}
void StyleSheetTableParser::storeData(const std::string &tagName, const std::string &className, const StyleSheetTable::AttributeMap &map) {
myTable.addMap(tagName, className, map);
void StyleSheetTableParser::storeData(const std::string &selector, const StyleSheetTable::AttributeMap &map) {
const std::vector<std::string> ids = ZLStringUtil::split(selector, ",");
for (std::vector<std::string>::const_iterator it = ids.begin(); it != ids.end(); ++it) {
std::string id = *it;
ZLStringUtil::stripWhiteSpaces(id);
if (!id.empty()) {
const size_t index = id.find('.');
if (index == std::string::npos) {
myTable.addMap(id, std::string(), map);
} else {
myTable.addMap(id.substr(0, index), id.substr(index + 1), map);
}
}
}
}
shared_ptr<ZLTextStyleEntry> StyleSheetSingleStyleParser::parseString(const char *text) {
@ -42,7 +54,7 @@ shared_ptr<ZLTextStyleEntry> StyleSheetSingleStyleParser::parseString(const char
return control;
}
StyleSheetParser::StyleSheetParser() : myReadState(TAG_NAME), myInsideComment(false) {
StyleSheetParser::StyleSheetParser() : myReadState(SELECTOR), myInsideComment(false) {
}
StyleSheetParser::~StyleSheetParser() {
@ -51,10 +63,9 @@ StyleSheetParser::~StyleSheetParser() {
void StyleSheetParser::reset() {
myWord.erase();
myAttributeName.erase();
myReadState = TAG_NAME;
myReadState = SELECTOR;
myInsideComment = false;
myTagName.erase();
myClassName.erase();
mySelectorString.erase();
myMap.clear();
}
@ -115,21 +126,20 @@ bool StyleSheetParser::isControlSymbol(const char symbol) {
}
}
void StyleSheetParser::storeData(const std::string&, const std::string&, const StyleSheetTable::AttributeMap&) {
void StyleSheetParser::storeData(const std::string&, const StyleSheetTable::AttributeMap&) {
}
void StyleSheetParser::processControl(const char control) {
switch (control) {
case '{':
myReadState = (myReadState == TAG_NAME) ? ATTRIBUTE_NAME : BROKEN;
myReadState = (myReadState == SELECTOR) ? ATTRIBUTE_NAME : BROKEN;
break;
case '}':
if (myReadState != BROKEN) {
storeData(myTagName, myClassName, myMap);
storeData(mySelectorString, myMap);
}
myReadState = TAG_NAME;
myTagName.erase();
myClassName.erase();
myReadState = SELECTOR;
mySelectorString.erase();
myMap.clear();
break;
case ';':
@ -163,23 +173,12 @@ void StyleSheetParser::processWord(std::string &word) {
void StyleSheetParser::processWordWithoutComments(const std::string &word) {
switch (myReadState) {
case TAG_NAME:
case SELECTOR:
{
int index = word.find('.');
if (index == -1) {
if (myTagName.empty()) {
myTagName = word;
} else {
myTagName += ' ' + word;
}
if (mySelectorString.empty()) {
mySelectorString = word;
} else {
if (myTagName.empty()) {
myTagName = word.substr(0, index);
myClassName = word.substr(index + 1);
} else {
myTagName += ' ' + word.substr(0, index);
myClassName += ' ' + word.substr(index + 1);
}
mySelectorString += ' ' + word;
}
myMap.clear();
break;

View file

@ -36,7 +36,7 @@ public:
void parse(const char *text, int len, bool final = false);
protected:
virtual void storeData(const std::string &tagName, const std::string &className, const StyleSheetTable::AttributeMap &map);
virtual void storeData(const std::string &selector, const StyleSheetTable::AttributeMap &map);
private:
bool isControlSymbol(const char symbol);
@ -48,14 +48,13 @@ private:
std::string myWord;
std::string myAttributeName;
enum {
TAG_NAME,
SELECTOR,
ATTRIBUTE_NAME,
ATTRIBUTE_VALUE,
BROKEN,
} myReadState;
bool myInsideComment;
std::string myTagName;
std::string myClassName;
std::string mySelectorString;
StyleSheetTable::AttributeMap myMap;
friend class StyleSheetSingleStyleParser;
@ -67,7 +66,7 @@ public:
StyleSheetTableParser(StyleSheetTable &table);
private:
void storeData(const std::string &tagName, const std::string &className, const StyleSheetTable::AttributeMap &map);
void storeData(const std::string &selector, const StyleSheetTable::AttributeMap &map);
private:
StyleSheetTable &myTable;

View file

@ -54,20 +54,25 @@ void StyleSheetTable::addMap(const std::string &tag, const std::string &aClass,
}
}
static void parseLength(const std::string &toParse, short &size, ZLTextStyleEntry::SizeUnit &unit) {
static bool parseLength(const std::string &toParse, short &size, ZLTextStyleEntry::SizeUnit &unit) {
if (ZLStringUtil::stringEndsWith(toParse, "%")) {
unit = ZLTextStyleEntry::SIZE_UNIT_PERCENT;
size = atoi(toParse.c_str());
return true;
} else if (ZLStringUtil::stringEndsWith(toParse, "em")) {
unit = ZLTextStyleEntry::SIZE_UNIT_EM_100;
size = (short)(100 * ZLStringUtil::stringToDouble(toParse, 0));
return true;
} else if (ZLStringUtil::stringEndsWith(toParse, "ex")) {
unit = ZLTextStyleEntry::SIZE_UNIT_EX_100;
size = (short)(100 * ZLStringUtil::stringToDouble(toParse, 0));
return true;
} else {
unit = ZLTextStyleEntry::SIZE_UNIT_PIXEL;
size = atoi(toParse.c_str());
return true;
}
return false;
}
void StyleSheetTable::setLength(ZLTextStyleEntry &entry, ZLTextStyleEntry::Feature fetureId, const AttributeMap &map, const std::string &attributeName) {
@ -79,8 +84,9 @@ void StyleSheetTable::setLength(ZLTextStyleEntry &entry, ZLTextStyleEntry::Featu
if (!values.empty() && !values[0].empty()) {
short size;
ZLTextStyleEntry::SizeUnit unit;
parseLength(values[0], size, unit);
entry.setLength(fetureId, size, unit);
if (parseLength(values[0], size, unit)) {
entry.setLength(fetureId, size, unit);
}
}
}

View file

@ -90,6 +90,19 @@ void ZLStringUtil::stripWhiteSpaces(std::string &str) {
str.erase(r_counter, length - r_counter);
}
std::vector<std::string> ZLStringUtil::split(const std::string &str, const std::string &delimiter) {
std::vector<std::string> result;
size_t start = 0;
size_t index = str.find(delimiter);
while (index != std::string::npos) {
result.push_back(str.substr(start, index - start));
start = index + delimiter.length();
index = str.find(delimiter, start);
}
result.push_back(str.substr(start, index - start));
return result;
}
std::string ZLStringUtil::printf(const std::string &format, const std::string &arg0) {
int index = format.find("%s");
if (index == -1) {

View file

@ -36,6 +36,8 @@ public:
static void append(std::string &str, const std::vector<std::string> &buffer);
static void stripWhiteSpaces(std::string &str);
static std::vector<std::string> split(const std::string &str, const std::string &delimiter);
static std::string printf(const std::string &format, const std::string &arg0);
static std::string doubleToString(double value);