Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 294 lines (288 sloc) 7.186 kb
96e6328c » nniclausse
2008-06-25 - forgotten files (TSUN-72)
1 %% @author Bob Ippolito <bob@mochimedia.com>
2 %% @copyright 2007 Mochi Media, Inc.
3
4 %% @doc Converts HTML 4 charrefs and entities to codepoints.
5 -module(mochiweb_charref).
6 -export([charref/1, test/0]).
7
8 %% External API.
9
10 %% @spec charref(S) -> integer() | undefined
11 %% @doc Convert a decimal charref, hex charref, or html entity to a unicode
12 %% codepoint, or return undefined on failure.
13 %% The input should not include an ampersand or semicolon.
14 %% charref("#38") = 38, charref("#x26") = 38, charref("amp") = 38.
15 charref(B) when is_binary(B) ->
16 charref(binary_to_list(B));
17 charref([$#, C | L]) when C =:= $x orelse C =:= $X ->
18 try erlang:list_to_integer(L, 16)
19 catch
20 error:badarg -> undefined
21 end;
22 charref([$# | L]) ->
23 try list_to_integer(L)
24 catch
25 error:badarg -> undefined
26 end;
27 charref(L) ->
28 entity(L).
29
30 %% @spec test() -> ok
31 %% @doc Run tests for mochiweb_charref.
32 test() ->
33 1234 = charref("#1234"),
34 255 = charref("#xfF"),
35 255 = charref("#XFf"),
36 38 = charref("amp"),
37 undefined = charref("not_an_entity"),
38 ok.
39
40 %% Internal API.
41
42 entity("nbsp") -> 160;
43 entity("iexcl") -> 161;
44 entity("cent") -> 162;
45 entity("pound") -> 163;
46 entity("curren") -> 164;
47 entity("yen") -> 165;
48 entity("brvbar") -> 166;
49 entity("sect") -> 167;
50 entity("uml") -> 168;
51 entity("copy") -> 169;
52 entity("ordf") -> 170;
53 entity("laquo") -> 171;
54 entity("not") -> 172;
55 entity("shy") -> 173;
56 entity("reg") -> 174;
57 entity("macr") -> 175;
58 entity("deg") -> 176;
59 entity("plusmn") -> 177;
60 entity("sup2") -> 178;
61 entity("sup3") -> 179;
62 entity("acute") -> 180;
63 entity("micro") -> 181;
64 entity("para") -> 182;
65 entity("middot") -> 183;
66 entity("cedil") -> 184;
67 entity("sup1") -> 185;
68 entity("ordm") -> 186;
69 entity("raquo") -> 187;
70 entity("frac14") -> 188;
71 entity("frac12") -> 189;
72 entity("frac34") -> 190;
73 entity("iquest") -> 191;
74 entity("Agrave") -> 192;
75 entity("Aacute") -> 193;
76 entity("Acirc") -> 194;
77 entity("Atilde") -> 195;
78 entity("Auml") -> 196;
79 entity("Aring") -> 197;
80 entity("AElig") -> 198;
81 entity("Ccedil") -> 199;
82 entity("Egrave") -> 200;
83 entity("Eacute") -> 201;
84 entity("Ecirc") -> 202;
85 entity("Euml") -> 203;
86 entity("Igrave") -> 204;
87 entity("Iacute") -> 205;
88 entity("Icirc") -> 206;
89 entity("Iuml") -> 207;
90 entity("ETH") -> 208;
91 entity("Ntilde") -> 209;
92 entity("Ograve") -> 210;
93 entity("Oacute") -> 211;
94 entity("Ocirc") -> 212;
95 entity("Otilde") -> 213;
96 entity("Ouml") -> 214;
97 entity("times") -> 215;
98 entity("Oslash") -> 216;
99 entity("Ugrave") -> 217;
100 entity("Uacute") -> 218;
101 entity("Ucirc") -> 219;
102 entity("Uuml") -> 220;
103 entity("Yacute") -> 221;
104 entity("THORN") -> 222;
105 entity("szlig") -> 223;
106 entity("agrave") -> 224;
107 entity("aacute") -> 225;
108 entity("acirc") -> 226;
109 entity("atilde") -> 227;
110 entity("auml") -> 228;
111 entity("aring") -> 229;
112 entity("aelig") -> 230;
113 entity("ccedil") -> 231;
114 entity("egrave") -> 232;
115 entity("eacute") -> 233;
116 entity("ecirc") -> 234;
117 entity("euml") -> 235;
118 entity("igrave") -> 236;
119 entity("iacute") -> 237;
120 entity("icirc") -> 238;
121 entity("iuml") -> 239;
122 entity("eth") -> 240;
123 entity("ntilde") -> 241;
124 entity("ograve") -> 242;
125 entity("oacute") -> 243;
126 entity("ocirc") -> 244;
127 entity("otilde") -> 245;
128 entity("ouml") -> 246;
129 entity("divide") -> 247;
130 entity("oslash") -> 248;
131 entity("ugrave") -> 249;
132 entity("uacute") -> 250;
133 entity("ucirc") -> 251;
134 entity("uuml") -> 252;
135 entity("yacute") -> 253;
136 entity("thorn") -> 254;
137 entity("yuml") -> 255;
138 entity("fnof") -> 402;
139 entity("Alpha") -> 913;
140 entity("Beta") -> 914;
141 entity("Gamma") -> 915;
142 entity("Delta") -> 916;
143 entity("Epsilon") -> 917;
144 entity("Zeta") -> 918;
145 entity("Eta") -> 919;
146 entity("Theta") -> 920;
147 entity("Iota") -> 921;
148 entity("Kappa") -> 922;
149 entity("Lambda") -> 923;
150 entity("Mu") -> 924;
151 entity("Nu") -> 925;
152 entity("Xi") -> 926;
153 entity("Omicron") -> 927;
154 entity("Pi") -> 928;
155 entity("Rho") -> 929;
156 entity("Sigma") -> 931;
157 entity("Tau") -> 932;
158 entity("Upsilon") -> 933;
159 entity("Phi") -> 934;
160 entity("Chi") -> 935;
161 entity("Psi") -> 936;
162 entity("Omega") -> 937;
163 entity("alpha") -> 945;
164 entity("beta") -> 946;
165 entity("gamma") -> 947;
166 entity("delta") -> 948;
167 entity("epsilon") -> 949;
168 entity("zeta") -> 950;
169 entity("eta") -> 951;
170 entity("theta") -> 952;
171 entity("iota") -> 953;
172 entity("kappa") -> 954;
173 entity("lambda") -> 955;
174 entity("mu") -> 956;
175 entity("nu") -> 957;
176 entity("xi") -> 958;
177 entity("omicron") -> 959;
178 entity("pi") -> 960;
179 entity("rho") -> 961;
180 entity("sigmaf") -> 962;
181 entity("sigma") -> 963;
182 entity("tau") -> 964;
183 entity("upsilon") -> 965;
184 entity("phi") -> 966;
185 entity("chi") -> 967;
186 entity("psi") -> 968;
187 entity("omega") -> 969;
188 entity("thetasym") -> 977;
189 entity("upsih") -> 978;
190 entity("piv") -> 982;
191 entity("bull") -> 8226;
192 entity("hellip") -> 8230;
193 entity("prime") -> 8242;
194 entity("Prime") -> 8243;
195 entity("oline") -> 8254;
196 entity("frasl") -> 8260;
197 entity("weierp") -> 8472;
198 entity("image") -> 8465;
199 entity("real") -> 8476;
200 entity("trade") -> 8482;
201 entity("alefsym") -> 8501;
202 entity("larr") -> 8592;
203 entity("uarr") -> 8593;
204 entity("rarr") -> 8594;
205 entity("darr") -> 8595;
206 entity("harr") -> 8596;
207 entity("crarr") -> 8629;
208 entity("lArr") -> 8656;
209 entity("uArr") -> 8657;
210 entity("rArr") -> 8658;
211 entity("dArr") -> 8659;
212 entity("hArr") -> 8660;
213 entity("forall") -> 8704;
214 entity("part") -> 8706;
215 entity("exist") -> 8707;
216 entity("empty") -> 8709;
217 entity("nabla") -> 8711;
218 entity("isin") -> 8712;
219 entity("notin") -> 8713;
220 entity("ni") -> 8715;
221 entity("prod") -> 8719;
222 entity("sum") -> 8721;
223 entity("minus") -> 8722;
224 entity("lowast") -> 8727;
225 entity("radic") -> 8730;
226 entity("prop") -> 8733;
227 entity("infin") -> 8734;
228 entity("ang") -> 8736;
229 entity("and") -> 8743;
230 entity("or") -> 8744;
231 entity("cap") -> 8745;
232 entity("cup") -> 8746;
233 entity("int") -> 8747;
234 entity("there4") -> 8756;
235 entity("sim") -> 8764;
236 entity("cong") -> 8773;
237 entity("asymp") -> 8776;
238 entity("ne") -> 8800;
239 entity("equiv") -> 8801;
240 entity("le") -> 8804;
241 entity("ge") -> 8805;
242 entity("sub") -> 8834;
243 entity("sup") -> 8835;
244 entity("nsub") -> 8836;
245 entity("sube") -> 8838;
246 entity("supe") -> 8839;
247 entity("oplus") -> 8853;
248 entity("otimes") -> 8855;
249 entity("perp") -> 8869;
250 entity("sdot") -> 8901;
251 entity("lceil") -> 8968;
252 entity("rceil") -> 8969;
253 entity("lfloor") -> 8970;
254 entity("rfloor") -> 8971;
255 entity("lang") -> 9001;
256 entity("rang") -> 9002;
257 entity("loz") -> 9674;
258 entity("spades") -> 9824;
259 entity("clubs") -> 9827;
260 entity("hearts") -> 9829;
261 entity("diams") -> 9830;
262 entity("quot") -> 34;
263 entity("amp") -> 38;
264 entity("lt") -> 60;
265 entity("gt") -> 62;
266 entity("OElig") -> 338;
267 entity("oelig") -> 339;
268 entity("Scaron") -> 352;
269 entity("scaron") -> 353;
270 entity("Yuml") -> 376;
271 entity("circ") -> 710;
272 entity("tilde") -> 732;
273 entity("ensp") -> 8194;
274 entity("emsp") -> 8195;
275 entity("thinsp") -> 8201;
276 entity("zwnj") -> 8204;
277 entity("zwj") -> 8205;
278 entity("lrm") -> 8206;
279 entity("rlm") -> 8207;
280 entity("ndash") -> 8211;
281 entity("mdash") -> 8212;
282 entity("lsquo") -> 8216;
283 entity("rsquo") -> 8217;
284 entity("sbquo") -> 8218;
285 entity("ldquo") -> 8220;
286 entity("rdquo") -> 8221;
287 entity("bdquo") -> 8222;
288 entity("dagger") -> 8224;
289 entity("Dagger") -> 8225;
290 entity("permil") -> 8240;
291 entity("lsaquo") -> 8249;
292 entity("rsaquo") -> 8250;
293 entity("euro") -> 8364;
294 entity(_) -> undefined.
295
Something went wrong with that request. Please try again.