Commit 10970d08 authored by jan.koester's avatar jan.koester
Browse files

test

parent f5ff4f62
Loading
Loading
Loading
Loading
+138 −17
Changes for src/edit.cpp: 138 added lines, 17 removed lines.
Original line number Diff line number Diff line
@@ -464,7 +464,8 @@ void blogi::webedit::WebEditor::mergeStylesheetRules(
    const std::string &tag,
    const std::string &cssClass,
    const std::string &id,
    std::map<std::string,std::string> &props)
    std::map<std::string,std::string> &props,
    std::string &mediaRules)
{
    // Split cssClass into individual classes
    std::vector<std::string> classes;
@@ -478,12 +479,47 @@ void blogi::webedit::WebEditor::mergeStylesheetRules(

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

        // For @-rules (e.g. "@media (max-width: 1024px) .cards"), split into
        // the @-wrapper and the inner selector, then match the inner selector.
        bool isAtRule = (!sel.empty() && sel[0] == '@');
        std::string atWrapper;   // e.g. "@media (max-width: 1024px)"
        std::string innerSel;    // e.g. ".cards"

        if (isAtRule) {
            // libhtmlpp flattens nested @-rules as: "@media (...) <inner-selector>"
            // Find the closing ')' of the @-rule condition, then take the rest as inner selector.
            size_t parenDepth = 0;
            size_t splitPos = std::string::npos;
            for (size_t i = 0; i < sel.size(); ++i) {
                if (sel[i] == '(') ++parenDepth;
                else if (sel[i] == ')') {
                    if (parenDepth > 0) --parenDepth;
                    if (parenDepth == 0) {
                        splitPos = i + 1;
                        break;
                    }
                }
            }
            if (splitPos != std::string::npos && splitPos < sel.size()) {
                atWrapper = sel.substr(0, splitPos);
                // Trim leading whitespace from inner selector
                size_t innerStart = sel.find_first_not_of(" \t\n\r", splitPos);
                if (innerStart != std::string::npos) {
                    innerSel = sel.substr(innerStart);
                }
            }
            // If no inner selector could be extracted (e.g. @font-face, @keyframes),
            // skip — these are global rules handled separately in importHtmlToWidgets.
            if (innerSel.empty()) continue;
        }

        // Skip @-rules
        if (!sel.empty() && sel[0] == '@') continue;
        // The selector to match: either the full selector or the inner part of an @-rule
        const std::string &matchTarget = isAtRule ? innerSel : sel;

        // Handle comma-separated selectors
        std::istringstream selStream(sel);
        std::istringstream selStream(matchTarget);
        std::string singleSel;
        while (std::getline(selStream, singleSel, ',')) {
            // Trim
@@ -551,6 +587,15 @@ void blogi::webedit::WebEditor::mergeStylesheetRules(
            }

            if (matches) {
                if (isAtRule) {
                    // Wrap matched properties in the @-rule block for CustomCss
                    std::string block = atWrapper + " { " + singleSel + " { ";
                    for (const auto &prop : rule.getDeclaration().getProperties()) {
                        block += prop.getName() + ": " + prop.getValue() + "; ";
                    }
                    block += "} } ";
                    mediaRules += block;
                } else {
                    // Stylesheet rules go first, inline styles override (already in props)
                    for (const auto &prop : rule.getDeclaration().getProperties()) {
                        if (props.find(prop.getName()) == props.end()) {
@@ -561,10 +606,12 @@ void blogi::webedit::WebEditor::mergeStylesheetRules(
            }
        }
    }
}

std::string blogi::webedit::WebEditor::extractCustomCss(
    std::map<std::string,std::string> &props,
    const std::vector<std::string> &knownKeys)
    const std::vector<std::string> &knownKeys,
    const std::string &mediaRules)
{
    std::string customCss;
    std::vector<std::string> toRemove;
@@ -580,6 +627,10 @@ std::string blogi::webedit::WebEditor::extractCustomCss(
        }
    }
    for (auto &k : toRemove) props.erase(k);
    if (!mediaRules.empty()) {
        if (!customCss.empty()) customCss += " ";
        customCss += mediaRules;
    }
    return customCss;
}

@@ -716,8 +767,9 @@ void blogi::webedit::WebEditor::htmlElementToWidgetXml(
            std::string cssClass = el->getAtributte("class");
            std::string elemId = el->getAtributte("id");

            // Merge matching stylesheet rules
            mergeStylesheetRules(sheet, tag, cssClass, elemId, cssProps);
            // Merge matching stylesheet rules (including @media → mediaRules)
            std::string mediaRules;
            mergeStylesheetRules(sheet, tag, cssClass, elemId, cssProps, mediaRules);

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

@@ -736,7 +788,7 @@ void blogi::webedit::WebEditor::htmlElementToWidgetXml(

                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);
                std::string custom = extractCustomCss(cssProps, knownCss, mediaRules);
                if (!custom.empty()) {
                    tinyxml2::XMLElement *ccEl = doc.NewElement("CustomCss");
                    ccEl->SetText(custom.c_str());
@@ -761,7 +813,7 @@ void blogi::webedit::WebEditor::htmlElementToWidgetXml(

                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);
                std::string custom = extractCustomCss(cssProps, knownCss, mediaRules);
                if (!custom.empty()) {
                    tinyxml2::XMLElement *ccEl = doc.NewElement("CustomCss");
                    ccEl->SetText(custom.c_str());
@@ -791,7 +843,7 @@ void blogi::webedit::WebEditor::htmlElementToWidgetXml(
                    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);
                    std::string custom = extractCustomCss(cssProps, knownCss, mediaRules);
                    if (!custom.empty()) {
                        tinyxml2::XMLElement *ccEl = doc.NewElement("CustomCss");
                        ccEl->SetText(custom.c_str());
@@ -800,7 +852,7 @@ void blogi::webedit::WebEditor::htmlElementToWidgetXml(
                } 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);
                    std::string custom = extractCustomCss(cssProps, knownCss, mediaRules);
                    if (!custom.empty()) {
                        tinyxml2::XMLElement *ccEl = doc.NewElement("CustomCss");
                        ccEl->SetText(custom.c_str());
@@ -827,7 +879,7 @@ void blogi::webedit::WebEditor::htmlElementToWidgetXml(
                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);
                std::string custom = extractCustomCss(cssProps, knownCss, mediaRules);
                if (!custom.empty()) {
                    tinyxml2::XMLElement *ccEl = doc.NewElement("CustomCss");
                    ccEl->SetText(custom.c_str());
@@ -844,7 +896,7 @@ void blogi::webedit::WebEditor::htmlElementToWidgetXml(
                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);
                std::string custom = extractCustomCss(cssProps, knownCss, mediaRules);
                if (!custom.empty()) {
                    tinyxml2::XMLElement *ccEl = doc.NewElement("CustomCss");
                    ccEl->SetText(custom.c_str());
@@ -945,7 +997,7 @@ void blogi::webedit::WebEditor::htmlElementToWidgetXml(
                }

                std::vector<std::string> knownCss = {"border","background-color","border-collapse","width"};
                std::string custom = extractCustomCss(cssProps, knownCss);
                std::string custom = extractCustomCss(cssProps, knownCss, mediaRules);
                if (!custom.empty()) {
                    tinyxml2::XMLElement *ccEl = doc.NewElement("CustomCss");
                    ccEl->SetText(custom.c_str());
@@ -983,7 +1035,7 @@ void blogi::webedit::WebEditor::htmlElementToWidgetXml(

                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);
                std::string custom = extractCustomCss(cssProps, knownCss, mediaRules);
                if (!custom.empty()) {
                    tinyxml2::XMLElement *ccEl = doc.NewElement("CustomCss");
                    ccEl->SetText(custom.c_str());
@@ -1018,7 +1070,7 @@ void blogi::webedit::WebEditor::htmlElementToWidgetXml(

                    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);
                    std::string custom = extractCustomCss(cssProps, knownCss, mediaRules);
                    if (!custom.empty()) {
                        tinyxml2::XMLElement *ccEl = doc.NewElement("CustomCss");
                        ccEl->SetText(custom.c_str());
@@ -1058,7 +1110,7 @@ void blogi::webedit::WebEditor::htmlElementToWidgetXml(

                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);
                std::string custom = extractCustomCss(cssProps, knownCss, mediaRules);
                if (!custom.empty()) {
                    tinyxml2::XMLElement *ccEl = doc.NewElement("CustomCss");
                    ccEl->SetText(custom.c_str());
@@ -1181,6 +1233,75 @@ void blogi::webedit::WebEditor::importHtmlToWidgets(const std::string &html) {
    CurRootEl->SetAttribute("Version", "1.0");
    CurDoc.InsertEndChild(CurRootEl);

    // Emit global CSS rules that can't be matched to individual elements:
    // :root (CSS variables), @keyframes, @font-face, *, html, body,
    // ::selection, ::-webkit-scrollbar, and other pseudo-elements.
    std::string globalCss;
    for (const auto &rule : stylesheet.getRules()) {
        const std::string &sel = rule.getSelector();
        if (sel.empty()) continue;

        bool isGlobal = false;

        if (sel[0] == '@') {
            // @media with inner selectors are handled per-element in mergeStylesheetRules.
            // Everything else (@keyframes, @font-face, @import, @charset, bare @media
            // with :root/::-scrollbar, etc.) is global.
            // Check if it has parentheses with an inner selector — those go per-element.
            size_t parenClose = std::string::npos;
            size_t parenDepth = 0;
            for (size_t i = 0; i < sel.size(); ++i) {
                if (sel[i] == '(') ++parenDepth;
                else if (sel[i] == ')') {
                    if (parenDepth > 0) --parenDepth;
                    if (parenDepth == 0) { parenClose = i; break; }
                }
            }
            if (parenClose != std::string::npos && parenClose + 1 < sel.size()) {
                // Has inner selector (e.g. "@media (...) .class") → per-element, skip
                // unless the inner selector is :root or other global pseudo
                size_t innerStart = sel.find_first_not_of(" \t\n\r", parenClose + 1);
                if (innerStart != std::string::npos) {
                    std::string inner = sel.substr(innerStart);
                    std::string innerLower = inner;
                    std::transform(innerLower.begin(), innerLower.end(), innerLower.begin(),
                        [](unsigned char c) { return std::tolower(c); });
                    if (innerLower.compare(0, 5, ":root") == 0 ||
                        innerLower.find("::") != std::string::npos ||
                        innerLower == "*" || innerLower == "html" || innerLower == "body") {
                        isGlobal = true;
                    }
                }
            } else {
                // No inner selector: @keyframes, @font-face, @import, @charset, etc.
                isGlobal = true;
            }
        } else {
            std::string selLower = sel;
            std::transform(selLower.begin(), selLower.end(), selLower.begin(),
                [](unsigned char c) { return std::tolower(c); });
            if (selLower == ":root" || selLower == "*" || selLower == "html" ||
                selLower == "body" || selLower.compare(0, 5, ":root") == 0 ||
                selLower.find("::") != std::string::npos) {
                isGlobal = true;
            }
        }

        if (isGlobal) {
            globalCss += rule.serialize(true);
            globalCss += "\n";
        }
    }

    if (!globalCss.empty()) {
        tinyxml2::XMLElement *w = CurDoc.NewElement("CustomHtml");
        tinyxml2::XMLElement *htmlContent = CurDoc.NewElement("HtmlContent");
        std::string styleBlock = "<style>\n" + globalCss + "</style>";
        htmlContent->SetText(styleBlock.c_str());
        w->InsertEndChild(htmlContent);
        CurRootEl->InsertEndChild(w);
    }

    if (root) {
        htmlElementToWidgetXml(root, CurDoc, CurRootEl, stylesheet);
    }
+6 −3
Changes for src/edit.h: 6 added lines, 3 removed lines.
Original line number Diff line number Diff line
@@ -91,12 +91,14 @@ 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
                // Look up matching CSS rules for an element and merge into inline props.
                // @media rules whose inner selector matches are collected in mediaRules.
                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);
                                                 std::map<std::string,std::string> &props,
                                                 std::string &mediaRules);

                // Convert CSSDeclaration to property map
                static std::map<std::string,std::string> declarationToMap(const libhtmlpp::CSSDeclaration &decl);
@@ -109,7 +111,8 @@ namespace blogi {

                // 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);
                                                    const std::vector<std::string> &knownKeys,
                                                    const std::string &mediaRules = "");
                // Helper: serialize libhtmlpp element children to HTML string
                static std::string serializeInnerHtml(libhtmlpp::HtmlElement *el);
                // Helper: serialize a single libhtmlpp element (outer HTML) to string