html.h 15 KB

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