diff --git a/src/calibre/srv/fast_css_transform.cpp b/src/calibre/srv/fast_css_transform.cpp index 8315ee3b17..ccdf80dd81 100644 --- a/src/calibre/srv/fast_css_transform.cpp +++ b/src/calibre/srv/fast_css_transform.cpp @@ -297,11 +297,13 @@ class Token { void serialize_string(std::u32string &out) const { const char32_t delim = text.find('"') == std::u32string::npos ? '"' : '\''; + out.push_back(delim); for (const auto ch : text) { if (ch == '\n') out.append({'\\', '\n'}); else if (ch == delim || ch == '\\') serialize_escaped_char(ch, out); else out.push_back(ch); } + out.push_back(delim); } public: @@ -313,7 +315,7 @@ class Token { Token(const TokenType type, const char32_t ch, size_t out_pos) : type(type), text(), unit_at(0), out_pos(out_pos) { text.reserve(16); - text.push_back(ch); + if (ch) text.push_back(ch); } Token(const Token& other) : diff --git a/src/calibre/srv/tests/fast_css_transform.py b/src/calibre/srv/tests/fast_css_transform.py index 58b3de9620..c1d8d14387 100644 --- a/src/calibre/srv/tests/fast_css_transform.py +++ b/src/calibre/srv/tests/fast_css_transform.py @@ -23,7 +23,15 @@ class TestTransform(SimpleTest): self.ae(transform_properties(src, is_declaration=is_declaration, url_callback=url_callback), expected) d(r'f\ont-s\69z\65 : 16\px', 'font-size: 1rem') + d('font -size: 16px', 'font -size: 16px') d('font-size: 16px', 'font-size: 1rem') d('fOnt-size :16px', 'fOnt-size :1rem') d('font-size:2%', 'font-size:2%') - d('font-size: 72pt; color: red; font-size: 2in', 'font-size: 6rem; color: red; font-size: 12rem') + d('font-size: 72pt; margin: 20px; font-size: 2in', 'font-size: 6rem; margin: 20px; font-size: 12rem') + d(r'''font: "some 'name" 32px''', 'font: "some \'name" 2rem') + d(r'''font: 'some "name' 32px''', 'font: \'some "name\' 2rem') + d(r'''font: 'some \n ame' 32px''', 'font: "some n ame" 2rem') + d('''font: 'some \\\nname' 32px''', 'font: "some name" 2rem') + d('font: sans-serif 16px/3', 'font: sans-serif 1rem/3') + + d('-epub-writing-mode: a; -webkit-writing-mode: b; writing-mode: c', 'writing-mode: a; writing-mode: b; writing-mode: c')