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/escape.cpp | |
download | ydb-1110808a9d39d4b808aef724c861a2e1a38d2a69.tar.gz |
intermediate changes
ref:cde9a383711a11544ce7e107a78147fb96cc4029
Diffstat (limited to 'library/cpp/html/escape/escape.cpp')
-rw-r--r-- | library/cpp/html/escape/escape.cpp | 66 |
1 files changed, 66 insertions, 0 deletions
diff --git a/library/cpp/html/escape/escape.cpp b/library/cpp/html/escape/escape.cpp new file mode 100644 index 00000000000..5b8ed60f045 --- /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); + } + +} |