summaryrefslogtreecommitdiffstats
path: root/khtml/html/html_miscimpl.cpp
blob: d5e8120294b52a7183d89e27de47324b996cf771 (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
/**
 * This file is part of the DOM implementation for KDE.
 *
 * Copyright (C) 1999 Lars Knoll (knoll@kde.org)
 *           (C) 1999 Antti Koivisto (koivisto@kde.org)
 *           (C) 2005 Maksim Orlovich (maksim@kde.org)
 *
 * 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.
 *
 */
// -------------------------------------------------------------------------
#include "html/html_tableimpl.h"
#include "html/html_miscimpl.h"
#include "html/html_formimpl.h"
#include "html/html_documentimpl.h"

#include "misc/htmlhashes.h"
#include "dom/dom_node.h"

using namespace DOM;

#include <kdebug.h>

HTMLBaseFontElementImpl::HTMLBaseFontElementImpl(DocumentImpl *doc)
    : HTMLElementImpl(doc)
{
}

HTMLBaseFontElementImpl::~HTMLBaseFontElementImpl()
{
}

NodeImpl::Id HTMLBaseFontElementImpl::id() const
{
    return ID_BASEFONT;
}

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

struct CollectionCache: public NodeListImpl::Cache
{
    static Cache* make() { return new CollectionCache; }

    TQDict<TQValueList<NodeImpl*> > nameCache;

    CollectionCache(): nameCache(127)
    {
        nameCache.setAutoDelete(true);
    }

    virtual void clear(DocumentImpl* doc)
    {
        Cache::clear(doc);
        //qDeletaAll here in Qt4
        nameCache.clear();
    }
};

HTMLCollectionImpl::HTMLCollectionImpl(NodeImpl *_base, int _type):
    NodeListImpl(_base, _type, CollectionCache::make)
{
    type = _type;
}

bool HTMLCollectionImpl::nodeMatches(NodeImpl *current, bool& deep) const
{
    if ( current->nodeType() != Node::ELEMENT_NODE )
    {
        deep = false;
        return false;
    }

    bool check = false;
    HTMLElementImpl *e = static_cast<HTMLElementImpl *>(current);
    switch(type)
    {
    case DOC_IMAGES:
        if(e->id() == ID_IMG)
            check = true;
        break;
    case DOC_SCRIPTS:
        if(e->id() == ID_SCRIPT)
            check = true;
        break;
    case DOC_FORMS:
        if(e->id() == ID_FORM)
            check = true;
        break;
    case DOC_LAYERS:
        if(e->id() == ID_LAYER || e->id() == ID_ILAYER)
            check = true;
        break;
    case TABLE_TBODIES:
        if(e->id() == ID_TBODY)
            check = true;
        else if(e->id() == ID_TABLE)
            deep = false;
        break;
    case TR_CELLS:
        if(e->id() == ID_TD || e->id() == ID_TH)
            check = true;
        else if(e->id() == ID_TABLE)
            deep = false;
        break;
    case TABLE_ROWS:
    case TSECTION_ROWS:
        if(e->id() == ID_TR)
            check = true;
        else if(e->id() == ID_TABLE)
            deep = false;
        break;
    case SELECT_OPTIONS:
        if(e->id() == ID_OPTION)
            check = true;
        break;
    case MAP_AREAS:
        if(e->id() == ID_AREA)
            check = true;
        break;
    case DOC_APPLETS:   // all OBJECT and APPLET elements
        if(e->id() == ID_OBJECT || e->id() == ID_APPLET || e->id() == ID_EMBED)
            check = true;
        break;
    case DOC_LINKS:     // all A _and_ AREA elements with a value for href
        if(e->id() == ID_A || e->id() == ID_AREA)
            if(!e->getAttribute(ATTR_HREF).isNull())
                check = true;
        break;
    case DOC_ANCHORS:      // all A elements with a value for name and/or id
        if(e->id() == ID_A) {
            if(e->hasID() || !e->getAttribute(ATTR_NAME).isNull())
                check = true;
        }
        break;
    case DOC_ALL:      // "all" elements
        check = true;
        break;
    case NODE_CHILDREN: // first-level children
        check = true;
        deep = false;
        break;
    default:
        kdDebug( 6030 ) << "Error in HTMLCollection, wrong tagId!" << endl;
    }

    return check;
}

bool HTMLCollectionImpl::checkForNameMatch(NodeImpl *node, const DOMString &name) const
{
    if ( node->nodeType() != Node::ELEMENT_NODE )
        return false;

    HTMLElementImpl *e = static_cast<HTMLElementImpl *>(node);

    //If ID matches, this is definitely a match
    if (e->getAttribute(ATTR_ID) == name)
        return true;

    //Despite what the DOM spec says, neither IE nor Gecko actually
    //care to prefer IDs. Instead, they just match everything
    //that has ID or a name for nodes that have a name.
    //Except for the form elements collection, Gecko always returns
    //just one item. IE is more complex: its namedItem
    //and call notation access return everything that matches,
    //but the subscript notation is sometimes different.
    //For now, we try to match IE, but without the subscript
    //oddness, which I don't understand -- Maks.

    bool checkName;
    switch (e->id())
    {
        case ID_A:
        case ID_APPLET:
        case ID_BUTTON:
        case ID_EMBED:
        case ID_FORM:
        case ID_IMG:
        case ID_INPUT:
        case ID_MAP:
        case ID_META:
        case ID_OBJECT:
        case ID_SELECT:
        case ID_TEXTAREA:
        case ID_FRAME:
        case ID_IFRAME:
        case ID_FRAMESET:
            checkName = true;
            break;
        default:
            checkName = false;
    }

    if (checkName)
       return e->getAttribute(ATTR_NAME) == name;
    else
       return false;
}

NodeImpl *HTMLCollectionImpl::item ( unsigned long index ) const
{
    //Most of the time, we just go in normal document order
    if (type != TABLE_ROWS)
        return NodeListImpl::item(index);

    //For table.rows, we first need to check header, then bodies, then footer.
    //we pack any extra headers/footer with bodies. This matches IE, and
    //means doing the usual thing with length is right
    const HTMLTableElementImpl* table = static_cast<const HTMLTableElementImpl*>(m_refNode);

    long                          sectionIndex;
    HTMLTableSectionElementImpl*  section;

    NodeImpl* found = 0;
    if (table->findRowSection(index, section, sectionIndex)) {
        HTMLCollectionImpl rows(section, TSECTION_ROWS);
        found = rows.item(sectionIndex);
    }

    m_cache->current.node = found; //namedItem needs this.
    m_cache->position     = index;
    return found;
}

unsigned long HTMLCollectionImpl::calcLength(NodeImpl *start) const
{
    if (type != TABLE_ROWS)
        return NodeListImpl::calcLength(start);

    unsigned length = 0;
    const HTMLTableElementImpl* table = static_cast<const HTMLTableElementImpl*>(m_refNode);
    for (NodeImpl* kid = table->firstChild(); kid; kid = kid->nextSibling()) {
        HTMLCollectionImpl rows(kid, TSECTION_ROWS);
        length += rows.length();
    }
    return length;
}

NodeImpl *HTMLCollectionImpl::firstItem() const
{
    return item(0);
}

NodeImpl *HTMLCollectionImpl::nextItem() const
{
    //### this assumes this is called immediately after nextItem --
    //it this sane?
    return item(m_cache->position + 1);
}

NodeImpl *HTMLCollectionImpl::namedItem( const DOMString &name ) const
{
    //Reset the position. The invariant is that nextNamedItem will start looking
    //from the current position.
    firstItem();

    return nextNamedItem(name);
}

NodeImpl *HTMLCollectionImpl::nextNamedItem( const DOMString &name ) const
{
    while (NodeImpl* candidate = m_cache->current.node)
    {
        //Always advance, for next call
        nextItem();
        if (checkForNameMatch(candidate, name))
            return candidate;
    }
    return 0;
}

TQValueList<NodeImpl*> HTMLCollectionImpl::namedItems( const DOMString &name ) const
{
    TQString key = name.string();

    //We use a work-conserving design for the name cache presently -- only
    //remember stuff about elements we were asked for.
    m_cache->updateNodeListInfo(m_refNode->getDocument());
    CollectionCache* cache = static_cast<CollectionCache*>(m_cache);
    if (TQValueList<NodeImpl*>* info = cache->nameCache.find(key)) {
        return *info;
    }
    else {
        TQValueList<NodeImpl*>* newInfo = new TQValueList<NodeImpl*>;

        NodeImpl* match = namedItem(name);
        while (match) {
            newInfo->append(match);
            match = nextNamedItem(name);
        }

        cache->nameCache.insert(key, newInfo);
        return *newInfo;
    }
}

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

HTMLFormCollectionImpl::HTMLFormCollectionImpl(NodeImpl* _base)
    : HTMLCollectionImpl(_base, FORM_ELEMENTS), currentNamePos(0), currentNameImgPos(0)
{}

NodeImpl *HTMLFormCollectionImpl::item( unsigned long index ) const
{
    m_cache->updateNodeListInfo(m_refNode->getDocument());

    unsigned int dist = index;
    unsigned int strt = 0;
    if (m_cache->current.index && m_cache->position <= index)
    {
        dist = index - m_cache->position;
        strt = m_cache->current.index;
    }

    TQPtrList<HTMLGenericFormElementImpl>& l = static_cast<HTMLFormElementImpl*>( m_refNode )->formElements;
    for (unsigned i = strt; i < l.count(); i++)
    {
        if (l.tqat( i )->isEnumeratable())
        {
            if (dist == 0)
            {
                //Found it!
                m_cache->position      = index;
                m_cache->current.index = i;
                return l.tqat( i );
            }
            else
                --dist;
        }
    }
    return 0;
}

unsigned long HTMLFormCollectionImpl::calcLength(NodeImpl *start) const
{
    unsigned length = 0;
    TQPtrList<HTMLGenericFormElementImpl> l = static_cast<HTMLFormElementImpl*>( start )->formElements;
    for ( unsigned i = 0; i < l.count(); i++ )
        if ( l.tqat( i )->isEnumeratable() )
            ++length;
    return length;
}

NodeImpl *HTMLFormCollectionImpl::namedItem( const DOMString &name ) const
{
    currentNamePos    = 0;
    currentNameImgPos = 0;
    foundInput        = false;
    return nextNamedItem(name);
}

NodeImpl *HTMLFormCollectionImpl::nextNamedItem( const DOMString &name ) const
{
    TQPtrList<HTMLGenericFormElementImpl>& l = static_cast<HTMLFormElementImpl*>( m_refNode )->formElements;

    //Go through the list, trying to find the appropriate named form element.
    for ( ; currentNamePos < l.count(); ++currentNamePos )
    {
        HTMLGenericFormElementImpl* el = l.tqat(currentNamePos);
        if (el->isEnumeratable() &&
             ((el->getAttribute(ATTR_ID)   == name) ||
              (el->getAttribute(ATTR_NAME) == name)))
        {
            ++currentNamePos; //Make next call start after this
            foundInput = true;//No need to look for img
            return el;
        }
    }

    //If we got this far, we may need to start looking through the images,
    //but only if no input tags were matched
    if (foundInput) return 0;

    TQPtrList<HTMLImageElementImpl>& il = static_cast<HTMLFormElementImpl*>( m_refNode )->imgElements;
    for ( ; currentNameImgPos < il.count(); ++currentNameImgPos )
    {
        HTMLImageElementImpl* el = il.tqat(currentNameImgPos);
        if ((el->getAttribute(ATTR_ID)   == name) ||
            (el->getAttribute(ATTR_NAME) == name))
        {
            ++currentNameImgPos; //Make next call start after this
            return el;
        }
    }

    return 0;
}

// -------------------------------------------------------------------------
HTMLMappedNameCollectionImpl::HTMLMappedNameCollectionImpl(NodeImpl* _base, int _type, const DOMString& _name):
    HTMLCollectionImpl(_base, NodeListImpl::UNCACHEABLE), name(_name)
{
    type = _type; //We pass uncacheable to collection, but need our own type internally.
}

bool HTMLMappedNameCollectionImpl::nodeMatches(NodeImpl *current, bool& deep) const
{
    if ( current->nodeType() != Node::ELEMENT_NODE )
    {
        deep = false;
        return false;
    }

    HTMLElementImpl *e = static_cast<HTMLElementImpl *>(current);

    return matchesName(e, type, name);
}

bool HTMLMappedNameCollectionImpl::matchesName( ElementImpl* el, int type, const DOMString& name )
{
    switch (el->id())
    {
    case ID_IMG:
    case ID_FORM:
        //Under document. these require non-empty name to see the element
        if (type == DOCUMENT_NAMED_ITEMS && el->getAttribute(ATTR_NAME).isNull())
            return false;
        //Otherwise, fallthrough
    case ID_OBJECT:
    case ID_EMBED:
    case ID_APPLET:
    case ID_LAYER:
        if (el->getAttribute(ATTR_NAME) == name || el->getAttribute(ATTR_ID) == name)
            return true;
        else
            return false;
    default:
        return false;
    }
}