File: csssearch.c

package info (click to toggle)
tk-html3 3.0~fossil20110109-6
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 6,644 kB
  • ctags: 5,882
  • sloc: ansic: 48,994; tcl: 26,030; sh: 1,190; yacc: 161; makefile: 24
file content (298 lines) | stat: -rw-r--r-- 8,188 bytes parent folder | download | duplicates (5)
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

#include "html.h"
#include "cssInt.h"

static const char rcsid[] = "$Id: csssearch.c,v 1.7 2007/10/27 08:37:50 hkoba Exp $";

/*-----------------------------------------------------------------------
 * 
 * Public interface to this subsystem:
 *
 *     Startup and shutdown:
 *
 *         HtmlCssSearchInit()
 *         HtmlCssSearchShutdown()
 *
 *     Query:
 *
 *         HtmlCssSearch()
 *
 *     Discard the contents of the cache:
 *
 *         HtmlCssSearchInvalidateCache()
 *
 */

#define SEARCH_MODE_ALL     1
#define SEARCH_MODE_INDEX   2
#define SEARCH_MODE_LENGTH  3

struct CssCachedSearch {
    int nAlloc;
    int nNode;
    HtmlNode **apNode;
};
typedef struct CssCachedSearch CssCachedSearch;

struct HtmlSearchCache {
    /* Map between CSS selector and search results. The contents
     * are discarded each time HtmlCssSearchInvalidateCache() is
     * called. 
     */
    Tcl_HashTable aCache;
};

struct CssSearch {
  CssRule *pRuleList;        /* The list of CSS selectors */
  HtmlTree *pTree;
  HtmlNode *pSearchRoot;     /* Root of sub-tree to search */
  CssCachedSearch *pCache;   /* Output */
};
typedef struct CssSearch CssSearch;

static int 
cssSearchCb(pTree, pNode, clientData)
    HtmlTree *pTree; 
    HtmlNode *pNode;
    ClientData clientData;
{
    CssSearch *pSearch = (CssSearch *)clientData;
    assert(pSearch->pRuleList);

    if (pNode != pSearch->pSearchRoot && 0 == HtmlNodeIsText(pNode)) {
        CssRule *p;
        for (
            p = pSearch->pRuleList; 
            p && 0 == HtmlCssSelectorTest(p->pSelector, pNode, 0);
            p = p->pNext
        );
        if (p) {
            CssCachedSearch *pCache = pSearch->pCache;
            if (pCache->nNode == pCache->nAlloc){
                pCache->nAlloc = (16 + (pCache->nAlloc * 2));
                pCache->apNode = (HtmlNode **)HtmlRealloc("SearchCache", 
                    pCache->apNode, (pCache->nAlloc * sizeof(HtmlNode *))
                );
            }
            pCache->apNode[pCache->nNode] = pNode;
            pCache->nNode++;
        }
    }
    return HTML_WALK_DESCEND;
}

int 
HtmlCssSearchInit(pTree)
    HtmlTree *pTree;
{
    pTree->pSearchCache = HtmlNew(HtmlSearchCache);
    Tcl_InitHashTable(&pTree->pSearchCache->aCache, TCL_STRING_KEYS);
    return TCL_OK;
}

int 
HtmlCssSearchInvalidateCache(pTree)
    HtmlTree *pTree;
{
    Tcl_HashSearch sSearch;
    Tcl_HashEntry *pEntry;
    Tcl_HashTable *p = &pTree->pSearchCache->aCache;

    while ((pEntry = Tcl_FirstHashEntry(p, &sSearch))) {
        CssCachedSearch *pCache = (CssCachedSearch *)Tcl_GetHashValue(pEntry);
	if (pCache) {
	  HtmlFree(pCache->apNode);
	  HtmlFree(pCache);
	}
        Tcl_DeleteHashEntry(pEntry);
    }
 
    return TCL_OK;
}

int 
HtmlCssSearchShutdown(pTree)
    HtmlTree *pTree;
{
    HtmlCssSearchInvalidateCache(pTree);
    Tcl_DeleteHashTable(&pTree->pSearchCache->aCache);
    HtmlFree(pTree->pSearchCache);
    pTree->pSearchCache = 0;
    return TCL_OK;
}

/*
 *---------------------------------------------------------------------------
 *
 * HtmlCssSearch --
 *
 *         widget search CSS-SELECTOR ?OPTIONS?
 *
 *     where OPTIONS are:
 *
 *         -root NODE              (Search the sub-tree at NODE)
 *         -index IDX              (return the idx'th list entry only)
 *         -length                 (return the length of the result only)
 *
 *     With no options, this command is used by the application to 
 *     query the widget for a list of node-handles that match the 
 *     specified css-selector. The nodes are returned in tree-order.
 *
 *     The -index and -length options are mutually exclusive.
 *
 * Results:
 *     None.
 *
 * Side effects:
 *
 *---------------------------------------------------------------------------
 */
