html.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633
  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. Dimen wspec; /* width spec */
  188. };
  189. struct Iimage
  190. {
  191. Item; /* (with tag ==Iimagetag) */
  192. Rune* imsrc; /* image src url */
  193. int imwidth; /* spec width (actual, if no spec) */
  194. int imheight; /* spec height (actual, if no spec) */
  195. Rune* altrep; /* alternate representation, in absence of image */
  196. Map* map; /* if non-nil, client side map */
  197. int ctlid; /* if animated */
  198. uchar align; /* vertical alignment */
  199. uchar hspace; /* in pixels; buffer space on each side */
  200. uchar vspace; /* in pixels; buffer space on top and bottom */
  201. uchar border; /* in pixels: border width to draw around image */
  202. Iimage* nextimage; /* next in list of document's images */
  203. void* aux;
  204. };
  205. struct Iformfield
  206. {
  207. Item; /* (with tag ==Iformfieldtag) */
  208. Formfield*formfield;
  209. void* aux;
  210. };
  211. struct Itable
  212. {
  213. Item; /* (with tag ==Itabletag) */
  214. Table* table;
  215. };
  216. struct Ifloat
  217. {
  218. Item; /* (with tag ==Ifloattag) */
  219. Item* item; /* table or image item that floats */
  220. int x; /* x coord of top (from right, if ALright) */
  221. int y; /* y coord of top */
  222. uchar side; /* margin it floats to: ALleft or ALright */
  223. uchar infloats; /* true if this has been added to a lay.floats */
  224. Ifloat* nextfloat; /* in list of floats */
  225. };
  226. struct Ispacer
  227. {
  228. Item; /* (with tag ==Ispacertag) */
  229. int spkind; /* ISPnull, etc. */
  230. };
  231. /* Item state flags and value fields */
  232. enum {
  233. IFbrk = 0x80000000, /* forced break before this item */
  234. IFbrksp = 0x40000000, /* add 1 line space to break (IFbrk set too) */
  235. IFnobrk = 0x20000000, /* break not allowed before this item */
  236. IFcleft = 0x10000000, /* clear left floats (IFbrk set too) */
  237. IFcright= 0x08000000, /* clear right floats (IFbrk set too) */
  238. IFwrap = 0x04000000, /* in a wrapping (non-pre) line */
  239. IFhang = 0x02000000, /* in a hanging (into left indent) item */
  240. IFrjust = 0x01000000, /* right justify current line */
  241. IFcjust = 0x00800000, /* center justify current line */
  242. IFsmap = 0x00400000, /* image is server-side map */
  243. IFindentshift = 8,
  244. IFindentmask = (255<<IFindentshift), /* current indent, in tab stops */
  245. IFhangmask = 255 /* current hang into left indent, in 1/10th tabstops */
  246. };
  247. /* Bias added to Itext's voff field */
  248. enum { Voffbias = 128 };
  249. /* Spacer kinds */
  250. enum {
  251. ISPnull, /* 0 height and width */
  252. ISPvline, /* height and ascent of current font */
  253. ISPhspace, /* width of space in current font */
  254. ISPgeneral /* other purposes (e.g., between markers and list) */
  255. };
  256. /* Generic attributes and events (not many elements will have any of these set) */
  257. struct Genattr
  258. {
  259. Rune* id;
  260. Rune* class;
  261. Rune* style;
  262. Rune* title;
  263. SEvent* events;
  264. };
  265. struct SEvent
  266. {
  267. SEvent* next; /* in list of events */
  268. int type; /* SEonblur, etc. */
  269. Rune* script;
  270. };
  271. enum {
  272. SEonblur, SEonchange, SEonclick, SEondblclick,
  273. SEonfocus, SEonkeypress, SEonkeyup, SEonload,
  274. SEonmousedown, SEonmousemove, SEonmouseout,
  275. SEonmouseover, SEonmouseup, SEonreset, SEonselect,
  276. SEonsubmit, SEonunload,
  277. Numscriptev
  278. };
  279. /* Form field types */
  280. enum {
  281. Ftext,
  282. Fpassword,
  283. Fcheckbox,
  284. Fradio,
  285. Fsubmit,
  286. Fhidden,
  287. Fimage,
  288. Freset,
  289. Ffile,
  290. Fbutton,
  291. Fselect,
  292. Ftextarea
  293. };
  294. /* Information about a field in a form */
  295. struct Formfield
  296. {
  297. Formfield*next; /* in list of fields for a form */
  298. int ftype; /* Ftext, Fpassword, etc. */
  299. int fieldid; /* serial no. of field within its form */
  300. Form* form; /* containing form */
  301. Rune* name; /* name attr */
  302. Rune* value; /* value attr */
  303. int size; /* size attr */
  304. int maxlength; /* maxlength attr */
  305. int rows; /* rows attr */
  306. int cols; /* cols attr */
  307. uchar flags; /* FFchecked, etc. */
  308. Option* options; /* for Fselect fields */
  309. Item* image; /* image item, for Fimage fields */
  310. int ctlid; /* identifies control for this field in layout */
  311. SEvent* events; /* same as genattr->events of containing item */
  312. };
  313. enum {
  314. FFchecked = (1<<7),
  315. FFmultiple = (1<<6)
  316. };
  317. /* Option holds info about an option in a "select" form field */
  318. struct Option
  319. {
  320. Option* next; /* next in list of options for a field */
  321. int selected; /* true if selected initially */
  322. Rune* value; /* value attr */
  323. Rune* display; /* display string */
  324. };
  325. /* Form holds info about a form */
  326. struct Form
  327. {
  328. Form* next; /* in list of forms for document */
  329. int formid; /* serial no. of form within its doc */
  330. Rune* name; /* name or id attr (netscape uses name, HTML 4.0 uses id) */
  331. Rune* action; /* action attr */
  332. int target; /* target attr as targetid */
  333. int method; /* HGet or HPost */
  334. int nfields; /* number of fields */
  335. Formfield*fields; /* field's forms, in input order */
  336. };
  337. /* Flags used in various table structures */
  338. enum {
  339. TFparsing = (1<<7),
  340. TFnowrap = (1<<6),
  341. TFisth = (1<<5)
  342. };
  343. /* Information about a table */
  344. struct Table
  345. {
  346. Table* next; /* next in list of document's tables */
  347. int tableid; /* serial no. of table within its doc */
  348. Tablerow*rows; /* array of row specs (list during parsing) */
  349. int nrow; /* total number of rows */
  350. Tablecol*cols; /* array of column specs */
  351. int ncol; /* total number of columns */
  352. Tablecell*cells; /* list of unique cells */
  353. int ncell; /* total number of cells */
  354. Tablecell***grid; /* 2-D array of cells */
  355. Align align; /* alignment spec for whole table */
  356. Dimen width; /* width spec for whole table */
  357. int border; /* border attr */
  358. int cellspacing; /* cellspacing attr */
  359. int cellpadding; /* cellpadding attr */
  360. Background background; /* table background */
  361. Item* caption; /* linked list of Items, giving caption */
  362. uchar caption_place; /* ALtop or ALbottom */
  363. Lay* caption_lay; /* layout of caption */
  364. int totw; /* total width */
  365. int toth; /* total height */
  366. int caph; /* caption height */
  367. int availw; /* used for previous 3 sizes */
  368. Token* tabletok; /* token that started the table */
  369. uchar flags; /* Lchanged, perhaps */
  370. };
  371. struct Tablecol
  372. {
  373. int width;
  374. Align align;
  375. Point pos;
  376. };
  377. struct Tablerow
  378. {
  379. Tablerow*next; /* Next in list of rows, during parsing */
  380. Tablecell*cells; /* Cells in row, linked through nextinrow */
  381. int height;
  382. int ascent;
  383. Align align;
  384. Background background;
  385. Point pos;
  386. uchar flags; /* 0 or TFparsing */
  387. };
  388. /*
  389. * A Tablecell is one cell of a table.
  390. * It may span multiple rows and multiple columns.
  391. * Cells are linked on two lists: the list for all the cells of
  392. * a document (the next pointers), and the list of all the
  393. * cells that start in a given row (the nextinrow pointers)
  394. */
  395. struct Tablecell
  396. {
  397. Tablecell*next; /* next in list of table's cells */
  398. Tablecell*nextinrow; /* next in list of row's cells */
  399. int cellid; /* serial no. of cell within table */
  400. Item* content; /* contents before layout */
  401. Lay* lay; /* layout of cell */
  402. int rowspan; /* number of rows spanned by this cell */
  403. int colspan; /* number of cols spanned by this cell */
  404. Align align; /* alignment spec */
  405. uchar flags; /* TFparsing, TFnowrap, TFisth */
  406. Dimen wspec; /* suggested width */
  407. int hspec; /* suggested height */
  408. Background background; /* cell background */
  409. int minw; /* minimum possible width */
  410. int maxw; /* maximum width */
  411. int ascent; /* cell's ascent */
  412. int row; /* row of upper left corner */
  413. int col; /* col of upper left corner */
  414. Point pos; /* nw corner of cell contents, in cell */
  415. };
  416. /* Anchor is for info about hyperlinks that go somewhere */
  417. struct Anchor
  418. {
  419. Anchor* next; /* next in list of document's anchors */
  420. int index; /* serial no. of anchor within its doc */
  421. Rune* name; /* name attr */
  422. Rune* href; /* href attr */
  423. int target; /* target attr as targetid */
  424. };
  425. /* DestAnchor is for info about hyperlinks that are destinations */
  426. struct DestAnchor
  427. {
  428. DestAnchor*next; /* next in list of document's destanchors */
  429. int index; /* serial no. of anchor within its doc */
  430. Rune* name; /* name attr */
  431. Item* item; /* the destination */
  432. };
  433. /* Maps (client side) */
  434. struct Map
  435. {
  436. Map* next; /* next in list of document's maps */
  437. Rune* name; /* map name */
  438. Area* areas; /* list of map areas */
  439. };
  440. struct Area
  441. {
  442. Area* next; /* next in list of a map's areas */
  443. int shape; /* SHrect, etc. */
  444. Rune* href; /* associated hypertext link */
  445. int target; /* associated target frame */
  446. Dimen* coords; /* array of coords for shape */
  447. int ncoords; /* size of coords array */
  448. };
  449. /* Area shapes */
  450. enum {
  451. SHrect, SHcircle, SHpoly
  452. };
  453. /* Fonts are represented by integers: style*NumSize + size */
  454. /* Font styles */
  455. enum {
  456. FntR, /* roman */
  457. FntI, /* italic */
  458. FntB, /* bold */
  459. FntT, /* typewriter */
  460. NumStyle
  461. };
  462. /* Font sizes */
  463. enum {
  464. Tiny,
  465. Small,
  466. Normal,
  467. Large,
  468. Verylarge,
  469. NumSize
  470. };
  471. enum {
  472. NumFnt = NumStyle*NumSize,
  473. DefFnt = FntR*NumSize+Normal,
  474. };
  475. /* Lines are needed through some text items, for underlining or strikethrough */
  476. enum {
  477. ULnone, ULunder, ULmid
  478. };
  479. /* Kidinfo flags */
  480. enum {
  481. FRnoresize = (1<<0),
  482. FRnoscroll = (1<<1),
  483. FRhscroll = (1<<2),
  484. FRvscroll = (1<<3),
  485. FRhscrollauto = (1<<4),
  486. FRvscrollauto = (1<<5)
  487. };
  488. /* Information about child frame or frameset */
  489. struct Kidinfo
  490. {
  491. Kidinfo*next; /* in list of kidinfos for a frameset */
  492. int isframeset;
  493. /* fields for "frame" */
  494. Rune* src; /* only nil if a "dummy" frame or this is frameset */
  495. Rune* name; /* always non-empty if this isn't frameset */
  496. int marginw;
  497. int marginh;
  498. int framebd;
  499. int flags;
  500. /* fields for "frameset" */
  501. Dimen* rows; /* array of row dimensions */
  502. int nrows; /* length of rows */
  503. Dimen* cols; /* array of col dimensions */
  504. int ncols; /* length of cols */
  505. Kidinfo*kidinfos;
  506. Kidinfo*nextframeset; /* parsing stack */
  507. };
  508. /* Document info (global information about HTML page) */
  509. struct Docinfo
  510. {
  511. /* stuff from HTTP headers, doc head, and body tag */
  512. Rune* src; /* original source of doc */
  513. Rune* base; /* base URL of doc */
  514. Rune* doctitle; /* from <title> element */
  515. Background background; /* background specification */
  516. Iimage* backgrounditem; /* Image Item for doc background image, or nil */
  517. int text; /* doc foreground (text) color */
  518. int link; /* unvisited hyperlink color */
  519. int vlink; /* visited hyperlink color */
  520. int alink; /* highlighting hyperlink color */
  521. int target; /* target frame default */
  522. int chset; /* ISO_8859, etc. */
  523. int mediatype; /* TextHtml, etc. */
  524. int scripttype; /* TextJavascript, etc. */
  525. int hasscripts; /* true if scripts used */
  526. Rune* refresh; /* content of <http-equiv=Refresh ...> */
  527. Kidinfo*kidinfo; /* if a frameset */
  528. int frameid; /* id of document frame */
  529. /* info needed to respond to user actions */
  530. Anchor* anchors; /* list of href anchors */
  531. DestAnchor*dests; /* list of destination anchors */
  532. Form* forms; /* list of forms */
  533. Table* tables; /* list of tables */
  534. Map* maps; /* list of maps */
  535. Iimage* images; /* list of image items (through nextimage links) */
  536. };
  537. extern int dimenkind(Dimen d);
  538. extern int dimenspec(Dimen d);
  539. extern void freedocinfo(Docinfo* d);
  540. extern void freeitems(Item* ithead);
  541. extern Item* parsehtml(uchar* data, int datalen, Rune* src, int mtype, int chset, Docinfo** pdi);
  542. extern void printitems(Item* items, char* msg);
  543. extern int targetid(Rune* s);
  544. extern Rune* targetname(int targid);
  545. extern int validitems(Item* i);
  546. #pragma varargck type "I" Item*
  547. /* Control print output */
  548. extern int warn;
  549. extern int dbglex;
  550. extern int dbgbuild;
  551. /*
  552. * To be provided by caller
  553. * emalloc and erealloc should not return if can't get memory.
  554. * emalloc should zero its memory.
  555. */
  556. extern void* emalloc(ulong);
  557. extern void* erealloc(void* p, ulong size);