diff options
author | Devtools Arcadia <arcadia-devtools@yandex-team.ru> | 2022-02-07 18:08:42 +0300 |
---|---|---|
committer | Devtools Arcadia <arcadia-devtools@mous.vla.yp-c.yandex.net> | 2022-02-07 18:08:42 +0300 |
commit | 1110808a9d39d4b808aef724c861a2e1a38d2a69 (patch) | |
tree | e26c9fed0de5d9873cce7e00bc214573dc2195b7 /library/cpp/html/escape | |
download | ydb-1110808a9d39d4b808aef724c861a2e1a38d2a69.tar.gz |
intermediate changes
ref:cde9a383711a11544ce7e107a78147fb96cc4029
Diffstat (limited to 'library/cpp/html/escape')
-rw-r--r-- | library/cpp/html/escape/escape.cpp | 66 | ||||
-rw-r--r-- | library/cpp/html/escape/escape.h | 9 | ||||
-rw-r--r-- | library/cpp/html/escape/ut/escape_ut.cpp | 16 | ||||
-rw-r--r-- | library/cpp/html/escape/ut/ya.make | 13 | ||||
-rw-r--r-- | library/cpp/html/escape/ya.make | 9 |
5 files changed, 113 insertions, 0 deletions
diff --git a/library/cpp/html/escape/escape.cpp b/library/cpp/html/escape/escape.cpp new file mode 100644 index 0000000000..5b8ed60f04 --- /dev/null +++ b/library/cpp/html/escape/escape.cpp @@ -0,0 +1,66 @@ +#include "escape.h" + +#include <util/generic/array_size.h> +#include <util/generic/strbuf.h> + +namespace NHtml { + namespace { + struct TReplace { + char Char; + bool ForText; + TStringBuf Entity; + }; + + TReplace Escapable[] = { + {'"', false, TStringBuf(""")}, + {'&', true, TStringBuf("&")}, + {'<', true, TStringBuf("<")}, + {'>', true, TStringBuf(">")}, + }; + + TString EscapeImpl(const TString& value, bool isText) { + auto ci = value.begin(); + // Looking for escapable characters. + for (; ci != value.end(); ++ci) { + for (size_t i = (isText ? 1 : 0); i < Y_ARRAY_SIZE(Escapable); ++i) { + if (*ci == Escapable[i].Char) { + goto escape; + } + } + } + + // There is no escapable characters, so return original value. + return value; + + escape: + TString tmp = TString(value.begin(), ci); + + for (; ci != value.end(); ++ci) { + size_t i = (isText ? 1 : 0); + + for (; i < Y_ARRAY_SIZE(Escapable); ++i) { + if (*ci == Escapable[i].Char) { + tmp += Escapable[i].Entity; + break; + } + } + + if (i == Y_ARRAY_SIZE(Escapable)) { + tmp += *ci; + } + } + + return tmp; + } + + } + + TString EscapeAttributeValue(const TString& value) { + return EscapeImpl(value, false); + } + + TString EscapeText(const TString& value) { + return EscapeImpl(value, true); + } + +} diff --git a/library/cpp/html/escape/escape.h b/library/cpp/html/escape/escape.h new file mode 100644 index 0000000000..1c45fc5193 --- /dev/null +++ b/library/cpp/html/escape/escape.h @@ -0,0 +1,9 @@ +#pragma once + +#include <util/generic/string.h> + +namespace NHtml { + TString EscapeAttributeValue(const TString& value); + TString EscapeText(const TString& value); + +} diff --git a/library/cpp/html/escape/ut/escape_ut.cpp b/library/cpp/html/escape/ut/escape_ut.cpp new file mode 100644 index 0000000000..cd7b955138 --- /dev/null +++ b/library/cpp/html/escape/ut/escape_ut.cpp @@ -0,0 +1,16 @@ +#include <library/cpp/html/escape/escape.h> +#include <library/cpp/testing/unittest/registar.h> + +using namespace NHtml; + +Y_UNIT_TEST_SUITE(TEscapeHtml) { + Y_UNIT_TEST(Escape) { + UNIT_ASSERT_EQUAL(EscapeText("in & out"), "in & out"); + UNIT_ASSERT_EQUAL(EscapeText("&&"), "&&"); + UNIT_ASSERT_EQUAL(EscapeText("&"), "&amp;"); + + UNIT_ASSERT_EQUAL(EscapeText("<script>"), "<script>"); + + UNIT_ASSERT_EQUAL(EscapeText("text"), "text"); + } +} diff --git a/library/cpp/html/escape/ut/ya.make b/library/cpp/html/escape/ut/ya.make new file mode 100644 index 0000000000..27d3a3d65a --- /dev/null +++ b/library/cpp/html/escape/ut/ya.make @@ -0,0 +1,13 @@ +UNITTEST() + +OWNER(stanly) + +SRCS( + escape_ut.cpp +) + +PEERDIR( + library/cpp/html/escape +) + +END() diff --git a/library/cpp/html/escape/ya.make b/library/cpp/html/escape/ya.make new file mode 100644 index 0000000000..93a31e33d6 --- /dev/null +++ b/library/cpp/html/escape/ya.make @@ -0,0 +1,9 @@ +LIBRARY() + +OWNER(stanly) + +SRCS( + escape.cpp +) + +END() |