| | |
| | | } |
| | | |
| | | namespace htf{ |
| | | // режим экранирования B(basic) или E(extended) |
| | | // изменяется функцией set_html_escaping_mode |
| | | static bool html_escaping_mode_is_extended = true; |
| | | |
| | | void set_html_escaping_mode(bool mode){ |
| | | html_escaping_mode_is_extended = mode; |
| | | } |
| | | |
| | | unsigned int symbol_digit(char *txtptr){ |
| | | if(txtptr){ |
| | | int encoding = GetDatabaseEncoding(); |
| | |
| | | return result.data; |
| | | } |
| | | char * |
| | | escape_to_textU(text *input) |
| | | { |
| | | const char *data = VARDATA_ANY(input); |
| | | int len = VARSIZE_ANY_EXHDR(input); |
| | | StringInfoData result; |
| | | |
| | | /* Инициализация буфера для результата */ |
| | | initStringInfo(&result); |
| | | if (len == 0) |
| | | return result.data; |
| | | |
| | | int encoding = GetDatabaseEncoding(); |
| | | int byte_pos = 0; |
| | | |
| | | while (byte_pos < len) |
| | | { |
| | | int char_len = pg_encoding_mblen(encoding, data + byte_pos); |
| | | if (char_len <= 0 || byte_pos + char_len > len) |
| | | break; |
| | | |
| | | if (char_len == 1) |
| | | { |
| | | unsigned char c = data[byte_pos]; |
| | | /* Экранируем только небезопасные символы: ", %, &, ', /, <, >, \, ` */ |
| | | if (c == '"' || c == '%' || c == '&' || c == '\'' || c == '/' || |
| | | c == '<' || c == '>' || c == '\\' || c == '`') { |
| | | appendStringInfo(&result, "\\u%04X", c); |
| | | byte_pos += char_len; |
| | | continue; |
| | | } |
| | | /* Остальные однобайтовые символы оставляем как есть */ |
| | | appendStringInfoChar(&result, c); |
| | | byte_pos += char_len; |
| | | continue; |
| | | } |
| | | |
| | | /* Для многобайтовых символов — экранируем как \uXXXX */ |
| | | pg_wchar wc_buf[2]; |
| | | int conv = pg_mb2wchar_with_len(data + byte_pos, wc_buf, char_len); |
| | | if (conv > 0){ |
| | | unsigned int wc = wc_buf[0]; |
| | | /* Форматируем как \uXXXX с ведущими нулями до 4 знаков */ |
| | | appendStringInfo(&result, "\\u%04X", wc); |
| | | } |
| | | else |
| | | appendStringInfoChar(&result, '?'); |
| | | |
| | | byte_pos += char_len; |
| | | } |
| | | |
| | | return result.data; |
| | | } |
| | | char * |
| | | escape_to_css(text *input) |
| | | { |
| | | const char *data = VARDATA_ANY(input); |
| | | int len = VARSIZE_ANY_EXHDR(input); |
| | | StringInfoData result; |
| | | |
| | | /* Инициализация буфера для результата */ |
| | | initStringInfo(&result); |
| | | if (len == 0) |
| | | return result.data; |
| | | |
| | | int encoding = GetDatabaseEncoding(); |
| | | int byte_pos = 0; |
| | | |
| | | while (byte_pos < len) |
| | | { |
| | | int char_len = pg_encoding_mblen(encoding, data + byte_pos); |
| | | if (char_len <= 0 || byte_pos + char_len > len) |
| | | break; |
| | | |
| | | if (char_len == 1) |
| | | { |
| | | unsigned char c = data[byte_pos]; |
| | | /* Экранируем ' и \ */ |
| | | if (c == '\'') { |
| | | appendStringInfo(&result, "\\'"); |
| | | byte_pos += char_len; |
| | | continue; |
| | | } |
| | | if (c == '\\') { |
| | | appendStringInfo(&result, "\\\\"); |
| | | byte_pos += char_len; |
| | | continue; |
| | | } |
| | | |
| | | appendStringInfoChar(&result, c); |
| | | byte_pos += char_len; |
| | | continue; |
| | | } |
| | | |
| | | |
| | | pg_wchar wc_buf[2]; |
| | | int conv = pg_mb2wchar_with_len(data + byte_pos, wc_buf, char_len); |
| | | if (conv > 0){ |
| | | unsigned int wc = wc_buf[0]; |
| | | appendStringInfo(&result, "\\%X", wc); |
| | | } |
| | | else |
| | | appendStringInfoChar(&result, '?'); |
| | | |
| | | byte_pos += char_len; |
| | | } |
| | | |
| | | return result.data; |
| | | } |
| | | char * |
| | | escape_to_ltgt(text *input) |
| | | { |
| | | const char *data = VARDATA_ANY(input); |
| | |
| | | result = escape_to_numeric_entity(arg_txt); |
| | | break; |
| | | case StringEscapeFormatOracleFull: |
| | | result = escape_to_numeric_entity(arg_txt, true); |
| | | if(html_escaping_mode_is_extended) |
| | | result = escape_to_numeric_entity(arg_txt, true); |
| | | else |
| | | result = escape_special_chars(arg_txt); |
| | | break; |
| | | case StringEscapeFormatTextU: |
| | | result = escape_to_textU(arg_txt); |
| | | break; |
| | | case StringEscapeFormatCss: |
| | | result = escape_to_css(arg_txt); |
| | | break; |
| | | case StringEscapeFormatNone: |
| | | result = text_to_cstring(arg_txt); |
| | | break; |
| | | default: |
| | | result = escape_to_ltgt(arg_txt); |
| | | break; |
| | |
| | | destroyStringInfo(result); |
| | | return ret; |
| | | } |
| | | text *generate_tabledata_tag(const char *cvalue, const char *cattributes){ |
| | | text *generate_tabledata_short_tag(const char *cvalue, const char *cattributes){ |
| | | StringInfo result = makeStringInfo(); |
| | | appendStringInfo(result, "<td"); |
| | | |
| | |
| | | return ret; |
| | | } |
| | | |
| | | text *generate_mailto_tag(const char *chref, const char *ctext, const char *cname, const char *cattributes){ |
| | | StringInfo result = makeStringInfo(); |
| | | appendStringInfo(result, "<a"); |
| | | |
| | | appendStringInfo(result, " href=\"mailto:%s\"", chref ? chref : ""); |
| | | append_attribute_quot(result, "name", cname); |
| | | append_attribute_string(result, cattributes); |
| | | |
| | | appendStringInfo(result, ">%s</a>", ctext ? ctext : ""); |
| | | text *ret = cstring_to_text(result->data); |
| | | destroyStringInfo(result); |
| | | return ret; |
| | | } |
| | | |
| | | text *generate_meta_tag(const char *chttp_equiv, const char *cname, const char *ccontent) |
| | | { |
| | | StringInfo result = makeStringInfo(); |
| | | appendStringInfo(result, "<meta"); |
| | | |
| | | append_attribute_quot(result, "http-equiv", chttp_equiv, true); |
| | | append_attribute_quot(result, "name", cname, true); |
| | | append_attribute_quot(result, "content", ccontent, true); |
| | | |
| | | appendStringInfo(result, " />"); |
| | | text *ret = cstring_to_text(result->data); |
| | | destroyStringInfo(result); |
| | | return ret; |
| | | } |
| | | |
| | | text *generate_nextid_tag(const char *cidentifier) |
| | | { |
| | | StringInfo result = makeStringInfo(); |
| | | appendStringInfo(result, "<nextid n=\"%s>", cidentifier ? cidentifier : ""); |
| | | text *ret = cstring_to_text(result->data); |
| | | destroyStringInfo(result); |
| | | return ret; |
| | | } |
| | | |
| | | text *generate_nobr_tag(const char *ctext) |
| | | { |
| | | StringInfo result = makeStringInfo(); |
| | | appendStringInfo(result, "<nobr>%s</nobr>", ctext ? ctext : ""); |
| | | text *ret = cstring_to_text(result->data); |
| | | destroyStringInfo(result); |
| | | return ret; |
| | | } |
| | | |
| | | text *generate_olistopen_tag(const char *cclear, const char *cwrap, const char *cattributes) |
| | | { |
| | | StringInfo result = makeStringInfo(); |
| | | appendStringInfo(result, "<ol"); |
| | | |
| | | append_attribute_quot(result, "clear", cclear); |
| | | append_attribute_quot(result, "wrap", cwrap); |
| | | append_attribute_string(result, cattributes); |
| | | |
| | | |
| | | appendStringInfo(result, ">"); |
| | | |
| | | text *ret = cstring_to_text(result->data); |
| | | destroyStringInfo(result); |
| | | return ret; |
| | | } |
| | | |
| | | text *generate_paragraph_tag(const char *calign, const char *cclear, const char *cnowrap, const char *cattributes) |
| | | { |
| | | StringInfo result = makeStringInfo(); |
| | | appendStringInfo(result, "<p"); |
| | | |
| | | append_attribute_quot(result, "align", calign); |
| | | append_attribute_quot(result, "clear", cclear); |
| | | if(cnowrap){ |
| | | appendStringInfo(result, " nowrap=\"nowrap\""); |
| | | } |
| | | append_attribute_string(result, cattributes); |
| | | |
| | | appendStringInfo(result, ">"); |
| | | |
| | | text *ret = cstring_to_text(result->data); |
| | | destroyStringInfo(result); |
| | | return ret; |
| | | } |
| | | |
| | | text *generate_param_tag(const char *cname, const char *cvalue) |
| | | { |
| | | StringInfo result = makeStringInfo(); |
| | | appendStringInfo(result, "<param"); |
| | | |
| | | append_attribute(result, "name", cname, true); |
| | | append_attribute_quot(result, "value", cvalue, true); |
| | | |
| | | appendStringInfo(result, " >"); |
| | | |
| | | text *ret = cstring_to_text(result->data); |
| | | destroyStringInfo(result); |
| | | return ret; |
| | | } |
| | | |
| | | text *generate_plaintext_tag(const char *cvalue, const char *cattributes){ |
| | | StringInfo result = makeStringInfo(); |
| | | appendStringInfo(result, "<plaintext"); |
| | | |
| | | append_attribute_string(result, cattributes); |
| | | appendStringInfo(result, ">%s</plaintext>", cvalue ? cvalue : ""); |
| | | |
| | | text *ret = cstring_to_text(result->data); |
| | | destroyStringInfo(result); |
| | | return ret; |
| | | } |
| | | |
| | | text *generate_preopen_tag(const char *cclear, const char *cwidth, const char *cattributes){ |
| | | StringInfo result = makeStringInfo(); |
| | | appendStringInfo(result, "<pre"); |
| | | |
| | | append_attribute_quot(result, "clear", cclear); |
| | | append_attribute_quot(result, "width", cwidth); |
| | | append_attribute_string(result, cattributes); |
| | | |
| | | appendStringInfo(result, ">"); |
| | | |
| | | text *ret = cstring_to_text(result->data); |
| | | destroyStringInfo(result); |
| | | return ret; |
| | | } |
| | | |
| | | |
| | | text *generate_s_tag(const char *ctext, const char *cattributes){ |
| | | StringInfo result = makeStringInfo(); |
| | | appendStringInfo(result, "<s"); |
| | | |
| | | append_attribute_string(result, cattributes); |
| | | appendStringInfo(result, ">%s</s>", ctext ? ctext : ""); |
| | | |
| | | text *ret = cstring_to_text(result->data); |
| | | destroyStringInfo(result); |
| | | return ret; |
| | | } |
| | | |
| | | text *generate_samp_tag(const char *ctext, const char *cattributes){ |
| | | StringInfo result = makeStringInfo(); |
| | | appendStringInfo(result, "<samp"); |
| | | |
| | | append_attribute_string(result, cattributes); |
| | | |
| | | appendStringInfo(result, ">%s</samp>", ctext ? ctext : ""); |
| | | |
| | | text *ret = cstring_to_text(result->data); |
| | | destroyStringInfo(result); |
| | | return ret; |
| | | } |
| | | |
| | | text *generate_script_tag(const char *cscript, const char *clanguage){ |
| | | StringInfo result = makeStringInfo(); |
| | | appendStringInfo(result, "<script"); |
| | | |
| | | if(clanguage) |
| | | appendStringInfo(result, " language='%s'", clanguage); |
| | | |
| | | appendStringInfo(result, ">'%s'</script>", cscript ? cscript : ""); |
| | | |
| | | text *ret = cstring_to_text(result->data); |
| | | destroyStringInfo(result); |
| | | return ret; |
| | | } |
| | | |
| | | text *generate_small_tag(const char *ctext, const char *cattributes){ |
| | | StringInfo result = makeStringInfo(); |
| | | appendStringInfo(result, "<small"); |
| | | |
| | | append_attribute_string(result, cattributes); |
| | | appendStringInfo(result, ">%s</small>", ctext ? ctext : ""); |
| | | |
| | | text *ret = cstring_to_text(result->data); |
| | | destroyStringInfo(result); |
| | | return ret; |
| | | } |
| | | |
| | | text *generate_strike_tag(const char *ctext, const char *cattributes){ |
| | | StringInfo result = makeStringInfo(); |
| | | appendStringInfo(result, "<strike"); |
| | | |
| | | append_attribute_string(result, cattributes); |
| | | appendStringInfo(result, ">%s</strike>", ctext ? ctext : ""); |
| | | |
| | | text *ret = cstring_to_text(result->data); |
| | | destroyStringInfo(result); |
| | | return ret; |
| | | } |
| | | |
| | | text *generate_strong_tag(const char *ctext, const char *cattributes){ |
| | | StringInfo result = makeStringInfo(); |
| | | appendStringInfo(result, "<strong"); |
| | | |
| | | append_attribute_string(result, cattributes); |
| | | appendStringInfo(result, ">%s</strong>", ctext ? ctext : ""); |
| | | |
| | | text *ret = cstring_to_text(result->data); |
| | | destroyStringInfo(result); |
| | | return ret; |
| | | } |
| | | |
| | | text *generate_style_tag(const char *ctext){ |
| | | StringInfo result = makeStringInfo(); |
| | | |
| | | appendStringInfo(result, "<style>%s</style>", ctext ? ctext : ""); |
| | | |
| | | text *ret = cstring_to_text(result->data); |
| | | destroyStringInfo(result); |
| | | return ret; |
| | | } |
| | | |
| | | text *generate_sub_tag(const char *ctext, const char *calign, const char *cattributes){ |
| | | StringInfo result = makeStringInfo(); |
| | | appendStringInfo(result, "<sub"); |
| | | |
| | | append_attribute_quot(result, "align", calign); |
| | | append_attribute_string(result, cattributes); |
| | | |
| | | appendStringInfo(result, ">%s</sub>", ctext ? ctext : ""); |
| | | text *ret = cstring_to_text(result->data); |
| | | destroyStringInfo(result); |
| | | return ret; |
| | | } |
| | | |
| | | text *generate_sup_tag(const char *ctext, const char *calign, const char *cattributes){ |
| | | StringInfo result = makeStringInfo(); |
| | | appendStringInfo(result, "<sup"); |
| | | |
| | | append_attribute_quot(result, "align", calign); |
| | | append_attribute_string(result, cattributes); |
| | | |
| | | appendStringInfo(result, ">%s</sup>", ctext ? ctext : ""); |
| | | text *ret = cstring_to_text(result->data); |
| | | destroyStringInfo(result); |
| | | return ret; |
| | | } |
| | | |
| | | text *generate_tablecaption_tag(const char *ctext, const char *calign, const char *cattributes){ |
| | | StringInfo result = makeStringInfo(); |
| | | appendStringInfo(result, "<caption"); |
| | | |
| | | append_attribute_quot(result, "align", calign); |
| | | append_attribute_string(result, cattributes); |
| | | |
| | | appendStringInfo(result, ">%s</caption>", ctext ? ctext : ""); |
| | | text *ret = cstring_to_text(result->data); |
| | | destroyStringInfo(result); |
| | | return ret; |
| | | } |
| | | |
| | | text *generate_tabledata_tag(const char *cvalue, const char *calign, const char *cdp, const char *cnowrap, const char *crowspan, const char *ccolspan, const char *cattributes){ |
| | | StringInfo result = makeStringInfo(); |
| | | appendStringInfo(result, "<td"); |
| | | |
| | | append_attribute_quot(result, "align", calign); |
| | | append_attribute_quot(result, "dp", cdp); |
| | | append_attribute_quot(result, "rowspan", crowspan); |
| | | append_attribute_quot(result, "colspan", ccolspan); |
| | | if(cnowrap){ |
| | | appendStringInfo(result, " nowrap=\"nowrap\""); |
| | | } |
| | | append_attribute_string(result, cattributes); |
| | | |
| | | appendStringInfo(result, ">%s</td>", cvalue ? cvalue : ""); |
| | | text *ret = cstring_to_text(result->data); |
| | | destroyStringInfo(result); |
| | | return ret; |
| | | } |
| | | |
| | | text *generate_tableheader_tag(const char *cvalue, const char *calign, const char *cdp, const char *cnowrap, const char *crowspan, const char *ccolspan, const char *cattributes){ |
| | | StringInfo result = makeStringInfo(); |
| | | appendStringInfo(result, "<th"); |
| | | |
| | | append_attribute_quot(result, "align", calign); |
| | | append_attribute_quot(result, "dp", cdp); |
| | | append_attribute_quot(result, "rowspan", crowspan); |
| | | append_attribute_quot(result, "colspan", ccolspan); |
| | | if(cnowrap){ |
| | | appendStringInfo(result, " nowrap=\"nowrap\""); |
| | | } |
| | | append_attribute_string(result, cattributes); |
| | | |
| | | appendStringInfo(result, ">%s</th>", cvalue ? cvalue : ""); |
| | | text *ret = cstring_to_text(result->data); |
| | | destroyStringInfo(result); |
| | | return ret; |
| | | } |
| | | |
| | | text *generate_table_tag(const char *ctext, const char *cnowrap, const char *calign, const char *cclear, const char *cattributes){ |
| | | StringInfo result = makeStringInfo(); |
| | | appendStringInfo(result, "<table"); |
| | | |
| | | append_attribute_string(result, ctext); |
| | | if(cnowrap){ |
| | | appendStringInfo(result, " nowrap=\"nowrap\""); |
| | | } |
| | | append_attribute_quot(result, "align", calign); |
| | | append_attribute_quot(result, "clear", cclear); |
| | | append_attribute_string(result, cattributes); |
| | | |
| | | appendStringInfo(result, ">"); |
| | | text *ret = cstring_to_text(result->data); |
| | | destroyStringInfo(result); |
| | | return ret; |
| | | } |
| | | |
| | | text *generate_tablerow_tag(const char *calign, const char *cvalign, const char *cdp, const char *cnowrap, const char *cattributes){ |
| | | StringInfo result = makeStringInfo(); |
| | | appendStringInfo(result, "<tr"); |
| | | |
| | | append_attribute_quot(result, "align", calign); |
| | | append_attribute_quot(result, "valign", cvalign); |
| | | append_attribute_quot(result, "dp", cdp); |
| | | if(cnowrap){ |
| | | appendStringInfo(result, " nowrap=\"nowrap\""); |
| | | } |
| | | append_attribute_string(result, cattributes); |
| | | |
| | | appendStringInfo(result, ">"); |
| | | text *ret = cstring_to_text(result->data); |
| | | destroyStringInfo(result); |
| | | return ret; |
| | | } |
| | | |
| | | text *generate_teletype_tag(const char *ctext, const char *cclass){ |
| | | StringInfo result = makeStringInfo(); |
| | | appendStringInfo(result, "<tt"); |
| | | |
| | | append_attribute_string(result, cclass); |
| | | appendStringInfo(result, ">%s</tt>", ctext ? ctext : ""); |
| | | |
| | | text *ret = cstring_to_text(result->data); |
| | | destroyStringInfo(result); |
| | | return ret; |
| | | } |
| | | text *generate_title_tag(const char *ctitle){ |
| | | StringInfo result = makeStringInfo(); |
| | | |
| | | appendStringInfo(result, "<title>%s</title>", ctitle ? ctitle : ""); |
| | | |
| | | text *ret = cstring_to_text(result->data); |
| | | destroyStringInfo(result); |
| | | return ret; |
| | | } |
| | | |
| | | text *generate_ulistopen_tag(const char *cclear, const char *cwrap, const char *cdingbat, const char *csrc, const char *cattributes) |
| | | { |
| | | StringInfo result = makeStringInfo(); |
| | | appendStringInfo(result, "<ul"); |
| | | |
| | | append_attribute_quot(result, "clear", cclear); |
| | | append_attribute_quot(result, "wrap", cwrap); |
| | | append_attribute_quot(result, "dingbat", cdingbat); |
| | | append_attribute_quot(result, "src", csrc); |
| | | append_attribute_string(result, cattributes); |
| | | |
| | | appendStringInfo(result, ">"); |
| | | |
| | | text *ret = cstring_to_text(result->data); |
| | | destroyStringInfo(result); |
| | | return ret; |
| | | } |
| | | |
| | | text *generate_underline_tag(const char *ctext, const char *cattributes) |
| | | { |
| | | StringInfo result = makeStringInfo(); |
| | | appendStringInfo(result, "<u"); |
| | | append_attribute_string(result, cattributes); |
| | | appendStringInfo(result, ">%s</u>", ctext ? ctext : ""); |
| | | |
| | | text *ret = cstring_to_text(result->data); |
| | | destroyStringInfo(result); |
| | | return ret; |
| | | } |
| | | |
| | | text *generate_variable_tag(const char *ctext, const char *cattr) |
| | | { |
| | | StringInfo result = makeStringInfo(); |
| | | appendStringInfo(result, "<var"); |
| | | append_attribute_string(result, cattr); |
| | | appendStringInfo(result, ">%s</var>", ctext ? ctext : ""); |
| | | |
| | | text *ret = cstring_to_text(result->data); |
| | | destroyStringInfo(result); |
| | | return ret; |
| | | } |
| | | |
| | | } // namespace htf |
| | | extern "C" |
| | | { |