Fix conversion of font sizes

This commit is contained in:
Kovid Goyal 2021-03-21 08:02:27 +05:30
parent 8755197b73
commit 3cd166993f
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
2 changed files with 8 additions and 7 deletions

View File

@ -417,8 +417,8 @@ class Token {
for (Py_ssize_t i = 0; i < PyUnicode_GET_LENGTH(src); i++) text[i] = PyUnicode_READ(kind, data, i);
}
void set_text(const char* src) {
text.resize(strlen(src));
void set_text(const char* src, size_t len=0) {
text.resize(len ? len : strlen(src));
for (size_t i = 0; i < text.size(); i++) text[i] = src[i];
}
@ -440,9 +440,10 @@ class Token {
double val = parse_css_number<std::string>(scratch, unit_at).as_double();
double new_val = convert_font_size(val, lit->second);
if (val == new_val) return false;
scratch.reserve(128); scratch.clear();
scratch.resize(std::snprintf(&scratch[0], scratch.capacity(), "%grem", new_val));
set_text(scratch);
char buf[64];
int num = std::snprintf(buf, sizeof(buf), "%grem", new_val);
if (num <= 0) throw std::runtime_error("Failed to format font size");
set_text(buf, num);
return true;
}

View File

@ -22,5 +22,5 @@ class TestTransform(SimpleTest):
def d(src, expected, is_declaration=True, url_callback=None):
self.ae(transform_properties(src, is_declaration=is_declaration, url_callback=url_callback), expected)
# d('font-size: 16px', 'font-size: 1rem')
# d('color: red', 'color: red')
d('font-size: 16px', 'font-size: 1rem')
d('color: red', 'color: red')