Loading src/edit.cpp +502 −0 Changes for src/edit.cpp: 502 added lines, 0 removed lines. Original line number Diff line number Diff line Loading @@ -440,6 +440,508 @@ blogi::webedit::EditPlugin* blogi::webedit::WebEditor::deepCopySubtree(tinyxml2: return newWidget; } // ============================================================================ // HTML Import: Convert arbitrary HTML to widget tree // ============================================================================ std::map<std::string,std::string> blogi::webedit::WebEditor::parseInlineStyle(const std::string &style) { std::map<std::string,std::string> result; std::istringstream ss(style); std::string token; while (std::getline(ss, token, ';')) { auto colon = token.find(':'); if (colon == std::string::npos) continue; std::string key = token.substr(0, colon); std::string val = token.substr(colon + 1); // trim whitespace auto ltrim = [](std::string &s) { s.erase(0, s.find_first_not_of(" \t\n\r")); }; auto rtrim = [](std::string &s) { auto pos = s.find_last_not_of(" \t\n\r"); if (pos != std::string::npos) s.erase(pos + 1); else s.clear(); }; ltrim(key); rtrim(key); ltrim(val); rtrim(val); if (!key.empty() && !val.empty()) { result[key] = val; } } return result; } std::string blogi::webedit::WebEditor::extractCustomCss( std::map<std::string,std::string> &props, const std::vector<std::string> &knownKeys) { std::string customCss; std::vector<std::string> toRemove; for (auto &[k, v] : props) { bool known = false; for (auto &kk : knownKeys) { if (k == kk) { known = true; break; } } if (!known) { if (!customCss.empty()) customCss += " "; customCss += k + ": " + v + ";"; toRemove.push_back(k); } } for (auto &k : toRemove) props.erase(k); return customCss; } std::string blogi::webedit::WebEditor::serializeInnerHtml(tinyxml2::XMLElement *el) { std::string result; tinyxml2::XMLNode *child = el->FirstChild(); while (child) { tinyxml2::XMLPrinter printer(nullptr, true); // compact mode child->Accept(&printer); result += printer.CStr(); child = child->NextSibling(); } return result; } void blogi::webedit::WebEditor::htmlElementToWidgetXml( tinyxml2::XMLElement *htmlEl, tinyxml2::XMLDocument &doc, tinyxml2::XMLElement *parentXml) { if (!htmlEl) return; tinyxml2::XMLElement *child = htmlEl->FirstChildElement(); tinyxml2::XMLNode *childNode = htmlEl->FirstChild(); // If this element has only text content and no child elements, skip wrapper // (handled by the caller) while (child || childNode) { // Process text nodes before the next element while (childNode && childNode != child) { if (childNode->ToText()) { const char *text = childNode->Value(); if (text && text[0] != '\0') { // Non-empty text → TextBox widget std::string trimmed = text; // Skip pure whitespace if (trimmed.find_first_not_of(" \t\n\r") != std::string::npos) { tinyxml2::XMLElement *tb = doc.NewElement("TextBox"); tb->SetText(trimmed.c_str()); parentXml->InsertEndChild(tb); } } } childNode = childNode->NextSibling(); } if (!child) break; std::string tag = child->Name() ? child->Name() : ""; // Lowercase the tag name std::transform(tag.begin(), tag.end(), tag.begin(), [](unsigned char c) { return std::tolower(c); }); std::string style; const char *styleAttr = child->Attribute("style"); if (styleAttr) style = styleAttr; auto cssProps = parseInlineStyle(style); std::string cssClass; const char *classAttr = child->Attribute("class"); if (classAttr) cssClass = classAttr; // ---- Map HTML tags to widget types ---- if (tag == "section") { tinyxml2::XMLElement *w = doc.NewElement("Section"); // Map known CSS properties if (cssProps.count("background-color")) w->SetAttribute("background_color", cssProps["background-color"].c_str()); if (cssProps.count("border-radius")) w->SetAttribute("border_radius", cssProps["border-radius"].c_str()); if (cssProps.count("border")) w->SetAttribute("border_style", cssProps["border"].c_str()); if (cssProps.count("opacity")) w->SetAttribute("opacity", cssProps["opacity"].c_str()); if (cssProps.count("padding")) w->SetAttribute("padding", cssProps["padding"].c_str()); if (cssProps.count("margin")) w->SetAttribute("margin", cssProps["margin"].c_str()); if (cssProps.count("max-width")) w->SetAttribute("max_width", cssProps["max-width"].c_str()); if (cssProps.count("min-height")) w->SetAttribute("min_height", cssProps["min-height"].c_str()); if (!cssClass.empty()) w->SetAttribute("css_class", cssClass.c_str()); std::vector<std::string> knownCss = {"background-color","border-radius","border","opacity", "padding","margin","max-width","min-height","display","flex-direction","justify-content"}; std::string custom = extractCustomCss(cssProps, knownCss); if (!custom.empty()) { tinyxml2::XMLElement *ccEl = doc.NewElement("CustomCss"); ccEl->SetText(custom.c_str()); w->InsertEndChild(ccEl); } parentXml->InsertEndChild(w); // Recurse into children htmlElementToWidgetXml(child, doc, w); } else if (tag == "div") { tinyxml2::XMLElement *w = doc.NewElement("Container"); if (cssProps.count("background-color")) w->SetAttribute("background_color", cssProps["background-color"].c_str()); if (cssProps.count("border-radius")) w->SetAttribute("border_radius", cssProps["border-radius"].c_str()); if (cssProps.count("border")) w->SetAttribute("border_style", cssProps["border"].c_str()); if (cssProps.count("opacity")) w->SetAttribute("opacity", cssProps["opacity"].c_str()); if (cssProps.count("padding")) w->SetAttribute("padding", cssProps["padding"].c_str()); if (cssProps.count("margin")) w->SetAttribute("margin", cssProps["margin"].c_str()); if (!cssClass.empty()) w->SetAttribute("css_class", cssClass.c_str()); // Check if it's grid-like bool isGrid = false; if (cssProps.count("display") && cssProps["display"] == "grid") { isGrid = true; } if (isGrid) { // Convert to Grid widget instead w->SetName("Grid"); if (cssProps.count("grid-template-columns")) w->SetAttribute("columns", cssProps["grid-template-columns"].c_str()); if (cssProps.count("grid-template-rows")) w->SetAttribute("rows", cssProps["grid-template-rows"].c_str()); if (cssProps.count("gap")) w->SetAttribute("gap", cssProps["gap"].c_str()); std::vector<std::string> knownCss = {"background-color","border-radius","border","opacity", "padding","margin","display","grid-template-columns","grid-template-rows","gap", "align-items"}; std::string custom = extractCustomCss(cssProps, knownCss); if (!custom.empty()) { tinyxml2::XMLElement *ccEl = doc.NewElement("CustomCss"); ccEl->SetText(custom.c_str()); w->InsertEndChild(ccEl); } } else { std::vector<std::string> knownCss = {"background-color","border-radius","border","opacity", "padding","margin","display","flex-direction","justify-content"}; std::string custom = extractCustomCss(cssProps, knownCss); if (!custom.empty()) { tinyxml2::XMLElement *ccEl = doc.NewElement("CustomCss"); ccEl->SetText(custom.c_str()); w->InsertEndChild(ccEl); } } parentXml->InsertEndChild(w); htmlElementToWidgetXml(child, doc, w); } else if (tag == "img") { tinyxml2::XMLElement *w = doc.NewElement("Image"); const char *src = child->Attribute("src"); const char *alt = child->Attribute("alt"); if (src) w->SetAttribute("src", src); if (alt) w->SetAttribute("alt", alt); if (cssProps.count("width")) w->SetAttribute("width", cssProps["width"].c_str()); if (cssProps.count("height")) w->SetAttribute("height", cssProps["height"].c_str()); if (cssProps.count("object-fit")) w->SetAttribute("object_fit", cssProps["object-fit"].c_str()); if (cssProps.count("border-radius")) w->SetAttribute("border_radius", cssProps["border-radius"].c_str()); std::vector<std::string> knownCss = {"width","height","object-fit","border-radius","display"}; std::string custom = extractCustomCss(cssProps, knownCss); if (!custom.empty()) { tinyxml2::XMLElement *ccEl = doc.NewElement("CustomCss"); ccEl->SetText(custom.c_str()); w->InsertEndChild(ccEl); } parentXml->InsertEndChild(w); } else if (tag == "video") { tinyxml2::XMLElement *w = doc.NewElement("Video"); const char *src = child->Attribute("src"); if (src) w->SetAttribute("src", src); if (child->Attribute("controls")) w->SetAttribute("controls", "true"); if (cssProps.count("width")) w->SetAttribute("width", cssProps["width"].c_str()); if (cssProps.count("height")) w->SetAttribute("height", cssProps["height"].c_str()); std::vector<std::string> knownCss = {"width","height","display"}; std::string custom = extractCustomCss(cssProps, knownCss); if (!custom.empty()) { tinyxml2::XMLElement *ccEl = doc.NewElement("CustomCss"); ccEl->SetText(custom.c_str()); w->InsertEndChild(ccEl); } parentXml->InsertEndChild(w); } else if (tag == "table") { tinyxml2::XMLElement *w = doc.NewElement("Table"); if (cssProps.count("border")) w->SetAttribute("border_style", cssProps["border"].c_str()); if (cssProps.count("background-color")) w->SetAttribute("background_color", cssProps["background-color"].c_str()); // Count rows and columns from the HTML table int numRows = 0, numCols = 0; bool hasHeader = false; // Check for thead tinyxml2::XMLElement *thead = child->FirstChildElement("thead"); tinyxml2::XMLElement *tbody = child->FirstChildElement("tbody"); // Count header rows if (thead) { for (auto *tr = thead->FirstChildElement("tr"); tr; tr = tr->NextSiblingElement("tr")) { numRows++; hasHeader = true; int cols = 0; for (auto *td = tr->FirstChildElement(); td; td = td->NextSiblingElement()) cols++; if (cols > numCols) numCols = cols; } } // Count body rows tinyxml2::XMLElement *rowParent = tbody ? tbody : child; for (auto *tr = rowParent->FirstChildElement("tr"); tr; tr = tr->NextSiblingElement("tr")) { numRows++; int cols = 0; for (auto *td = tr->FirstChildElement(); td; td = td->NextSiblingElement()) cols++; if (cols > numCols) numCols = cols; } w->SetAttribute("num_rows", numRows); w->SetAttribute("num_cols", numCols); w->SetAttribute("has_header", hasHeader ? "true" : "false"); // Serialize cell data int row = 0; if (thead) { for (auto *tr = thead->FirstChildElement("tr"); tr; tr = tr->NextSiblingElement("tr")) { tinyxml2::XMLElement *rowEl = doc.NewElement("Row"); rowEl->SetAttribute("index", row); int col = 0; for (auto *td = tr->FirstChildElement(); td; td = td->NextSiblingElement()) { tinyxml2::XMLElement *cellEl = doc.NewElement("Cell"); cellEl->SetAttribute("col", col); std::string cellText = serializeInnerHtml(td); cellEl->SetText(cellText.c_str()); rowEl->InsertEndChild(cellEl); col++; } w->InsertEndChild(rowEl); row++; } } for (auto *tr = rowParent->FirstChildElement("tr"); tr; tr = tr->NextSiblingElement("tr")) { tinyxml2::XMLElement *rowEl = doc.NewElement("Row"); rowEl->SetAttribute("index", row); int col = 0; for (auto *td = tr->FirstChildElement(); td; td = td->NextSiblingElement()) { tinyxml2::XMLElement *cellEl = doc.NewElement("Cell"); cellEl->SetAttribute("col", col); std::string cellText = serializeInnerHtml(td); cellEl->SetText(cellText.c_str()); rowEl->InsertEndChild(cellEl); col++; } w->InsertEndChild(rowEl); row++; } std::vector<std::string> knownCss = {"border","background-color","border-collapse","width"}; std::string custom = extractCustomCss(cssProps, knownCss); if (!custom.empty()) { tinyxml2::XMLElement *ccEl = doc.NewElement("CustomCss"); ccEl->SetText(custom.c_str()); w->InsertEndChild(ccEl); } parentXml->InsertEndChild(w); } else if (tag == "ul" || tag == "ol") { tinyxml2::XMLElement *w = doc.NewElement("List"); w->SetAttribute("list_type", tag.c_str()); if (cssProps.count("font-size")) w->SetAttribute("font_size", cssProps["font-size"].c_str()); if (cssProps.count("color")) w->SetAttribute("font_color", cssProps["color"].c_str()); if (cssProps.count("line-height")) w->SetAttribute("line_height", cssProps["line-height"].c_str()); if (cssProps.count("list-style-type")) w->SetAttribute("list_style_type", cssProps["list-style-type"].c_str()); if (cssProps.count("padding")) w->SetAttribute("padding", cssProps["padding"].c_str()); if (cssProps.count("margin")) w->SetAttribute("margin", cssProps["margin"].c_str()); // Extract list items for (auto *li = child->FirstChildElement("li"); li; li = li->NextSiblingElement("li")) { tinyxml2::XMLElement *itemEl = doc.NewElement("Item"); std::string itemHtml = serializeInnerHtml(li); itemEl->SetText(itemHtml.c_str()); w->InsertEndChild(itemEl); } std::vector<std::string> knownCss = {"font-size","color","line-height","list-style-type", "padding","margin","display","flex-direction","gap"}; std::string custom = extractCustomCss(cssProps, knownCss); if (!custom.empty()) { tinyxml2::XMLElement *ccEl = doc.NewElement("CustomCss"); ccEl->SetText(custom.c_str()); w->InsertEndChild(ccEl); } parentXml->InsertEndChild(w); } else if (tag == "a" && child->FirstChildElement() == nullptr) { // Simple link with text only → Button widget tinyxml2::XMLElement *w = doc.NewElement("Button"); const char *href = child->Attribute("href"); if (href) w->SetAttribute("link_url", href); const char *target = child->Attribute("target"); if (target && strcmp(target, "_blank") == 0) w->SetAttribute("open_new_tab", "true"); std::string label = serializeInnerHtml(child); w->SetAttribute("label", label.c_str()); if (cssProps.count("background-color")) w->SetAttribute("background_color", cssProps["background-color"].c_str()); if (cssProps.count("color")) w->SetAttribute("text_color", cssProps["color"].c_str()); if (cssProps.count("font-size")) w->SetAttribute("font_size", cssProps["font-size"].c_str()); if (cssProps.count("padding")) w->SetAttribute("padding", cssProps["padding"].c_str()); if (cssProps.count("border-radius")) w->SetAttribute("border_radius", cssProps["border-radius"].c_str()); if (cssProps.count("border")) w->SetAttribute("border_style", cssProps["border"].c_str()); std::vector<std::string> knownCss = {"background-color","color","font-size","padding", "border-radius","border","display","text-decoration","cursor","text-align"}; std::string custom = extractCustomCss(cssProps, knownCss); if (!custom.empty()) { tinyxml2::XMLElement *ccEl = doc.NewElement("CustomCss"); ccEl->SetText(custom.c_str()); w->InsertEndChild(ccEl); } parentXml->InsertEndChild(w); } else if (tag == "h1" || tag == "h2" || tag == "h3" || tag == "h4" || tag == "h5" || tag == "h6" || tag == "p" || tag == "span" || tag == "b" || tag == "i" || tag == "em" || tag == "strong" || tag == "blockquote" || tag == "pre" || tag == "code") { // Text-like elements → TextBox widget tinyxml2::XMLElement *w = doc.NewElement("TextBox"); // Serialize the full inner HTML (preserving formatting tags) std::string innerHtml; // For heading tags, wrap content to preserve semantics if (tag[0] == 'h' && tag.size() == 2) { innerHtml = "<" + tag + ">" + serializeInnerHtml(child) + "</" + tag + ">"; } else if (tag == "blockquote" || tag == "pre") { innerHtml = "<" + tag + ">" + serializeInnerHtml(child) + "</" + tag + ">"; } else { innerHtml = serializeInnerHtml(child); } w->SetText(innerHtml.c_str()); if (cssProps.count("font-family")) w->SetAttribute("font_family", cssProps["font-family"].c_str()); if (cssProps.count("font-size")) w->SetAttribute("font_size", cssProps["font-size"].c_str()); if (cssProps.count("color")) w->SetAttribute("font_color", cssProps["color"].c_str()); if (cssProps.count("text-align")) w->SetAttribute("text_align", cssProps["text-align"].c_str()); if (cssProps.count("font-weight")) w->SetAttribute("font_weight", cssProps["font-weight"].c_str()); if (cssProps.count("font-style")) w->SetAttribute("font_style", cssProps["font-style"].c_str()); std::vector<std::string> knownCss = {"font-family","font-size","color","text-align", "font-weight","font-style","padding","margin","line-height"}; std::string custom = extractCustomCss(cssProps, knownCss); if (!custom.empty()) { tinyxml2::XMLElement *ccEl = doc.NewElement("CustomCss"); ccEl->SetText(custom.c_str()); w->InsertEndChild(ccEl); } parentXml->InsertEndChild(w); } else if (tag == "style") { // Skip <style> elements during import (CSS is extracted via inline styles) } else if (tag == "script") { // Skip <script> elements during import for security } else if (tag == "br" || tag == "hr") { // Simple elements → CustomHtml tinyxml2::XMLElement *w = doc.NewElement("CustomHtml"); tinyxml2::XMLElement *htmlContent = doc.NewElement("HtmlContent"); std::string htmlStr = "<" + tag + ">"; htmlContent->SetText(htmlStr.c_str()); w->InsertEndChild(htmlContent); parentXml->InsertEndChild(w); } else { // Everything else → CustomHtml widget tinyxml2::XMLElement *w = doc.NewElement("CustomHtml"); // Serialize the entire element (outer HTML) as the HTML content tinyxml2::XMLPrinter printer(nullptr, true); child->Accept(&printer); std::string outerHtml = printer.CStr(); tinyxml2::XMLElement *htmlContent = doc.NewElement("HtmlContent"); htmlContent->SetText(outerHtml.c_str()); w->InsertEndChild(htmlContent); // Extract CSS to custom field if (!style.empty()) { tinyxml2::XMLElement *cssEl = doc.NewElement("CssContent"); cssEl->SetText(style.c_str()); w->InsertEndChild(cssEl); } parentXml->InsertEndChild(w); } // Move to next sibling child = child->NextSiblingElement(); if (childNode) childNode = child; } // Handle trailing text nodes after the last child element if (childNode) { while (childNode) { if (childNode->ToText()) { const char *text = childNode->Value(); if (text && text[0] != '\0') { std::string trimmed = text; if (trimmed.find_first_not_of(" \t\n\r") != std::string::npos) { tinyxml2::XMLElement *tb = doc.NewElement("TextBox"); tb->SetText(trimmed.c_str()); parentXml->InsertEndChild(tb); } } } childNode = childNode->NextSibling(); } } } void blogi::webedit::WebEditor::importHtmlToWidgets(const std::string &html) { // Clear existing document DocElements.clear(); DocRoot = nullptr; CurDoc.Clear(); CurRootEl = nullptr; // Parse the HTML using tinyxml2 (wrap in root element for well-formedness) tinyxml2::XMLDocument htmlDoc; std::string wrapped = "<ImportRoot>" + html + "</ImportRoot>"; tinyxml2::XMLError err = htmlDoc.Parse(wrapped.c_str()); if (err != tinyxml2::XML_SUCCESS) { // Fallback: import as single CustomHtml widget CurDoc.Clear(); CurRootEl = CurDoc.NewElement("WebEditor"); CurRootEl->SetAttribute("Version", "1.0"); CurDoc.InsertEndChild(CurRootEl); tinyxml2::XMLElement *w = CurDoc.NewElement("CustomHtml"); tinyxml2::XMLElement *htmlContent = CurDoc.NewElement("HtmlContent"); htmlContent->SetText(html.c_str()); w->InsertEndChild(htmlContent); CurRootEl->InsertEndChild(w); loadWidgetsFromXml(CurRootEl, nullptr); return; } // Build widget XML CurDoc.Clear(); CurRootEl = CurDoc.NewElement("WebEditor"); CurRootEl->SetAttribute("Version", "1.0"); CurDoc.InsertEndChild(CurRootEl); tinyxml2::XMLElement *importRoot = htmlDoc.RootElement(); if (importRoot) { htmlElementToWidgetXml(importRoot, CurDoc, CurRootEl); } // Load the generated widget XML into the live tree loadWidgetsFromXml(CurRootEl, nullptr); } void blogi::webedit::WebEditor::saveDocumentXml(libhttppp::HttpRequest &curreq) { // Serialize the current document tree to XML CurDoc.Clear(); Loading src/edit.h +18 −0 File changed.Preview size limit exceeded, changes collapsed. Show changes Loading
src/edit.cpp +502 −0 Changes for src/edit.cpp: 502 added lines, 0 removed lines. Original line number Diff line number Diff line Loading @@ -440,6 +440,508 @@ blogi::webedit::EditPlugin* blogi::webedit::WebEditor::deepCopySubtree(tinyxml2: return newWidget; } // ============================================================================ // HTML Import: Convert arbitrary HTML to widget tree // ============================================================================ std::map<std::string,std::string> blogi::webedit::WebEditor::parseInlineStyle(const std::string &style) { std::map<std::string,std::string> result; std::istringstream ss(style); std::string token; while (std::getline(ss, token, ';')) { auto colon = token.find(':'); if (colon == std::string::npos) continue; std::string key = token.substr(0, colon); std::string val = token.substr(colon + 1); // trim whitespace auto ltrim = [](std::string &s) { s.erase(0, s.find_first_not_of(" \t\n\r")); }; auto rtrim = [](std::string &s) { auto pos = s.find_last_not_of(" \t\n\r"); if (pos != std::string::npos) s.erase(pos + 1); else s.clear(); }; ltrim(key); rtrim(key); ltrim(val); rtrim(val); if (!key.empty() && !val.empty()) { result[key] = val; } } return result; } std::string blogi::webedit::WebEditor::extractCustomCss( std::map<std::string,std::string> &props, const std::vector<std::string> &knownKeys) { std::string customCss; std::vector<std::string> toRemove; for (auto &[k, v] : props) { bool known = false; for (auto &kk : knownKeys) { if (k == kk) { known = true; break; } } if (!known) { if (!customCss.empty()) customCss += " "; customCss += k + ": " + v + ";"; toRemove.push_back(k); } } for (auto &k : toRemove) props.erase(k); return customCss; } std::string blogi::webedit::WebEditor::serializeInnerHtml(tinyxml2::XMLElement *el) { std::string result; tinyxml2::XMLNode *child = el->FirstChild(); while (child) { tinyxml2::XMLPrinter printer(nullptr, true); // compact mode child->Accept(&printer); result += printer.CStr(); child = child->NextSibling(); } return result; } void blogi::webedit::WebEditor::htmlElementToWidgetXml( tinyxml2::XMLElement *htmlEl, tinyxml2::XMLDocument &doc, tinyxml2::XMLElement *parentXml) { if (!htmlEl) return; tinyxml2::XMLElement *child = htmlEl->FirstChildElement(); tinyxml2::XMLNode *childNode = htmlEl->FirstChild(); // If this element has only text content and no child elements, skip wrapper // (handled by the caller) while (child || childNode) { // Process text nodes before the next element while (childNode && childNode != child) { if (childNode->ToText()) { const char *text = childNode->Value(); if (text && text[0] != '\0') { // Non-empty text → TextBox widget std::string trimmed = text; // Skip pure whitespace if (trimmed.find_first_not_of(" \t\n\r") != std::string::npos) { tinyxml2::XMLElement *tb = doc.NewElement("TextBox"); tb->SetText(trimmed.c_str()); parentXml->InsertEndChild(tb); } } } childNode = childNode->NextSibling(); } if (!child) break; std::string tag = child->Name() ? child->Name() : ""; // Lowercase the tag name std::transform(tag.begin(), tag.end(), tag.begin(), [](unsigned char c) { return std::tolower(c); }); std::string style; const char *styleAttr = child->Attribute("style"); if (styleAttr) style = styleAttr; auto cssProps = parseInlineStyle(style); std::string cssClass; const char *classAttr = child->Attribute("class"); if (classAttr) cssClass = classAttr; // ---- Map HTML tags to widget types ---- if (tag == "section") { tinyxml2::XMLElement *w = doc.NewElement("Section"); // Map known CSS properties if (cssProps.count("background-color")) w->SetAttribute("background_color", cssProps["background-color"].c_str()); if (cssProps.count("border-radius")) w->SetAttribute("border_radius", cssProps["border-radius"].c_str()); if (cssProps.count("border")) w->SetAttribute("border_style", cssProps["border"].c_str()); if (cssProps.count("opacity")) w->SetAttribute("opacity", cssProps["opacity"].c_str()); if (cssProps.count("padding")) w->SetAttribute("padding", cssProps["padding"].c_str()); if (cssProps.count("margin")) w->SetAttribute("margin", cssProps["margin"].c_str()); if (cssProps.count("max-width")) w->SetAttribute("max_width", cssProps["max-width"].c_str()); if (cssProps.count("min-height")) w->SetAttribute("min_height", cssProps["min-height"].c_str()); if (!cssClass.empty()) w->SetAttribute("css_class", cssClass.c_str()); std::vector<std::string> knownCss = {"background-color","border-radius","border","opacity", "padding","margin","max-width","min-height","display","flex-direction","justify-content"}; std::string custom = extractCustomCss(cssProps, knownCss); if (!custom.empty()) { tinyxml2::XMLElement *ccEl = doc.NewElement("CustomCss"); ccEl->SetText(custom.c_str()); w->InsertEndChild(ccEl); } parentXml->InsertEndChild(w); // Recurse into children htmlElementToWidgetXml(child, doc, w); } else if (tag == "div") { tinyxml2::XMLElement *w = doc.NewElement("Container"); if (cssProps.count("background-color")) w->SetAttribute("background_color", cssProps["background-color"].c_str()); if (cssProps.count("border-radius")) w->SetAttribute("border_radius", cssProps["border-radius"].c_str()); if (cssProps.count("border")) w->SetAttribute("border_style", cssProps["border"].c_str()); if (cssProps.count("opacity")) w->SetAttribute("opacity", cssProps["opacity"].c_str()); if (cssProps.count("padding")) w->SetAttribute("padding", cssProps["padding"].c_str()); if (cssProps.count("margin")) w->SetAttribute("margin", cssProps["margin"].c_str()); if (!cssClass.empty()) w->SetAttribute("css_class", cssClass.c_str()); // Check if it's grid-like bool isGrid = false; if (cssProps.count("display") && cssProps["display"] == "grid") { isGrid = true; } if (isGrid) { // Convert to Grid widget instead w->SetName("Grid"); if (cssProps.count("grid-template-columns")) w->SetAttribute("columns", cssProps["grid-template-columns"].c_str()); if (cssProps.count("grid-template-rows")) w->SetAttribute("rows", cssProps["grid-template-rows"].c_str()); if (cssProps.count("gap")) w->SetAttribute("gap", cssProps["gap"].c_str()); std::vector<std::string> knownCss = {"background-color","border-radius","border","opacity", "padding","margin","display","grid-template-columns","grid-template-rows","gap", "align-items"}; std::string custom = extractCustomCss(cssProps, knownCss); if (!custom.empty()) { tinyxml2::XMLElement *ccEl = doc.NewElement("CustomCss"); ccEl->SetText(custom.c_str()); w->InsertEndChild(ccEl); } } else { std::vector<std::string> knownCss = {"background-color","border-radius","border","opacity", "padding","margin","display","flex-direction","justify-content"}; std::string custom = extractCustomCss(cssProps, knownCss); if (!custom.empty()) { tinyxml2::XMLElement *ccEl = doc.NewElement("CustomCss"); ccEl->SetText(custom.c_str()); w->InsertEndChild(ccEl); } } parentXml->InsertEndChild(w); htmlElementToWidgetXml(child, doc, w); } else if (tag == "img") { tinyxml2::XMLElement *w = doc.NewElement("Image"); const char *src = child->Attribute("src"); const char *alt = child->Attribute("alt"); if (src) w->SetAttribute("src", src); if (alt) w->SetAttribute("alt", alt); if (cssProps.count("width")) w->SetAttribute("width", cssProps["width"].c_str()); if (cssProps.count("height")) w->SetAttribute("height", cssProps["height"].c_str()); if (cssProps.count("object-fit")) w->SetAttribute("object_fit", cssProps["object-fit"].c_str()); if (cssProps.count("border-radius")) w->SetAttribute("border_radius", cssProps["border-radius"].c_str()); std::vector<std::string> knownCss = {"width","height","object-fit","border-radius","display"}; std::string custom = extractCustomCss(cssProps, knownCss); if (!custom.empty()) { tinyxml2::XMLElement *ccEl = doc.NewElement("CustomCss"); ccEl->SetText(custom.c_str()); w->InsertEndChild(ccEl); } parentXml->InsertEndChild(w); } else if (tag == "video") { tinyxml2::XMLElement *w = doc.NewElement("Video"); const char *src = child->Attribute("src"); if (src) w->SetAttribute("src", src); if (child->Attribute("controls")) w->SetAttribute("controls", "true"); if (cssProps.count("width")) w->SetAttribute("width", cssProps["width"].c_str()); if (cssProps.count("height")) w->SetAttribute("height", cssProps["height"].c_str()); std::vector<std::string> knownCss = {"width","height","display"}; std::string custom = extractCustomCss(cssProps, knownCss); if (!custom.empty()) { tinyxml2::XMLElement *ccEl = doc.NewElement("CustomCss"); ccEl->SetText(custom.c_str()); w->InsertEndChild(ccEl); } parentXml->InsertEndChild(w); } else if (tag == "table") { tinyxml2::XMLElement *w = doc.NewElement("Table"); if (cssProps.count("border")) w->SetAttribute("border_style", cssProps["border"].c_str()); if (cssProps.count("background-color")) w->SetAttribute("background_color", cssProps["background-color"].c_str()); // Count rows and columns from the HTML table int numRows = 0, numCols = 0; bool hasHeader = false; // Check for thead tinyxml2::XMLElement *thead = child->FirstChildElement("thead"); tinyxml2::XMLElement *tbody = child->FirstChildElement("tbody"); // Count header rows if (thead) { for (auto *tr = thead->FirstChildElement("tr"); tr; tr = tr->NextSiblingElement("tr")) { numRows++; hasHeader = true; int cols = 0; for (auto *td = tr->FirstChildElement(); td; td = td->NextSiblingElement()) cols++; if (cols > numCols) numCols = cols; } } // Count body rows tinyxml2::XMLElement *rowParent = tbody ? tbody : child; for (auto *tr = rowParent->FirstChildElement("tr"); tr; tr = tr->NextSiblingElement("tr")) { numRows++; int cols = 0; for (auto *td = tr->FirstChildElement(); td; td = td->NextSiblingElement()) cols++; if (cols > numCols) numCols = cols; } w->SetAttribute("num_rows", numRows); w->SetAttribute("num_cols", numCols); w->SetAttribute("has_header", hasHeader ? "true" : "false"); // Serialize cell data int row = 0; if (thead) { for (auto *tr = thead->FirstChildElement("tr"); tr; tr = tr->NextSiblingElement("tr")) { tinyxml2::XMLElement *rowEl = doc.NewElement("Row"); rowEl->SetAttribute("index", row); int col = 0; for (auto *td = tr->FirstChildElement(); td; td = td->NextSiblingElement()) { tinyxml2::XMLElement *cellEl = doc.NewElement("Cell"); cellEl->SetAttribute("col", col); std::string cellText = serializeInnerHtml(td); cellEl->SetText(cellText.c_str()); rowEl->InsertEndChild(cellEl); col++; } w->InsertEndChild(rowEl); row++; } } for (auto *tr = rowParent->FirstChildElement("tr"); tr; tr = tr->NextSiblingElement("tr")) { tinyxml2::XMLElement *rowEl = doc.NewElement("Row"); rowEl->SetAttribute("index", row); int col = 0; for (auto *td = tr->FirstChildElement(); td; td = td->NextSiblingElement()) { tinyxml2::XMLElement *cellEl = doc.NewElement("Cell"); cellEl->SetAttribute("col", col); std::string cellText = serializeInnerHtml(td); cellEl->SetText(cellText.c_str()); rowEl->InsertEndChild(cellEl); col++; } w->InsertEndChild(rowEl); row++; } std::vector<std::string> knownCss = {"border","background-color","border-collapse","width"}; std::string custom = extractCustomCss(cssProps, knownCss); if (!custom.empty()) { tinyxml2::XMLElement *ccEl = doc.NewElement("CustomCss"); ccEl->SetText(custom.c_str()); w->InsertEndChild(ccEl); } parentXml->InsertEndChild(w); } else if (tag == "ul" || tag == "ol") { tinyxml2::XMLElement *w = doc.NewElement("List"); w->SetAttribute("list_type", tag.c_str()); if (cssProps.count("font-size")) w->SetAttribute("font_size", cssProps["font-size"].c_str()); if (cssProps.count("color")) w->SetAttribute("font_color", cssProps["color"].c_str()); if (cssProps.count("line-height")) w->SetAttribute("line_height", cssProps["line-height"].c_str()); if (cssProps.count("list-style-type")) w->SetAttribute("list_style_type", cssProps["list-style-type"].c_str()); if (cssProps.count("padding")) w->SetAttribute("padding", cssProps["padding"].c_str()); if (cssProps.count("margin")) w->SetAttribute("margin", cssProps["margin"].c_str()); // Extract list items for (auto *li = child->FirstChildElement("li"); li; li = li->NextSiblingElement("li")) { tinyxml2::XMLElement *itemEl = doc.NewElement("Item"); std::string itemHtml = serializeInnerHtml(li); itemEl->SetText(itemHtml.c_str()); w->InsertEndChild(itemEl); } std::vector<std::string> knownCss = {"font-size","color","line-height","list-style-type", "padding","margin","display","flex-direction","gap"}; std::string custom = extractCustomCss(cssProps, knownCss); if (!custom.empty()) { tinyxml2::XMLElement *ccEl = doc.NewElement("CustomCss"); ccEl->SetText(custom.c_str()); w->InsertEndChild(ccEl); } parentXml->InsertEndChild(w); } else if (tag == "a" && child->FirstChildElement() == nullptr) { // Simple link with text only → Button widget tinyxml2::XMLElement *w = doc.NewElement("Button"); const char *href = child->Attribute("href"); if (href) w->SetAttribute("link_url", href); const char *target = child->Attribute("target"); if (target && strcmp(target, "_blank") == 0) w->SetAttribute("open_new_tab", "true"); std::string label = serializeInnerHtml(child); w->SetAttribute("label", label.c_str()); if (cssProps.count("background-color")) w->SetAttribute("background_color", cssProps["background-color"].c_str()); if (cssProps.count("color")) w->SetAttribute("text_color", cssProps["color"].c_str()); if (cssProps.count("font-size")) w->SetAttribute("font_size", cssProps["font-size"].c_str()); if (cssProps.count("padding")) w->SetAttribute("padding", cssProps["padding"].c_str()); if (cssProps.count("border-radius")) w->SetAttribute("border_radius", cssProps["border-radius"].c_str()); if (cssProps.count("border")) w->SetAttribute("border_style", cssProps["border"].c_str()); std::vector<std::string> knownCss = {"background-color","color","font-size","padding", "border-radius","border","display","text-decoration","cursor","text-align"}; std::string custom = extractCustomCss(cssProps, knownCss); if (!custom.empty()) { tinyxml2::XMLElement *ccEl = doc.NewElement("CustomCss"); ccEl->SetText(custom.c_str()); w->InsertEndChild(ccEl); } parentXml->InsertEndChild(w); } else if (tag == "h1" || tag == "h2" || tag == "h3" || tag == "h4" || tag == "h5" || tag == "h6" || tag == "p" || tag == "span" || tag == "b" || tag == "i" || tag == "em" || tag == "strong" || tag == "blockquote" || tag == "pre" || tag == "code") { // Text-like elements → TextBox widget tinyxml2::XMLElement *w = doc.NewElement("TextBox"); // Serialize the full inner HTML (preserving formatting tags) std::string innerHtml; // For heading tags, wrap content to preserve semantics if (tag[0] == 'h' && tag.size() == 2) { innerHtml = "<" + tag + ">" + serializeInnerHtml(child) + "</" + tag + ">"; } else if (tag == "blockquote" || tag == "pre") { innerHtml = "<" + tag + ">" + serializeInnerHtml(child) + "</" + tag + ">"; } else { innerHtml = serializeInnerHtml(child); } w->SetText(innerHtml.c_str()); if (cssProps.count("font-family")) w->SetAttribute("font_family", cssProps["font-family"].c_str()); if (cssProps.count("font-size")) w->SetAttribute("font_size", cssProps["font-size"].c_str()); if (cssProps.count("color")) w->SetAttribute("font_color", cssProps["color"].c_str()); if (cssProps.count("text-align")) w->SetAttribute("text_align", cssProps["text-align"].c_str()); if (cssProps.count("font-weight")) w->SetAttribute("font_weight", cssProps["font-weight"].c_str()); if (cssProps.count("font-style")) w->SetAttribute("font_style", cssProps["font-style"].c_str()); std::vector<std::string> knownCss = {"font-family","font-size","color","text-align", "font-weight","font-style","padding","margin","line-height"}; std::string custom = extractCustomCss(cssProps, knownCss); if (!custom.empty()) { tinyxml2::XMLElement *ccEl = doc.NewElement("CustomCss"); ccEl->SetText(custom.c_str()); w->InsertEndChild(ccEl); } parentXml->InsertEndChild(w); } else if (tag == "style") { // Skip <style> elements during import (CSS is extracted via inline styles) } else if (tag == "script") { // Skip <script> elements during import for security } else if (tag == "br" || tag == "hr") { // Simple elements → CustomHtml tinyxml2::XMLElement *w = doc.NewElement("CustomHtml"); tinyxml2::XMLElement *htmlContent = doc.NewElement("HtmlContent"); std::string htmlStr = "<" + tag + ">"; htmlContent->SetText(htmlStr.c_str()); w->InsertEndChild(htmlContent); parentXml->InsertEndChild(w); } else { // Everything else → CustomHtml widget tinyxml2::XMLElement *w = doc.NewElement("CustomHtml"); // Serialize the entire element (outer HTML) as the HTML content tinyxml2::XMLPrinter printer(nullptr, true); child->Accept(&printer); std::string outerHtml = printer.CStr(); tinyxml2::XMLElement *htmlContent = doc.NewElement("HtmlContent"); htmlContent->SetText(outerHtml.c_str()); w->InsertEndChild(htmlContent); // Extract CSS to custom field if (!style.empty()) { tinyxml2::XMLElement *cssEl = doc.NewElement("CssContent"); cssEl->SetText(style.c_str()); w->InsertEndChild(cssEl); } parentXml->InsertEndChild(w); } // Move to next sibling child = child->NextSiblingElement(); if (childNode) childNode = child; } // Handle trailing text nodes after the last child element if (childNode) { while (childNode) { if (childNode->ToText()) { const char *text = childNode->Value(); if (text && text[0] != '\0') { std::string trimmed = text; if (trimmed.find_first_not_of(" \t\n\r") != std::string::npos) { tinyxml2::XMLElement *tb = doc.NewElement("TextBox"); tb->SetText(trimmed.c_str()); parentXml->InsertEndChild(tb); } } } childNode = childNode->NextSibling(); } } } void blogi::webedit::WebEditor::importHtmlToWidgets(const std::string &html) { // Clear existing document DocElements.clear(); DocRoot = nullptr; CurDoc.Clear(); CurRootEl = nullptr; // Parse the HTML using tinyxml2 (wrap in root element for well-formedness) tinyxml2::XMLDocument htmlDoc; std::string wrapped = "<ImportRoot>" + html + "</ImportRoot>"; tinyxml2::XMLError err = htmlDoc.Parse(wrapped.c_str()); if (err != tinyxml2::XML_SUCCESS) { // Fallback: import as single CustomHtml widget CurDoc.Clear(); CurRootEl = CurDoc.NewElement("WebEditor"); CurRootEl->SetAttribute("Version", "1.0"); CurDoc.InsertEndChild(CurRootEl); tinyxml2::XMLElement *w = CurDoc.NewElement("CustomHtml"); tinyxml2::XMLElement *htmlContent = CurDoc.NewElement("HtmlContent"); htmlContent->SetText(html.c_str()); w->InsertEndChild(htmlContent); CurRootEl->InsertEndChild(w); loadWidgetsFromXml(CurRootEl, nullptr); return; } // Build widget XML CurDoc.Clear(); CurRootEl = CurDoc.NewElement("WebEditor"); CurRootEl->SetAttribute("Version", "1.0"); CurDoc.InsertEndChild(CurRootEl); tinyxml2::XMLElement *importRoot = htmlDoc.RootElement(); if (importRoot) { htmlElementToWidgetXml(importRoot, CurDoc, CurRootEl); } // Load the generated widget XML into the live tree loadWidgetsFromXml(CurRootEl, nullptr); } void blogi::webedit::WebEditor::saveDocumentXml(libhttppp::HttpRequest &curreq) { // Serialize the current document tree to XML CurDoc.Clear(); Loading