[project @ 2002-06-26 23:27:30 by bursa]

Implied table elements and colspan implemented.

svn path=/import/netsurf/; revision=24
This commit is contained in:
James Bursa 2002-06-26 23:27:30 +00:00
parent 900a3a9b24
commit df3341cb43
6 changed files with 135 additions and 46 deletions

View File

@ -1,5 +1,5 @@
/** /**
* $Id: box.c,v 1.7 2002/06/26 12:19:24 bursa Exp $ * $Id: box.c,v 1.8 2002/06/26 23:27:30 bursa Exp $
*/ */
#include <assert.h> #include <assert.h>
@ -18,6 +18,7 @@
*/ */
void box_add_child(struct box * parent, struct box * child); void box_add_child(struct box * parent, struct box * child);
struct box * box_create(xmlNode * node, box_type type, struct css_style * style);
struct css_style * box_get_style(struct css_stylesheet * stylesheet, struct css_style * parent_style, struct css_style * box_get_style(struct css_stylesheet * stylesheet, struct css_style * parent_style,
xmlNode * n, struct css_selector * selector, unsigned int depth); xmlNode * n, struct css_selector * selector, unsigned int depth);
@ -37,6 +38,18 @@ void box_add_child(struct box * parent, struct box * child)
child->parent = parent; child->parent = parent;
} }
/**
* create a box tree node
*/
struct box * box_create(xmlNode * node, box_type type, struct css_style * style)
{
struct box * box = xcalloc(1, sizeof(struct box));
box->node = node;
box->type = type;
box->style = style;
return box;
}
/** /**
* make a box tree with style data from an xml tree * make a box tree with style data from an xml tree
@ -60,17 +73,17 @@ struct box * xml_to_box(xmlNode * n, struct css_style * parent_style, struct css
{ {
struct box * box; struct box * box;
struct box * inline_container_c; struct box * inline_container_c;
struct css_style * style; struct css_style * style, * style2;
xmlNode * c; xmlNode * c;
xmlChar * s; char * s;
if (n->type == XML_ELEMENT_NODE) { if (n->type == XML_ELEMENT_NODE) {
/* work out the style for this element */ /* work out the style for this element */
*selector = xrealloc(*selector, (depth + 1) * sizeof(struct css_selector)); *selector = xrealloc(*selector, (depth + 1) * sizeof(struct css_selector));
(*selector)[depth].element = (const char *) n->name; (*selector)[depth].element = (const char *) n->name;
(*selector)[depth].class = (*selector)[depth].id = 0; (*selector)[depth].class = (*selector)[depth].id = 0;
if ((s = xmlGetProp(n, (xmlChar *) "class"))) if ((s = (char *) xmlGetProp(n, (xmlChar *) "class")))
(*selector)[depth].class = (char *) s; (*selector)[depth].class = s;
style = box_get_style(stylesheet, parent_style, n, *selector, depth + 1); style = box_get_style(stylesheet, parent_style, n, *selector, depth + 1);
} }
@ -81,6 +94,33 @@ struct box * xml_to_box(xmlNode * n, struct css_style * parent_style, struct css
if (inline_container == 0) { /* this is the first inline node: make a container */ if (inline_container == 0) { /* this is the first inline node: make a container */
inline_container = xcalloc(1, sizeof(struct box)); inline_container = xcalloc(1, sizeof(struct box));
inline_container->type = BOX_INLINE_CONTAINER; inline_container->type = BOX_INLINE_CONTAINER;
switch (parent->type) {
case BOX_TABLE:
/* insert implied table row and cell */
style2 = xcalloc(1, sizeof(struct css_style));
memcpy(style2, parent_style, sizeof(struct css_style));
css_cascade(style2, &css_blank_style);
box = box_create(0, BOX_TABLE_ROW, style2);
box_add_child(parent, box);
parent = box;
/* fall through */
case BOX_TABLE_ROW:
/* insert implied table cell */
style2 = xcalloc(1, sizeof(struct css_style));
memcpy(style2, parent_style, sizeof(struct css_style));
css_cascade(style2, &css_blank_style);
box = box_create(0, BOX_TABLE_CELL, style2);
box->colspan = 1;
box_add_child(parent, box);
parent = box;
break;
case BOX_BLOCK:
case BOX_TABLE_CELL:
case BOX_FLOAT:
break;
default:
assert(0);
}
box_add_child(parent, inline_container); box_add_child(parent, inline_container);
} }
box = calloc(1, sizeof(struct box)); box = calloc(1, sizeof(struct box));
@ -102,10 +142,34 @@ struct box * xml_to_box(xmlNode * n, struct css_style * parent_style, struct css
} else if (n->type == XML_ELEMENT_NODE) { } else if (n->type == XML_ELEMENT_NODE) {
switch (style->display) { switch (style->display) {
case CSS_DISPLAY_BLOCK: /* blocks get a node in the box tree */ case CSS_DISPLAY_BLOCK: /* blocks get a node in the box tree */
box = xcalloc(1, sizeof(struct box)); switch (parent->type) {
box->node = n; case BOX_TABLE:
box->type = BOX_BLOCK; /* insert implied table row and cell */
box->style = style; style2 = xcalloc(1, sizeof(struct css_style));
memcpy(style2, parent_style, sizeof(struct css_style));
css_cascade(style2, &css_blank_style);
box = box_create(0, BOX_TABLE_ROW, style2);
box_add_child(parent, box);
parent = box;
/* fall through */
case BOX_TABLE_ROW:
/* insert implied table cell */
style2 = xcalloc(1, sizeof(struct css_style));
memcpy(style2, parent_style, sizeof(struct css_style));
css_cascade(style2, &css_blank_style);
box = box_create(0, BOX_TABLE_CELL, style2);
box->colspan = 1;
box_add_child(parent, box);
parent = box;
break;
case BOX_BLOCK:
case BOX_TABLE_CELL:
case BOX_FLOAT:
break;
default:
assert(0);
}
box = box_create(n, BOX_BLOCK, style);
box_add_child(parent, box); box_add_child(parent, box);
inline_container_c = 0; inline_container_c = 0;
for (c = n->children; c != 0; c = c->next) for (c = n->children; c != 0; c = c->next)
@ -119,10 +183,7 @@ struct box * xml_to_box(xmlNode * n, struct css_style * parent_style, struct css
selector, depth + 1, parent, inline_container); selector, depth + 1, parent, inline_container);
break; break;
case CSS_DISPLAY_TABLE: case CSS_DISPLAY_TABLE:
box = xcalloc(1, sizeof(struct box)); box = box_create(n, BOX_TABLE, style);
box->node = n;
box->type = BOX_TABLE;
box->style = style;
box_add_child(parent, box); box_add_child(parent, box);
for (c = n->children; c != 0; c = c->next) for (c = n->children; c != 0; c = c->next)
xml_to_box(c, style, stylesheet, xml_to_box(c, style, stylesheet,
@ -130,11 +191,17 @@ struct box * xml_to_box(xmlNode * n, struct css_style * parent_style, struct css
inline_container = 0; inline_container = 0;
break; break;
case CSS_DISPLAY_TABLE_ROW: case CSS_DISPLAY_TABLE_ROW:
if (parent->type != BOX_TABLE) {
/* insert implied table */
style2 = xcalloc(1, sizeof(struct css_style));
memcpy(style2, parent_style, sizeof(struct css_style));
css_cascade(style2, &css_blank_style);
box = box_create(0, BOX_TABLE, style2);
box_add_child(parent, box);
parent = box;
}
assert(parent->type == BOX_TABLE); assert(parent->type == BOX_TABLE);
box = xcalloc(1, sizeof(struct box)); box = box_create(n, BOX_TABLE_ROW, style);
box->node = n;
box->type = BOX_TABLE_ROW;
box->style = style;
box_add_child(parent, box); box_add_child(parent, box);
for (c = n->children; c != 0; c = c->next) for (c = n->children; c != 0; c = c->next)
xml_to_box(c, style, stylesheet, xml_to_box(c, style, stylesheet,
@ -143,10 +210,12 @@ struct box * xml_to_box(xmlNode * n, struct css_style * parent_style, struct css
break; break;
case CSS_DISPLAY_TABLE_CELL: case CSS_DISPLAY_TABLE_CELL:
assert(parent->type == BOX_TABLE_ROW); assert(parent->type == BOX_TABLE_ROW);
box = xcalloc(1, sizeof(struct box)); box = box_create(n, BOX_TABLE_CELL, style);
box->node = n; if ((s = (char *) xmlGetProp(n, (xmlChar *) "colspan"))) {
box->type = BOX_TABLE_CELL; if ((box->colspan = strtol(s, 0, 10)) == 0)
box->style = style; box->colspan = 1;
} else
box->colspan = 1;
box_add_child(parent, box); box_add_child(parent, box);
inline_container_c = 0; inline_container_c = 0;
for (c = n->children; c != 0; c = c->next) for (c = n->children; c != 0; c = c->next)
@ -221,16 +290,19 @@ void box_dump(struct box * box, unsigned int depth)
fprintf(stderr, "x%li y%li w%li h%li ", box->x, box->y, box->width, box->height); fprintf(stderr, "x%li y%li w%li h%li ", box->x, box->y, box->width, box->height);
switch (box->type) { switch (box->type) {
case BOX_BLOCK: fprintf(stderr, "BOX_BLOCK <%s> ", box->node->name); break; case BOX_BLOCK: fprintf(stderr, "BOX_BLOCK "); break;
case BOX_INLINE_CONTAINER: fprintf(stderr, "BOX_INLINE_CONTAINER "); break; case BOX_INLINE_CONTAINER: fprintf(stderr, "BOX_INLINE_CONTAINER "); break;
case BOX_INLINE: fprintf(stderr, "BOX_INLINE '%.*s' ", case BOX_INLINE: fprintf(stderr, "BOX_INLINE '%.*s' ",
(int) box->length, box->text); break; (int) box->length, box->text); break;
case BOX_TABLE: fprintf(stderr, "BOX_TABLE <%s> ", box->node->name); break; case BOX_TABLE: fprintf(stderr, "BOX_TABLE "); break;
case BOX_TABLE_ROW: fprintf(stderr, "BOX_TABLE_ROW <%s> ", box->node->name); break; case BOX_TABLE_ROW: fprintf(stderr, "BOX_TABLE_ROW "); break;
case BOX_TABLE_CELL: fprintf(stderr, "BOX_TABLE_CELL <%s> ", box->node->name); break; case BOX_TABLE_CELL: fprintf(stderr, "BOX_TABLE_CELL [colspan %i] ",
case BOX_FLOAT: fprintf(stderr, "BOX_FLOAT <%s> ", box->node->name); break; box->colspan); break;
case BOX_FLOAT: fprintf(stderr, "BOX_FLOAT "); break;
default: fprintf(stderr, "Unknown box type "); default: fprintf(stderr, "Unknown box type ");
} }
if (box->node)
fprintf(stderr, "<%s> ", box->node->name);
if (box->style) if (box->style)
css_dump_style(box->style); css_dump_style(box->style);
fprintf(stderr, "\n"); fprintf(stderr, "\n");

View File

@ -1,19 +1,24 @@
/** /**
* $Id: box.h,v 1.3 2002/06/18 21:24:21 bursa Exp $ * $Id: box.h,v 1.4 2002/06/26 23:27:30 bursa Exp $
*/ */
/** /**
* structures * structures
*/ */
typedef enum {
BOX_BLOCK, BOX_INLINE_CONTAINER, BOX_INLINE,
BOX_TABLE, BOX_TABLE_ROW, BOX_TABLE_CELL, BOX_FLOAT
} box_type;
struct box { struct box {
enum { BOX_BLOCK, BOX_INLINE_CONTAINER, BOX_INLINE, box_type type;
BOX_TABLE, BOX_TABLE_ROW, BOX_TABLE_CELL, BOX_FLOAT } type;
xmlNode * node; xmlNode * node;
struct css_style * style; struct css_style * style;
unsigned long x, y, width, height; unsigned long x, y, width, height;
const char * text; const char * text;
unsigned int length; unsigned int length;
unsigned int colspan;
struct box * next; struct box * next;
struct box * children; struct box * children;
struct box * last; struct box * last;

View File

@ -1,5 +1,5 @@
/** /**
* $Id: css.h,v 1.5 2002/06/21 18:16:24 bursa Exp $ * $Id: css.h,v 1.6 2002/06/26 23:27:30 bursa Exp $
*/ */
#include "css_enum.h" #include "css_enum.h"
@ -76,6 +76,7 @@ struct css_selector {
extern const struct css_style css_base_style; extern const struct css_style css_base_style;
extern const struct css_style css_empty_style; extern const struct css_style css_empty_style;
extern const struct css_style css_blank_style;
/** /**
* interface * interface

View File

@ -1,5 +1,5 @@
/** /**
* $Id: layout.c,v 1.9 2002/06/26 12:19:24 bursa Exp $ * $Id: layout.c,v 1.10 2002/06/26 23:27:30 bursa Exp $
*/ */
#include <assert.h> #include <assert.h>
@ -327,13 +327,13 @@ struct box * layout_line(struct box * first, unsigned long width, unsigned long
/* the last box went over the end */ /* the last box went over the end */
char * space = strchr(c->text, ' '); char * space = strchr(c->text, ' ');
char * space2 = space; char * space2 = space;
unsigned long w, wp = w; unsigned long w, wp;
struct box * c2; struct box * c2;
if (space == 0) if (space == 0)
w = font_width(c->style, c->text, c->length); wp = w = font_width(c->style, c->text, c->length);
else else
w = font_width(c->style, c->text, space - c->text); wp = w = font_width(c->style, c->text, space - c->text);
if (x1 - x0 < xp + w && left == 0 && right == 0 && c == first) { if (x1 - x0 < xp + w && left == 0 && right == 0 && c == first) {
/* first word doesn't fit, but no floats and first on line so force in */ /* first word doesn't fit, but no floats and first on line so force in */
@ -447,6 +447,7 @@ void layout_table(struct box * table, unsigned long width, struct box * cont,
unsigned long y = 0; unsigned long y = 0;
unsigned long * xs; unsigned long * xs;
unsigned int i; unsigned int i;
unsigned int subcol;
struct box * c; struct box * c;
struct box * r; struct box * r;
@ -479,10 +480,10 @@ void layout_table(struct box * table, unsigned long width, struct box * cont,
break; break;
case CSS_WIDTH_AUTO: case CSS_WIDTH_AUTO:
default: default:
auto_columns++; auto_columns += c->colspan;
break; break;
} }
columns++; columns += c->colspan;
} }
if (auto_columns == 0 && table->style->width.width != CSS_WIDTH_AUTO) if (auto_columns == 0 && table->style->width.width != CSS_WIDTH_AUTO)
@ -495,13 +496,14 @@ void layout_table(struct box * table, unsigned long width, struct box * cont,
/* find column widths */ /* find column widths */
xs = xcalloc(columns + 1, sizeof(*xs)); xs = xcalloc(columns + 1, sizeof(*xs));
xs[0] = x = 0; xs[0] = x = 0;
for (i = 1, c = table->children->children; c != 0; i++, c = c->next) { for (i = 1, c = table->children->children, subcol = 1; c != 0; i++) {
switch (c->style->width.width) { switch (c->style->width.width) {
case CSS_WIDTH_LENGTH: case CSS_WIDTH_LENGTH:
x += len(&c->style->width.value.length, c->style) + extra_width; x += len(&c->style->width.value.length, c->style) / c->colspan + extra_width;
break; break;
case CSS_WIDTH_PERCENT: case CSS_WIDTH_PERCENT:
x += table_width * c->style->width.value.percent / 100 + extra_width; x += table_width * c->style->width.value.percent / 100 / c->colspan
+ extra_width;
break; break;
case CSS_WIDTH_AUTO: case CSS_WIDTH_AUTO:
default: default:
@ -509,6 +511,11 @@ void layout_table(struct box * table, unsigned long width, struct box * cont,
break; break;
} }
xs[i] = x; xs[i] = x;
if (subcol == c->colspan)
c = c->next,
subcol = 1;
else
subcol++;
/*printf("%i ", x);*/ /*printf("%i ", x);*/
} }
/*printf("\n");*/ /*printf("\n");*/
@ -519,8 +526,8 @@ void layout_table(struct box * table, unsigned long width, struct box * cont,
/* position cells */ /* position cells */
for (r = table->children; r != 0; r = r->next) { for (r = table->children; r != 0; r = r->next) {
unsigned long height = 0; unsigned long height = 0;
for (i = 0, c = r->children; c != 0; i++, c = c->next) { for (i = 0, c = r->children; c != 0; i += c->colspan, c = c->next) {
c->width = xs[i+1] - xs[i]; c->width = xs[i + c->colspan] - xs[i];
c->float_children = 0; c->float_children = 0;
c->height = layout_block_children(c, c->width, c, 0, 0); c->height = layout_block_children(c, c->width, c, 0, 0);
switch (c->style->height.height) { switch (c->style->height.height) {

View File

@ -1,5 +1,5 @@
/** /**
* $Id: render.c,v 1.15 2002/06/26 12:19:24 bursa Exp $ * $Id: render.c,v 1.16 2002/06/26 23:27:30 bursa Exp $
*/ */
#include <assert.h> #include <assert.h>
@ -102,7 +102,7 @@ void render_dump(struct box * box, unsigned long x, unsigned long y)
case BOX_TABLE_ROW: case BOX_TABLE_ROW:
case BOX_TABLE_CELL: case BOX_TABLE_CELL:
case BOX_FLOAT: case BOX_FLOAT:
case BOX_BLOCK: name = (const char *) box->node->name; case BOX_BLOCK: if (box->node) name = (const char *) box->node->name;
break; break;
case BOX_INLINE: case BOX_INLINE:
case BOX_INLINE_CONTAINER: case BOX_INLINE_CONTAINER:
@ -117,6 +117,10 @@ void render_dump(struct box * box, unsigned long x, unsigned long y)
for (i = 0; i < box->length; i++) { for (i = 0; i < box->length; i++) {
if (box->text[i] == '"') if (box->text[i] == '"')
printf("\\\""); printf("\\\"");
else if (box->text[i] == '[')
printf("\\[");
else if (box->text[i] == '$')
printf("\\$");
else else
printf("%c", box->text[i]); printf("%c", box->text[i]);
} }

View File

@ -3,10 +3,10 @@ html { font-size: medium }
address, blockquote, body, dd, div, address, blockquote, body, dd, div,
dl, dt, fieldset, form, dl, dt, fieldset, form,
h1, h2, h3, h4, h5, h6, html, h1, h2, h3, h4, h5, h6, html,
object, ol, p, ul object, ol, p, ul,
hr, menu, pre { display: block } hr, menu, pre { display: block }
a, abbr, acronym, b, code a, abbr, acronym, b, code,
em, i, q, s, strong, u, em, i, q, s, strong, u, font, span,
var { display: inline } var { display: inline }
li { display: list-item } li { display: list-item }
head { display: none } head { display: none }