summaryrefslogtreecommitdiff
path: root/render/list.c
blob: 5bcf36ffcf8e23678668e7368ea3723a579f5971 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
/*
 * Copyright 2005 Richard Wilson <info@tinct.net>
 *
 * This file is part of NetSurf, http://www.netsurf-browser.org/
 *
 * NetSurf is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; version 2 of the License.
 *
 * NetSurf is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/** \file
 * HTML lists (implementation).
 */
#include <assert.h>
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
#include "css/css.h"
#include "render/list.h"
#include "utils/log.h"


struct list_counter {
  	char *name;				/** Counter name */
  	struct list_counter_state *first;	/** First counter state */
  	struct list_counter_state *state;	/** Current counter state */
	struct list_counter *next;		/** Next counter */
};

struct list_counter_state {
	int count;				/** Current count */
	struct list_counter_state *parent;	/** Parent counter, or NULL */
	struct list_counter_state *next;	/** Next counter, or NULL */
};

static struct list_counter *list_counter_pool = NULL;
static char list_counter_workspace[16];

static const char *list_counter_roman[] = { "I", "IV", "V", "IX",
					    "X", "XL", "L", "XC",
					    "C", "CD", "D", "CM",
					    "M"};
static const int list_counter_decimal[] = {    1,   4,   5,   9,
					      10,  40,  50,  90,
					     100, 400, 500, 900,
					     1000};
#define ROMAN_DECIMAL_CONVERSIONS (sizeof(list_counter_decimal) \
		/ sizeof(list_counter_decimal[0]))


static struct list_counter *render_list_find_counter(char *name);
static char *render_list_encode_counter(struct list_counter_state *state,
		css_list_style_type style);
static char *render_list_encode_roman(int value);

/*
static void render_list_counter_output(char *name);
*/

/**
 * Finds a counter from the current pool, or adds a new one.
 *
 * \param name  the name of the counter to find
 * \return the counter, or NULL if it couldn't be found/created.
 */
static struct list_counter *render_list_find_counter(char *name) {
	struct list_counter *counter;

	assert(name);
	/* find a current counter */
	for (counter = list_counter_pool; counter; counter = counter->next)
		if (!strcasecmp(name, counter->name))
			return counter;

	/* create a new counter */
	counter = calloc(1, sizeof(struct list_counter));
	if (!counter) {
		LOG(("No memory for calloc()"));
		return NULL;
	}
	counter->name = malloc(strlen(name) + 1);
	if (!counter->name) {
		LOG(("No memory for malloc()"));
		return NULL;
	}
	strcpy(counter->name, name);
	counter->next = list_counter_pool;
	list_counter_pool = counter;
	return counter;
}


/**
 * Removes all counters from the current pool.
 */
void render_list_destroy_counters(void) {
	struct list_counter *counter = list_counter_pool;
	struct list_counter *next_counter;
	struct list_counter_state *state;
	struct list_counter_state *next_state;

	while (counter) {
		next_counter = counter->next;
		free(counter->name);
		state = counter->first;
		free(counter);
		counter = next_counter;
		while (state) {
			next_state = state->next;
			free(state);
			state = next_state;
		}
	}
	list_counter_pool = NULL;
}


/**
 * Resets a counter in accordance with counter-reset (CSS 2.1/12.4).
 *
 * \param name	 the name of the counter to reset
 * \param value  the value to reset the counter to
 * \return true on success, false on failure.
 */
bool render_list_counter_reset(char *name, int value) {
	struct list_counter *counter;
	struct list_counter_state *state;
	struct list_counter_state *link;

	assert(name);
	counter = render_list_find_counter(name);
	if (!counter)
		return false;
	state = calloc(1, sizeof(struct list_counter_state));
	if (!state) {
		LOG(("No memory for calloc()"));
		return false;
	}
	state->count = value;
	state->parent = counter->state;
	counter->state = state;
	if (!counter->first) {
		counter->first = state;
	} else {
	  	for (link = counter->first; link->next; link = link->next);
	  	link->next = state;
	}
/*	render_list_counter_output(name);
*/	return true;
}


/**
 * Increments a counter in accordance with counter-increment (CSS 2.1/12.4).
 *
 * \param name	 the name of the counter to reset
 * \param value  the value to increment the counter by
 * \return true on success, false on failure.
 */
