123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566 |
- #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);
- }
- }
|