PageRenderTime 46ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/erl_cgi/src/html.erl

https://github.com/bmizerany/jungerl
Erlang | 468 lines | 411 code | 39 blank | 18 comment | 0 complexity | c3af48b3648fef3871859d623cd22ea5 MD5 | raw file
Possible License(s): LGPL-2.1, BSD-3-Clause, AGPL-1.0
  1. %%% File : html.erl
  2. %%% Author : Tony Rogvall <tony@localhost.localdomain>
  3. %%% Description : HTML utility functions
  4. %%% Created : 5 May 2002 by Tony Rogvall <tony@localhost.localdomain>
  5. -module(html).
  6. -export([inline/0, flow/0, block/0]).
  7. -export([is_fontstyle/1,
  8. is_phrase/1,
  9. is_special/1,
  10. is_formctrl/1,
  11. is_inline/1,
  12. is_heading/1,
  13. is_lst/1,
  14. is_preformatted/1,
  15. is_block/1,
  16. is_flow/1,
  17. is_pre_exclusion/1,
  18. is_head_content/1,
  19. is_head_misc/1,
  20. is_head/1]).
  21. -export([coreattrs/1,
  22. i18n/1,
  23. events/1,
  24. attrs/1]).
  25. -export([end_tag_optional/1]).
  26. -export([chars/0, value/1]).
  27. inline() -> fun(T) -> is_inline(T) end.
  28. flow() -> fun(T) -> is_flow(T) end.
  29. block() -> fun(T) -> is_block(T) end.
  30. %% ENTITY % fontstyle
  31. is_fontstyle(tt) -> true;
  32. is_fontstyle(i) -> true;
  33. is_fontstyle(b) -> true;
  34. is_fontstyle(u) -> true;
  35. is_fontstyle(s) -> true;
  36. is_fontstyle(strike) -> true;
  37. is_fontstyle(big) -> true;
  38. is_fontstyle(small) -> true;
  39. is_fontstyle(erl) -> true; %% EXTENSION
  40. is_fontstyle(_) -> false.
  41. %% ENTITY % phrase
  42. is_phrase(em) -> true;
  43. is_phrase(strong) -> true;
  44. is_phrase(dfn) -> true;
  45. is_phrase(code) -> true;
  46. is_phrase(samp) -> true;
  47. is_phrase(kbd) -> true;
  48. is_phrase(var) -> true;
  49. is_phrase(cite) -> true;
  50. is_phrase(abbr) -> true;
  51. is_phrase(acronym) -> true;
  52. is_phrase(erl) -> true; %% EXTENSION
  53. is_phrase(_) -> false.
  54. %% ENTITY % special
  55. is_special(a) -> true;
  56. is_special(img) -> true;
  57. is_special(applet) -> true;
  58. is_special(object) -> true;
  59. is_special(font) -> true;
  60. is_special(basefont) -> true;
  61. is_special(br) -> true;
  62. is_special(script) -> true;
  63. is_special(map) -> true;
  64. is_special(q) -> true;
  65. is_special(sub) -> true;
  66. is_special(sup) -> true;
  67. is_special(span) -> true;
  68. is_special(bdo) -> true;
  69. is_special(iframe) -> true;
  70. is_special(erl) -> true; %% EXTENSION
  71. is_special(_) -> false.
  72. %% ENTITY % formctrl
  73. is_formctrl(input) -> true;
  74. is_formctrl(select) -> true;
  75. is_formctrl(textarea) -> true;
  76. is_formctrl(label) -> true;
  77. is_formctrl(button) -> true;
  78. is_formctrl(erl) -> true; %% EXTENSION
  79. is_formctrl(_) -> false.
  80. %% ENTITY % inline
  81. is_inline(pcdata) -> true;
  82. is_inline(ilayer) -> true;
  83. is_inline(Tag) ->
  84. is_fontstyle(Tag) orelse is_phrase(Tag) orelse
  85. is_special(Tag) orelse is_formctrl(Tag).
  86. %% ENTITY % heading
  87. is_heading(h1) -> true;
  88. is_heading(h2) -> true;
  89. is_heading(h3) -> true;
  90. is_heading(h4) -> true;
  91. is_heading(h5) -> true;
  92. is_heading(h6) -> true;
  93. is_heading(erl) -> true; %% EXTENSION
  94. is_heading(_) -> false.
  95. %% ENTITY % list
  96. is_lst(ul) -> true;
  97. is_lst(ol) -> true;
  98. is_lst(dir) -> true;
  99. is_lst(menu) -> true;
  100. is_lst(erl) -> true; %% EXTENSION
  101. is_lst(_) -> false.
  102. %% ENTITY % preformatted
  103. is_preformatted(pre) -> true;
  104. is_preformatted(erl) -> true; %% EXTENSION
  105. is_preformatted(_) -> false.
  106. %% ENTITY % block
  107. is_block(p) -> true;
  108. is_block(dl) -> true;
  109. is_block('div') -> true;
  110. is_block(center) -> true;
  111. is_block(noscript) -> true;
  112. is_block(noframes) -> true;
  113. is_block(blockquote) -> true;
  114. is_block(form) -> true;
  115. is_block(isindex) -> true;
  116. is_block(hr) -> true;
  117. is_block(table) -> true;
  118. is_block(fieldset) -> true;
  119. is_block(address) -> true;
  120. is_block(ilayer) -> true;
  121. is_block(layer) -> true;
  122. is_block(nolayer) -> true;
  123. is_block(Tag) ->
  124. is_heading(Tag) orelse is_lst(Tag) orelse is_preformatted(Tag).
  125. %% ENTITY % flow
  126. is_flow(Tag) ->
  127. is_block(Tag) orelse is_inline(Tag).
  128. %% ENTITY % pre.exclusion
  129. is_pre_exclusion(img) -> true;
  130. is_pre_exclusion(object) -> true;
  131. is_pre_exclusion(applet) -> true;
  132. is_pre_exclusion(big) -> true;
  133. is_pre_exclusion(small) -> true;
  134. is_pre_exclusion(sub) -> true;
  135. is_pre_exclusion(sup) -> true;
  136. is_pre_exclusion(font) -> true;
  137. is_pre_exclusion(basefont) -> true;
  138. is_pre_exclusion(erl) -> true; %% EXTENSION
  139. is_pre_exclusion(_) -> false.
  140. %% ENTITY % head.content
  141. is_head_content(title) -> true;
  142. is_head_content(base) -> true;
  143. is_head_content(erl) -> true; %% EXTENSION
  144. is_head_content(_) -> false.
  145. %% ENTITY % head.misc
  146. is_head_misc(script) -> true;
  147. is_head_misc(style) -> true;
  148. is_head_misc(meta) -> true;
  149. is_head_misc(link) -> true;
  150. is_head_misc(object) -> true;
  151. is_head_misc(erl) -> true; %% EXTENSION
  152. is_head_misc(_) -> false.
  153. is_head(Tag) ->
  154. is_head_content(Tag) orelse is_head_misc(Tag).
  155. end_tag_optional(html) -> true;
  156. end_tag_optional(head) -> true;
  157. end_tag_optional(body) -> true;
  158. end_tag_optional(p) -> true;
  159. end_tag_optional(li) -> true;
  160. end_tag_optional(dt) -> true;
  161. end_tag_optional(dd) -> true;
  162. end_tag_optional(thead) -> true;
  163. end_tag_optional(tfoot) -> true;
  164. end_tag_optional(tbody) -> true;
  165. end_tag_optional(colgroup) -> true;
  166. end_tag_optional(tr) -> true;
  167. end_tag_optional(th) -> true;
  168. end_tag_optional(td) -> true;
  169. end_tag_optional(option) -> true;
  170. end_tag_optional(_) -> false.
  171. is_type(length) -> cdata;
  172. is_type(multilength) -> cdata;
  173. is_type(mulitlengths) -> cdata;
  174. is_type(pixels) -> cdata;
  175. is_type(ialign) -> {enum,[top,middle,bottom,left,right]};
  176. is_type(contenttype) -> cdata;
  177. is_type(contenttypes) -> cdata;
  178. is_type(charset) -> cdata;
  179. is_type(charsets) -> cdata;
  180. is_type(languagecode) -> name;
  181. is_type(character) -> cdata;
  182. is_type(linktypes) -> cdata;
  183. is_type(mediadesc) -> cdata;
  184. is_type(uri) -> cdata;
  185. is_type(datetime) -> cdata;
  186. is_type(script) -> cdata;
  187. is_type(stylesheet) -> cdata;
  188. is_type(frametarget) -> cdata;
  189. is_type(text) -> cdata;
  190. is_type(color) -> cdata; %% #RRGGBB | name
  191. is_type(A) -> cdata. %% add more
  192. is_attr(table, Attr) ->
  193. case Attr of
  194. summary -> text;
  195. width -> length;
  196. border -> pixels;
  197. frame -> tframe;
  198. rules -> trules;
  199. cellspacing -> length;
  200. cellpadding -> length;
  201. bgcolor -> color;
  202. datapagesize -> cdata;
  203. _ -> attrs(Attr)
  204. end;
  205. is_attr(sub, Attr) -> attrs(Attr);
  206. is_attr(sup, Attr) -> attrs(Attr);
  207. is_attr(span, Attr) -> attrs(Attr); %% + reserved ??
  208. is_attr(select,Attr) ->
  209. case Attr of
  210. name -> cdata;
  211. size -> number;
  212. multiple -> boolean;
  213. disabled -> boolean;
  214. tabindex -> number;
  215. onfocus -> script;
  216. onblur -> script;
  217. onchange -> script;
  218. _ -> attrs(Attr)
  219. end;
  220. is_attr(option,Attr) ->
  221. case Attr of
  222. selected -> boolean;
  223. disabled -> boolean;
  224. label -> text;
  225. value -> cdata;
  226. _ -> attrs(Attr)
  227. end;
  228. is_attr(optgroup,Attr) ->
  229. case Attr of
  230. disabled -> boolean;
  231. label -> text;
  232. _ -> attrs(Attr)
  233. end;
  234. is_attr(Tag, Attr) ->
  235. case is_fontstyle(Tag) of
  236. false -> case is_phrase(Tag) of
  237. true -> attrs(Attr);
  238. false -> false
  239. end;
  240. true -> attrs(Attr)
  241. end.
  242. bodycolors(bgcolor) -> color;
  243. bodycolors(text) -> color;
  244. bodycolors(link) -> color;
  245. bodycolors(vlink) -> color;
  246. bodycolors(alink) -> color;
  247. bodycolors(_) -> false.
  248. coreattrs(id) -> id; %% document-wide unique id
  249. coreattrs(class) -> cdata; %% space separated list of classes
  250. coreattrs(style) -> stylesheet; %% associated style info
  251. coreattrs(title) -> text; %% advisory title/amplification
  252. coreattrs(_) -> false.
  253. i18n(lang) -> languagecode; %% language code
  254. i18n(dir) -> {enum,[ltr,rtl]}; %% direction for weak/neutral text
  255. i18n(_) -> false.
  256. events(onclick) -> script; %% a pointer button was clicked
  257. events(ondblclick) -> script; %% a pointer button was double clicked
  258. events(onmousedown) -> script; %% a pointer button was pressed down
  259. events(onmouseup) -> script; %% a pointer button was released
  260. events(onmouseover) -> script; %% a pointer was moved onto
  261. events(onmousemove) -> script; %% a pointer was moved within
  262. events(onmouseout) -> script; %% a pointer was moved away
  263. events(onkeypress) -> script; %% a key was pressed and released
  264. events(onkeydown) -> script; %% a key was pressed down
  265. events(onkeyup) -> script; %% a key was released
  266. events(_) -> false.
  267. reserved(datasrc) -> uri;
  268. reserved(datafld) -> cdata;
  269. reserved(dataformatas) -> {enum,[plaintext,html]};
  270. reserved(_) -> false.
  271. attrs(A) ->
  272. case coreattrs(A) of
  273. false ->
  274. case i18n(A) of
  275. false ->
  276. events(A);
  277. Type -> Type
  278. end;
  279. Type -> Type
  280. end.
  281. %% HTML char table
  282. chars() ->
  283. {
  284. "&#00;", "&#01;", "&#02;", "&#03;", "&#04;", "&#05;",
  285. "&#06;", "&#07;", "&#08;", "&#09;", "&#0A;", "&#0B;",
  286. "&#0C;", "&#0D;", "&#0E;", "&#0F;", "&#10;", "&#11;",
  287. "&#12;", "&#13;", "&#14;", "&#15;", "&#16;", "&#17;",
  288. "&#18;", "&#19;", "&#1A;", "&#1B;", "&#1C;", "&#1D;",
  289. "&#1E;", "&#1F;",
  290. " ", "!", "&quot;", "#", "\$", "%", "&amp;", "'", "(", ")",
  291. "*", "+", ",", "-", ".", "/",
  292. "0", "1", "2", "3", "4", "5", "6", "7", "8", "9",
  293. ":", ";", "&lt;", "=", "&gt;", "?", "@",
  294. "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K",
  295. "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V",
  296. "W", "X", "Y", "Z",
  297. "[", "\\", "]", [$^], %% Emacs mode bailing out
  298. "_", "`",
  299. "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k",
  300. "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v",
  301. "w", "x", "y", "z",
  302. "{", "|", "}", "~",
  303. "&#7F;", "&#80;", "&#81;", "&#82;", "&#83;", "&#84;",
  304. "&#85;", "&#86;", "&#87;", "&#88;", "&#89;", "&#8A;",
  305. "&#8B;", "&#8C;", "&#8D;", "&#8E;", "&#8F;", "&#90;",
  306. "&#91;", "&#92;", "&#93;", "&#94;", "&#95;", "&#96;",
  307. "&#97;", "&#98;", "&#99;", "&#9A;", "&#9B;", "&#9C;",
  308. "&#9D;", "&#9E;", "&#9F;",
  309. "&nbsp;", "&iexcl;", "&cent;", "&pound;", "&curren;", "&yen;", "&brvbar;",
  310. "&sect;", "&uml;", "&copy;", "&ordf;", "&laquo;", "&not;", "&shy;",
  311. "&reg;", "&macr;", "&deg;", "&plusmn;", "&sup2;", "&sup3;", "&acute;",
  312. "&micro;", "&para;", "&middot;", "&cedil;", "&sup1;", "&ordm;", "&raquo;",
  313. "&frac14;", "&frac12;", "&frac34;", "&iquest;", "&Agrave;", "&Aacute;",
  314. "&Acirc;", "&Atilde;", "&Auml;", "&Aring;", "&AElig;", "&Ccedil;",
  315. "&Egrave;", "&Eacute;", "&Ecirc;", "&Euml;", "&Igrave;", "&Iacute;",
  316. "&Icirc;", "&Iuml;", "&ETH;", "&Ntilde;", "&Ograve;", "&Oacute;",
  317. "&Ocirc;", "&Otilde;", "&Ouml;", "&times;", "&Oslash;", "&Ugrave;",
  318. "&Uacute;", "&Ucirc;", "&Uuml;", "&Yacute;", "&THORN;", "&szlig;",
  319. "&agrave;", "&aacute;", "&acirc;", "&atilde;", "&auml;", "&aring;",
  320. "&aelig;", "&ccedil;", "&egrave;", "&eacute;", "&ecirc;", "&euml;",
  321. "&igrave;", "&iacute;", "&icirc;", "&iuml;", "&eth;", "&ntilde;",
  322. "&ograve;", "&oacute;", "&ocirc;", "&otilde;", "&ouml;", "&divide;",
  323. "&oslash;", "&ugrave;", "&uacute;", "&ucirc;", "&uuml;", "&yacute;",
  324. "&thorn;", "&yuml;"
  325. }.
  326. value(Name) ->
  327. case Name of
  328. "lt" -> 60;
  329. "gt" -> 62;
  330. "amp" -> 38;
  331. "quot" -> $";
  332. "nbsp" -> 160;
  333. "iexcl" -> 161;
  334. "cent" -> 162;
  335. "pound" -> 163;
  336. "curren" -> 164;
  337. "yen" -> 165;
  338. "brvbar" -> 166;
  339. "sect" -> 167;
  340. "uml" -> 168;
  341. "copy" -> 169;
  342. "ordf" -> 170;
  343. "laquo" -> 171;
  344. "not" -> 172;
  345. "shy" -> 173;
  346. "reg" -> 174;
  347. "macr" -> 175;
  348. "deg" -> 176;
  349. "plusmn" -> 177;
  350. "sup2" -> 178;
  351. "sup3" -> 179;
  352. "acute" -> 180;
  353. "micro" -> 181;
  354. "para" -> 182;
  355. "middot" -> 183;
  356. "cedil" -> 184;
  357. "sup1" -> 185;
  358. "ordm" -> 186;
  359. "raquo" -> 187;
  360. "frac14" -> 188;
  361. "frac12" -> 189;
  362. "frac34" -> 190;
  363. "iquest" -> 191;
  364. "Agrave" -> 192;
  365. "Aacute" -> 193;
  366. "Acirc" -> 194;
  367. "Atilde" -> 195;
  368. "Auml" -> 196;
  369. "Aring" -> 197;
  370. "AElig" -> 198;
  371. "Ccedil" -> 199;
  372. "Egrave" -> 200;
  373. "Eacute" -> 201;
  374. "Ecirc" -> 202;
  375. "Euml" -> 203;
  376. "Igrave" -> 204;
  377. "Iacute" -> 205;
  378. "Icirc" -> 206;
  379. "Iuml" -> 207;
  380. "ETH" -> 208;
  381. "Ntilde" -> 209;
  382. "Ograve" -> 210;
  383. "Oacute" -> 211;
  384. "Ocirc"-> 212;
  385. "Otilde" -> 213;
  386. "Ouml" -> 214;
  387. "times" -> 215;
  388. "Oslash" -> 216;
  389. "Ugrave" -> 217;
  390. "Uacute" -> 218;
  391. "Ucirc" -> 219;
  392. "Uuml" -> 220;
  393. "Yacute" -> 221;
  394. "THORN" -> 222;
  395. "szlig" -> 223;
  396. "agrave" -> 224;
  397. "aacute" -> 225;
  398. "acirc" -> 226;
  399. "atilde" -> 227;
  400. "auml" -> 228;
  401. "aring" -> 229;
  402. "aelig" -> 230;
  403. "ccedil" -> 231;
  404. "egrave" -> 232;
  405. "eacute" -> 233;
  406. "ecirc" -> 234;
  407. "euml" -> 235;
  408. "igrave" -> 236;
  409. "iacute" -> 237;
  410. "icirc" -> 238;
  411. "iuml" -> 239;
  412. "eth" -> 240;
  413. "ntilde" -> 241;
  414. "ograve" -> 242;
  415. "oacute" -> 243;
  416. "ocirc" -> 244;
  417. "otilde" -> 245;
  418. "ouml" -> 246;
  419. "divide" -> 247;
  420. "oslash" -> 248;
  421. "ugrave" -> 249;
  422. "uacute" -> 250;
  423. "ucirc" -> 251;
  424. "uuml" -> 252;
  425. "yacute" -> 253;
  426. "thorn" -> 254;
  427. "yuml" -> 255;
  428. _ -> error
  429. end.