summaryrefslogtreecommitdiffstats
path: root/tdehtml/css/css_base.cpp
blob: a4dda95a07bf0482c9550bcc74d8d27c8fa41049 (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
/*
 * This file is part of the DOM implementation for KDE.
 *
 * Copyright (C) 1999-2003 Lars Knoll ([email protected])
 *               1999 Waldo Bastian ([email protected])
 *               2001 Andreas Schlapbach ([email protected])
 *               2001-2003 Dirk Mueller ([email protected])
 *               2002 Apple Computer, Inc.
 *               2004 Allan Sandfeld Jensen ([email protected])
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library 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
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public License
 * along with this library; see the file COPYING.LIB.  If not, write to
 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 */

//#define CSS_DEBUG

#include <assert.h>
#include <kdebug.h>

#include "css_base.h"

#ifdef CSS_DEBUG
#include "cssproperties.h"
#endif

#include "css_stylesheetimpl.h"
#include "xml/dom_docimpl.h"
#include "misc/htmlhashes.h"
#include "css_valueimpl.h"
using namespace DOM;

void StyleBaseImpl::checkLoaded() const
{
    if(m_parent) m_parent->checkLoaded();
}

StyleSheetImpl* StyleBaseImpl::stylesheet()
{
    StyleBaseImpl* b = this;
    while(b && !b->isStyleSheet())
        b = b->m_parent;
    return static_cast<StyleSheetImpl *>(b);
}

KURL StyleBaseImpl::baseURL()
{
    // try to find the style sheet. If found look for its url.
    // If it has none, look for the parentsheet, or the parentNode and
    // try to find out about their url

    StyleSheetImpl *sheet = stylesheet();

    if(!sheet) return KURL();

    if(!sheet->href().isNull())
        return KURL( sheet->href().string() );

    // find parent
    if(sheet->parent()) return sheet->parent()->baseURL();

    if(!sheet->ownerNode()) return KURL();

    return sheet->ownerNode()->getDocument()->baseURL();
}

void StyleBaseImpl::setParsedValue(int propId, const CSSValueImpl *parsedValue,
				   bool important, bool nonCSSHint, TQPtrList<CSSProperty> *propList)
{
    TQPtrListIterator<CSSProperty> propIt(*propList);
    propIt.toLast(); // just remove the top one - not sure what should happen if we have multiple instances of the property
    while (propIt.current() &&
           ( propIt.current()->m_id != propId || propIt.current()->nonCSSHint != nonCSSHint ||
             propIt.current()->m_important != important) )
        --propIt;
    if (propIt.current())
        propList->removeRef(propIt.current());

    CSSProperty *prop = new CSSProperty();
    prop->m_id = propId;
    prop->setValue((CSSValueImpl *) parsedValue);
    prop->m_important = important;
    prop->nonCSSHint = nonCSSHint;

    propList->append(prop);
#ifdef CSS_DEBUG
    kdDebug( 6080 ) << "added property: " << getPropertyName(propId).string()
                    // non implemented yet << ", value: " << parsedValue->cssText().string()
                    << " important: " << prop->m_important
                    << " nonCSS: " << prop->nonCSSHint << endl;
#endif
}

// ------------------------------------------------------------------------------

StyleListImpl::~StyleListImpl()
{
    StyleBaseImpl *n;

    if(!m_lstChildren) return;

    for( n = m_lstChildren->first(); n != 0; n = m_lstChildren->next() )
    {
        n->setParent(0);
        if( !n->refCount() ) delete n;
    }
    delete m_lstChildren;
}

// --------------------------------------------------------------------------------

void CSSSelector::print(void)
{
    kdDebug( 6080 ) << "[Selector: tag = " <<       TQString::number(tag,16) << ", attr = \"" << attr << "\", match = \"" << match
		    << "\" value = \"" << value.string().latin1() << "\" relation = " << (int)relation
		    << "]" << endl;
    if ( tagHistory )
        tagHistory->print();
    kdDebug( 6080 ) << "    specificity = " << specificity() << endl;
}

unsigned int CSSSelector::specificity() const
{
    if ( nonCSSHint )
        return 0;

    int s = ((localNamePart(tag) == anyLocalName) ? 0 : 1);
    switch(match)
    {
    case Id:
	s += 0x10000;
	break;
    case Exact:
    case Set:
    case List:
    case Class:
    case Hyphen:
    case PseudoClass:
    case PseudoElement:
    case Contain:
    case Begin:
    case End:
        s += 0x100;
    case None:
        break;
    }
    if(tagHistory)
        s += tagHistory->specificity();
    // make sure it doesn't overflow
    return s & 0xffffff;
}

void CSSSelector::extractPseudoType() const
{
    if (match != PseudoClass && match != PseudoElement)
        return;
    _pseudoType = PseudoOther;
    bool element = false;
    bool compat = false;
    if (!value.isEmpty()) {
        value = value.lower();
        switch (value[0]) {
            case '-':
                if (value == "-tdehtml-replaced")
                    _pseudoType = PseudoReplaced;
                else
                if (value == "-tdehtml-marker")
                    _pseudoType = PseudoMarker;
                element = true;
                break;
            case 'a':
                if (value == "active")
                    _pseudoType = PseudoActive;
                else if (value == "after") {
                    _pseudoType = PseudoAfter;
                    element = compat = true;
                }
                break;
            case 'b':
                if (value == "before") {
                    _pseudoType = PseudoBefore;
                    element = compat = true;
                }
                break;
            case 'c':
                if (value == "checked")
                    _pseudoType = PseudoChecked;
                else if (value == "contains(")
                    _pseudoType = PseudoContains;
                break;
            case 'd':
                if (value == "disabled")
                    _pseudoType = PseudoDisabled;
                break;
            case 'e':
                if (value == "empty")
                    _pseudoType = PseudoEmpty;
                else if (value == "enabled")
                    _pseudoType = PseudoEnabled;
                break;
            case 'f':
                if (value == "first-child")
                    _pseudoType = PseudoFirstChild;
                else if (value == "first-letter") {
                    _pseudoType = PseudoFirstLetter;
                    element = compat = true;
                }
                else if (value == "first-line") {
                    _pseudoType = PseudoFirstLine;
                    element = compat = true;
                }
                else if (value == "first-of-type")
                    _pseudoType = PseudoFirstOfType;
                else if (value == "focus")
                    _pseudoType = PseudoFocus;
                break;
            case 'h':
                if (value == "hover")
                    _pseudoType = PseudoHover;
                break;
            case 'i':
                if (value == "indeterminate")
                    _pseudoType = PseudoIndeterminate;
                break;
            case 'l':
                if (value == "link")
                    _pseudoType = PseudoLink;
                else if (value == "lang(")
                    _pseudoType = PseudoLang;
                else if (value == "last-child")
                    _pseudoType = PseudoLastChild;
                else if (value == "last-of-type")
                    _pseudoType = PseudoLastOfType;
                break;
            case 'n':
                if (value == "not(")
                    _pseudoType = PseudoNot;
                else if (value == "nth-child(")
                    _pseudoType = PseudoNthChild;
                else if (value == "nth-last-child(")
                    _pseudoType = PseudoNthLastChild;
                else if (value == "nth-of-type(")
                    _pseudoType = PseudoNthOfType;
                else if (value == "nth-last-of-type(")
                    _pseudoType = PseudoNthLastOfType;
                break;
            case 'o':
                if (value == "only-child")
                    _pseudoType = PseudoOnlyChild;
                else if (value == "only-of-type")
                    _pseudoType = PseudoOnlyOfType;
                break;
            case 'r':
                if (value == "root")
                    _pseudoType = PseudoRoot;
                break;
            case 's':
                if (value == "selection") {
                    _pseudoType = PseudoSelection;
                    element = true;
                }
                break;
            case 't':
                if (value == "target")
                    _pseudoType = PseudoTarget;
                break;
            case 'v':
                if (value == "visited")
                    _pseudoType = PseudoVisited;
                break;
        }
    }
    if (match == PseudoClass && element)
        if (!compat) _pseudoType = PseudoOther;
        else match = PseudoElement;
    else
    if (match == PseudoElement && !element)
        _pseudoType = PseudoOther;
}


bool CSSSelector::operator == ( const CSSSelector &other ) const
{
    const CSSSelector *sel1 = this;
    const CSSSelector *sel2 = &other;

    while ( sel1 && sel2 ) {
        //assert(sel1->_pseudoType != PseudoNotParsed);
        //assert(sel2->_pseudoType != PseudoNotParsed);
	if ( sel1->tag != sel2->tag || sel1->attr != sel2->attr ||
	     sel1->relation != sel2->relation || sel1->match != sel2->match ||
	     sel1->nonCSSHint != sel2->nonCSSHint ||
	     sel1->value != sel2->value ||
             sel1->pseudoType() != sel2->pseudoType() ||
             sel1->string_arg != sel2->string_arg)
	    return false;
	sel1 = sel1->tagHistory;
	sel2 = sel2->tagHistory;
    }
    if ( sel1 || sel2 )
	return false;
    return true;
}

DOMString CSSSelector::selectorText() const
{
    // FIXME: Support namespaces when dumping the selector text.  This requires preserving
    // the original namespace prefix used. Ugh. -dwh
    DOMString str;
    const CSSSelector* cs = this;
    TQ_UINT16 tag = localNamePart(cs->tag);
    if (tag == anyLocalName && cs->match == CSSSelector::None)
        str = "*";
    else if (tag != anyLocalName)
        str = getTagName( cs->tag );

    const CSSSelector* op = 0;
    while (true) {
        if ( cs->attr == ATTR_ID && cs->match == CSSSelector::Id )
        {
            str += "#";
            str += cs->value;
        }
        else if ( cs->match == CSSSelector::Class )
        {
            str += ".";
            str += cs->value;
        }
        else if ( cs->match == CSSSelector::PseudoClass )
        {
            str += ":";
            str += cs->value;
            if (!cs->string_arg.isEmpty()) { // e.g :nth-child(...)
                str += cs->string_arg;
                str += ")";
            } else if (cs->simpleSelector && !op) { // :not(...)
                op = cs;
                cs = cs->simpleSelector;
                continue;
            }
        }
        else if ( cs->match == CSSSelector::PseudoElement )
        {
            str += "::";
            str += cs->value;
        }
        // optional attribute
        else if ( cs->attr ) {
            DOMString attrName = getAttrName( cs->attr );
            str += "[";
            str += attrName;
            switch (cs->match) {
            case CSSSelector::Exact:
                str += "=";
                break;
            case CSSSelector::Set:
                break;
            case CSSSelector::List:
                str += "~=";
                break;
            case CSSSelector::Hyphen:
                str += "|=";
                break;
            case CSSSelector::Begin:
                str += "^=";
                break;
            case CSSSelector::End:
                str += "$=";
                break;
            case CSSSelector::Contain:
                str += "*=";
                break;
            default:
                kdWarning(6080) << "Unhandled case in CSSStyleRuleImpl::selectorText : match=" << cs->match << endl;
            }
            if (cs->match != CSSSelector::Set) {
                str += "\"";
                str += cs->value;
                str += "\"";
            }
            str += "]";
        }
        if (op && !cs->tagHistory) {
            cs=op;
            op=0;
            str += ")";
        }

        if ((cs->relation != CSSSelector::SubSelector && !op) || !cs->tagHistory)
            break;
        cs = cs->tagHistory;
    }

    if ( cs->tagHistory ) {
        DOMString tagHistoryText = cs->tagHistory->selectorText();
        if ( cs->relation == DirectAdjacent )
            str = tagHistoryText + " + " + str;
        else if ( cs->relation == IndirectAdjacent )
            str = tagHistoryText + " ~ " + str;
        else if ( cs->relation == Child )
            str = tagHistoryText + " > " + str;
        else // Descendant
            str = tagHistoryText + " " + str;
    }
    return str;
}

// ----------------------------------------------------------------------------