Commit 241f2cc9 authored by jan.koester's avatar jan.koester
Browse files

tet

parent b1037f14
Loading
Loading
Loading
Loading
+32 −1
Changes for client/widgets/textbox/textbox.cpp: 32 added lines, 1 removed line.
Original line number Diff line number Diff line
@@ -517,8 +517,39 @@ extern "C" {
                if (!content.empty()) {
                    // Content contains HTML markup from the rich text editor;
                    // parse it into an element tree and append to the box.
                    // Auto-close any unclosed block-level tags before parsing.
                    std::string safeContent = content;
                    {
                        static const char* blockTags[] = {
                            "h1","h2","h3","h4","h5","h6",
                            "p","blockquote","pre","div","ul","ol","li",
                            "strong","em","b","i","a","span","code",nullptr
                        };
                        for (int t = 0; blockTags[t]; ++t) {
                            std::string tag = blockTags[t];
                            std::string openTag = "<" + tag;
                            std::string closeTag = "</" + tag + ">";
                            // Count opens (including with attributes like <h1 class="x">)
                            int opens = 0, closes = 0;
                            size_t pos = 0;
                            while ((pos = safeContent.find(openTag, pos)) != std::string::npos) {
                                size_t after = pos + openTag.size();
                                if (after < safeContent.size() &&
                                    (safeContent[after] == '>' || safeContent[after] == ' '))
                                    ++opens;
                                pos = after;
                            }
                            pos = 0;
                            while ((pos = safeContent.find(closeTag, pos)) != std::string::npos) {
                                ++closes;
                                pos += closeTag.size();
                            }
                            for (int i = closes; i < opens; ++i)
                                safeContent += closeTag;
                        }
                    }
                    try {
                        libhtmlpp::HtmlString htmlBuf("<div>" + content + "</div>");
                        libhtmlpp::HtmlString htmlBuf("<div>" + safeContent + "</div>");
                        libhtmlpp::Element &parsed = htmlBuf.parse();
                        box.appendChild(parsed);
                    } catch (...) {