File: htmlstyle.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 (876 lines) | stat: -rw-r--r-- 26,192 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
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
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876

/*
 * htmlstyle.c ---
 *
 *     This file applies the cascade algorithm using the stylesheet
 *     code in css.c to the tree built with code in htmltree.c
 *
 *--------------------------------------------------------------------------
 * Copyright (c) 2005 Eolas Technologies Inc.
 * All rights reserved.
 *
 * This Open Source project was made possible through the financial support
 * of Eolas Technologies Inc.
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 * 
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *     * Neither the name of the <ORGANIZATION> nor the names of its
 *       contributors may be used to endorse or promote products derived from
 *       this software without specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */
static const char rcsid[] = "$Id: htmlstyle.c,v 1.61 2007/12/12 04:50:29 danielk1977 Exp $";

#include "html.h"
#include <assert.h>
#include <string.h>

void
HtmlDelScrollbars(pTree, pNode)
    HtmlTree *pTree;
    HtmlNode *pNode;
{
    HtmlElementNode *pElem = (HtmlElementNode *)pNode;

    if (!HtmlNodeIsText(pNode) && pElem->pScrollbar) {
        HtmlNodeScrollbars *p = pElem->pScrollbar;
        if (p->vertical.win) {
	    /* Remove any entry from the HtmlTree.pMapped list. */
            if (&p->vertical == pTree->pMapped) {
                pTree->pMapped = p->vertical.pNext;
            } else {
                HtmlNodeReplacement *pCur = pTree->pMapped; 
                while( pCur && pCur->pNext != &p->vertical ) pCur = pCur->pNext;
                if (pCur) {
                    pCur->pNext = p->vertical.pNext;
                }
            }

            Tk_DestroyWindow(p->vertical.win);
            Tcl_DecrRefCount(p->vertical.pReplace);
        }
        if (p->horizontal.win) {
	    /* Remove any entry from the HtmlTree.pMapped list. */
            if (&p->horizontal == pTree->pMapped) {
                pTree->pMapped = p->horizontal.pNext;
            } else {
                HtmlNodeReplacement *pCur = pTree->pMapped; 
                while( pCur && pCur->pNext != &p->horizontal ) {
                    pCur = pCur->pNext;
                }
                if (pCur) {
                    pCur->pNext = p->horizontal.pNext;
                }
            }

            Tk_DestroyWindow(p->horizontal.win);
            Tcl_DecrRefCount(p->horizontal.pReplace);
        }
        HtmlFree(p);
        pElem->pScrollbar = 0;
    }
}

void
HtmlDelStackingInfo(pTree, pElem)
    HtmlTree *pTree;
    HtmlElementNode *pElem;
{
    HtmlNodeStack *pStack = pElem->pStack;
    if (pStack && pStack->pElem == pElem){
        if (pStack->pPrev) {
            pStack->pPrev->pNext = pStack->pNext;
        } 
        if (pStack->pNext) {
            pStack->pNext->pPrev = pStack->pPrev;
        } 
        if (pStack==pTree->pStack) {
          pTree->pStack = pStack->pNext;
        }
        assert(!pTree->pStack || !pTree->pStack->pPrev);

        HtmlFree(pStack);
        pTree->nStack--;
    }
    pElem->pStack = 0;
}

#define STACK_NONE      0
#define STACK_FLOAT     1
#define STACK_AUTO      2
#define STACK_CONTEXT   3
static int 
stackType(p) 
    HtmlNode *p;
{
    HtmlComputedValues *pV = HtmlNodeComputedValues(p);

    /* STACK_CONTEXT is created by the root element and any
     * positioned block with (z-index!='auto).
     */
    if (
        (!HtmlNodeParent(p)) ||
        (pV->ePosition != CSS_CONST_STATIC && pV->iZIndex != PIXELVAL_AUTO)
    ) {
        return STACK_CONTEXT;
    }

    /* Postioned elements with 'auto' z-index are STACK_AUTO. */
    if (pV->ePosition != CSS_CONST_STATIC) {
        return STACK_AUTO;
    }

    /* Floating boxes are STACK_FLOAT. */
    if (pV->eFloat != CSS_CONST_NONE){
        return STACK_FLOAT;
    }

    return STACK_NONE;
}

