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

compound CSS selectors support (in progress)

This commit is contained in:
Nikolay Pultsin 2014-10-11 20:51:49 +02:00
parent 237e3b191e
commit 4d4a096e3f
7 changed files with 180 additions and 32 deletions

View file

@ -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 \

View file

@ -0,0 +1,86 @@
/*
* Copyright (C) 2004-2014 Geometer Plus <contact@geometerplus.com>
*
* 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 <cctype>
#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<CSSSelector> selector) : Delimiter(delimiter), Selector(selector) {
}
void CSSSelector::update(shared_ptr<CSSSelector> &selector, const char *&start, const char *end, char delimiter) {
shared_ptr<CSSSelector> 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> CSSSelector::parse(const std::string &data) {
shared_ptr<CSSSelector> 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;
}

View file

@ -0,0 +1,59 @@
/*
* Copyright (C) 2004-2014 Geometer Plus <contact@geometerplus.com>
*
* 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 <string>
#include <shared_ptr.h>
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<CSSSelector> selector);
const Relation Delimiter;
const shared_ptr<CSSSelector> Selector;
};
public:
static shared_ptr<CSSSelector> parse(const std::string &data);
private:
static void update(shared_ptr<CSSSelector> &selector, const char *&start, const char *end, char delimiter);
private:
CSSSelector(const std::string &simple);
public:
std::string Tag;
std::string Class;
shared_ptr<Component> Next;
};
#endif /* __CSSSELECTOR_H__ */

View file

@ -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<std::string> ids = ZLStringUtil::split(s, ",", true);
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 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<CSSSelector> 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<CSSSelector> 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> fontMap, shared_ptr<EncryptionMap> 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<CSSSelector> selector, const StyleSheetTable::AttributeMap &map) {
myStyleTable.addMap(selector, map);
}
StyleSheetParserWithCache::StyleSheetParserWithCache(const ZLFile &file, const std::string &pathPrefix, shared_ptr<FontMap> fontMap, shared_ptr<EncryptionMap> 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<CSSSelector> 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<shared_ptr<Entry> >::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);
}

View file

@ -27,6 +27,7 @@
#include <FileEncryptionInfo.h>
#include "StyleSheetTable.h"
#include "CSSSelector.h"
#include "FontMap.h"
class ZLFile;
@ -89,7 +90,7 @@ protected:
StyleSheetMultiStyleParser(const std::string &pathPrefix, shared_ptr<FontMap> fontMap, shared_ptr<EncryptionMap> encryptionMap);
protected:
virtual void store(const std::string &tag, const std::string &aClass, const StyleSheetTable::AttributeMap &map) = 0;
virtual void store(shared_ptr<CSSSelector> 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> fontMap, shared_ptr<EncryptionMap> encryptionMap);
private:
void store(const std::string &tag, const std::string &aClass, const StyleSheetTable::AttributeMap &map);
void store(shared_ptr<CSSSelector> 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<CSSSelector> Selector;
const StyleSheetTable::AttributeMap Map;
Entry(const std::string &tag, const std::string &aClass, const StyleSheetTable::AttributeMap &map);
Entry(shared_ptr<CSSSelector> 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<CSSSelector> selector, const StyleSheetTable::AttributeMap &map);
void importCSS(const std::string &path);
private:
@ -136,7 +136,7 @@ private:
std::set<std::string> 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<CSSSelector> selector, const StyleSheetTable::AttributeMap &map) : Selector(selector), Map(map) {
}
#endif /* __STYLESHEETPARSER_H__ */

View file

@ -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<CSSSelector> 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");

View file

@ -29,6 +29,8 @@
#include <ZLTextParagraph.h>
#include <ZLTextStyleEntry.h>
class CSSSelector;
class StyleSheetTable {
public:
@ -36,7 +38,7 @@ public:
static shared_ptr<ZLTextStyleEntry> createOrUpdateControl(const AttributeMap &map, shared_ptr<ZLTextStyleEntry> entry = 0);
private:
void addMap(const std::string &tag, const std::string &aClass, const AttributeMap &map);
void addMap(shared_ptr<CSSSelector> 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__ */