#include "Document.h" namespace web { namespace { // Long blocks are split, so that they appear while the page loads and are quick to lay out constexpr size_t MAX_BLOCK_BYTES = 2048; constexpr size_t MAX_BLOCK_RUNS = 256; constexpr size_t MAX_FORMS = 64; constexpr size_t MAX_FIELDS = 200; } bool Document::reserve(size_t bytes) { if (truncated || used + bytes > budget) { truncated = true; return false; } used += bytes; return true; } void Document::commit() { const size_t count = runs.size() - current.firstRun; if (count > 0) { current.runCount = static_cast(count); blocks.push_back(current); used += sizeof(Block); } } void Document::beginBlock(BlockKind kind, uint8_t indent, bool gap) { // An empty block passes its gap on const bool inherited = open && !hasOpenText() && current.gap; endBlock(); current = { static_cast(runs.size()), 0, kind, indent, gap || inherited }; open = true; } void Document::endBlock() { if (!open) return; // Trailing spaces would only cause empty lines while (hasOpenText() && current.kind != BlockKind::Preformatted && text.back() == ' ') { text.pop_back(); if (--runs.back().length == 0) runs.pop_back(); } commit(); open = false; } void Document::append(const char* data, size_t length, uint8_t style, uint16_t link) { if (!open) beginBlock(BlockKind::Text, 0, false); if (length == 0) return; const size_t blockBytes = hasOpenText() ? text.size() - runs[current.firstRun].offset : 0; const bool atBreak = current.kind == BlockKind::Preformatted ? (hasOpenText() && text.back() == '\n') : data[0] == ' '; const bool tooLong = blockBytes > MAX_BLOCK_BYTES && (atBreak || blockBytes > MAX_BLOCK_BYTES * 2); if (tooLong || runs.size() - current.firstRun >= MAX_BLOCK_RUNS) { const Block previous = current; commit(); current = { static_cast(runs.size()), 0, previous.kind, previous.indent, false }; if (current.kind != BlockKind::Preformatted && data[0] == ' ') { data++; if (--length == 0) return; } } const bool extend = hasOpenText() && runs.back().style == style && runs.back().link == link; if (!reserve(length + (extend ? 0 : sizeof(Run)))) return; if (extend) { runs.back().length += static_cast(length); } else { runs.push_back({ static_cast(text.size()), static_cast(length), link, style }); } text.append(data, length); } void Document::addRule() { endBlock(); if (!reserve(sizeof(Block))) return; blocks.push_back({ static_cast(runs.size()), 0, BlockKind::Rule, 0, true }); } void Document::addImage(const ImageSource& source, const std::string& text, uint16_t link, uint8_t indent) { if (text.empty() || images.size() >= NO_IMAGE || !reserve(source.url.size() + sizeof(ImageSource))) return; images.push_back(source); beginBlock(BlockKind::Image, indent, false); current.image = static_cast(images.size() - 1); append(text.data(), text.size(), STYLE_IMAGE, link); endBlock(); } uint16_t Document::addLink(const std::string& url) { if (url.empty()) return NO_LINK; if (links.size() >= NO_LINK || !reserve(url.size() + sizeof(String))) return NO_LINK; links.push_back(toString(url)); return static_cast(links.size() - 1); } void Document::addAnchor(const std::string& name) { // Anchors may use at most an eighth of the budget if (name.empty() || name.size() > 80 || anchors.size() * sizeof(Anchor) > budget / 8) return; if (!reserve(name.size() + sizeof(Anchor))) return; anchors.push_back({ toString(name), static_cast(blocks.size() + (hasOpenText() ? 1 : 0)) }); } uint16_t Document::addForm(const std::string& action, bool post, bool unsupported) { if (forms.size() >= MAX_FORMS || !reserve(action.size() + sizeof(Form))) return NO_FIELD; forms.push_back({ toString(action), post, unsupported }); return static_cast(forms.size() - 1); } void Document::addField(Field field, const std::string& label, uint8_t indent) { size_t size = sizeof(Field) + field.name.size() + field.value.size() + field.placeholder.size(); for (const auto& text : field.labels) size += sizeof(String) + text.size(); for (const auto& text : field.values) size += sizeof(String) + text.size(); if (field.form >= forms.size() || fields.size() >= MAX_FIELDS || !reserve(size)) return; field.link = NO_LINK; if (field.type != FieldType::Hidden) { if (links.size() >= NO_LINK || !reserve(sizeof(String))) return; // A link without an address: nothing to follow, but selected and clicked in the same way links.push_back(String()); field.link = static_cast(links.size() - 1); } const uint16_t link = field.link; const FieldType type = field.type; fields.push_back(std::move(field)); if (type == FieldType::Text || type == FieldType::Password || type == FieldType::Select) { beginBlock(BlockKind::Field, indent, false); current.image = static_cast(fields.size() - 1); append("field", 5, STYLE_FIELD, link); endBlock(); } else if (type == FieldType::Checkbox) { append("[_]", 3, STYLE_FIELD, link); } else if (type == FieldType::Radio) { append("(_)", 3, STYLE_FIELD, link); } else if (type == FieldType::Submit) { const std::string button = "[ " + label + " ]"; append(button.data(), button.size(), STYLE_FIELD, link); } } uint16_t Document::fieldOfLink(uint16_t link) const { if (link == NO_LINK) return NO_FIELD; for (size_t i = 0; i < fields.size(); i++) { if (fields[i].link == link) return static_cast(i); } return NO_FIELD; } void Document::toggle(size_t index) { if (index >= fields.size()) return; Field& field = fields[index]; if (field.type == FieldType::Checkbox) { field.checked = !field.checked; } else if (field.type == FieldType::Radio) { for (auto& other : fields) { if (other.type == FieldType::Radio && other.form == field.form && other.name == field.name) other.checked = false; } field.checked = true; } } void Document::setValue(size_t index, const std::string& value) { if (index < fields.size() && fields[index].type == FieldType::Text) fields[index].value = toString(value); } void Document::select(size_t index, size_t option) { if (index < fields.size() && option < fields[index].values.size()) fields[index].selected = static_cast(option); } int32_t Document::findAnchor(const std::string& name) const { for (const auto& anchor : anchors) { if (anchor.name == name.c_str()) return static_cast(anchor.block); } return -1; } }