static void
addStackingInfo(pTree, pElem)
    HtmlTree *pTree;
    HtmlElementNode *pElem;
{
    HtmlNode *pNode = (HtmlNode *)pElem;
    int eStack = stackType(pNode);
    
    /* A node forms a new stacking context if it is positioned or floating.
     * Or if it is the root node. We only need create an HtmlNodeStack if this
     * is the case.
     */
    if (eStack != STACK_NONE) {
        HtmlNodeStack *pStack = HtmlNew(HtmlNodeStack);

        pStack->pElem = pElem;
        pStack->eType = eStack;
        pStack->pNext = pTree->pStack;
        if( pStack->pNext ){
            pStack->pNext->pPrev = pStack;
        }
        pTree->pStack = pStack;
        pElem->pStack = pStack;
        pTree->cb.flags |= HTML_STACK;
        pTree->nStack++;
    } else {
        pElem->pStack = ((HtmlElementNode *)HtmlNodeParent(pNode))->pStack;
    }
    assert(pElem->pStack);
}

#define STACK_STACKING  1
#define STACK_BLOCK     3
#define STACK_INLINE    5
typedef struct StackCompare StackCompare;
struct StackCompare {
    HtmlNodeStack *pStack;
    int eStack;
};


/*
 *---------------------------------------------------------------------------
 *
 * scoreStack --
 *
 * Results:
 *     1 -> Border and background of stacking context box.
 *     2 -> Descendants with negative z-index values.
 *     3 -> In-flow, non-inline descendants.
 *     4 -> Floats and their contents.
 *     5 -> In-flow, inline descendants.
 *     6 -> Positioned descendants with z-index values of "auto" or "0".
 *     7 -> Descendants with positive z-index values.
 *
 * Side effects:
 *     None.
 *
 *---------------------------------------------------------------------------
 */
static int
scoreStack(pParentStack, pStack, eStack)
    HtmlNodeStack *pParentStack;
    HtmlNodeStack *pStack;
    int eStack;
{
    int z;
    if (pStack == pParentStack) {
        return eStack;
    }
    assert(pStack->pElem->node.pParent);
    if (pStack->eType == STACK_FLOAT) return 4;
    if (pStack->eType == STACK_AUTO) return 6;
    z = pStack->pElem->pPropertyValues->iZIndex;
    assert(z != PIXELVAL_AUTO);
    if (z == 0) return 6;
    if (z < 0) return 2;
    return 7;
}

#define IS_STACKING_CONTEXT(x) (                                    \
         x == x->pStack->pElem && x->pStack->eType == STACK_CONTEXT \
)

static void setStackingContext(p, ppOut)
    HtmlElementNode *p;
    HtmlNodeStack **ppOut;
{
    if (p == p->pStack->pElem) {
        HtmlNodeStack *pS = p->pStack;
        if (pS->eType == STACK_CONTEXT || (*ppOut)->eType != STACK_CONTEXT) {
            *ppOut = pS;
        }
    }
}


