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

test

parent 959cb943
Loading
Loading
Loading
Loading
+137 −27
Changes for src/edit.cpp: 137 added lines, 27 removed lines.
Original line number Diff line number Diff line
@@ -444,33 +444,105 @@ blogi::webedit::EditPlugin* blogi::webedit::WebEditor::deepCopySubtree(tinyxml2:
// 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> blogi::webedit::WebEditor::declarationToMap(
    const libhtmlpp::CSSDeclaration &decl)
{
    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;
        }
    for (const auto &prop : decl.getProperties()) {
        result[prop.getName()] = prop.getValue();
    }
    return result;
}

void blogi::webedit::WebEditor::mergeStylesheetRules(
    const libhtmlpp::CSSStyleSheet &sheet,
    const std::string &tag,
    const std::string &cssClass,
    const std::string &id,
    std::map<std::string,std::string> &props)
{
    // Split cssClass into individual classes
    std::vector<std::string> classes;
    if (!cssClass.empty()) {
        std::istringstream iss(cssClass);
        std::string cls;
        while (iss >> cls) {
            classes.push_back(cls);
        }
    }

    for (const auto &rule : sheet.getRules()) {
        const std::string &sel = rule.getSelector();

        // Skip @-rules
        if (!sel.empty() && sel[0] == '@') continue;

        // Handle comma-separated selectors
        std::istringstream selStream(sel);
        std::string singleSel;
        while (std::getline(selStream, singleSel, ',')) {
            // Trim
            size_t start = singleSel.find_first_not_of(" \t\n\r");
            size_t end = singleSel.find_last_not_of(" \t\n\r");
            if (start == std::string::npos) continue;
            singleSel = singleSel.substr(start, end - start + 1);

            bool matches = false;

            // Simple selector matching (no combinators, just tag, .class, #id)
            if (!singleSel.empty() && singleSel[0] == '#') {
                // ID selector
                if (!id.empty() && singleSel == "#" + id) matches = true;
            } else if (!singleSel.empty() && singleSel[0] == '.') {
                // Class selector
                std::string clsSel = singleSel.substr(1);
                for (const auto &c : classes) {
                    if (c == clsSel) { matches = true; break; }
                }
            } else {
                // Tag selector (case-insensitive)
                std::string selLower = singleSel;
                std::transform(selLower.begin(), selLower.end(), selLower.begin(),
                    [](unsigned char c) { return std::tolower(c); });
                if (selLower == tag) matches = true;

                // Tag.class selector (e.g., "div.header")
                auto dotPos = singleSel.find('.');
                if (!matches && dotPos != std::string::npos) {
                    std::string tagPart = singleSel.substr(0, dotPos);
                    std::string clsPart = singleSel.substr(dotPos + 1);
                    std::transform(tagPart.begin(), tagPart.end(), tagPart.begin(),
                        [](unsigned char c) { return std::tolower(c); });
                    if (tagPart == tag) {
                        for (const auto &c : classes) {
                            if (c == clsPart) { matches = true; break; }
                        }
                    }
                }

                // Tag#id selector (e.g., "div#main")
                auto hashPos = singleSel.find('#');
                if (!matches && hashPos != std::string::npos) {
                    std::string tagPart = singleSel.substr(0, hashPos);
                    std::string idPart = singleSel.substr(hashPos + 1);
                    std::transform(tagPart.begin(), tagPart.end(), tagPart.begin(),
                        [](unsigned char c) { return std::tolower(c); });
                    if (tagPart == tag && idPart == id) matches = true;
                }
            }

            if (matches) {
                // Stylesheet rules go first, inline styles override (already in props)
                for (const auto &prop : rule.getDeclaration().getProperties()) {
                    if (props.find(prop.getName()) == props.end()) {
                        props[prop.getName()] = prop.getValue();
                    }
                }
            }
        }
    }
}

std::string blogi::webedit::WebEditor::extractCustomCss(
    std::map<std::string,std::string> &props,
    const std::vector<std::string> &knownKeys)