bool render_list_counter_increment(char *name, int value) {
	struct list_counter *counter;

	assert(name);
	counter = render_list_find_counter(name);
	if (!counter)
		return false;
	/* if no counter-reset used, it is assumed the counter has been reset
	 * to 0 by the root element. */
	if (!counter->state) {
	  	if (counter->first) {
	  	  	counter->state = counter->first;
	  	  	counter->state->count = 0;
	  	} else {
			render_list_counter_reset(name, 0);
		}
	}
	if (counter->state)
		counter->state->count += value;
/*	render_list_counter_output(name);
*/	return (counter->state);
}


/**
 * Ends the scope of a counter.
 *
 * \param name	 the name of the counter to end the scope for
 * \return true on success, false on failure.
 */
bool render_list_counter_end_scope(char *name) {
	struct list_counter *counter;

	assert(name);
	counter = render_list_find_counter(name);
	if ((!counter) || (!counter->state))
		return false;
	counter->state = counter->state->parent;
/*	render_list_counter_output(name);
*/	return true;
}


/**
 * Returns a textual representation of a counter for counter() or counters()
 * (CSS 2.1/12.2).
 *
 * \param css_counter  the counter to convert
 * \return a textual representation of the counter, or NULL on failure
 */
char *render_list_counter(struct css_counter *css_counter) {
	struct list_counter *counter;
	struct list_counter_state *state;
	char *compound = NULL;
	char *merge, *extend;

	assert(css_counter);
	counter = render_list_find_counter(css_counter->name);
	if (!counter) {
	  	LOG(("Failed to find/create counter for conversion"));
	  	return NULL;
	}

	/* handle counter() first */
	if (!css_counter->separator)
		return render_list_encode_counter(counter->state,
				css_counter->style);

	/* loop through all states for counters() */
	for (state = counter->first; state; state = state->next) {
	  	merge = render_list_encode_counter(state,
	  			css_counter->style);
	  	if (!merge) {
	  	  	free(compound);
	  	  	return NULL;
	  	}
	  	if (!compound) {
	  		compound = merge;
	  	} else {
	  		extend = realloc(compound, strlen(compound) +
	  				strlen(merge) + 1);
	  		if (!extend) {
	  		  	LOG(("No memory for realloc()"));
	  		  	free(compound);
	  		  	free(merge);
	  		  	return NULL;
	  		}
	  		compound = extend;
	  		strcat(compound, merge);
	  	}
		if (state->next) {
			merge = realloc(compound, strlen(compound) +
					strlen(css_counter->separator) + 1);
			if (!merge) {
	  		  	LOG(("No memory for realloc()"));
				free(compound);
				return NULL;
			}
			compound = merge;
			strcat(compound, css_counter->separator);
		}
	}
	return compound;
}


/**
 * Returns a textual representation of a counter state in a specified style.
 *
 * \param state  the counter state to represent
 * \param style  the counter style to use
 * \return a textual representation of the counter state, or NULL on failure
 */
static char *render_list_encode_counter(struct list_counter_state *state,
		css_list_style_type style) {
	char *result = NULL;
	int i;

	/* no counter state means that the counter is currently out of scope */
	if (!state) {
		result = malloc(1);
		if (!result)
			return NULL;
		result[0] = '\0';
		return result;
	}

	/* perform the relevant encoding to upper case where applicable */
	switch (style) {
		case CSS_LIST_STYLE_TYPE_LOWER_ALPHA:
		case CSS_LIST_STYLE_TYPE_UPPER_ALPHA:
			if (state->count <= 0)
			  	result = calloc(1, 1);
			else
				result = malloc(2);
			if (!result)
				return NULL;
			if (state->count > 0) {
				result[0] = 'A' + (state->count - 1) % 26;
				result[1] = '\0';
			}
			break;
		case CSS_LIST_STYLE_TYPE_DISC:
		case CSS_LIST_STYLE_TYPE_CIRCLE:
		case CSS_LIST_STYLE_TYPE_SQUARE:
			result = malloc(2);
			if (!result)
				return NULL;
			result[0] = '?';
			result[1] = '\0';
			break;
		case CSS_LIST_STYLE_TYPE_LOWER_ROMAN:
		case CSS_LIST_STYLE_TYPE_UPPER_ROMAN:
			result = render_list_encode_roman(state->count);
			if (!result)
				return NULL;
			break;
		case CSS_LIST_STYLE_TYPE_DECIMAL:
			snprintf(list_counter_workspace,
					sizeof list_counter_workspace,
					"%i", state->count);
			result = malloc(strlen(list_counter_workspace) + 1);
			if (!result)
				return NULL;
			strcpy(result, list_counter_workspace);
			break;
		case CSS_LIST_STYLE_TYPE_NONE:
			result = malloc(1);
			if (!result)
				return NULL;
			result[0] = '\0';
			break;
		case CSS_LIST_STYLE_TYPE_INHERIT:
		case CSS_LIST_STYLE_TYPE_UNKNOWN:
		case CSS_LIST_STYLE_TYPE_NOT_SET:
			assert(0);
			break;
	}

	/* perform case conversion */
	if ((style == CSS_LIST_STYLE_TYPE_LOWER_ALPHA) ||
			(style == CSS_LIST_STYLE_TYPE_LOWER_ROMAN))
		for (i = 0; result[i]; i++)
			result[i] = tolower(result[i]);

	return result;
}


