html.h 15 KB

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