Using the LibCSS API ==================== This document explains how to use LibCSS. In addition to this document, please see the examples and the headers (found in /usr/local/include/libcss or a similar location). Experience with C is assumed. Using the library consists of the following general steps: 1. Initialize the library. 2. Load one or more CSS files. 3. Use the Selection API to determine styles. 4. Use the computed styles. 5. Shut down the library. Please see example1.c for a demonstration of these steps. Initialize the library ---------------------- The library is initialized using css_initialise(): css_initialise("Aliases", myrealloc, 0); The first argument is the pathname of an Aliases file, which maps character encoding names to their canonical form. For an example, see test/data/Aliases. The 2nd argument is an allocation function. All allocations required by library initialization will be made by calling this function. It takes the same arguments as realloc(); a pointer and a size. If pointer is NULL a new block is being requested. If size is 0 the block should be freed. Otherwise an existing block is being resized. In many cases this function can simply call realloc(). The allocation function also takes a private data pointer, which is the third argument to css_initialise(). This is not used by LibCSS but may be used to communicate context to the allocation function. The allocation function pointer and private data pointer are arguments to many LibCSS functions and work in the same way. css_initialise() returns a css_error value. It is CSS_OK if everything worked, and an error code otherwise. The error codes are defined in libcss/errors.h. Many LibCSS functions return a css_error value. Checking the return value of every call that does is advised, for example: css_error code; code = css_initialise("../test/data/Aliases", myrealloc, 0); if (code != CSS_OK) { fprintf(stderr, "ERROR: css_initialise failed: %s\n", css_error_to_string(code)); exit(EXIT_FAILURE); } LibCSS depends on LibWapcaplet. This must be initialized before LibCSS. For example: lwc_code = lwc_initialise(myrealloc, NULL, 0); if (lwc_code != lwc_error_ok) ... Load one or more CSS files -------------------------- A stylesheet is represented by the opaque type css_stylesheet. To create one, use css_stylesheet_create(), for example: css_stylesheet *sheet; code = css_stylesheet_create(CSS_LEVEL_DEFAULT, "UTF-8", "", NULL, false, false, myrealloc, 0, resolve_url, 0, &sheet); if (code != CSS_OK) ... The arguments are as follows: css_language_level level Which version of CSS the stylesheet should be treated as. It currently has no effect and is reserved for future use. The recommended value is CSS_LEVEL_DEFAULT. const char *charset The encoding of the stylesheet data, or NULL if LibCSS should attempt to detect it. If the encoding is known, for example from the Content-Type header or a file attribute, then it should be supplied here. const char *url The URL that the stylesheet was retrieved from. LibCSS uses this along with the resolve function (see below) to convert relative URLs in the stylesheet (e.g. imports, background images) to absolute URLs. If the stylesheet has no URL, use "". const char *title This is intended for the stylesheet title (for example from the tag). The title is not used by LibCSS but may be retrieved using css_stylesheet_get_title(). May be NULL if there is no title. bool allow_quirks bool inline_style css_allocator_fn alloc void *alloc_pw css_url_resolution_fn resolve void *resolve_pw css_stylesheet **stylesheet Updated with the newly created stylesheet object. Once the stylesheet has been created, CSS source data can be added to it. LibCSS parses the data into internal structures. Only data in memory is supported; you must handle reading from files or the network if required. Data is added using css_stylesheet_append_data(), for example: code = css_stylesheet_append_data(sheet, data, length); if (code != CSS_OK && code != CSS_NEEDDATA) ... The second argument is a pointer to a buffer containing some CSS to be parsed, with length in bytes given in the 3rd argument. This function may be called repeatedly with more data from the same stylesheet, for example as data arrives over the network. The return value may be CSS_NEEDDATA instead of CSS_OK. This indicates that more data may be expected. The two states can be treated identically. When all the data has been supplied, css_stylesheet_data_done() completes the processing: code = css_stylesheet_data_done(sheet); if (code != CSS_OK) ... The stylesheet is now in memory and ready for further use. Use the Selection API to determine styles ----------------------------------------- The Selection API is currently the only way to get information about styles from stylesheets that have been loaded. It takes a document node as input and returns the computed style that applies to that node. For example, it can be used to answer the question "What style should this