html.h 15 KB

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