int 
HtmlCssSearch(clientData, interp, objc, objv)
    ClientData clientData;             /* The HTML widget */
    Tcl_Interp *interp;                /* The interpreter */
    int objc;                          /* Number of arguments */
    Tcl_Obj *CONST objv[];             /* List of all arguments */
{
    HtmlTree *pTree = (HtmlTree *)clientData;
    char *zOrig;
    int n;
    CssStyleSheet *pStyle = 0;

    /* Search only descendants of this node (NULL means search whole tree) */
    HtmlNode *pSearchRoot = 0;
    int eMode = SEARCH_MODE_ALL;
    int iIndex = 0;

    int iArg;

    Tcl_HashEntry *pEntry = 0;
    CssCachedSearch *pCache = 0;
    int isNew;

    /* Options passed to this command. */
    struct HtmlCssOption {
        const char *zCommand;
        int isBoolean;
        Tcl_Obj *pArg;
    } aOption [] = {
        {"-root",   0, 0}, 
        {"-length", 1, 0}, 
        {"-index",  0, 0}, 
        {0, 0, 0}
    };

    if (objc < 3){
        Tcl_WrongNumArgs(interp, 2, objv, "CSS-SELECTOR ?OPTIONS?");
        return TCL_ERROR;
    }

    for (iArg = 3; iArg < objc; iArg++) {
        int iChoice;
        if (Tcl_GetIndexFromObjStruct(interp, objv[iArg], aOption, 
            sizeof(struct HtmlCssOption), "option", 0, &iChoice)
        ){
            return TCL_ERROR;
        }
        if (!aOption[iChoice].isBoolean) {
            iArg++;
            if (iArg == objc) {
                const char *z = Tcl_GetString(objv[iArg - 1]);
                Tcl_AppendResult(interp, "option requires an argument: ", z, 0);
                return TCL_ERROR;
            }
        } 
        aOption[iChoice].pArg = objv[iArg];
    }

    if (aOption[1].pArg && aOption[2].pArg) {
        const char z[] = "options -length and -index are mutually exclusive";
        Tcl_AppendResult(interp, z, 0);
        return TCL_ERROR;
    }

    if (aOption[0].pArg) {      /* Handle -root option */
        const char *zArg = Tcl_GetString(aOption[0].pArg);
        if (*zArg) {
            pSearchRoot = HtmlNodeGetPointer(pTree, zArg);
        }
    }
    if (aOption[1].pArg) {      /* Handle -length option */
        eMode = SEARCH_MODE_LENGTH;
    }
    if (aOption[2].pArg) {      /* Handle -index option */
        eMode = SEARCH_MODE_INDEX;
        if (Tcl_GetIntFromObj(interp, aOption[2].pArg, &iIndex)) {
            return TCL_ERROR;
        }
    }

    zOrig = Tcl_GetStringFromObj(objv[2], &n);
    if (pSearchRoot) {
        isNew = 1;
    } else {
        pEntry = Tcl_CreateHashEntry(&pTree->pSearchCache->aCache, zOrig, &isNew);
    }
    if (isNew) {
        char *z;
        CssSearch sSearch;

        assert(n == strlen(zOrig));
        n += 11;
        z = (char *)HtmlAlloc("temp", n);
        sprintf(z, "%s {width:0}", zOrig);
        HtmlCssSelectorParse(pTree, n, z, &pStyle);
        if ( !pStyle || !pStyle->pUniversalRules) {
            Tcl_AppendResult(interp, "Bad css selector: \"", zOrig, "\"", 0); 
            return TCL_ERROR;
        }
        sSearch.pRuleList = pStyle->pUniversalRules;
        sSearch.pTree = pTree;
        sSearch.pSearchRoot = pSearchRoot;
        sSearch.pCache = HtmlNew(CssCachedSearch);
        HtmlWalkTree(pTree, pSearchRoot, cssSearchCb, (ClientData)&sSearch);
        pCache = sSearch.pCache;
        HtmlCssStyleSheetFree(pStyle);
        HtmlFree(z);

        if (pEntry) {
            Tcl_SetHashValue(pEntry, sSearch.pCache);
        }
    } else {
        pCache = (CssCachedSearch *)Tcl_GetHashValue(pEntry);
    }

    switch (eMode) {
        case SEARCH_MODE_ALL: {
            Tcl_Obj *pRet = Tcl_NewObj();
            int ii;
            for(ii = 0; ii < pCache->nNode; ii++){
                Tcl_Obj *pCmd = HtmlNodeCommand(pTree, pCache->apNode[ii]);
                Tcl_ListObjAppendElement(interp, pRet, pCmd);
            }
            Tcl_SetObjResult(interp, pRet);
            break;
        }

        case SEARCH_MODE_LENGTH:
            Tcl_SetObjResult(interp, Tcl_NewIntObj(pCache->nNode));
            break;

        case SEARCH_MODE_INDEX:
            if (iIndex >= 0 && iIndex < pCache->nNode) {
                Tcl_SetObjResult(
                    interp, HtmlNodeCommand(pTree, pCache->apNode[iIndex])
                );
            }
            break;
    }

    if (pSearchRoot) {
        HtmlFree(pCache->apNode);
        HtmlFree(pCache);
    }

    return TCL_OK;
}