diff --git a/jni/Android.mk b/jni/Android.mk index 76622c9a1..38330ab25 100644 --- a/jni/Android.mk +++ b/jni/Android.mk @@ -105,6 +105,7 @@ LOCAL_SRC_FILES := \ NativeFormats/fbreader/src/formats/fb2/FB2TagManager.cpp \ NativeFormats/fbreader/src/formats/fb2/FB2UidReader.cpp \ NativeFormats/fbreader/src/formats/css/CSSInputStream.cpp \ + NativeFormats/fbreader/src/formats/css/CSSSelector.cpp \ NativeFormats/fbreader/src/formats/css/StringInputStream.cpp \ NativeFormats/fbreader/src/formats/css/StyleSheetParser.cpp \ NativeFormats/fbreader/src/formats/css/StyleSheetTable.cpp \ diff --git a/jni/NativeFormats/fbreader/src/formats/css/CSSSelector.cpp b/jni/NativeFormats/fbreader/src/formats/css/CSSSelector.cpp new file mode 100644 index 000000000..1955b04c1 --- /dev/null +++ b/jni/NativeFormats/fbreader/src/formats/css/CSSSelector.cpp @@ -0,0 +1,86 @@ +/* + * Copyright (C) 2004-2014 Geometer Plus + * + * 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. + * + * This program 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 this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA + * 02110-1301, USA. + */ + +#include + +#include "CSSSelector.h" + +CSSSelector::CSSSelector(const std::string &simple) { + const std::size_t index = simple.find('.'); + if (index == std::string::npos) { + Tag = simple; + } else { + Tag = simple.substr(0, index); + Class = simple.substr(index + 1); + } +} + +CSSSelector::Component::Component(Relation delimiter, shared_ptr selector) : Delimiter(delimiter), Selector(selector) { +} + +void CSSSelector::update(shared_ptr &selector, const char *&start, const char *end, char delimiter) { + shared_ptr newSelector = new CSSSelector(std::string(start, end - start)); + if (!selector.isNull()) { + Relation rel = Ancestor; + switch (delimiter) { + case '+': + rel = Previuos; + break; + case '~': + rel = Predecessor; + break; + case '>': + rel = Parent; + break; + } + newSelector->Next = new CSSSelector::Component(rel, selector); + } + selector = newSelector; + start = 0; +} + +shared_ptr CSSSelector::parse(const std::string &data) { + shared_ptr selector; + + const char *start = data.data(); + const char *end = start + data.size(); + const char *wordStart = 0; + char delimiter = '?'; + + for (const char *ptr = start; ptr < end; ++ptr) { + if (*ptr == '+' || *ptr == '<' || *ptr == '~') { + if (wordStart != 0) { + update(selector, wordStart, ptr, delimiter); + } + delimiter = *ptr; + } else if (std::isspace(*ptr)) { + if (wordStart != 0) { + update(selector, wordStart, ptr, delimiter); + delimiter = ' '; + } + } else if (wordStart == 0) { + wordStart = ptr; + } + } + if (wordStart != 0) { + update(selector, wordStart, end, delimiter); + } + + return selector; +} diff --git a/jni/NativeFormats/fbreader/src/formats/css/CSSSelector.h b/jni/NativeFormats/fbreader/src/formats/css/CSSSelector.h new file mode 100644 index 000000000..892989cf2 --- /dev/null +++ b/jni/NativeFormats/fbreader/src/formats/css/CSSSelector.h @@ -0,0 +1,59 @@ +/* + * Copyright (C) 2004-2014 Geometer Plus + * + * 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. + * + * This program 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 this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA + * 02110-1301, USA. + */ + +#ifndef __CSSSELECTOR_H__ +#define __CSSSELECTOR_H__ + +#include + +#include + +class CSSSelector { + +public: + enum Relation { + Ancestor, // "X Y" selector, X is ancestor for Y + Parent, // "X > Y" selector, X is parent for Y + Previuos, // "X + Y" selector, X is previous sibling for Y + Predecessor, // "X ~ Y", X is a sibling for Y that was occured before Y + }; + + struct Component { + Component(Relation delimiter, shared_ptr selector); + + const Relation Delimiter; + const shared_ptr Selector; + }; + +public: + static shared_ptr parse(const std::string &data); + +private: + static void update(shared_ptr &selector, const char *&start, const char *end, char delimiter); + +private: + CSSSelector(const std::string &simple); + +public: + std::string Tag; + std::string Class; + shared_ptr Next; +}; + +#endif /* __CSSSELECTOR_H__ */ diff --git a/jni/NativeFormats/fbreader/src/formats/css/StyleSheetParser.cpp b/jni/NativeFormats/fbreader/src/formats/css/StyleSheetParser.cpp index a9e0271b9..feaf24263 100644 --- a/jni/NativeFormats/fbreader/src/formats/css/StyleSheetParser.cpp +++ b/jni/NativeFormats/fbreader/src/formats/css/StyleSheetParser.cpp @@ -33,6 +33,7 @@ StyleSheetParser::StyleSheetParser(const std::string &pathPrefix) : myPathPrefix(pathPrefix) { //ZLLogger::Instance().registerClass("CSS-IMPORT"); + ZLLogger::Instance().registerClass("CSS-SELECTOR"); reset(); } @@ -263,14 +264,15 @@ void StyleSheetMultiStyleParser::storeData(const std::string &selector, const St const std::vector ids = ZLStringUtil::split(s, ",", true); for (std::vector::const_iterator it = ids.begin(); it != ids.end(); ++it) { - std::string id = *it; - ZLStringUtil::stripWhiteSpaces(id); - if (!id.empty()) { - const std::size_t index = id.find('.'); - if (index == std::string::npos) { - store(id, std::string(), map); - } else { - store(id.substr(0, index), id.substr(index + 1), map); + ZLLogger::Instance().println("CSS-SELECTOR", "STRING = '" + *it + "'"); + shared_ptr selector = CSSSelector::parse(*it); + if (!selector.isNull()) { + ZLLogger::Instance().println("CSS-SELECTOR", "SELE = '" + selector->Tag + "." + selector->Class + "'"); + store(selector, map); + while (!selector->Next.isNull()) { + shared_ptr s = selector->Next->Selector; + selector = s; + ZLLogger::Instance().println("CSS-SELECTOR", "SUB SELE = '" + selector->Tag + "." + selector->Class + "'"); } } } @@ -328,16 +330,16 @@ void StyleSheetMultiStyleParser::processAtRule(const std::string &name, const St StyleSheetTableParser::StyleSheetTableParser(const std::string &pathPrefix, StyleSheetTable &styleTable, shared_ptr fontMap, shared_ptr encryptionMap) : StyleSheetMultiStyleParser(pathPrefix, fontMap, encryptionMap), myStyleTable(styleTable) { } -void StyleSheetTableParser::store(const std::string &tag, const std::string &aClass, const StyleSheetTable::AttributeMap &map) { - myStyleTable.addMap(tag, aClass, map); +void StyleSheetTableParser::store(shared_ptr selector, const StyleSheetTable::AttributeMap &map) { + myStyleTable.addMap(selector, map); } StyleSheetParserWithCache::StyleSheetParserWithCache(const ZLFile &file, const std::string &pathPrefix, shared_ptr fontMap, shared_ptr encryptionMap) : StyleSheetMultiStyleParser(pathPrefix, fontMap, encryptionMap) { myProcessedFiles.insert(file.path()); } -void StyleSheetParserWithCache::store(const std::string &tag, const std::string &aClass, const StyleSheetTable::AttributeMap &map) { - myEntries.push_back(new Entry(tag, aClass, map)); +void StyleSheetParserWithCache::store(shared_ptr selector, const StyleSheetTable::AttributeMap &map) { + myEntries.push_back(new Entry(selector, map)); } void StyleSheetParserWithCache::importCSS(const std::string &path) { @@ -362,7 +364,7 @@ void StyleSheetParserWithCache::importCSS(const std::string &path) { void StyleSheetParserWithCache::applyToTables(StyleSheetTable &table, FontMap &fontMap) const { for (std::list >::const_iterator it = myEntries.begin(); it != myEntries.end(); ++it) { const Entry &entry = **it; - table.addMap(entry.Tag, entry.Class, entry.Map); + table.addMap(entry.Selector, entry.Map); } fontMap.merge(*myFontMap); } diff --git a/jni/NativeFormats/fbreader/src/formats/css/StyleSheetParser.h b/jni/NativeFormats/fbreader/src/formats/css/StyleSheetParser.h index 30406a1dc..2c3f2f321 100644 --- a/jni/NativeFormats/fbreader/src/formats/css/StyleSheetParser.h +++ b/jni/NativeFormats/fbreader/src/formats/css/StyleSheetParser.h @@ -27,6 +27,7 @@ #include #include "StyleSheetTable.h" +#include "CSSSelector.h" #include "FontMap.h" class ZLFile; @@ -89,7 +90,7 @@ protected: StyleSheetMultiStyleParser(const std::string &pathPrefix, shared_ptr fontMap, shared_ptr encryptionMap); protected: - virtual void store(const std::string &tag, const std::string &aClass, const StyleSheetTable::AttributeMap &map) = 0; + virtual void store(shared_ptr selector, const StyleSheetTable::AttributeMap &map) = 0; private: void storeData(const std::string &selector, const StyleSheetTable::AttributeMap &map); @@ -106,7 +107,7 @@ public: StyleSheetTableParser(const std::string &pathPrexix, StyleSheetTable &styleTable, shared_ptr fontMap, shared_ptr encryptionMap); private: - void store(const std::string &tag, const std::string &aClass, const StyleSheetTable::AttributeMap &map); + void store(shared_ptr selector, const StyleSheetTable::AttributeMap &map); private: StyleSheetTable &myStyleTable; @@ -116,11 +117,10 @@ class StyleSheetParserWithCache : public StyleSheetMultiStyleParser { private: struct Entry { - const std::string Tag; - const std::string Class; + shared_ptr Selector; const StyleSheetTable::AttributeMap Map; - Entry(const std::string &tag, const std::string &aClass, const StyleSheetTable::AttributeMap &map); + Entry(shared_ptr selector, const StyleSheetTable::AttributeMap &map); }; public: @@ -128,7 +128,7 @@ public: void applyToTables(StyleSheetTable &table, FontMap &fontMap) const; private: - void store(const std::string &tag, const std::string &aClass, const StyleSheetTable::AttributeMap &map); + void store(shared_ptr selector, const StyleSheetTable::AttributeMap &map); void importCSS(const std::string &path); private: @@ -136,7 +136,7 @@ private: std::set myProcessedFiles; }; -inline StyleSheetParserWithCache::Entry::Entry(const std::string &tag, const std::string &aClass, const StyleSheetTable::AttributeMap &map) : Tag(tag), Class(aClass), Map(map) { +inline StyleSheetParserWithCache::Entry::Entry(shared_ptr selector, const StyleSheetTable::AttributeMap &map) : Selector(selector), Map(map) { } #endif /* __STYLESHEETPARSER_H__ */ diff --git a/jni/NativeFormats/fbreader/src/formats/css/StyleSheetTable.cpp b/jni/NativeFormats/fbreader/src/formats/css/StyleSheetTable.cpp index 087e65661..a7e20e464 100644 --- a/jni/NativeFormats/fbreader/src/formats/css/StyleSheetTable.cpp +++ b/jni/NativeFormats/fbreader/src/formats/css/StyleSheetTable.cpp @@ -23,14 +23,15 @@ #include "StyleSheetTable.h" #include "StyleSheetUtil.h" +#include "CSSSelector.h" bool StyleSheetTable::isEmpty() const { return myControlMap.empty() && myPageBreakBeforeMap.empty() && myPageBreakAfterMap.empty(); } -void StyleSheetTable::addMap(const std::string &tag, const std::string &aClass, const AttributeMap &map) { - if ((!tag.empty() || !aClass.empty()) && !map.empty()) { - const Key key(tag, aClass); +void StyleSheetTable::addMap(shared_ptr selector, const AttributeMap &map) { + if (!selector.isNull() && selector->Next.isNull() && !map.empty()) { + const Key key(selector->Tag, selector->Class); myControlMap[key] = createOrUpdateControl(map, myControlMap[key]); const std::string &pbb = value(map, "page-break-before"); diff --git a/jni/NativeFormats/fbreader/src/formats/css/StyleSheetTable.h b/jni/NativeFormats/fbreader/src/formats/css/StyleSheetTable.h index 8dd84766a..c7793a756 100644 --- a/jni/NativeFormats/fbreader/src/formats/css/StyleSheetTable.h +++ b/jni/NativeFormats/fbreader/src/formats/css/StyleSheetTable.h @@ -29,6 +29,8 @@ #include #include +class CSSSelector; + class StyleSheetTable { public: @@ -36,7 +38,7 @@ public: static shared_ptr createOrUpdateControl(const AttributeMap &map, shared_ptr entry = 0); private: - void addMap(const std::string &tag, const std::string &aClass, const AttributeMap &map); + void addMap(shared_ptr selector, const AttributeMap &map); static void setLength(ZLTextStyleEntry &entry, ZLTextStyleEntry::Feature featureId, const AttributeMap &map, const std::string &attributeName); static const std::string &value(const AttributeMap &map, const std::string &name); @@ -50,13 +52,6 @@ public: void clear(); private: - enum KeyRelation { - Ancestor, // "X Y" selector, X is ancestor for Y - Parent, // "X > Y" selector, X is parent for Y - PreviuosSibling, // "X + Y" selector, X is previous sibling for Y - Predecessor, // "Y ~ X", X is an ancestor for Y, or X is a sibling for Y that was occured before Y - }; - struct Key { Key(const std::string &tag, const std::string &aClass); @@ -78,7 +73,11 @@ inline StyleSheetTable::Key::Key(const std::string &tag, const std::string &aCla } inline bool StyleSheetTable::Key::operator < (const StyleSheetTable::Key &key) const { - return (TagName < key.TagName) || ((TagName == key.TagName) && (ClassName < key.ClassName)); + const int diff = TagName.compare(key.TagName); + if (diff != 0) { + return diff < 0; + } + return ClassName < key.ClassName; } #endif /* __STYLESHEETTABLE_H__ */