/**
 * Encodes a value in roman numerals.
 * For values that cannot be represented (ie <=0) an empty string is returned.
 *
 * \param value  the value to represent
 * \return a string containing the representation, or NULL on failure
 */
static char *render_list_encode_roman(int value) {
	int i, overflow, p = 0;
	char temp[10];
	char *result;

	/* zero and below is returned as an empty string and not erred as
	 * if it is counters() will fail to complete other scopes. */
	if (value <= 0) {
		result = malloc(1);
		if (!result)
			return NULL;
		result[0] = '\0';
		return result;
	}

	/* we only calculate 1->999 and then add a M for each thousand */
	overflow = value / 1000;
	value = value % 1000;

	/* work backwards through the array */
	for (i = ROMAN_DECIMAL_CONVERSIONS - 1; i >= 0; i--) {
		while (value >= list_counter_decimal[0]) {
			if (value - list_counter_decimal[i] <
					list_counter_decimal[0] - 1)
				break;
			value -= list_counter_decimal[i];
			temp[p++] = list_counter_roman[i][0];
			if (i & 0x1)
				temp[p++] = list_counter_roman[i][1];
		}
	}
	temp[p] = '\0';

	/* create a copy for the caller including thousands */
	result = malloc(p + overflow + 1);
	if (!result)
		return NULL;
	for (i = 0; i < overflow; i++)
		result[i] = 'M';
	strcpy(&result[overflow], temp);
	return result;
}








void render_list_test(void) {
  	/* example given in CSS2.1/12.4.1 */
/*	render_list_counter_reset("item", 0);
	render_list_counter_increment("item", 1);
	render_list_counter_increment("item", 1);
	render_list_counter_reset("item", 0);
	render_list_counter_increment("item", 1);
	render_list_counter_increment("item", 1);
	render_list_counter_increment("item", 1);
	render_list_counter_reset("item", 0);
	render_list_counter_increment("item", 1);
	render_list_counter_end_scope("item");
	render_list_counter_reset("item", 0);
	render_list_counter_increment("item", 1);
	render_list_counter_end_scope("item");
	render_list_counter_increment("item", 1);
	render_list_counter_end_scope("item");
	render_list_counter_increment("item", 1);
	render_list_counter_increment("item", 1);
	render_list_counter_end_scope("item");
	render_list_counter_reset("item", 0);
	render_list_counter_increment("item", 1);
	render_list_counter_increment("item", 1);
	render_list_counter_end_scope("item");
*/
}
/*
static void render_list_counter_output(char *name) {
	struct list_counter *counter;
	char *result;
	struct css_counter css_counter;

	assert(name);
	counter = render_list_find_counter(name);
	if (!counter) {
	  	fprintf(stderr, "Unable to create counter '%s'\n", name);
		return;
	}

	css_counter.name = name;
	css_counter.style = CSS_LIST_STYLE_TYPE_LOWER_ALPHA;
	css_counter.separator = NULL;
	result = render_list_counter(&css_counter);
	if (!result) {
		fprintf(stderr, "Failed to output counter('%s')\n", name);
	} else {
		fprintf(stderr, "counter('%s') is '%s'\n", name, result);
		free(result);
	}
	css_counter.separator = ".";
	result = render_list_counter(&css_counter);
	if (!result) {
		fprintf(stderr, "Failed to output counters('%s', '.')\n", name);
	} else {
		fprintf(stderr, "counters('%s', '.') is '%s'\n", name, result);
		free(result);
	}
}
*/