2005-09-15 09:58:31 -04:00
|
|
|
/* CSS stylesheet handling */
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include "elinks.h"
|
|
|
|
|
|
|
|
#include "document/css/property.h"
|
|
|
|
#include "document/css/stylesheet.h"
|
|
|
|
#include "util/error.h"
|
|
|
|
#include "util/lists.h"
|
|
|
|
#include "util/memory.h"
|
|
|
|
#include "util/string.h"
|
|
|
|
|
|
|
|
|
|
|
|
/* You can find some mysterious functions commented out here. I planned to use
|
|
|
|
* them for various smart things (well they all report to
|
|
|
|
* merge_css_stylesheets()), but it turns out it makes no sense to merge
|
|
|
|
* stylesheets now (and maybe it won't in the future neither...). But maybe you
|
|
|
|
* will find them useful at some time, so... Dunno. --pasky */
|
|
|
|
|
|
|
|
|
|
|
|
struct css_selector *
|
2007-07-15 13:04:39 -04:00
|
|
|
find_css_selector(struct css_selector_set *sels, enum css_selector_type type,
|
2005-09-15 09:58:31 -04:00
|
|
|
enum css_selector_relation rel,
|
2007-07-15 15:19:30 -04:00
|
|
|
const unsigned char *name, int namelen)
|
2005-09-15 09:58:31 -04:00
|
|
|
{
|
|
|
|
struct css_selector *selector;
|
|
|
|
|
|
|
|
assert(sels && name);
|
|
|
|
|
2007-07-15 13:04:39 -04:00
|
|
|
foreach_css_selector (selector, sels) {
|
2005-09-15 09:58:31 -04:00
|
|
|
if (type != selector->type || rel != selector->relation)
|
|
|
|
continue;
|
|
|
|
if (strlcasecmp(name, namelen, selector->name, -1))
|
|
|
|
continue;
|
|
|
|
return selector;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct css_selector *
|
2007-07-15 14:53:35 -04:00
|
|
|
init_css_selector(struct css_selector_set *sels,
|
|
|
|
enum css_selector_type type,
|
|
|
|
enum css_selector_relation relation,
|
2005-09-15 09:58:31 -04:00
|
|
|
unsigned char *name, int namelen)
|
|
|
|
{
|
|
|
|
struct css_selector *selector;
|
|
|
|
|
|
|
|
selector = mem_calloc(1, sizeof(*selector));
|
|
|
|
if (!selector) return NULL;
|
|
|
|
|
2007-07-15 14:53:35 -04:00
|
|
|
selector->relation = relation;
|
2007-07-15 13:04:39 -04:00
|
|
|
init_css_selector_set(&selector->leaves);
|
2005-09-15 09:58:31 -04:00
|
|
|
|
|
|
|
selector->type = type;
|
|
|
|
init_list(selector->properties);
|
|
|
|
|
|
|
|
if (name) {
|
|
|
|
if (namelen < 0)
|
|
|
|
namelen = strlen(name);
|
|
|
|
selector->name = memacpy(name, namelen);
|
|
|
|
if (!selector->name) {
|
2007-07-15 13:04:39 -04:00
|
|
|
done_css_selector_set(&selector->leaves);
|
2005-09-15 09:58:31 -04:00
|
|
|
mem_free(selector);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
set_mem_comment(selector, name, namelen);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (sels) {
|
2007-07-15 13:04:39 -04:00
|
|
|
add_css_selector_to_set(selector, sels);
|
2005-09-15 09:58:31 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return selector;
|
|
|
|
}
|
|
|
|
|
2007-07-15 14:53:35 -04:00
|
|
|
void
|
|
|
|
set_css_selector_relation(struct css_selector *selector,
|
|
|
|
enum css_selector_relation relation)
|
|
|
|
{
|
|
|
|
/* Changing the relation after the selector is in a set might
|
|
|
|
* require setting css_relation_set.may_contain_rel_ancestor,
|
|
|
|
* but we don't have a pointer to the set here. */
|
|
|
|
assert(!css_selector_is_in_set(selector));
|
|
|
|
selector->relation = relation;
|
|
|
|
}
|
|
|
|
|
2005-09-15 09:58:31 -04:00
|
|
|
struct css_selector *
|
2007-07-15 13:04:39 -04:00
|
|
|
get_css_selector(struct css_selector_set *sels, enum css_selector_type type,
|
2005-09-15 09:58:31 -04:00
|
|
|
enum css_selector_relation rel,
|
|
|
|
unsigned char *name, int namelen)
|
|
|
|
{
|
|
|
|
struct css_selector *selector = NULL;
|
|
|
|
|
|
|
|
if (sels && name && namelen) {
|
|
|
|
selector = find_css_selector(sels, type, rel, name, namelen);
|
|
|
|
if (selector)
|
|
|
|
return selector;
|
|
|
|
}
|
|
|
|
|
2007-07-15 14:53:35 -04:00
|
|
|
return init_css_selector(sels, type, rel, name, namelen);
|
2005-09-15 09:58:31 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static struct css_selector *
|
|
|
|
copy_css_selector(struct css_stylesheet *css, struct css_selector *orig)
|
|
|
|
{
|
|
|
|
struct css_selector *copy;
|
|
|
|
|
|
|
|
assert(css && orig);
|
2007-07-15 14:53:35 -04:00
|
|
|
assert(orig->relation == CSR_ROOT);
|
2005-09-15 09:58:31 -04:00
|
|
|
|
2007-07-15 14:53:35 -04:00
|
|
|
copy = init_css_selector(&css->selectors, orig->type, CSR_ROOT,
|
2005-09-15 09:58:31 -04:00
|
|
|
orig->name, strlen(orig->name));
|
|
|
|
if (!copy)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
return copy;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
add_selector_property(struct css_selector *selector, struct css_property *prop)
|
|
|
|
{
|
|
|
|
struct css_property *newprop = mem_alloc(sizeof(*newprop));
|
|
|
|
|
|
|
|
if (newprop) {
|
|
|
|
copy_struct(newprop, prop);
|
|
|
|
add_to_list(selector->properties, newprop);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
add_selector_properties(struct css_selector *selector,
|
|
|
|
struct list_head *properties)
|
|
|
|
{
|
|
|
|
struct css_property *prop;
|
|
|
|
|
|
|
|
foreach (prop, *properties) {
|
|
|
|
add_selector_property(selector, prop);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct css_selector *
|
|
|
|
clone_css_selector(struct css_stylesheet *css, struct css_selector *orig)
|
|
|
|
{
|
|
|
|
struct css_selector *copy;
|
|
|
|
|
|
|
|
assert(css && orig);
|
|
|
|
|
|
|
|
copy = copy_css_selector(css, orig);
|
|
|
|
if (!copy)
|
|
|
|
return NULL;
|
|
|
|
add_selector_properties(copy, &orig->properties);
|
|
|
|
return copy;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
merge_css_selectors(struct css_selector *sel1, struct css_selector *sel2)
|
|
|
|
{
|
|
|
|
struct css_property *prop;
|
|
|
|
|
|
|
|
foreach (prop, sel2->properties) {
|
|
|
|
struct css_property *origprop;
|
|
|
|
|
|
|
|
foreach (origprop, sel1->properties)
|
|
|
|
if (origprop->type == prop->type) {
|
|
|
|
del_from_list(origprop);
|
|
|
|
mem_free(origprop);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Not there yet, let's add it. */
|
|
|
|
add_selector_property(sel1, prop);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
done_css_selector(struct css_selector *selector)
|
|
|
|
{
|
2007-07-15 13:04:39 -04:00
|
|
|
done_css_selector_set(&selector->leaves);
|
2005-09-15 09:58:31 -04:00
|
|
|
|
2007-07-15 13:04:39 -04:00
|
|
|
if (css_selector_is_in_set(selector))
|
|
|
|
del_css_selector_from_set(selector);
|
2005-09-15 09:58:31 -04:00
|
|
|
free_list(selector->properties);
|
|
|
|
mem_free_if(selector->name);
|
|
|
|
mem_free(selector);
|
|
|
|
}
|
|
|
|
|
2007-07-15 14:53:35 -04:00
|
|
|
void
|
|
|
|
init_css_selector_set(struct css_selector_set *set)
|
|
|
|
{
|
|
|
|
set->may_contain_rel_ancestor = 0;
|
|
|
|
init_list(set->list);
|
|
|
|
}
|
|
|
|
|
2007-07-15 13:04:39 -04:00
|
|
|
void
|
|
|
|
done_css_selector_set(struct css_selector_set *set)
|
|
|
|
{
|
|
|
|
while (!css_selector_set_empty(set)) {
|
|
|
|
done_css_selector(css_selector_set_front(set));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-07-15 14:53:35 -04:00
|
|
|
void
|
|
|
|
add_css_selector_to_set(struct css_selector *selector,
|
|
|
|
struct css_selector_set *set)
|
|
|
|
{
|
|
|
|
assert(!css_selector_is_in_set(selector));
|
|
|
|
|
|
|
|
add_to_list(set->list, selector);
|
|
|
|
if (selector->relation == CSR_ANCESTOR)
|
|
|
|
set->may_contain_rel_ancestor = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
del_css_selector_from_set(struct css_selector *selector)
|
|
|
|
{
|
|
|
|
del_from_list(selector);
|
|
|
|
selector->next = NULL;
|
|
|
|
selector->prev = NULL;
|
|
|
|
}
|
|
|
|
|
2005-09-15 09:58:31 -04:00
|
|
|
#ifdef DEBUG_CSS
|
|
|
|
void
|
2007-07-15 13:04:39 -04:00
|
|
|
dump_css_selector_tree_iter(struct css_selector_set *sels, int level)
|
2005-09-15 09:58:31 -04:00
|
|
|
{
|
|
|
|
struct css_selector *sel;
|
|
|
|
|
2007-07-15 13:04:39 -04:00
|
|
|
foreach_css_selector (sel, sels) {
|
2005-09-15 09:58:31 -04:00
|
|
|
struct css_property *prop;
|
|
|
|
|
|
|
|
fprintf(stderr, "%*s +- [%s] type %d rel %d props",
|
|
|
|
level * 4, " ",
|
|
|
|
sel->name, sel->type, sel->relation);
|
|
|
|
foreach (prop, sel->properties) {
|
|
|
|
fprintf(stderr, " [%d]", prop->type);
|
|
|
|
}
|
|
|
|
fprintf(stderr, "\n");
|
|
|
|
dump_css_selector_tree_iter(&sel->leaves, level + 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2007-07-15 13:04:39 -04:00
|
|
|
dump_css_selector_tree(struct css_selector_set *sels)
|
2005-09-15 09:58:31 -04:00
|
|
|
{
|
|
|
|
dump_css_selector_tree_iter(sels, 0);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
struct css_stylesheet *
|
|
|
|
init_css_stylesheet(css_stylesheet_importer_T importer, void *import_data)
|
|
|
|
{
|
|
|
|
struct css_stylesheet *css;
|
|
|
|
|
|
|
|
css = mem_calloc(1, sizeof(*css));
|
|
|
|
if (!css)
|
|
|
|
return NULL;
|
|
|
|
css->import = importer;
|
|
|
|
css->import_data = import_data;
|
2007-07-15 13:04:39 -04:00
|
|
|
init_css_selector_set(&css->selectors);
|
2005-09-15 09:58:31 -04:00
|
|
|
return css;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
mirror_css_stylesheet(struct css_stylesheet *css1, struct css_stylesheet *css2)
|
|
|
|
{
|
|
|
|
struct css_selector *selector;
|
|
|
|
|
2007-07-15 13:04:39 -04:00
|
|
|
foreach_css_selector (selector, &css1->selectors) {
|
2005-09-15 09:58:31 -04:00
|
|
|
clone_css_selector(css2, selector);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#if 0
|
|
|
|
struct css_stylesheet *
|
|
|
|
clone_css_stylesheet(struct css_stylesheet *orig)
|
|
|
|
{
|
|
|
|
struct css_stylesheet *copy;
|
|
|
|
struct css_selector *selector;
|
|
|
|
|
|
|
|
copy = init_css_stylesheet(orig->import, orig->import_data);
|
|
|
|
if (!copy)
|
|
|
|
return NULL;
|
|
|
|
mirror_css_stylesheet(orig, copy);
|
|
|
|
return copy;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
merge_css_stylesheets(struct css_stylesheet *css1,
|
|
|
|
struct css_stylesheet *css2)
|
|
|
|
{
|
|
|
|
struct css_selector *selector;
|
|
|
|
|
|
|
|
assert(css1 && css2);
|
|
|
|
|
|
|
|
/* This is 100% evil. And gonna be a huge bottleneck. Essentially
|
|
|
|
* O(N^2) where we could be much smarter (ie. sort it once and then
|
|
|
|
* always be O(N)). */
|
|
|
|
|
2007-07-15 13:04:39 -04:00
|
|
|
foreach_css_selector (selector, &css2->selectors) {
|
2005-09-15 09:58:31 -04:00
|
|
|
struct css_selector *origsel;
|
|
|
|
|
|
|
|
origsel = find_css_selector(&css1->selectors, selector->name,
|
|
|
|
strlen(selector->name));
|
|
|
|
if (!origsel) {
|
|
|
|
clone_css_selector(css1, selector);
|
|
|
|
} else {
|
|
|
|
merge_css_selectors(origsel, selector);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
void
|
|
|
|
done_css_stylesheet(struct css_stylesheet *css)
|
|
|
|
{
|
2007-07-15 13:04:39 -04:00
|
|
|
done_css_selector_set(&css->selectors);
|
2005-09-15 09:58:31 -04:00
|
|
|
}
|