static int
stackCompare(pVoidLeft, pVoidRight)
    const void *pVoidLeft;
    const void *pVoidRight;
{
    StackCompare *pLeft = (StackCompare *)pVoidLeft;
    StackCompare *pRight = (StackCompare *)pVoidRight;

    HtmlNodeStack *pLeftStack = pLeft->pStack;
    HtmlNodeStack *pRightStack = pRight->pStack;
    HtmlNodeStack *pParentStack = 0;

    int nLeftDepth = -1;        /* Tree depth of node pLeftStack->pNode */
    int nRightDepth = -1;       /* Tree depth of node pRightStack->pNode */

    int iLeft;
    int iRight;
    int iRes;
    int iTreeOrder = 0;

    int ii;
    HtmlElementNode *pL;
    HtmlElementNode *pR;

    /* There are three scenarios:
     *
     *     1) pLeft and pRight are associated with the same HtmlNodeStack
     *        structure. In this case "inline" beats "block" and "block"
     *        beats "stacking".
     *
     *     2) pLeft is descended from pRight, or vice versa.
     *
     *     3) Both are descended from a common stacking context.
     */

    /* Calculate pLeftStack, pRightStack and pParentStack */
    for (pL = pLeftStack->pElem; pL; pL = HtmlElemParent(pL)) nLeftDepth++;
    for (pR = pRightStack->pElem; pR; pR = HtmlElemParent(pR)) nRightDepth++;
    pL = pLeftStack->pElem;
    pR = pRightStack->pElem;
    for (ii = 0; ii < MAX(0, nLeftDepth - nRightDepth); ii++) {
        setStackingContext(pL, &pLeftStack);
        pL = HtmlElemParent(pL);
        iTreeOrder = +1;
    }
    for (ii = 0; ii < MAX(0, nRightDepth - nLeftDepth); ii++) {
        setStackingContext(pR, &pRightStack);
        pR = HtmlElemParent(pR);
        iTreeOrder = -1;
    }
    while (pR != pL) {
        HtmlElementNode *pParentL = HtmlElemParent(pL);
        HtmlElementNode *pParentR = HtmlElemParent(pR);
        setStackingContext(pL, &pLeftStack);
        setStackingContext(pR, &pRightStack);
        if (pParentL == pParentR) {
            iTreeOrder = 0;
            for (ii = 0; 0 == iTreeOrder; ii++) {
                HtmlNode *pChild = HtmlNodeChild(&pParentL->node, ii);
                if (pChild == (HtmlNode *)pL) {
                    iTreeOrder = -1;
                }
                if (pChild == (HtmlNode *)pR) {
                    iTreeOrder = +1;
                }
            }
            assert(iTreeOrder != 0);
        }
        pL = pParentL;
        pR = pParentR;
        assert(pL && pR);
    }
    while (pR->pStack->pElem != pR) {
        pR = HtmlElemParent(pR);
        assert(pR);
    }
    pParentStack = pR->pStack;

    iLeft = scoreStack(pParentStack, pLeftStack, pLeft->eStack);
    iRight = scoreStack(pParentStack, pRightStack, pRight->eStack);

    iRes = iLeft - iRight;
    if (iRes == 0 && (iRight == 2 || iRight == 6 || iRight == 7)) {
        int z1 = pLeftStack->pElem->pPropertyValues->iZIndex;
        int z2 = pRightStack->pElem->pPropertyValues->iZIndex;
        if (z1 == PIXELVAL_AUTO) z1 = 0;
        if (z2 == PIXELVAL_AUTO) z2 = 0;
        iRes = z1 - z2;
    }
    /* if (iRes == 0 && (iRight == 4 || pLeftStack == pRightStack)) { */
    if (iRes == 0 && pLeftStack == pRightStack) {
        iRes = (pLeft->eStack - pRight->eStack);
    }
    if (iRes == 0) {
        assert(iTreeOrder != 0);
        iRes = iTreeOrder;
    }
    return iRes;
}

/*
 *---------------------------------------------------------------------------
 *
 * checkStackSort --
 *
 *     This function is equivalent to an assert() statement. It is not
 *     part of the functionality of Tkhtml3, but is used to check the
 *     integrity of internal data structures.
 *
 *     The first argument, aStack, should be an array of nStack (the
 *     second arg) StackCompare structure. An assert fails inside
 *     this function if the array is not sorted in ascending order.
 *
 *     The primary purpose of this test is to ensure that the stackCompare()
 *     comparision function is stable. It is quite an expensive check,
 *     so is normally disabled at compile time. Change the "#if 0"
 *     below to reenable the checking.
 *
 *     NOTE: If you got this file from tkhtml.tcl.tk and there is an 
 *           "#if 1" in the code below, I have checked it in by mistake.
 *
 * Results:
 *     None.
 *
 * Side effects:
 *     None.
 *
 *---------------------------------------------------------------------------
 */
#ifdef NDEBUG
  #define checkStackSort(a,b,c)
