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

test

parent e0abee58
Loading
Loading
Loading
Loading
+1 −1
Changes for editor/documentmanager.cpp: 1 added line, 1 removed line.
Original line number Diff line number Diff line
@@ -174,7 +174,7 @@ QString DocumentManager::renderToHtml() {
        }

        libhtmlpp::HtmlString htmlOut;
        libhtmlpp::print(&wrapper, htmlOut, true);
        libhtmlpp::print(wrapper, htmlOut, true);
        return QString::fromStdString(htmlOut.c_str());
    } catch (libhtmlpp::HTMLException &e) {
        return QString("<div style=\"color: red; border: 1px solid red; padding: 10px; margin: 10px; background: #ffeeee;\">"
+49 −2
Changes for editor/htmlimport.cpp: 49 added lines, 2 removed lines.
Original line number Diff line number Diff line
@@ -30,6 +30,8 @@
#include <algorithm>
#include <sstream>
#include <stack>
#include <regex>
#include <vector>

using namespace blogi::htmlimport;

@@ -716,6 +718,49 @@ void blogi::htmlimport::htmlElementToWidgetXml(
    }
}

static std::string sanitizeHtml(const std::string& input) {
    std::string html = input;
    html = std::regex_replace(html, std::regex{"<!DOCTYPE[^>]*>", std::regex_constants::icase}, "");

    std::regex tag_regex{"<(/?)(\\w+)[^>]*>"};
    std::sregex_iterator begin(html.begin(), html.end(), tag_regex);
    std::sregex_iterator end;

    std::vector<std::string> stack;
    std::vector<std::string> void_elements = {"area", "base", "br", "col", "embed", "hr", "img", "input", "link", "meta", "source", "track", "wbr", "!doctype"};
    
    for (auto i = begin; i != end; ++i) {
        std::smatch match = *i;
        bool is_closing = match[1].str() == "/";
        std::string tag_name = match[2].str();
        for (auto &c: tag_name) c = std::tolower(c);
        
        bool is_void = false;
        for (const auto& v : void_elements) {
            if (v == tag_name) { is_void = true; break; }
        }
        if (is_void) continue;
        
        if (is_closing) {
            if (!stack.empty() && stack.back() == tag_name) {
                stack.pop_back();
            } else {
                auto it = std::find(stack.rbegin(), stack.rend(), tag_name);
                if(it != stack.rend()) {
                    stack.erase((it+1).base(), stack.end());
                }
            }
        } else {
            stack.push_back(tag_name);
        }
    }
    
    for (auto it = stack.rbegin(); it != stack.rend(); ++it) {
        html += "</" + *it + ">";
    }
    return html;
}

bool blogi::htmlimport::convertHtmlToWidgetXml(const std::string &html,
                                               std::string &outXml,
                                               std::string &outGlobalCss)
@@ -730,14 +775,16 @@ bool blogi::htmlimport::convertHtmlToWidgetXml(const std::string &html,
    xmlRoot->SetAttribute("editor", "blogi");
    xmlDoc.InsertEndChild(xmlRoot);

    std::string safeHtml = sanitizeHtml(html);

    try {
        page.loadString(index, html);
        page.loadString(index, safeHtml);
        root = &index;
    } catch (...) {
        // Fallback: single CustomHtml widget with raw HTML
        tinyxml2::XMLElement *w = xmlDoc.NewElement("CustomHtml");
        tinyxml2::XMLElement *htmlContent = xmlDoc.NewElement("HtmlContent");
        htmlContent->SetText(html.c_str());
        htmlContent->SetText(safeHtml.c_str());
        w->InsertEndChild(htmlContent);
        xmlRoot->InsertEndChild(w);

+1 −1
Changes for editor/main.cpp: 1 added line, 1 removed line.
Original line number Diff line number Diff line
@@ -74,7 +74,7 @@ int main(int argc, char *argv[]) {
    if (pluginDir.isEmpty()) {
        #ifdef _WIN32
        pluginDir = QCoreApplication::applicationDirPath() + "/../lib/blogi/plugins/webedit";
        #elif
        #else
        pluginDir = WEBEDIT_PLUGIN_DIR;
        #endif
    }