html.h 15 KB

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