NCBI C++ ToolKit
htmlhelper.hpp
Go to the documentation of this file.

Go to the SVN repository for this file.

1 #ifndef HTML___HTMLHELPER__HPP
2 #define HTML___HTMLHELPER__HPP
3 
4 /* $Id: htmlhelper.hpp 72570 2016-05-16 15:12:46Z ivanov $
5  * ===========================================================================
6  *
7  * PUBLIC DOMAIN NOTICE
8  * National Center for Biotechnology Information
9  *
10  * This software/database is a "United States Government Work" under the
11  * terms of the United States Copyright Act. It was written as part of
12  * the author's official duties as a United States Government employee and
13  * thus cannot be copyrighted. This software/database is freely available
14  * to the public for use. The National Library of Medicine and the U.S.
15  * Government have not placed any restriction on its use or reproduction.
16  *
17  * Although all reasonable efforts have been taken to ensure the accuracy
18  * and reliability of the software and data, the NLM and the U.S.
19  * Government do not and cannot warrant the performance or results that
20  * may be obtained by using this software or data. The NLM and the U.S.
21  * Government disclaim all warranties, express or implied, including
22  * warranties of performance, merchantability or fitness for any particular
23  * purpose.
24  *
25  * Please cite the author in any work or product based on this material.
26  *
27  * ===========================================================================
28  *
29  * Author: Eugene Vasilchenko
30  *
31  */
32 
33 /// @file htmlhelper.hpp
34 /// HTML library helper classes and functions.
35 
36 
37 #include <corelib/ncbistd.hpp>
38 #include <map>
39 #include <set>
40 #include <list>
41 #include <algorithm>
42 
43 
44 /** @addtogroup HTMLHelper
45  *
46  * @{
47  */
48 
49 
51 
52 
53 class CNCBINode;
54 class CHTML_form;
55 
56 
57 // Utility functions
58 
60 {
61 public:
62 
63  /// Flags for HTMLEncode
65  fEncodeAll = 0, ///< Encode all symbols
66  fSkipLiteralEntities = 1 << 1, ///< Skip "&entity;"
67  fSkipNumericEntities = 1 << 2, ///< Skip "&#NNNN;"
68  fSkipEntities = fSkipLiteralEntities | fSkipNumericEntities,
69  fCheckPreencoded = 1 << 3 ///< Print warning if some preencoded
70  ///< entity found in the string
71  };
72  typedef int THTMLEncodeFlags; //< bitwise OR of "EHTMLEncodeFlags"
73 
74  /// HTML encodes a string. E.g. &lt;.
75  static string HTMLEncode(const string& str,
76  THTMLEncodeFlags flags = fEncodeAll);
77 
79  fCharRef_Entity = 1, ///< Character entity reference(s) was found
80  fCharRef_Numeric = 1 << 1, ///< Numeric character reference(s) was found
81  fEncoding = 1 << 2 ///< Character encoding changed
82  };
83  typedef int THTMLDecodeFlags; //< bitwise OR of "EHTMLDecodeFlags"
84 
85  /// Decode HTML entities and character references
86  static CStringUTF8 HTMLDecode(const string& str,
87  EEncoding encoding = eEncoding_Unknown,
88  THTMLDecodeFlags* result_flags = NULL);
89 
90  /// Encode a string for JavaScript.
91  ///
92  /// Call HTMLEncode and also encode all non-printable characters.
93  /// The non-printable characters will be represented as "\S"
94  /// where S is the symbol code or as "\xDD" where DD is
95  /// the character's code in hexadecimal.
96  /// @sa NStr::JavaScriptEncode, HTMLEncode
97  static string HTMLJavaScriptEncode(const string& str,
98  THTMLEncodeFlags flags = fEncodeAll);
99 
100  /// HTML encodes a tag attribute ('&' and '"' symbols).
101  static string HTMLAttributeEncode(const string& str,
102  THTMLEncodeFlags flags = fSkipEntities);
103 
104  /// Strip all HTML code from a string.
105  static string StripHTML(const string& str);
106 
107  /// Strip all HTML tags from a string.
108  static string StripTags(const string& str);
109 
110  /// Strip all named and numeric character entities from a string.
111  static string StripSpecialChars(const string& str);
112 
113  // Platform-dependent newline symbol.
114  // Default value is "\n" as in UNIX.
115  // Application program is to set it as correct.
116  static void SetNL(const string& nl);
117 
118  static string GetNL(void)
119  { return sm_newline; }
120 
121 protected:
122  static const char* sm_newline;
123 };
124 
125 
126 /// CIDs class to hold sorted list of IDs.
127 ///
128 /// It is here by historical reasons.
129 /// We cannot find place for it in any other library now.
130 
131 class CIDs : public list<int>
132 {
133 public:
134  CIDs(void) {};
135  ~CIDs(void) {};
136 
137  // If 'id' is not in list, return false.
138  // If 'id' in list - return true and remove 'id' from list.
139  bool ExtractID(int id);
140 
141  // Add 'id' to list.
142  void AddID(int id) { push_back(id); }
143 
144  // Return number of ids in list.
145  size_t CountIDs(void) const { return size(); }
146 
147  // Decode id list from text representation.
148  void Decode(const string& str);
149 
150  // Encode id list to text representation.
151  string Encode(void) const;
152 
153 private:
154  // Decoder helpers.
155  int GetNumber(const string& str);
156  int AddID(char cmd, int id, int number);
157 };
158 
159 
160 
161 //=============================================================================
162 // Inline methods
163 //=============================================================================
164 
165 inline
168 {
170 }
171 
172 inline
173 string CHTMLHelper::StripHTML(const string& str)
174 {
176 }
177 
178 inline
179 bool CIDs::ExtractID(int id)
180 {
181  iterator i = find(begin(), end(), id);
182  if ( i != end() ) {
183  erase(i);
184  return true;
185  }
186  return false;
187 }
188 
189 inline
190 int CIDs::GetNumber(const string& str)
191 {
192  return NStr::StringToInt(str);
193 }
194 
195 
196 inline
197 void CIDs::Decode(const string& str)
198 {
199  if ( str.empty() ) {
200  return;
201  }
202  int id = 0; // previous ID
203  SIZE_TYPE pos; // current position
204  char cmd = str[0]; // command
205 
206  // If string begins with digit
207  if ( cmd >= '0' && cmd <= '9' ) {
208  cmd = ','; // default command: direct ID
209  pos = 0; // start of number
210  }
211  else {
212  pos = 1; // start of number
213  }
214 
215  SIZE_TYPE end_pos; // end of number
216  while ( (end_pos = str.find_first_of(" +_,", pos)) != NPOS ) {
217  id = AddID(cmd, id, GetNumber(str.substr(pos, end_pos - pos)));
218  cmd = str[end_pos];
219  pos = end_pos + 1;
220  }
221  AddID(cmd, id, GetNumber(str.substr(pos)));
222 }
223 
224 
225 inline
226 int CIDs::AddID(char cmd, int id, int number)
227 {
228  switch ( cmd ) {
229  case ' ':
230  case '+':
231  case '_':
232  // incremental ID
233  id += number;
234  break;
235  default:
236  id = number;
237  break;
238  }
239  AddID(id);
240  return id;
241 }
242 
243 
244 inline
245 string CIDs::Encode(void) const
246 {
247  string out;
248  int idPrev = 0;
249  for ( const_iterator i = begin(); i != end(); ++i ) {
250  int id = *i;
251  if ( !out.empty() )
252  out += ' ';
253  out += NStr::IntToString(id - idPrev);
254  idPrev = id;
255  }
256  return out;
257 }
258 
260 
261 
262 /* @} */
263 
264 #endif /* HTMLHELPER__HPP */
CIDs class to hold sorted list of IDs.
Definition: htmlhelper.hpp:132
Include a standard set of the NCBI C++ Toolkit most basic headers.
static CS_COMMAND * cmd
Definition: ct_dynamic.c:26
static uch flags
std::ofstream out("events_result.xml")
main entry point for tests
#define NULL
Definition: ncbistd.hpp:225
static string GetNL(void)
Definition: htmlhelper.hpp:118
static string HTMLJavaScriptEncode(const string &str, THTMLEncodeFlags flags=fEncodeAll)
Encode a string for JavaScript.
Definition: htmlhelper.hpp:166
EHTMLEncodeFlags
Flags for HTMLEncode.
Definition: htmlhelper.hpp:64
CIDs(void)
Definition: htmlhelper.hpp:134
bool ExtractID(int id)
Definition: htmlhelper.hpp:179
static const char * sm_newline
Definition: htmlhelper.hpp:122
size_t CountIDs(void) const
Definition: htmlhelper.hpp:145
static string HTMLEncode(const string &str, THTMLEncodeFlags flags=fEncodeAll)
HTML encodes a string. E.g. <.
Definition: htmlhelper.cpp:146
string Encode(void) const
Definition: htmlhelper.hpp:245
int THTMLEncodeFlags
Definition: htmlhelper.hpp:72
void Decode(const string &str)
Definition: htmlhelper.hpp:197
~CIDs(void)
Definition: htmlhelper.hpp:135
int GetNumber(const string &str)
Definition: htmlhelper.hpp:190
void AddID(int id)
Definition: htmlhelper.hpp:142
int THTMLDecodeFlags
Definition: htmlhelper.hpp:83
static string StripHTML(const string &str)
Strip all HTML code from a string.
Definition: htmlhelper.hpp:173
static string StripSpecialChars(const string &str)
Strip all named and numeric character entities from a string.
Definition: htmlhelper.cpp:200
static string StripTags(const string &str)
Strip all HTML tags from a string.
Definition: htmlhelper.cpp:159
#define END_NCBI_SCOPE
End previously defined NCBI scope.
Definition: ncbistl.hpp:103
#define BEGIN_NCBI_SCOPE
Define ncbi namespace.
Definition: ncbistl.hpp:100
std::string CStringUTF8
Definition: ncbistl.hpp:254
NCBI_NS_STD::string::size_type SIZE_TYPE
Definition: ncbistr.hpp:132
EEncoding
Definition: ncbistr.hpp:199
static int StringToInt(const CTempString str, TStringToNumFlags flags=0, int base=10)
Convert string to int.
Definition: ncbistr.cpp:630
#define NPOS
Definition: ncbistr.hpp:133
static string IntToString(int value, TNumToStringFlags flags=0, int base=10)
Convert int to string.
Definition: ncbistr.hpp:5083
static string JavaScriptEncode(const CTempString str)
Encode a string for JavaScript.
Definition: ncbistr.cpp:3955
@ eEncoding_Unknown
Definition: ncbistr.hpp:200
#define NCBI_XHTML_EXPORT
Definition: ncbi_export.h:1139
int i
const struct ncbi::grid::netcache::search::fields::SIZE size
static BOOL number
Definition: pcregrep.c:193
static const char * str(char *buf, int n)
Definition: stats.c:84
Modified on Mon Dec 11 02:36:19 2023 by modify_doxy.py rev. 669887