@@ -507,7 +579,8 @@ std::string blogi::webedit::WebEditor::serializeInnerHtml(tinyxml2::XMLElement *
void blogi::webedit::WebEditor::htmlElementToWidgetXml(
    tinyxml2::XMLElement *htmlEl,
    tinyxml2::XMLDocument &doc,
    tinyxml2::XMLElement *parentXml)
    tinyxml2::XMLElement *parentXml,
    const libhtmlpp::CSSStyleSheet &sheet)
{
    if (!htmlEl) return;

@@ -546,12 +619,20 @@ void blogi::webedit::WebEditor::htmlElementToWidgetXml(
        std::string style;
        const char *styleAttr = child->Attribute("style");
        if (styleAttr) style = styleAttr;
        auto cssProps = parseInlineStyle(style);
        // Parse inline style using libhtmlpp CSS parser
        auto cssProps = declarationToMap(libhtmlpp::CSSStyleSheet::parseInlineStyle(style));

        std::string cssClass;
        const char *classAttr = child->Attribute("class");
        if (classAttr) cssClass = classAttr;

        std::string elemId;
        const char *idAttr = child->Attribute("id");
        if (idAttr) elemId = idAttr;

        // Merge matching stylesheet rules (stylesheet props go first, inline overrides)
        mergeStylesheetRules(sheet, tag, cssClass, elemId, cssProps);

        // ---- Map HTML tags to widget types ----

        if (tag == "section") {
@@ -578,7 +659,7 @@ void blogi::webedit::WebEditor::htmlElementToWidgetXml(

            parentXml->InsertEndChild(w);
            // Recurse into children
            htmlElementToWidgetXml(child, doc, w);
            htmlElementToWidgetXml(child, doc, w, sheet);

        } else if (tag == "div") {
            tinyxml2::XMLElement *w = doc.NewElement("Container");
@@ -623,7 +704,7 @@ void blogi::webedit::WebEditor::htmlElementToWidgetXml(
            }

            parentXml->InsertEndChild(w);
            htmlElementToWidgetXml(child, doc, w);
            htmlElementToWidgetXml(child, doc, w, sheet);

        } else if (tag == "img") {
            tinyxml2::XMLElement *w = doc.NewElement("Image");
@@ -935,7 +1016,36 @@ void blogi::webedit::WebEditor::importHtmlToWidgets(const std::string &html) {

    tinyxml2::XMLElement *importRoot = htmlDoc.RootElement();
    if (importRoot) {
        htmlElementToWidgetXml(importRoot, CurDoc, CurRootEl);
        // Collect all <style> element content and parse with libhtmlpp
        libhtmlpp::CSSStyleSheet stylesheet;
        for (auto *el = importRoot->FirstChildElement("style"); el;
             el = el->NextSiblingElement("style")) {
            const char *cssText = el->GetText();
            if (cssText && cssText[0] != '\0') {
                libhtmlpp::CSSStyleSheet partial;
                partial.parse(cssText);
                for (const auto &rule : partial.getRules()) {
                    stylesheet.addRule(rule);
                }
            }
        }
        // Also look for <style> inside <head>
        for (auto *head = importRoot->FirstChildElement("head"); head;
             head = head->NextSiblingElement("head")) {
            for (auto *el = head->FirstChildElement("style"); el;
                 el = el->NextSiblingElement("style")) {
                const char *cssText = el->GetText();
                if (cssText && cssText[0] != '\0') {
                    libhtmlpp::CSSStyleSheet partial;
                    partial.parse(cssText);
                    for (const auto &rule : partial.getRules()) {
                        stylesheet.addRule(rule);
                    }
                }
            }
        }

        htmlElementToWidgetXml(importRoot, CurDoc, CurRootEl, stylesheet);
    }

    // Load the generated widget XML into the live tree
+14 −3
Changes for src/edit.h: 14 added lines, 3 removed lines.
Original line number Diff line number Diff line
@@ -26,6 +26,7 @@
 *******************************************************************************/

#include <httppp/http.h>
#include <htmlpp/css.h>
#include <tinyxml2.h>
#include <json-c/json.h>
#include <map>
@@ -89,13 +90,23 @@ namespace blogi {

                // Import arbitrary HTML: convert DOM to widget XML, then load
                void importHtmlToWidgets(const std::string &html);

                // Look up matching CSS rules for an element and merge into inline props
                static void mergeStylesheetRules(const libhtmlpp::CSSStyleSheet &sheet,
                                                 const std::string &tag,
                                                 const std::string &cssClass,
                                                 const std::string &id,
                                                 std::map<std::string,std::string> &props);

                // Convert CSSDeclaration to property map
                static std::map<std::string,std::string> declarationToMap(const libhtmlpp::CSSDeclaration &decl);

                // Recursive helper: converts tinyxml2 HTML element tree to widget XML
                void htmlElementToWidgetXml(tinyxml2::XMLElement *htmlEl,
                                            tinyxml2::XMLDocument &doc,
                                            tinyxml2::XMLElement *parentXml);
                                            tinyxml2::XMLElement *parentXml,
                                            const libhtmlpp::CSSStyleSheet &sheet);

                // Helper: parse CSS style string into key-value map
                static std::map<std::string,std::string> parseInlineStyle(const std::string &style);
                // Helper: extract and remove known CSS props, return remainder as custom_css
                static std::string extractCustomCss(std::map<std::string,std::string> &props,
                                                    const std::vector<std::string> &knownKeys);