#else
static void 
checkStackSort(pTree, aStack, nStack)
    HtmlTree *pTree;
    StackCompare *aStack;
    int nStack;
{
#if 0
    int ii;
    int jj;
    for (ii = 0; ii < nStack; ii++) {
      for (jj = ii + 1; jj < nStack; jj++) {
        int r1 = stackCompare(&aStack[ii], &aStack[jj]);
        int r2 = stackCompare(&aStack[jj], &aStack[ii]);
        assert(r1 && r2);
        assert((r1 * r2) < 0);
        assert(r1 < 0);
      }
    }
#endif
}
#endif


/*
 *---------------------------------------------------------------------------
 *
 * HtmlRestackNodes --
 *
 *     This function is called from with the callbackHandler() routine
 *     after updating the computed properties of the tree.
 *
 * Results:
 *     None.
 *
 * Side effects:
 *     None.
 *
 *---------------------------------------------------------------------------
 */
void
HtmlRestackNodes(pTree)
    HtmlTree *pTree;
{
    HtmlNodeStack *pStack;
    StackCompare *apTmp;
    int iTmp = 0;
    if (0 == (pTree->cb.flags & HTML_STACK)) return;

    apTmp = (StackCompare *)HtmlAlloc(0, sizeof(StackCompare)*pTree->nStack*3);

    for (pStack = pTree->pStack; pStack; pStack = pStack->pNext) {
        apTmp[iTmp].pStack = pStack;
        apTmp[iTmp].eStack = STACK_BLOCK;
        apTmp[iTmp+1].pStack = pStack;
        apTmp[iTmp+1].eStack = STACK_INLINE;
        apTmp[iTmp+2].pStack = pStack;
        apTmp[iTmp+2].eStack = STACK_STACKING;
        iTmp += 3;
    }
    assert(iTmp == pTree->nStack * 3);

    qsort(apTmp, pTree->nStack * 3, sizeof(StackCompare), stackCompare);

    for (iTmp = 0; iTmp < pTree->nStack * 3; iTmp++) {
#if 0
printf("Stack %d: %s %s\n", iTmp, 
    Tcl_GetString(HtmlNodeCommand(pTree, &apTmp[iTmp].pStack->pElem->node)),
    (apTmp[iTmp].eStack == STACK_INLINE ? "inline" : 
     apTmp[iTmp].eStack == STACK_BLOCK ? "block" : "stacking")
);
#endif
        switch (apTmp[iTmp].eStack) {
            case STACK_INLINE:
                apTmp[iTmp].pStack->iInlineZ = iTmp;
                break;
            case STACK_BLOCK:
                apTmp[iTmp].pStack->iBlockZ = iTmp;
                break;
            case STACK_STACKING:
                apTmp[iTmp].pStack->iStackingZ = iTmp;
                break;
        }
    }
    checkStackSort(pTree, apTmp, pTree->nStack * 3);

    pTree->cb.flags &= (~HTML_STACK);
    HtmlFree(apTmp);
}

/*
 *---------------------------------------------------------------------------
 *
 * styleNode --
 *
 * Results:
 *     None.
 *
 * Side effects:
 *     None.
 *
 *---------------------------------------------------------------------------
 */
