Commit fd4c571c authored by jan.koester's avatar jan.koester
Browse files

test

parent 329c15f2
Loading
Loading
Loading
Loading
+36 −23
Changes for src/edit.cpp: 36 added lines, 23 removed lines.
Original line number Diff line number Diff line
@@ -627,19 +627,32 @@ void blogi::webedit::WebEditor::htmlElementToWidgetXml(
{
    if (!htmlEl) return;

    libhtmlpp::Element *cur = htmlEl;
    // Iterative traversal using an explicit heap-allocated stack to avoid
    // thread stack overflow (musl default thread stack is only 128KB).
    struct Frame { libhtmlpp::Element *cur; tinyxml2::XMLElement *pXml; };
    std::stack<Frame> workStack;
    workStack.push({htmlEl, parentXml});

    while (!workStack.empty()) {
        auto frame = workStack.top();
        workStack.pop();

        // Collect child frames to push later (in reverse for document order)
        std::vector<Frame> childFrames;

        libhtmlpp::Element *cur = frame.cur;
        tinyxml2::XMLElement *pXml = frame.pXml;

        while (cur) {
            int type = cur->getType();

            if (type == libhtmlpp::TextEl) {
            // Text node
                libhtmlpp::TextElement *textEl = static_cast<libhtmlpp::TextElement*>(cur);
                std::string text = textEl->getText();
                if (text.find_first_not_of(" \t\n\r") != std::string::npos) {
                    tinyxml2::XMLElement *tb = doc.NewElement("TextBox");
                    tb->SetText(text.c_str());
                parentXml->InsertEndChild(tb);
                    pXml->InsertEndChild(tb);
                }
                cur = cur->nextElement();
                continue;
@@ -651,7 +664,6 @@ void blogi::webedit::WebEditor::htmlElementToWidgetXml(
            }

            if (type == libhtmlpp::ScriptEL) {
            // Skip script elements for security
                cur = cur->nextElement();
                continue;
            }
@@ -673,7 +685,7 @@ void blogi::webedit::WebEditor::htmlElementToWidgetXml(
                tag == "aside") {
                libhtmlpp::Element *child = el->firstChild();
                if (child) {
                htmlElementToWidgetXml(child, doc, parentXml, sheet);
                    childFrames.push_back({child, pXml});
                }
                cur = cur->nextElement();
                continue;
@@ -711,9 +723,9 @@ void blogi::webedit::WebEditor::htmlElementToWidgetXml(
                    ccEl->SetText(custom.c_str());
                    w->InsertEndChild(ccEl);
                }
            parentXml->InsertEndChild(w);
                pXml->InsertEndChild(w);
                libhtmlpp::Element *child = el->firstChild();
            if (child) htmlElementToWidgetXml(child, doc, w, sheet);
                if (child) childFrames.push_back({child, w});

            } else if (tag == "div") {
                tinyxml2::XMLElement *w = doc.NewElement("Container");
@@ -750,9 +762,9 @@ void blogi::webedit::WebEditor::htmlElementToWidgetXml(
                        w->InsertEndChild(ccEl);
                    }
                }
            parentXml->InsertEndChild(w);
                pXml->InsertEndChild(w);
                libhtmlpp::Element *child = el->firstChild();
            if (child) htmlElementToWidgetXml(child, doc, w, sheet);
                if (child) childFrames.push_back({child, w});

            } else if (tag == "img") {
                tinyxml2::XMLElement *w = doc.NewElement("Image");
@@ -776,7 +788,7 @@ void blogi::webedit::WebEditor::htmlElementToWidgetXml(
                    ccEl->SetText(custom.c_str());
                    w->InsertEndChild(ccEl);
                }
            parentXml->InsertEndChild(w);
                pXml->InsertEndChild(w);

            } else if (tag == "video") {
                tinyxml2::XMLElement *w = doc.NewElement("Video");
@@ -793,18 +805,16 @@ void blogi::webedit::WebEditor::htmlElementToWidgetXml(
                    ccEl->SetText(custom.c_str());
                    w->InsertEndChild(ccEl);
                }
            parentXml->InsertEndChild(w);
                pXml->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());

            // Walk table children to count rows/cols and extract cell data
                int numRows = 0, numCols = 0;
                bool hasHeader = false;

            // Helper lambda to process <tr> elements
                struct RowData { std::vector<std::string> cells; bool isHeader; };
                std::vector<RowData> rows;

@@ -830,7 +840,6 @@ void blogi::webedit::WebEditor::htmlElementToWidgetXml(
                    if (header) hasHeader = true;
                };

            // Walk table children: thead, tbody, or direct tr
                libhtmlpp::Element *tblChild = el->firstChild();
                while (tblChild) {
                    if (tblChild->getType() == libhtmlpp::HtmlEl) {
@@ -897,7 +906,7 @@ void blogi::webedit::WebEditor::htmlElementToWidgetXml(
                    ccEl->SetText(custom.c_str());
                    w->InsertEndChild(ccEl);
                }
            parentXml->InsertEndChild(w);
                pXml->InsertEndChild(w);

            } else if (tag == "ul" || tag == "ol") {
                tinyxml2::XMLElement *w = doc.NewElement("List");
@@ -935,10 +944,9 @@ void blogi::webedit::WebEditor::htmlElementToWidgetXml(
                    ccEl->SetText(custom.c_str());
                    w->InsertEndChild(ccEl);
                }
            parentXml->InsertEndChild(w);
                pXml->InsertEndChild(w);

            } else if (tag == "a") {
            // Check if simple text link → Button, otherwise recurse
                bool hasChildElements = false;
                libhtmlpp::Element *aChild = el->firstChild();
                while (aChild) {
@@ -971,14 +979,13 @@ void blogi::webedit::WebEditor::htmlElementToWidgetXml(
                        ccEl->SetText(custom.c_str());
                        w->InsertEndChild(ccEl);
                    }
                parentXml->InsertEndChild(w);
                    pXml->InsertEndChild(w);
                } else {
                // Link wrapping child elements → CustomHtml
                    tinyxml2::XMLElement *w = doc.NewElement("CustomHtml");
                    tinyxml2::XMLElement *htmlContent = doc.NewElement("HtmlContent");
                    htmlContent->SetText(serializeElement(cur).c_str());
                    w->InsertEndChild(htmlContent);
                parentXml->InsertEndChild(w);
                    pXml->InsertEndChild(w);
                }

            } else if (tag == "h1" || tag == "h2" || tag == "h3" || tag == "h4" ||
@@ -1010,7 +1017,7 @@ void blogi::webedit::WebEditor::htmlElementToWidgetXml(
                    ccEl->SetText(custom.c_str());
                    w->InsertEndChild(ccEl);
                }
            parentXml->InsertEndChild(w);
                pXml->InsertEndChild(w);

            } else if (tag == "style") {
                // Already collected during preprocessing — skip
@@ -1020,7 +1027,7 @@ void blogi::webedit::WebEditor::htmlElementToWidgetXml(
                tinyxml2::XMLElement *htmlContent = doc.NewElement("HtmlContent");
                htmlContent->SetText(("<" + tag + ">").c_str());
                w->InsertEndChild(htmlContent);
            parentXml->InsertEndChild(w);
                pXml->InsertEndChild(w);

            } else if (tag == "meta" || tag == "link" || tag == "title" || tag == "base" ||
                       tag == "DOCTYPE" || tag == "!doctype") {
@@ -1038,11 +1045,17 @@ void blogi::webedit::WebEditor::htmlElementToWidgetXml(
                    cssEl->SetText(style.c_str());
                    w->InsertEndChild(cssEl);
                }
            parentXml->InsertEndChild(w);
                pXml->InsertEndChild(w);
            }

            cur = cur->nextElement();
        }

        // Push child frames in reverse so they are processed in document order (LIFO)
        for (auto it = childFrames.rbegin(); it != childFrames.rend(); ++it) {
            workStack.push(*it);
        }
    }
}

void blogi::webedit::WebEditor::importHtmlToWidgets(const std::string &html) {