Skip to content

Commit

Permalink
ResolveHtmlEntities() => ResolveHtmlEntitiesTemp()
Browse files Browse the repository at this point in the history
  • Loading branch information
kjk committed May 23, 2024
1 parent d2d7622 commit 430d1cf
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 38 deletions.
39 changes: 15 additions & 24 deletions src/EbookDoc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -506,9 +506,8 @@ void EpubDoc::ParseMetadata(const char* content) {
tok = pullParser.Next();
if (tok && tok->IsText()) {
auto prop = epubPropsMap[idx];
char* val = ResolveHtmlEntities(tok->s, tok->sLen);
TempStr val = ResolveHtmlEntitiesTemp(tok->s, tok->sLen);
AddProp(props, prop, val);
str::Free(val);
}
break;
}
Expand Down Expand Up @@ -886,53 +885,49 @@ bool Fb2Doc::Load() {
break;
}
if (tok->IsText()) {
char* val = ResolveHtmlEntities(tok->s, tok->sLen);
TempStr val = ResolveHtmlEntitiesTemp(tok->s, tok->sLen);
AddProp(props, kPropTitle, val);
str::Free(val);
}
} else if ((inTitleInfo || inDocInfo) && tok->IsStartTag() && tok->NameIsNS("author", FB2_MAIN_NS)) {
AutoFreeStr docAuthor;
TempStr docAuthor = nullptr;
while ((tok = parser.Next()) != nullptr && !tok->IsError() &&
!(tok->IsEndTag() && tok->NameIsNS("author", FB2_MAIN_NS))) {
if (tok->IsText()) {
AutoFreeStr author = ResolveHtmlEntities(tok->s, tok->sLen);
TempStr author = ResolveHtmlEntitiesTemp(tok->s, tok->sLen);
if (docAuthor) {
docAuthor.Set(str::Join(docAuthor, " ", author));
docAuthor = str::JoinTemp(docAuthor, " ", author);
} else {
docAuthor.Set(author.Release());
docAuthor = author;
}
}
}
if (docAuthor) {
str::NormalizeWSInPlace(docAuthor);
if (!str::IsEmpty(docAuthor.CStr())) {
char* val = docAuthor.CStr();
if (!str::IsEmpty(docAuthor)) {
char* val = docAuthor;
bool replaceIfExists = inTitleInfo != 0;
AddProp(props, kPropAuthor, val, replaceIfExists);
}
}
} else if (inTitleInfo && tok->IsStartTag() && tok->NameIsNS("date", FB2_MAIN_NS)) {
AttrInfo* attr = tok->GetAttrByNameNS("value", FB2_MAIN_NS);
if (attr) {
char* val = ResolveHtmlEntities(attr->val, attr->valLen);
TempStr val = ResolveHtmlEntitiesTemp(attr->val, attr->valLen);
AddProp(props, kPropCreationDate, val);
str::Free(val);
}
} else if (inDocInfo && tok->IsStartTag() && tok->NameIsNS("date", FB2_MAIN_NS)) {
AttrInfo* attr = tok->GetAttrByNameNS("value", FB2_MAIN_NS);
if (attr) {
char* val = ResolveHtmlEntities(attr->val, attr->valLen);
TempStr val = ResolveHtmlEntitiesTemp(attr->val, attr->valLen);
AddProp(props, kPropModificationDate, val);
str::Free(val);
}
} else if (inDocInfo && tok->IsStartTag() && tok->NameIsNS("program-used", FB2_MAIN_NS)) {
if ((tok = parser.Next()) == nullptr || tok->IsError()) {
break;
}
if (tok->IsText()) {
char* val = ResolveHtmlEntities(tok->s, tok->sLen);
TempStr val = ResolveHtmlEntitiesTemp(tok->s, tok->sLen);
AddProp(props, kPropCreatorApp, val);
str::Free(val);
}
} else if (inTitleInfo && tok->IsStartTag() && tok->NameIsNS("coverpage", FB2_MAIN_NS)) {
tok = parser.Next();
Expand Down Expand Up @@ -1290,27 +1285,23 @@ bool HtmlDoc::Load() {
if (tok->IsStartTag() && Tag_Title == tok->tag) {
tok = parser.Next();
if (tok && tok->IsText()) {
char* val = ResolveHtmlEntities(tok->s, tok->sLen);
TempStr val = ResolveHtmlEntitiesTemp(tok->s, tok->sLen);
AddProp(props, kPropTitle, val);
str::Free(val);
}
} else if ((tok->IsStartTag() || tok->IsEmptyElementEndTag()) && Tag_Meta == tok->tag) {
AttrInfo* attrName = tok->GetAttrByName("name");
AttrInfo* attrValue = tok->GetAttrByName("content");
if (!attrName || !attrValue) {
/* ignore this tag */;
} else if (attrName->ValIs("author")) {
char* val = ResolveHtmlEntities(attrValue->val, attrValue->valLen);
TempStr val = ResolveHtmlEntitiesTemp(attrValue->val, attrValue->valLen);
AddProp(props, kPropAuthor, val);
str::Free(val);
} else if (attrName->ValIs("date")) {
char* val = ResolveHtmlEntities(attrValue->val, attrValue->valLen);
TempStr val = ResolveHtmlEntitiesTemp(attrValue->val, attrValue->valLen);
AddProp(props, kPropCreationDate, val);
str::Free(val);
} else if (attrName->ValIs("copyright")) {
char* val = ResolveHtmlEntities(attrValue->val, attrValue->valLen);
TempStr val = ResolveHtmlEntitiesTemp(attrValue->val, attrValue->valLen);
AddProp(props, kPropCopyright, val);
str::Free(val);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/EngineEbook.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -465,7 +465,7 @@ IPageElement* EngineEbook::CreatePageLink(DrawInstr* link, Rect rect, int pageNo
DrawInstr* baseAnchor = baseAnchors.at(pageNo - 1);
if (baseAnchor) {
char* basePath = str::DupTemp(baseAnchor->str.s, baseAnchor->str.len);
AutoFreeStr relPath = ResolveHtmlEntities(link->str.s, link->str.len);
TempStr relPath = ResolveHtmlEntitiesTemp(link->str.s, link->str.len);
AutoFreeStr absPath = NormalizeURL(relPath, basePath);
url = str::DupTemp(absPath.Get());
}
Expand Down
16 changes: 8 additions & 8 deletions src/EngineImages.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1002,12 +1002,12 @@ struct ComicInfoParser : json::ValueVisitor {
void Parse(const ByteSlice& xmlData);
};

static char* GetTextContent(HtmlPullParser& parser) {
static TempStr GetTextContentTemp(HtmlPullParser& parser) {
HtmlToken* tok = parser.Next();
if (!tok || !tok->IsText()) {
return nullptr;
}
return ResolveHtmlEntities(tok->s, tok->sLen);
return ResolveHtmlEntitiesTemp(tok->s, tok->sLen);
}

// extract ComicInfo.xml metadata
Expand All @@ -1021,33 +1021,33 @@ void ComicInfoParser::Parse(const ByteSlice& xmlData) {
continue;
}
if (tok->NameIs("Title")) {
AutoFreeStr value = GetTextContent(parser);
TempStr value = GetTextContentTemp(parser);
if (value) {
Visit("/ComicBookInfo/1.0/title", value, json::Type::String);
}
} else if (tok->NameIs("Year")) {
AutoFreeStr value = GetTextContent(parser);
TempStr value = GetTextContentTemp(parser);
if (value) {
Visit("/ComicBookInfo/1.0/publicationYear", value, json::Type::Number);
}
} else if (tok->NameIs("Month")) {
AutoFreeStr value = GetTextContent(parser);
TempStr value = GetTextContentTemp(parser);
if (value) {
Visit("/ComicBookInfo/1.0/publicationMonth", value, json::Type::Number);
}
} else if (tok->NameIs("Summary")) {
AutoFreeStr value = GetTextContent(parser);
TempStr value = GetTextContentTemp(parser);
if (value) {
Visit("/X-summary", value, json::Type::String);
}
} else if (tok->NameIs("Writer")) {
AutoFreeStr value = GetTextContent(parser);
TempStr value = GetTextContentTemp(parser);
if (value) {
Visit("/ComicBookInfo/1.0/credits[0]/person", value, json::Type::String);
Visit("/ComicBookInfo/1.0/credits[0]/primary", "true", json::Type::Bool);
}
} else if (tok->NameIs("Penciller")) {
AutoFreeStr value = GetTextContent(parser);
TempStr value = GetTextContentTemp(parser);
if (value) {
Visit("/ComicBookInfo/1.0/credits[1]/person", value, json::Type::String);
Visit("/ComicBookInfo/1.0/credits[1]/primary", "true", json::Type::Bool);
Expand Down
3 changes: 2 additions & 1 deletion src/ifilter/EpubFilter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,8 @@ static WCHAR* ExtractHtmlText(EpubDoc* doc) {
t->sLen--;
}
if (t->sLen > 0) {
text.AppendAndFree(ResolveHtmlEntities(t->s, t->sLen));
TempStr s = ResolveHtmlEntitiesTemp(t->s, t->sLen);
text.Append(s);
text.AppendChar(' ');
}
} else if (t->IsStartTag()) {
Expand Down
2 changes: 1 addition & 1 deletion src/utils/HtmlPullParser.h
Original file line number Diff line number Diff line change
Expand Up @@ -118,4 +118,4 @@ int HtmlEntityNameToRune(const WCHAR* name, size_t nameLen);
const char* ResolveHtmlEntity(const char* s, size_t len, int& rune);
const char* ResolveHtmlEntities(const char* s, const char* end, Allocator* alloc);
char* ResolveHtmlEntities(const char* s, size_t len);
TempStr ResolveHtmlEntities(const char* s, size_t len);
TempStr ResolveHtmlEntitiesTemp(const char* s, size_t len);
6 changes: 3 additions & 3 deletions src/utils/tests/HtmlPullParser_ut.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,11 @@ static void HtmlEntities() {
utassert(got == entities[i].rune);
utassert((-1 == got) == !entEnd);
}
Allocator* ta = GetTempAllocator();
const char* unchanged[] = {"foo", "", " as;d "};
for (size_t i = 0; i < dimof(unchanged); i++) {
const char* s = unchanged[i];
const char* res = ResolveHtmlEntities(s, s + str::Len(s), nullptr);
TempStr res = (TempStr)ResolveHtmlEntities(s, s + str::Len(s), ta);
utassert(res == s);
}

Expand All @@ -67,9 +68,8 @@ static void HtmlEntities() {
};
for (size_t i = 0; i < dimof(changed); i++) {
const char* s = changed[i].s;
const char* res = ResolveHtmlEntities(s, s + str::Len(s), nullptr);
TempStr res = (TempStr)ResolveHtmlEntities(s, s + str::Len(s), ta);
utassert(str::Eq(res, changed[i].res));
str::Free(res);
}
}

Expand Down

0 comments on commit 430d1cf

Please sign in to comment.