static int 
styleNode(pTree, pNode, clientData)
    HtmlTree *pTree;
    HtmlNode *pNode;
    ClientData clientData;
{
    CONST char *zStyle;      /* Value of "style" attribute for node */
    int trashDynamics = (int)((size_t) clientData);

    HtmlElementNode *pElem = (HtmlElementNode *)pNode;
    HtmlComputedValues *pV = pElem->pPropertyValues;
    pElem->pPropertyValues = 0;
    HtmlDelStackingInfo(pTree, pElem);

    /* If the clientData was set to a non-zero value, then the 
     * stylesheet configuration has changed. In this case we need to
     * recalculate the nodes list of dynamic conditions.
     */
    if (trashDynamics) {
        HtmlCssFreeDynamics(pElem);
    }

    /* If there is a "style" attribute on this node, parse the attribute
     * value and put the resulting mini-stylesheet in pNode->pStyle. 
     *
     * We assume that if the pStyle attribute is not NULL, then this node
     * has been styled before. The stylesheet configuration may have
     * changed since then, so we have to recalculate pNode->pProperties,
     * but the "style" attribute is constant so pStyle is never invalid.
     *
     * Actually, the style attribute can be modified by the user, using 
     * the [$node attribute style "new-value"] command. In this case
     * the style attribute is treated as a special case and the 
     * pElem->pStyle structure is invalidated/recalculated as required.
     */
    if (!pElem->pStyle) {
        zStyle = HtmlNodeAttr(pNode, "style");
        if (zStyle) {
            HtmlCssInlineParse(pTree, -1, zStyle, &pElem->pStyle);
        }
    }

    /* Recalculate the properties for this node */
    HtmlCssStyleSheetApply(pTree, pNode);
    HtmlComputedValuesRelease(pTree, pElem->pPreviousValues);
    pElem->pPreviousValues = pV;

    addStackingInfo(pTree, pElem);

    /* Compare the new computed property set with the old. If
     * ComputedValuesCompare() returns 0, then the properties have
     * not changed (in any way that affects rendering). If it
     * returns 1, then some aspect has changed that does not
     * require a relayout (i.e. 'color', or 'text-decoration'). 
     * If it returns 2, then something has changed that does require
     * relayout (i.e. 'display', 'font-size').
     */
    return HtmlComputedValuesCompare(pElem->pPropertyValues, pV);
}

typedef struct StyleCounter StyleCounter;
struct StyleCounter {
  char *zName;
  int iValue;
};

struct StyleApply {
  /* Node to begin recalculating style at */
  HtmlNode *pRestyle;

  /* True if currently traversing pRestyle, or a descendent, right-sibling
   * or descendent of a right-sibling of pRestyle.
   */
  int doStyle;

  int doContent;

  /* True if the whole tree is being restyled. */
  int isRoot;

  StyleCounter **apCounter;
  int nCounter;
  int nCounterAlloc;
  int nCounterStartScope;

  /* True if we have seen one or more "fixed" items */
  int isFixed;
};
typedef struct StyleApply StyleApply;

