From 2d1f0d09e62afb957abe54627846e64b0c8610f2 Mon Sep 17 00:00:00 2001 From: Andrew Kane Date: Sun, 13 Apr 2025 02:31:15 -0700 Subject: [PATCH] Make htmlescape function public (#300) Co-authored-by: Berscheid <1885260+pantor@users.noreply.github.com> --- include/inja/renderer.hpp | 35 +++++++++++++++++++---------------- single_include/inja/inja.hpp | 35 +++++++++++++++++++---------------- 2 files changed, 38 insertions(+), 32 deletions(-) diff --git a/include/inja/renderer.hpp b/include/inja/renderer.hpp index 0f2de46..069855b 100644 --- a/include/inja/renderer.hpp +++ b/include/inja/renderer.hpp @@ -25,6 +25,25 @@ namespace inja { +/*! +@brief Escapes HTML +*/ +inline std::string htmlescape(const std::string& data) { + std::string buffer; + buffer.reserve((unsigned int)(1.1 * data.size())); + for (size_t pos = 0; pos != data.size(); ++pos) { + switch (data[pos]) { + case '&': buffer.append("&"); break; + case '\"': buffer.append("""); break; + case '\'': buffer.append("'"); break; + case '<': buffer.append("<"); break; + case '>': buffer.append(">"); break; + default: buffer.append(&data[pos], 1); break; + } + } + return buffer; +} + /*! * \brief Class for rendering a Template with data. */ @@ -63,22 +82,6 @@ class Renderer : public NodeVisitor { return !data->empty(); } - static std::string htmlescape(const std::string& data) { - std::string buffer; - buffer.reserve((unsigned int)(1.1 * data.size())); - for (size_t pos = 0; pos != data.size(); ++pos) { - switch (data[pos]) { - case '&': buffer.append("&"); break; - case '\"': buffer.append("""); break; - case '\'': buffer.append("'"); break; - case '<': buffer.append("<"); break; - case '>': buffer.append(">"); break; - default: buffer.append(&data[pos], 1); break; - } - } - return buffer; - } - void print_data(const std::shared_ptr& value) { if (value->is_string()) { if (config.html_autoescape) { diff --git a/single_include/inja/inja.hpp b/single_include/inja/inja.hpp index c881c2f..27bf401 100644 --- a/single_include/inja/inja.hpp +++ b/single_include/inja/inja.hpp @@ -2132,6 +2132,25 @@ public: namespace inja { +/*! +@brief Escapes HTML +*/ +inline std::string htmlescape(const std::string& data) { + std::string buffer; + buffer.reserve((unsigned int)(1.1 * data.size())); + for (size_t pos = 0; pos != data.size(); ++pos) { + switch (data[pos]) { + case '&': buffer.append("&"); break; + case '\"': buffer.append("""); break; + case '\'': buffer.append("'"); break; + case '<': buffer.append("<"); break; + case '>': buffer.append(">"); break; + default: buffer.append(&data[pos], 1); break; + } + } + return buffer; +} + /*! * \brief Class for rendering a Template with data. */ @@ -2170,22 +2189,6 @@ class Renderer : public NodeVisitor { return !data->empty(); } - static std::string htmlescape(const std::string& data) { - std::string buffer; - buffer.reserve((unsigned int)(1.1 * data.size())); - for (size_t pos = 0; pos != data.size(); ++pos) { - switch (data[pos]) { - case '&': buffer.append("&"); break; - case '\"': buffer.append("""); break; - case '\'': buffer.append("'"); break; - case '<': buffer.append("<"); break; - case '>': buffer.append(">"); break; - default: buffer.append(&data[pos], 1); break; - } - } - return buffer; - } - void print_data(const std::shared_ptr& value) { if (value->is_string()) { if (config.html_autoescape) {