1
0

html.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639
  1. /*
  2. * This file is part of the UCB release of Plan 9. It is subject to the license
  3. * terms in the LICENSE file found in the top-level directory of this
  4. * distribution and at http://akaros.cs.berkeley.edu/files/Plan9License. No
  5. * part of the UCB release of Plan 9, including this file, may be copied,
  6. * modified, propagated, or distributed except according to the terms contained
  7. * in the LICENSE file.
  8. */
  9. /* UTILS */
  10. extern uint8_t* fromStr(Rune* buf, int n, int chset);
  11. extern Rune* toStr(uint8_t* buf, int n, int chset);
  12. /* Common LEX and BUILD enums */
  13. /* Media types */
  14. enum
  15. {
  16. ApplMsword,
  17. ApplOctets,
  18. ApplPdf,
  19. ApplPostscript,
  20. ApplRtf,
  21. ApplFramemaker,
  22. ApplMsexcel,
  23. ApplMspowerpoint,
  24. UnknownType,
  25. Audio32kadpcm,
  26. AudioBasic,
  27. ImageCgm,
  28. ImageG3fax,
  29. ImageGif,
  30. ImageIef,
  31. ImageJpeg,
  32. ImagePng,
  33. ImageTiff,
  34. ImageXBit,
  35. ImageXBit2,
  36. ImageXBitmulti,
  37. ImageXXBitmap,
  38. ModelVrml,
  39. MultiDigest,
  40. MultiMixed,
  41. TextCss,
  42. TextEnriched,
  43. TextHtml,
  44. TextJavascript,
  45. TextPlain,
  46. TextRichtext,
  47. TextSgml,
  48. TextTabSeparatedValues,
  49. TextXml,
  50. VideoMpeg,
  51. VideoQuicktime,
  52. NMEDIATYPES
  53. };
  54. /* HTTP methods */
  55. enum
  56. {
  57. HGet,
  58. HPost
  59. };
  60. /* Charsets */
  61. enum
  62. {
  63. UnknownCharset,
  64. US_Ascii,
  65. ISO_8859_1,
  66. UTF_8,
  67. Unicode,
  68. NCHARSETS
  69. };
  70. /* Frame Target IDs */
  71. enum {
  72. FTtop,
  73. FTself,
  74. FTparent,
  75. FTblank
  76. };
  77. /* LEX */
  78. typedef struct Token Token;
  79. typedef struct Attr Attr;
  80. /* BUILD */
  81. typedef struct Item Item;
  82. typedef struct Itext Itext;
  83. typedef struct Irule Irule;
  84. typedef struct Iimage Iimage;
  85. typedef struct Iformfield Iformfield;
  86. typedef struct Itable Itable;
  87. typedef struct Ifloat Ifloat;
  88. typedef struct Ispacer Ispacer;
  89. typedef struct Genattr Genattr;
  90. typedef struct SEvent SEvent;
  91. typedef struct Formfield Formfield;
  92. typedef struct Option Option;
  93. typedef struct Form Form;
  94. typedef struct Table Table;
  95. typedef struct Tablecol Tablecol;
  96. typedef struct Tablerow Tablerow;
  97. typedef struct Tablecell Tablecell;
  98. typedef struct Align Align;
  99. typedef struct Dimen Dimen;
  100. typedef struct Anchor Anchor;
  101. typedef struct DestAnchor DestAnchor;
  102. typedef struct Map Map;
  103. typedef struct Area Area;
  104. typedef struct Background Background;
  105. typedef struct Kidinfo Kidinfo;
  106. typedef struct Docinfo Docinfo;
  107. typedef struct Stack Stack;
  108. typedef struct Pstate Pstate;
  109. typedef struct ItemSource ItemSource;
  110. typedef struct Lay Lay; /* defined in Layout module */
  111. /* Alignment types */
  112. enum {
  113. ALnone = 0, ALleft, ALcenter, ALright, ALjustify,
  114. ALchar, ALtop, ALmiddle, ALbottom, ALbaseline,
  115. };
  116. struct Align
  117. {
  118. uint8_t halign; /* one of ALnone, ALleft, etc. */
  119. uint8_t valign; /* one of ALnone, ALtop, etc. */
  120. };
  121. /*
  122. * A Dimen holds a dimension specification, especially for those
  123. * cases when a number can be followed by a % or a * to indicate
  124. * percentage of total or relative weight.
  125. * Dnone means no dimension was specified
  126. */
  127. /* To fit in a word, use top bits to identify kind, rest for value */
  128. enum {
  129. Dnone = 0,
  130. Dpixels = (1<<29),
  131. Dpercent = (2<<29),
  132. Drelative = (3<<29),
  133. Dkindmask = (3<<29),
  134. Dspecmask = (~Dkindmask)
  135. };
  136. struct Dimen
  137. {
  138. int kindspec; /* kind | spec */
  139. };
  140. /*
  141. * Background is either an image or a color.
  142. * If both are set, the image has precedence.
  143. */
  144. struct Background
  145. {
  146. Rune* image; /* url */
  147. int color;
  148. };
  149. /*
  150. * There are about a half dozen Item variants.
  151. * The all look like this at the start (using Plan 9 C's
  152. * anonymous structure member mechanism),
  153. * and then the tag field dictates what extra fields there are.
  154. */
  155. struct Item
  156. {
  157. Item* next; /* successor in list of items */
  158. int width; /* width in pixels (0 for floating items) */
  159. int height; /* height in pixels */
  160. int ascent; /* ascent (from top to baseline) in pixels */
  161. int anchorid; /* if nonzero, which anchor we're in */
  162. int state; /* flags and values (see below) */
  163. Genattr*genattr; /* generic attributes and events */
  164. int tag; /* variant discriminator: Itexttag, etc. */
  165. };
  166. /* Item variant tags */
  167. enum {
  168. Itexttag,
  169. Iruletag,
  170. Iimagetag,
  171. Iformfieldtag,
  172. Itabletag,
  173. Ifloattag,
  174. Ispacertag
  175. };
  176. struct Itext
  177. {
  178. Item; /* (with tag ==Itexttag) */
  179. Rune* s; /* the characters */
  180. int fnt; /* style*NumSize+size (see font stuff, below) */
  181. int fg; /* Pixel (color) for text */
  182. uint8_t voff; /* Voffbias+vertical offset from baseline, in pixels (+ve == down) */
  183. uint8_t ul; /* ULnone, ULunder, or ULmid */
  184. };
  185. struct Irule
  186. {
  187. Item; /* (with tag ==Iruletag) */
  188. uint8_t align; /* alignment spec */
  189. uint8_t noshade; /* if true, don't shade */
  190. int size; /* size attr (rule height) */
  191. int color; /* color attr */
  192. Dimen wspec; /* width spec */
  193. };
  194. struct Iimage
  195. {
  196. Item; /* (with tag ==Iimagetag) */
  197. Rune* imsrc; /* image src url */
  198. int imwidth; /* spec width (actual, if no spec) */
  199. int imheight; /* spec height (actual, if no spec) */
  200. Rune* altrep; /* alternate representation, in absence of image */
  201. Map* map; /* if non-nil, client side map */
  202. int ctlid; /* if animated */
  203. uint8_t align; /* vertical alignment */
  204. uint8_t hspace; /* in pixels; buffer space on each side */
  205. uint8_t vspace; /* in pixels; buffer space on top and bottom */
  206. uint8_t border; /* in pixels: border width to draw around image */
  207. Iimage* nextimage; /* next in list of document's images */
  208. void* aux;
  209. };
  210. struct Iformfield
  211. {
  212. Item; /* (with tag ==Iformfieldtag) */
  213. Formfield*formfield;
  214. void* aux;
  215. };
  216. struct Itable
  217. {
  218. Item; /* (with tag ==Itabletag) */
  219. Table* table;
  220. };
  221. struct Ifloat
  222. {
  223. Item; /* (with tag ==Ifloattag) */
  224. Item* item; /* table or image item that floats */
  225. int x; /* x coord of top (from right, if ALright) */
  226. int y; /* y coord of top */
  227. uint8_t side; /* margin it floats to: ALleft or ALright */
  228. uint8_t infloats; /* true if this has been added to a lay.floats */
  229. Ifloat* nextfloat; /* in list of floats */
  230. };
  231. struct Ispacer
  232. {
  233. Item; /* (with tag ==Ispacertag) */
  234. int spkind; /* ISPnull, etc. */
  235. };
  236. /* Item state flags and value fields */
  237. enum {
  238. IFbrk = 0x80000000, /* forced break before this item */
  239. IFbrksp = 0x40000000, /* add 1 line space to break (IFbrk set too) */
  240. IFnobrk = 0x20000000, /* break not allowed before this item */
  241. IFcleft = 0x10000000, /* clear left floats (IFbrk set too) */
  242. IFcright= 0x08000000, /* clear right floats (IFbrk set too) */
  243. IFwrap = 0x04000000, /* in a wrapping (non-pre) line */
  244. IFhang = 0x02000000, /* in a hanging (into left indent) item */
  245. IFrjust = 0x01000000, /* right justify current line */
  246. IFcjust = 0x00800000, /* center justify current line */
  247. IFsmap = 0x00400000, /* image is server-side map */
  248. IFindentshift = 8,
  249. IFindentmask = (255<<IFindentshift), /* current indent, in tab stops */
  250. IFhangmask = 255 /* current hang into left indent, in 1/10th tabstops */
  251. };
  252. /* Bias added to Itext's voff field */
  253. enum { Voffbias = 128 };
  254. /* Spacer kinds */
  255. enum {
  256. ISPnull, /* 0 height and width */
  257. ISPvline, /* height and ascent of current font */
  258. ISPhspace, /* width of space in current font */
  259. ISPgeneral /* other purposes (e.g., between markers and list) */
  260. };
  261. /* Generic attributes and events (not many elements will have any of these set) */
  262. struct Genattr
  263. {
  264. Rune* id;
  265. Rune* class;
  266. Rune* style;
  267. Rune* title;
  268. SEvent* events;
  269. };
  270. struct SEvent
  271. {
  272. SEvent* next; /* in list of events */
  273. int type; /* SEonblur, etc. */
  274. Rune* script;
  275. };
  276. enum {
  277. SEonblur, SEonchange, SEonclick, SEondblclick,
  278. SEonfocus, SEonkeypress, SEonkeyup, SEonload,
  279. SEonmousedown, SEonmousemove, SEonmouseout,
  280. SEonmouseover, SEonmouseup, SEonreset, SEonselect,
  281. SEonsubmit, SEonunload,
  282. Numscriptev
  283. };
  284. /* Form field types */
  285. enum {
  286. Ftext,
  287. Fpassword,
  288. Fcheckbox,
  289. Fradio,
  290. Fsubmit,
  291. Fhidden,
  292. Fimage,
  293. Freset,
  294. Ffile,
  295. Fbutton,
  296. Fselect,
  297. Ftextarea
  298. };
  299. /* Information about a field in a form */
  300. struct Formfield
  301. {
  302. Formfield*next; /* in list of fields for a form */
  303. int ftype; /* Ftext, Fpassword, etc. */
  304. int fieldid; /* serial no. of field within its form */
  305. Form* form; /* containing form */
  306. Rune* name; /* name attr */
  307. Rune* value; /* value attr */
  308. int size; /* size attr */
  309. int maxlength; /* maxlength attr */
  310. int rows; /* rows attr */
  311. int cols; /* cols attr */
  312. uint8_t flags; /* FFchecked, etc. */
  313. Option* options; /* for Fselect fields */
  314. Item* image; /* image item, for Fimage fields */
  315. int ctlid; /* identifies control for this field in layout */
  316. SEvent* events; /* same as genattr->events of containing item */
  317. };
  318. enum {
  319. FFchecked = (1<<7),
  320. FFmultiple = (1<<6)
  321. };
  322. /* Option holds info about an option in a "select" form field */
  323. struct Option
  324. {
  325. Option* next; /* next in list of options for a field */
  326. int selected; /* true if selected initially */
  327. Rune* value; /* value attr */
  328. Rune* display; /* display string */
  329. };
  330. /* Form holds info about a form */
  331. struct Form
  332. {
  333. Form* next; /* in list of forms for document */
  334. int formid; /* serial no. of form within its doc */
  335. Rune* name; /* name or id attr (netscape uses name, HTML 4.0 uses id) */
  336. Rune* action; /* action attr */
  337. int target; /* target attr as targetid */
  338. int method; /* HGet or HPost */
  339. int nfields; /* number of fields */
  340. Formfield*fields; /* field's forms, in input order */
  341. };
  342. /* Flags used in various table structures */
  343. enum {
  344. TFparsing = (1<<7),
  345. TFnowrap = (1<<6),
  346. TFisth = (1<<5)
  347. };
  348. /* Information about a table */
  349. struct Table
  350. {
  351. Table* next; /* next in list of document's tables */
  352. int tableid; /* serial no. of table within its doc */
  353. Tablerow*rows; /* array of row specs (list during parsing) */
  354. int nrow; /* total number of rows */
  355. Tablecol*cols; /* array of column specs */
  356. int ncol; /* total number of columns */
  357. Tablecell*cells; /* list of unique cells */
  358. int ncell; /* total number of cells */
  359. Tablecell***grid; /* 2-D array of cells */
  360. Align align; /* alignment spec for whole table */
  361. Dimen width; /* width spec for whole table */
  362. int border; /* border attr */
  363. int cellspacing; /* cellspacing attr */
  364. int cellpadding; /* cellpadding attr */
  365. Background background; /* table background */
  366. Item* caption; /* linked list of Items, giving caption */
  367. uint8_t caption_place; /* ALtop or ALbottom */
  368. Lay* caption_lay; /* layout of caption */
  369. int totw; /* total width */
  370. int toth; /* total height */
  371. int caph; /* caption height */
  372. int availw; /* used for previous 3 sizes */
  373. Token* tabletok; /* token that started the table */
  374. uint8_t flags; /* Lchanged, perhaps */
  375. };
  376. struct Tablecol
  377. {
  378. int width;
  379. Align align;
  380. Point pos;
  381. };
  382. struct Tablerow
  383. {
  384. Tablerow*next; /* Next in list of rows, during parsing */
  385. Tablecell*cells; /* Cells in row, linked through nextinrow */
  386. int height;
  387. int ascent;
  388. Align align;
  389. Background background;
  390. Point pos;
  391. uint8_t flags; /* 0 or TFparsing */
  392. };
  393. /*
  394. * A Tablecell is one cell of a table.
  395. * It may span multiple rows and multiple columns.
  396. * Cells are linked on two lists: the list for all the cells of
  397. * a document (the next pointers), and the list of all the
  398. * cells that start in a given row (the nextinrow pointers)
  399. */
  400. struct Tablecell
  401. {
  402. Tablecell*next; /* next in list of table's cells */
  403. Tablecell*nextinrow; /* next in list of row's cells */
  404. int cellid; /* serial no. of cell within table */
  405. Item* content; /* contents before layout */
  406. Lay* lay; /* layout of cell */
  407. int rowspan; /* number of rows spanned by this cell */
  408. int colspan; /* number of cols spanned by this cell */
  409. Align align; /* alignment spec */
  410. uint8_t flags; /* TFparsing, TFnowrap, TFisth */
  411. Dimen wspec; /* suggested width */
  412. int hspec; /* suggested height */
  413. Background background; /* cell background */
  414. int minw; /* minimum possible width */
  415. int maxw; /* maximum width */
  416. int ascent; /* cell's ascent */
  417. int row; /* row of upper left corner */
  418. int col; /* col of upper left corner */
  419. Point pos; /* nw corner of cell contents, in cell */
  420. };
  421. /* Anchor is for info about hyperlinks that go somewhere */
  422. struct Anchor
  423. {
  424. Anchor* next; /* next in list of document's anchors */
  425. int index; /* serial no. of anchor within its doc */
  426. Rune* name; /* name attr */
  427. Rune* href; /* href attr */
  428. int target; /* target attr as targetid */
  429. };
  430. /* DestAnchor is for info about hyperlinks that are destinations */
  431. struct DestAnchor
  432. {
  433. DestAnchor*next; /* next in list of document's destanchors */
  434. int index; /* serial no. of anchor within its doc */
  435. Rune* name; /* name attr */
  436. Item* item; /* the destination */
  437. };
  438. /* Maps (client side) */
  439. struct Map
  440. {
  441. Map* next; /* next in list of document's maps */
  442. Rune* name; /* map name */
  443. Area* areas; /* list of map areas */
  444. };
  445. struct Area
  446. {
  447. Area* next; /* next in list of a map's areas */
  448. int shape; /* SHrect, etc. */
  449. Rune* href; /* associated hypertext link */
  450. int target; /* associated target frame */
  451. Dimen* coords; /* array of coords for shape */
  452. int ncoords; /* size of coords array */
  453. };
  454. /* Area shapes */
  455. enum {
  456. SHrect, SHcircle, SHpoly
  457. };
  458. /* Fonts are represented by integers: style*NumSize + size */
  459. /* Font styles */
  460. enum {
  461. FntR, /* roman */
  462. FntI, /* italic */
  463. FntB, /* bold */
  464. FntT, /* typewriter */
  465. NumStyle
  466. };
  467. /* Font sizes */
  468. enum {
  469. Tiny,
  470. Small,
  471. Normal,
  472. Large,
  473. Verylarge,
  474. NumSize
  475. };
  476. enum {
  477. NumFnt = NumStyle*NumSize,
  478. DefFnt = FntR*NumSize+Normal,
  479. };
  480. /* Lines are needed through some text items, for underlining or strikethrough */
  481. enum {
  482. ULnone, ULunder, ULmid
  483. };
  484. /* Kidinfo flags */
  485. enum {
  486. FRnoresize = (1<<0),
  487. FRnoscroll = (1<<1),
  488. FRhscroll = (1<<2),
  489. FRvscroll = (1<<3),
  490. FRhscrollauto = (1<<4),
  491. FRvscrollauto = (1<<5)
  492. };
  493. /* Information about child frame or frameset */
  494. struct Kidinfo
  495. {
  496. Kidinfo*next; /* in list of kidinfos for a frameset */
  497. int isframeset;
  498. /* fields for "frame" */
  499. Rune* src; /* only nil if a "dummy" frame or this is frameset */
  500. Rune* name; /* always non-empty if this isn't frameset */
  501. int marginw;
  502. int marginh;
  503. int framebd;
  504. int flags;
  505. /* fields for "frameset" */
  506. Dimen* rows; /* array of row dimensions */
  507. int nrows; /* length of rows */
  508. Dimen* cols; /* array of col dimensions */
  509. int ncols; /* length of cols */
  510. Kidinfo*kidinfos;
  511. Kidinfo*nextframeset; /* parsing stack */
  512. };
  513. /* Document info (global information about HTML page) */
  514. struct Docinfo
  515. {
  516. /* stuff from HTTP headers, doc head, and body tag */
  517. Rune* src; /* original source of doc */
  518. Rune* base; /* base URL of doc */
  519. Rune* doctitle; /* from <title> element */
  520. Background background; /* background specification */
  521. Iimage* backgrounditem; /* Image Item for doc background image, or nil */
  522. int text; /* doc foreground (text) color */
  523. int link; /* unvisited hyperlink color */
  524. int vlink; /* visited hyperlink color */
  525. int alink; /* highlighting hyperlink color */
  526. int target; /* target frame default */
  527. int chset; /* ISO_8859, etc. */
  528. int mediatype; /* TextHtml, etc. */
  529. int scripttype; /* TextJavascript, etc. */
  530. int hasscripts; /* true if scripts used */
  531. Rune* refresh; /* content of <http-equiv=Refresh ...> */
  532. Kidinfo*kidinfo; /* if a frameset */
  533. int frameid; /* id of document frame */
  534. /* info needed to respond to user actions */
  535. Anchor* anchors; /* list of href anchors */
  536. DestAnchor*dests; /* list of destination anchors */
  537. Form* forms; /* list of forms */
  538. Table* tables; /* list of tables */
  539. Map* maps; /* list of maps */
  540. Iimage* images; /* list of image items (through nextimage links) */
  541. };
  542. extern int dimenkind(Dimen d);
  543. extern int dimenspec(Dimen d);
  544. extern void freedocinfo(Docinfo* d);
  545. extern void freeitems(Item* ithead);
  546. extern Item* parsehtml(uint8_t* data, int datalen, Rune* src,
  547. int mtype, int chset, Docinfo** pdi);
  548. extern void printitems(Item* items, char* msg);
  549. extern int targetid(Rune* s);
  550. extern Rune* targetname(int targid);
  551. extern int validitems(Item* i);
  552. /* Control print output */
  553. extern int warn;
  554. extern int dbglex;
  555. extern int dbgbuild;
  556. /*
  557. * To be provided by caller
  558. * emalloc and erealloc should not return if can't get memory.
  559. * emalloc should zero its memory.
  560. */
  561. extern void* emalloc(unsigned long);
  562. extern void* erealloc(void* p, uint32_t size);