static void 
styleApply(pTree, pNode, p)
    HtmlTree *pTree;
    HtmlNode *pNode;
    StyleApply *p;
{
    int i;
    int doStyle;
    int nCounterStartScope;
    int redrawmode = 0;
    HtmlElementNode *pElem = HtmlNodeAsElement(pNode);

    /* Text nodes do not have an associated style. */
    if (!pElem) return;

    if (p->pRestyle == pNode) {
        p->doStyle = 1;
    }

    if (p->doStyle) {
        redrawmode = styleNode(pTree, pNode, (ClientData) ((size_t) p->isRoot));

        /* If there has been a style-callback configured (-stylecmd option to
         * the [nodeHandle replace] command) for this node, invoke it now.
         */
        if (pElem->pReplacement && pElem->pReplacement->pStyleCmd) {
            Tcl_Obj *pCmd = pElem->pReplacement->pStyleCmd;
            int rc = Tcl_EvalObjEx(pTree->interp, pCmd, TCL_EVAL_GLOBAL);
            if (rc != TCL_OK) {
                Tcl_BackgroundError(pTree->interp);
            }
        }
    }

    HtmlStyleHandleCounters(pTree, HtmlNodeComputedValues(pNode));
    nCounterStartScope = p->nCounterStartScope;
    p->nCounterStartScope = p->nCounter;

    if (p->doStyle || p->doContent) {
        /* Destroy current generated content */
        if (pElem->pBefore || pElem->pAfter) {
            HtmlNodeClearGenerated(pTree, pElem);
            redrawmode = MAX(redrawmode, 2);
        }

        /* Generate :before content */
        HtmlCssStyleGenerateContent(pTree, pElem, 1);
        if (pElem->pBefore) {
            ((HtmlElementNode *)(pElem->pBefore))->pStack = pElem->pStack;
            pElem->pBefore->pParent = pNode;
            pElem->pBefore->iNode = -1;
        }
    } else if (pElem->pBefore) {
        HtmlStyleHandleCounters(pTree, HtmlNodeComputedValues(pElem->pBefore));
    }

    doStyle = p->doStyle;
    for (i = 0; i < HtmlNodeNumChildren(pNode); i++) {
        styleApply(pTree, HtmlNodeChild(pNode, i), p);
    }
    p->doStyle = doStyle;

    if (p->doStyle || p->doContent) {
        /* Generate :after content */
        HtmlCssStyleGenerateContent(pTree, pElem, 0);
        if (pElem->pAfter) {
            ((HtmlElementNode *)(pElem->pAfter))->pStack = pElem->pStack;
            pElem->pAfter->pParent = pNode;
            pElem->pAfter->iNode = -1;
        }

        if (pElem->pBefore || pElem->pAfter) {
            redrawmode = MAX(redrawmode, 2);
        }
    } else if(pElem->pAfter) {
        HtmlStyleHandleCounters(pTree, HtmlNodeComputedValues(pElem->pAfter));
    }

    for (i = p->nCounterStartScope; i < p->nCounter; i++) {
        HtmlFree(p->apCounter[i]);
    }
    p->nCounter = p->nCounterStartScope;
    p->nCounterStartScope = nCounterStartScope;

    if (redrawmode == 3) {
        HtmlCallbackLayout(pTree, pNode);
        HtmlCallbackDamageNode(pTree, pNode);
        p->doContent = 1;
    } else if (redrawmode == 2) {
        HtmlCallbackLayout(pTree, pNode);
        HtmlCallbackDamageNode(pTree, pNode);
    } else if (redrawmode == 1) {
        /* HtmlCallbackLayout(pTree, pNode); */
        HtmlCallbackDamageNode(pTree, pNode);
    }

    /* If this element was either the <body> or <html> nodes,
     * go ahead and repaint the entire display. The worst that
     * can happen is that we have to paint a little extra
     * area if the document background is set by the <HTML>
     * element.
     */
    if (redrawmode && (
            (HtmlNode *)pElem == pTree->pRoot || 
            (HtmlNode *)pElem == HtmlNodeChild(pTree->pRoot, 1)
        )
    ) {
        HtmlCallbackDamage(pTree, 0, 0, 1000000, 1000000);
    }

    if (pElem->pPropertyValues->eDisplay != CSS_CONST_NONE && (
        pElem->pPropertyValues->ePosition == CSS_CONST_FIXED ||
        pElem->pPropertyValues->eBackgroundAttachment == CSS_CONST_FIXED
    )) {
        p->isFixed = 1;
    }
}

static void addCounterEntry(p, zName, iValue)
    StyleApply *p;
    const char *zName;
    int iValue;
{
    StyleCounter *pCounter;

    int n;
    if (p->nCounterAlloc < (p->nCounter + 1)) {
        int nByte;
        p->nCounterAlloc += 10;
        nByte = p->nCounterAlloc * sizeof(HtmlCounterList *);
        p->apCounter = (StyleCounter **)HtmlRealloc(
            "apCounter", p->apCounter, nByte
        );
    }

    n = sizeof(StyleCounter) + strlen(zName) + 1;
    pCounter = (StyleCounter *)HtmlAlloc("StyleCounter", n);
    pCounter->zName = (char *)&pCounter[1];
    strcpy(pCounter->zName, zName);
    pCounter->iValue = iValue;
    p->apCounter[p->nCounter] = pCounter;
    p->nCounter++;
}

