1
0
Fork 0
mirror of https://github.com/geometer/FBReaderJ.git synced 2025-10-05 02:39:23 +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 DONE encodings list for native plugins
* CSS: * CSS:
** why do we use CSS stack? ** why do we use CSS stack?
** parse tag lists like 'h1,h2,h3' DONE parse tag lists like 'h1,h2,h3'
** "metric" style params ** "metric" style params
** font size ** font size
** underline ** underline

View file

@ -30,8 +30,20 @@ StyleSheetTableParser::StyleSheetTableParser(StyleSheetTable &table) : myTable(t
//ZLLogger::Instance().registerClass("CSS"); //ZLLogger::Instance().registerClass("CSS");
} }
void StyleSheetTableParser::storeData(const std::string &tagName, const std::string &className, const StyleSheetTable::AttributeMap &map) { void StyleSheetTableParser::storeData(const std::string &selector, const StyleSheetTable::AttributeMap &map) {
myTable.addMap(tagName, className, 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) { shared_ptr<ZLTextStyleEntry> StyleSheetSingleStyleParser::parseString(const char *text) {
@ -42,7 +54,7 @@ shared_ptr<ZLTextStyleEntry> StyleSheetSingleStyleParser::parseString(const char
return control; return control;
} }
StyleSheetParser::StyleSheetParser() : myReadState(TAG_NAME), myInsideComment(false) { StyleSheetParser::StyleSheetParser() : myReadState(SELECTOR), myInsideComment(false) {
} }
StyleSheetParser::~StyleSheetParser() { StyleSheetParser::~StyleSheetParser() {
@ -51,10 +63,9 @@ StyleSheetParser::~StyleSheetParser() {
void StyleSheetParser::reset() { void StyleSheetParser::reset() {
myWord.erase(); myWord.erase();
myAttributeName.erase(); myAttributeName.erase();
myReadState = TAG_NAME; myReadState = SELECTOR;
myInsideComment = false; myInsideComment = false;
myTagName.erase(); mySelectorString.erase();
myClassName.erase();
myMap.clear(); 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) { void StyleSheetParser::processControl(const char control) {
switch (control) { switch (control) {
case '{': case '{':
myReadState = (myReadState == TAG_NAME) ? ATTRIBUTE_NAME : BROKEN; myReadState = (myReadState == SELECTOR) ? ATTRIBUTE_NAME : BROKEN;
break; break;
case '}': case '}':
if (myReadState != BROKEN) { if (myReadState != BROKEN) {
storeData(myTagName, myClassName, myMap); storeData(mySelectorString, myMap);
} }
myReadState = TAG_NAME; myReadState = SELECTOR;
myTagName.erase(); mySelectorString.erase();
myClassName.erase();
myMap.clear(); myMap.clear();
break; break;
case ';': case ';':
@ -163,23 +173,12 @@ void StyleSheetParser::processWord(std::string &word) {
void StyleSheetParser::processWordWithoutComments(const std::string &word) { void StyleSheetParser::processWordWithoutComments(const std::string &word) {
switch (myReadState) { switch (myReadState) {
case TAG_NAME: case SELECTOR:
{ {
int index = word.find('.'); if (mySelectorString.empty()) {
if (index == -1) { mySelectorString = word;
if (myTagName.empty()) {
myTagName = word;
} else { } else {
myTagName += ' ' + word; 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);
}
} }
myMap.clear(); myMap.clear();
break; break;

View file

@ -36,7 +36,7 @@ public:
void parse(const char *text, int len, bool final = false); void parse(const char *text, int len, bool final = false);
protected: 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: private:
bool isControlSymbol(const char symbol); bool isControlSymbol(const char symbol);
@ -48,14 +48,13 @@ private:
std::string myWord; std::string myWord;
std::string myAttributeName; std::string myAttributeName;
enum { enum {
TAG_NAME, SELECTOR,
ATTRIBUTE_NAME, ATTRIBUTE_NAME,
ATTRIBUTE_VALUE, ATTRIBUTE_VALUE,
BROKEN, BROKEN,
} myReadState; } myReadState;
bool myInsideComment; bool myInsideComment;
std::string myTagName; std::string mySelectorString;
std::string myClassName;
StyleSheetTable::AttributeMap myMap; StyleSheetTable::AttributeMap myMap;
friend class StyleSheetSingleStyleParser; friend class StyleSheetSingleStyleParser;
@ -67,7 +66,7 @@ public:
StyleSheetTableParser(StyleSheetTable &table); StyleSheetTableParser(StyleSheetTable &table);
private: 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: private:
StyleSheetTable &myTable; 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, "%")) { if (ZLStringUtil::stringEndsWith(toParse, "%")) {
unit = ZLTextStyleEntry::SIZE_UNIT_PERCENT; unit = ZLTextStyleEntry::SIZE_UNIT_PERCENT;
size = atoi(toParse.c_str()); size = atoi(toParse.c_str());
return true;
} else if (ZLStringUtil::stringEndsWith(toParse, "em")) { } else if (ZLStringUtil::stringEndsWith(toParse, "em")) {
unit = ZLTextStyleEntry::SIZE_UNIT_EM_100; unit = ZLTextStyleEntry::SIZE_UNIT_EM_100;
size = (short)(100 * ZLStringUtil::stringToDouble(toParse, 0)); size = (short)(100 * ZLStringUtil::stringToDouble(toParse, 0));
return true;
} else if (ZLStringUtil::stringEndsWith(toParse, "ex")) { } else if (ZLStringUtil::stringEndsWith(toParse, "ex")) {
unit = ZLTextStyleEntry::SIZE_UNIT_EX_100; unit = ZLTextStyleEntry::SIZE_UNIT_EX_100;
size = (short)(100 * ZLStringUtil::stringToDouble(toParse, 0)); size = (short)(100 * ZLStringUtil::stringToDouble(toParse, 0));
return true;
} else { } else {
unit = ZLTextStyleEntry::SIZE_UNIT_PIXEL; unit = ZLTextStyleEntry::SIZE_UNIT_PIXEL;
size = atoi(toParse.c_str()); size = atoi(toParse.c_str());
return true;
} }
return false;
} }
void StyleSheetTable::setLength(ZLTextStyleEntry &entry, ZLTextStyleEntry::Feature fetureId, const AttributeMap &map, const std::string &attributeName) { void StyleSheetTable::setLength(ZLTextStyleEntry &entry, ZLTextStyleEntry::Feature fetureId, const AttributeMap &map, const std::string &attributeName) {
@ -79,9 +84,10 @@ void StyleSheetTable::setLength(ZLTextStyleEntry &entry, ZLTextStyleEntry::Featu
if (!values.empty() && !values[0].empty()) { if (!values.empty() && !values[0].empty()) {
short size; short size;
ZLTextStyleEntry::SizeUnit unit; ZLTextStyleEntry::SizeUnit unit;
parseLength(values[0], size, unit); if (parseLength(values[0], size, unit)) {
entry.setLength(fetureId, size, unit); entry.setLength(fetureId, size, unit);
} }
}
} }
bool StyleSheetTable::doBreakBefore(const std::string &tag, const std::string &aClass) const { bool StyleSheetTable::doBreakBefore(const std::string &tag, const std::string &aClass) const {

View file

@ -90,6 +90,19 @@ void ZLStringUtil::stripWhiteSpaces(std::string &str) {
str.erase(r_counter, length - r_counter); 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) { std::string ZLStringUtil::printf(const std::string &format, const std::string &arg0) {
int index = format.find("%s"); int index = format.find("%s");
if (index == -1) { if (index == -1) {

View file

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