void
HtmlStyleHandleCounters(pTree, pComputed)
    HtmlTree *pTree;
    HtmlComputedValues *pComputed;
{
    StyleApply *p = (StyleApply *)pTree->pStyleApply;

    HtmlCounterList *pReset = pComputed->clCounterReset;
    HtmlCounterList *pIncr = pComputed->clCounterIncrement;


    /* Section 12.4.3 of CSS 2.1: Elements with "display:none" neither
     * increment or reset counters.
     */
    if (pComputed->eDisplay == CSS_CONST_NONE) {
        return;
    }

    if (pReset) {
        int ii;

        for (ii = 0; ii < pReset->nCounter; ii++) {
            StyleCounter *pCounter = 0;
            int jj;
            for (jj = p->nCounterStartScope; jj < p->nCounter; jj++) {
                if (!strcmp(pReset->azCounter[ii], p->apCounter[jj]->zName)) {
                    pCounter = p->apCounter[jj];
                    pCounter->iValue = pReset->anValue[ii];
                    break;
                }
            }
            if (pCounter == 0) {
                addCounterEntry(p, pReset->azCounter[ii], pReset->anValue[ii]);
            }
        }
    }

    if (pIncr) {
        int ii;
        for (ii = 0; ii < pIncr->nCounter; ii++) {
            int jj;
            for (jj = p->nCounter - 1; jj >= 0; jj--) {
                if (0 == strcmp(pIncr->azCounter[ii],p->apCounter[jj]->zName)) {
                    p->apCounter[jj]->iValue += pIncr->anValue[ii];
                    break;
                }
            }

            if (jj < 0) {
                /* No counter with the specified name is found. Act as if 
                 * there is a 'counter-reset: zName iValue' directive.
                 */
                addCounterEntry(p, pIncr->azCounter[ii], pIncr->anValue[ii]);
            }
        }
    }
}

int HtmlStyleCounters(pTree, zName, aValue, nValue)
    HtmlTree *pTree;
    const char *zName;
    int *aValue;
    int nValue;
{
    int ii;
    StyleApply *p = (StyleApply *)(pTree->pStyleApply);

    int n = 0;

    for (ii = 0; ii < p->nCounter && n < nValue; ii++) {
        if (0 == strcmp(zName, p->apCounter[ii]->zName)) {
            aValue[n] = p->apCounter[ii]->iValue;
            n++;
        }
    }

    if (n == 0) {
        n = 1;
        aValue[0] = 0;
    }

    return n;
}

int HtmlStyleCounter(pTree, zName)
    HtmlTree *pTree;
    const char *zName;
{
    int ii;
    StyleApply *p = (StyleApply *)(pTree->pStyleApply);

    for (ii = p->nCounter - 1; ii >= 0; ii--) {
        if (0 == strcmp(zName, p->apCounter[ii]->zName)) {
            return p->apCounter[ii]->iValue;
        }
    }

    return 0;
}

/*
 *---------------------------------------------------------------------------
 *
 * HtmlStyleApply --
 *
 * Results:
 *     None.
 *
 * Side effects:
 *     None.
 *
 *---------------------------------------------------------------------------
 */
int 
HtmlStyleApply(pTree, pNode)
    HtmlTree *pTree;
    HtmlNode *pNode;
{
    StyleApply sApply;
    int isRoot = ((pNode == pTree->pRoot) ? 1 : 0);
    HtmlLog(pTree, "STYLEENGINE", "START");

    memset(&sApply, 0, sizeof(StyleApply));
    sApply.pRestyle = pNode;
    sApply.isRoot = isRoot;

    assert(pTree->pStyleApply == 0);
    pTree->pStyleApply = (void *)&sApply;
    styleApply(pTree, pTree->pRoot, &sApply);
    pTree->pStyleApply = 0;
    pTree->isFixed = sApply.isFixed;
    HtmlFree(sApply.apCounter);
    return TCL_OK;
}

/*
 *---------------------------------------------------------------------------
 *
 * HtmlStyleSyntaxErrs --
 *
 * Results:
 *     None.
 *
 * Side effects:
 *     None.
 *
 *---------------------------------------------------------------------------
 */
int 
HtmlStyleSyntaxErrs(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;
    int nSyntaxErrs = 0;
    if( pTree->pStyle ){
        nSyntaxErrs = HtmlCssStyleSheetSyntaxErrs(pTree->pStyle);
    }
    Tcl_SetObjResult(interp, Tcl_NewIntObj(nSyntaxErrs));
    return TCL_OK;
}