html_tableimpl.cpp 28.4 KB
Newer Older
George Staikos's avatar
George Staikos committed
1 2 3 4 5 6 7 8 9
/**
 * This file is part of the DOM implementation for KDE.
 *
 * Copyright (C) 1997 Martin Jones (mjones@kde.org)
 *           (C) 1997 Torben Weis (weis@kde.org)
 *           (C) 1998 Waldo Bastian (bastian@kde.org)
 *           (C) 1999-2003 Lars Knoll (knoll@kde.org)
 *           (C) 1999 Antti Koivisto (koivisto@kde.org)
 *           (C) 2003 Apple Computer, Inc.
Maks Orlovich's avatar
Maks Orlovich committed
10
 *           (C) 2006 Maksim Orlovich (maksim@kde.org)
George Staikos's avatar
George Staikos committed
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
 *
 * 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.
 */

Alex Merry's avatar
Alex Merry committed
28
#include "html_tableimpl.h"
George Staikos's avatar
George Staikos committed
29

Alex Merry's avatar
Alex Merry committed
30 31
#include "html_documentimpl.h"

Alex Merry's avatar
Alex Merry committed
32 33
#include <dom/dom_exception.h>
#include <dom/dom_node.h>
George Staikos's avatar
George Staikos committed
34

Alex Merry's avatar
Alex Merry committed
35 36
#include <khtmlview.h>
#include <khtml_part.h>
George Staikos's avatar
George Staikos committed
37

Alex Merry's avatar
Alex Merry committed
38 39 40
#include <css/cssstyleselector.h>
#include <css/cssproperties.h>
#include <css/cssvalues.h>
George Staikos's avatar
George Staikos committed
41

Alex Merry's avatar
Alex Merry committed
42
#include <rendering/render_table.h>
George Staikos's avatar
George Staikos committed
43 44 45 46 47 48 49

#include <kdebug.h>
#include <kglobal.h>

using namespace khtml;
using namespace DOM;

Germain Garand's avatar
Germain Garand committed
50
HTMLTableElementImpl::HTMLTableElementImpl(DocumentImpl *doc)
George Staikos's avatar
George Staikos committed
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
  : HTMLElementImpl(doc)
{
    rules = None;
    frame = Void;

    padding = 1;

    m_solid = false;
}

HTMLTableElementImpl::~HTMLTableElementImpl()
{
}

NodeImpl::Id HTMLTableElementImpl::id() const
{
    return ID_TABLE;
}

NodeImpl* HTMLTableElementImpl::setCaption( HTMLTableCaptionElementImpl *c )
{
    int exceptioncode = 0;
    NodeImpl* r;
Maks Orlovich's avatar
Maks Orlovich committed
74 75
    if(ElementImpl* cap = caption()) {
        replaceChild ( c, cap, exceptioncode );
George Staikos's avatar
George Staikos committed
76 77 78 79 80 81 82 83 84 85 86 87
        r = c;
    }
    else
        r = insertBefore( c, firstChild(), exceptioncode );
    tCaption = c;
    return r;
}

NodeImpl* HTMLTableElementImpl::setTHead( HTMLTableSectionElementImpl *s )
{
    int exceptioncode = 0;
    NodeImpl* r;
Maks Orlovich's avatar
Maks Orlovich committed
88
    if( ElementImpl* head = tHead() ) {
George Staikos's avatar
George Staikos committed
89 90 91
        replaceChild( s, head, exceptioncode );
        r = s;
    }
Maks Orlovich's avatar
Maks Orlovich committed
92
    else if(ElementImpl* foot = tFoot())
George Staikos's avatar
George Staikos committed
93
        r = insertBefore( s, foot, exceptioncode );
Maks Orlovich's avatar
Maks Orlovich committed
94
    else if(ElementImpl* firstBody = tFirstBody())
George Staikos's avatar
George Staikos committed
95 96 97 98 99 100 101 102 103 104 105 106
        r = insertBefore( s, firstBody, exceptioncode );
    else
        r = appendChild( s, exceptioncode );

    head = s;
    return r;
}

NodeImpl* HTMLTableElementImpl::setTFoot( HTMLTableSectionElementImpl *s )
{
    int exceptioncode = 0;
    NodeImpl* r;
Maks Orlovich's avatar
Maks Orlovich committed
107
    if(ElementImpl* foot = tFoot()) {
George Staikos's avatar
George Staikos committed
108 109
        replaceChild ( s, foot, exceptioncode );
        r = s;
Maks Orlovich's avatar
Maks Orlovich committed
110
    } else if(ElementImpl* firstBody = tFirstBody())
George Staikos's avatar
George Staikos committed
111 112 113 114 115 116 117 118 119 120 121 122
        r = insertBefore( s, firstBody, exceptioncode );
    else
        r = appendChild( s, exceptioncode );
    foot = s;
    return r;
}

NodeImpl* HTMLTableElementImpl::setTBody( HTMLTableSectionElementImpl *s )
{
    int exceptioncode = 0;
    NodeImpl* r;

Maks Orlovich's avatar
Maks Orlovich committed
123
    if(ElementImpl* firstBody = tFirstBody()) {
George Staikos's avatar
George Staikos committed
124 125 126 127 128 129 130 131 132 133 134
        replaceChild ( s, firstBody, exceptioncode );
        r = s;
    } else
        r = appendChild( s, exceptioncode );
    firstBody = s;

    return r;
}

HTMLElementImpl *HTMLTableElementImpl::createTHead(  )
{
Maks Orlovich's avatar
Maks Orlovich committed
135
    if(!tHead())
George Staikos's avatar
George Staikos committed
136 137
    {
        int exceptioncode = 0;
Maks Orlovich's avatar
Maks Orlovich committed
138 139
        ElementImpl* head = new HTMLTableSectionElementImpl(docPtr(), ID_THEAD, true /* implicit */);
        if(ElementImpl* foot = tFoot())
George Staikos's avatar
George Staikos committed
140
            insertBefore( head, foot, exceptioncode );
Maks Orlovich's avatar
Maks Orlovich committed
141
        else if(ElementImpl* firstBody = tFirstBody())
George Staikos's avatar
George Staikos committed
142 143 144 145
            insertBefore( head, firstBody, exceptioncode);
        else
            appendChild(head, exceptioncode);
    }
Maks Orlovich's avatar
Maks Orlovich committed
146
    return tHead();
George Staikos's avatar
George Staikos committed
147 148 149 150
}

void HTMLTableElementImpl::deleteTHead(  )
{
Maks Orlovich's avatar
Maks Orlovich committed
151
    if(ElementImpl* head = tHead()) {
George Staikos's avatar
George Staikos committed
152
        int exceptioncode = 0;
Maks Orlovich's avatar
Maks Orlovich committed
153
        removeChild(head, exceptioncode);
George Staikos's avatar
George Staikos committed
154 155 156 157 158
    }
}

HTMLElementImpl *HTMLTableElementImpl::createTFoot(  )
{
Maks Orlovich's avatar
Maks Orlovich committed
159
    if(!tFoot())
George Staikos's avatar
George Staikos committed
160 161
    {
        int exceptioncode = 0;
Maks Orlovich's avatar
Maks Orlovich committed
162 163
        ElementImpl* foot = new HTMLTableSectionElementImpl(docPtr(), ID_TFOOT, true /*implicit */);
        if(ElementImpl* firstBody = tFirstBody())
George Staikos's avatar
George Staikos committed
164 165 166 167
            insertBefore( foot, firstBody, exceptioncode );
        else
            appendChild(foot, exceptioncode);
    }
Maks Orlovich's avatar
Maks Orlovich committed
168
    return tFoot();
George Staikos's avatar
George Staikos committed
169 170 171 172
}

void HTMLTableElementImpl::deleteTFoot(  )
{
Maks Orlovich's avatar
Maks Orlovich committed
173
    if(ElementImpl* foot = tFoot()) {
George Staikos's avatar
George Staikos committed
174
        int exceptioncode = 0;
Maks Orlovich's avatar
Maks Orlovich committed
175
        removeChild(foot, exceptioncode);
George Staikos's avatar
George Staikos committed
176 177 178 179 180
    }
}

HTMLElementImpl *HTMLTableElementImpl::createCaption(  )
{
Maks Orlovich's avatar
Maks Orlovich committed
181
    if(!caption())
George Staikos's avatar
George Staikos committed
182 183
    {
        int exceptioncode = 0;
Maks Orlovich's avatar
Maks Orlovich committed
184
        ElementImpl* tCaption = new HTMLTableCaptionElementImpl(docPtr());
George Staikos's avatar
George Staikos committed
185 186
        insertBefore( tCaption, firstChild(), exceptioncode );
    }
Maks Orlovich's avatar
Maks Orlovich committed
187
    return caption();
George Staikos's avatar
George Staikos committed
188 189 190 191
}

void HTMLTableElementImpl::deleteCaption(  )
{
Maks Orlovich's avatar
Maks Orlovich committed
192
    if(ElementImpl* tCaption = caption()) {
George Staikos's avatar
George Staikos committed
193
        int exceptioncode = 0;
Maks Orlovich's avatar
Maks Orlovich committed
194
        removeChild(tCaption, exceptioncode);
George Staikos's avatar
George Staikos committed
195 196 197
    }
}

Maks Orlovich's avatar
Maks Orlovich committed
198 199 200
/**
 Helper. This checks whether the section contains the desired index, and if so,
 returns the section. Otherwise, it adjust the index, and returns 0.
201
 indices < 0 are considered to be infinite.
Maks Orlovich's avatar
Maks Orlovich committed
202 203 204 205 206

 lastSection is adjusted to reflect the parameter passed in.
*/
static inline HTMLTableSectionElementImpl* processSection(HTMLTableSectionElementImpl* section,
                                HTMLTableSectionElementImpl* &lastSection, long& index)
George Staikos's avatar
George Staikos committed
207
{
Maks Orlovich's avatar
Maks Orlovich committed
208 209 210
    lastSection = section;
    if ( index < 0 ) //append/last mode
        return 0;
211

Maks Orlovich's avatar
Maks Orlovich committed
212 213 214 215 216 217 218 219 220 221 222 223 224 225 226
    long rows   = section->numRows();
    if ( index >= rows ) {
        section =  0;
        index   -= rows;
    }
    return section;
}


bool HTMLTableElementImpl::findRowSection(long index,
                        HTMLTableSectionElementImpl*& outSection,
                        long&                         outIndex) const
{
    HTMLTableSectionElementImpl* foot = tFoot();
    HTMLTableSectionElementImpl* head = tHead();
George Staikos's avatar
George Staikos committed
227 228 229

    HTMLTableSectionElementImpl* section = 0L;
    HTMLTableSectionElementImpl* lastSection = 0L;
Maks Orlovich's avatar
Maks Orlovich committed
230 231 232 233 234 235 236 237 238 239 240

    if ( head )
        section = processSection( head, lastSection, index );

    if ( !section ) {
        for ( NodeImpl *node = firstChild(); node; node = node->nextSibling() ) {
            if ( ( node->id() == ID_THEAD || node->id() == ID_TFOOT || node->id() == ID_TBODY ) &&
                   node != foot && node != head ) {
                section = processSection( static_cast<HTMLTableSectionElementImpl *>(node),
                        lastSection, index );
                if ( section )
George Staikos's avatar
George Staikos committed
241 242 243 244 245
                    break;
            }
        }
    }

Maks Orlovich's avatar
Maks Orlovich committed
246 247 248 249 250 251 252
    if ( !section && foot )
        section = processSection( foot, lastSection, index );

    outIndex = index;
    if ( section ) {
        outSection = section;
        return true;
George Staikos's avatar
George Staikos committed
253
    } else {
Maks Orlovich's avatar
Maks Orlovich committed
254 255
        outSection = lastSection;
        return false;
George Staikos's avatar
George Staikos committed
256 257 258
    }
}

Maks Orlovich's avatar
Maks Orlovich committed
259
HTMLElementImpl *HTMLTableElementImpl::insertRow( long index, int &exceptioncode )
George Staikos's avatar
George Staikos committed
260
{
Maks Orlovich's avatar
Maks Orlovich committed
261 262 263 264 265 266 267 268 269
    // The DOM requires that we create a tbody if the table is empty
    // (cf DOM2TS HTMLTableElement31 test). This includes even the cases where
    // there are <tr>'s immediately under the table, as they're essentially
    // ignored by these functions.
    HTMLTableSectionElementImpl* foot = tFoot();
    HTMLTableSectionElementImpl* head = tHead();
    if(!tFirstBody() && !foot && !head)
        setTBody( new HTMLTableSectionElementImpl(docPtr(), ID_TBODY, true /* implicit */) );

Thiago Macieira's avatar
Thiago Macieira committed
270
    //kDebug(6030) << index;
Maks Orlovich's avatar
Maks Orlovich committed
271 272 273 274 275 276 277

    long sectionIndex;
    HTMLTableSectionElementImpl* section;
    if ( findRowSection( index, section, sectionIndex ) )
        return section->insertRow( sectionIndex, exceptioncode );
    else if ( index == -1 || sectionIndex == 0 )
        return section->insertRow( section->numRows(), exceptioncode );
George Staikos's avatar
George Staikos committed
278

Maks Orlovich's avatar
Maks Orlovich committed
279 280 281 282 283 284 285 286 287 288 289 290 291
    // The index is too big.
    exceptioncode = DOMException::INDEX_SIZE_ERR;
    return 0L;
}

void HTMLTableElementImpl::deleteRow( long index, int &exceptioncode )
{
    long sectionIndex;
    HTMLTableSectionElementImpl* section;
    if ( findRowSection( index, section, sectionIndex ) )
        section->deleteRow( sectionIndex, exceptioncode );
    else if ( section && index == -1 )
        section->deleteRow( -1, exceptioncode );
George Staikos's avatar
George Staikos committed
292 293 294 295 296 297
    else
        exceptioncode = DOMException::INDEX_SIZE_ERR;
}

NodeImpl *HTMLTableElementImpl::appendChild(NodeImpl *child, int &exceptioncode)
{
Maks Orlovich's avatar
Maks Orlovich committed
298 299 300 301 302 303 304 305
     NodeImpl* retval = HTMLElementImpl::appendChild( child, exceptioncode );
     if(retval)
         handleChildAppend( child );
     return retval;
}

void HTMLTableElementImpl::handleChildAdd( NodeImpl *child )
{
306
    if (!child) return;
Maks Orlovich's avatar
Maks Orlovich committed
307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324
    switch(child->id()) {
    case ID_CAPTION:
        tCaption.childAdded(this, child);
        break;
    case ID_THEAD:
        head.childAdded(this, child);
        break;
    case ID_TFOOT:
        foot.childAdded(this, child);
        break;
    case ID_TBODY:
        firstBody.childAdded(this, child);
        break;
    }
}

void HTMLTableElementImpl::handleChildAppend( NodeImpl *child )
{
325
    if (!child) return;
Maks Orlovich's avatar
Maks Orlovich committed
326 327 328 329 330 331 332 333 334 335 336 337 338
    switch(child->id()) {
    case ID_CAPTION:
        tCaption.childAppended(child);
        break;
    case ID_THEAD:
        head.childAppended(child);
        break;
    case ID_TFOOT:
        foot.childAppended(child);
        break;
    case ID_TBODY:
        firstBody.childAppended(child);
        break;
George Staikos's avatar
George Staikos committed
339
    }
Maks Orlovich's avatar
Maks Orlovich committed
340
}
George Staikos's avatar
George Staikos committed
341

Maks Orlovich's avatar
Maks Orlovich committed
342 343
void HTMLTableElementImpl::handleChildRemove( NodeImpl *child )
{
344
    if (!child) return;
Maks Orlovich's avatar
Maks Orlovich committed
345 346 347 348 349 350 351 352 353 354 355 356 357 358
    switch(child->id()) {
    case ID_CAPTION:
        tCaption.childRemoved(this, child);
        break;
    case ID_THEAD:
        head.childRemoved(this, child);
        break;
    case ID_TFOOT:
        foot.childRemoved(this, child);
        break;
    case ID_TBODY:
        firstBody.childRemoved(this, child);
        break;
    }
George Staikos's avatar
George Staikos committed
359 360 361 362 363
}

NodeImpl *HTMLTableElementImpl::addChild(NodeImpl *child)
{
#ifdef DEBUG_LAYOUT
Laurent Montel's avatar
Laurent Montel committed
364
    kDebug( 6030 ) << nodeName().string() << "(Table)::addChild( " << child->nodeName().string() << " )";
George Staikos's avatar
George Staikos committed
365 366
#endif

Maks Orlovich's avatar
Maks Orlovich committed
367 368 369 370 371 372 373 374 375 376 377 378 379
    NodeImpl *retval = HTMLElementImpl::addChild( child );
    if ( retval )
        handleChildAppend( child );

    return retval;
}

NodeImpl *HTMLTableElementImpl::insertBefore ( NodeImpl *newChild, NodeImpl *refChild, int &exceptioncode )
{
    NodeImpl* retval = HTMLElementImpl::insertBefore( newChild, refChild, exceptioncode);
    if (retval)
        handleChildAdd( newChild );

George Staikos's avatar
George Staikos committed
380 381 382
    return retval;
}

383
void HTMLTableElementImpl::replaceChild ( NodeImpl *newChild, NodeImpl *oldChild, int &exceptioncode )
Maks Orlovich's avatar
Maks Orlovich committed
384 385
{
    handleChildRemove( oldChild ); //Always safe.
386 387
    HTMLElementImpl::replaceChild( newChild, oldChild, exceptioncode );
    if ( !exceptioncode )
Maks Orlovich's avatar
Maks Orlovich committed
388 389 390
        handleChildAdd( newChild );
}

391
void HTMLTableElementImpl::removeChild ( NodeImpl *oldChild, int &exceptioncode )
Maks Orlovich's avatar
Maks Orlovich committed
392 393
{
    handleChildRemove( oldChild );
394
    HTMLElementImpl::removeChild( oldChild, exceptioncode);
Maks Orlovich's avatar
Maks Orlovich committed
395
}
Maks Orlovich's avatar
Maks Orlovich committed
396 397 398 399 400 401 402 403 404 405

void HTMLTableElementImpl::removeChildren()
{
    HTMLElementImpl::removeChildren();
    tCaption.clear();
    head.clear();
    foot.clear();
    firstBody.clear();
}

406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422
static inline bool isTableCellAncestor(NodeImpl* n)
{
    return n->id() == ID_THEAD || n->id() == ID_TBODY ||
           n->id() == ID_TFOOT || n->id() == ID_TR;
}

static bool setTableCellsChanged(NodeImpl* n)
{
    assert(n);
    bool cellChanged = false;

    if (n->id() == ID_TD || n->id() == ID_TH)
        cellChanged = true;
    else if (isTableCellAncestor(n)) {
        for (NodeImpl* child = n->firstChild(); child; child = child->nextSibling())
            cellChanged |= setTableCellsChanged(child);
    }
Maks Orlovich's avatar
Maks Orlovich committed
423

424 425 426 427 428
    if (cellChanged)
       n->setChanged();

    return cellChanged;
}
429

George Staikos's avatar
George Staikos committed
430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449
void HTMLTableElementImpl::parseAttribute(AttributeImpl *attr)
{
    // ### to CSS!!
    switch(attr->id())
    {
    case ATTR_WIDTH:
        if (!attr->value().isEmpty())
            addCSSLength( CSS_PROP_WIDTH, attr->value() );
        else
            removeCSSProperty(CSS_PROP_WIDTH);
        break;
    case ATTR_HEIGHT:
        if (!attr->value().isEmpty())
            addCSSLength(CSS_PROP_HEIGHT, attr->value());
        else
            removeCSSProperty(CSS_PROP_HEIGHT);
        break;
    case ATTR_BORDER:
    {
        int border;
Germain Garand's avatar
Germain Garand committed
450
        bool ok = true;
George Staikos's avatar
George Staikos committed
451 452 453 454 455 456 457
        // ### this needs more work, as the border value is not only
        //     the border of the box, but also between the cells
        if(!attr->val())
            border = 0;
        else if(attr->val()->l == 0)
            border = 1;
        else
Germain Garand's avatar
Germain Garand committed
458 459 460
            border = attr->val()->toInt(&ok);
        if (!ok)
            border = 1;
George Staikos's avatar
George Staikos committed
461 462 463 464 465 466
#ifdef DEBUG_DRAW_BORDER
        border=1;
#endif
        DOMString v = QString::number( border );
        addCSSLength(CSS_PROP_BORDER_WIDTH, v );

Maks Orlovich's avatar
Maks Orlovich committed
467 468
        attr->rewriteValue( v );

George Staikos's avatar
George Staikos committed
469 470 471 472 473 474
        // wanted by HTML4 specs
        if(!border)
            frame = Void, rules = None;
        else
            frame = Box, rules = All;

475 476 477 478 479
        if (attached()) {
            updateFrame();
            if (tFirstBody())
                setTableCellsChanged(tFirstBody());
        }
George Staikos's avatar
George Staikos committed
480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497
        break;
    }
    case ATTR_BGCOLOR:
        if (!attr->value().isEmpty())
            addHTMLColor(CSS_PROP_BACKGROUND_COLOR, attr->value());
        else
            removeCSSProperty(CSS_PROP_BACKGROUND_COLOR);
        break;
    case ATTR_BORDERCOLOR:
        if(!attr->value().isEmpty()) {
            addHTMLColor(CSS_PROP_BORDER_COLOR, attr->value());
            m_solid = true;
        }

        if (attached()) updateFrame();
        break;
    case ATTR_BACKGROUND:
    {
498
        QString url = attr->value().trimSpaces().string();
499
        if (!url.isEmpty()) {
500
            url = document()->completeURL( url );
George Staikos's avatar
George Staikos committed
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
            addCSSProperty(CSS_PROP_BACKGROUND_IMAGE, DOMString("url('"+url+"')") );
        }
        else
            removeCSSProperty(CSS_PROP_BACKGROUND_IMAGE);
        break;
    }
    case ATTR_FRAME:

        if ( strcasecmp( attr->value(), "void" ) == 0 )
            frame = Void;
        else if ( strcasecmp( attr->value(), "border" ) == 0 )
            frame = Box;
        else if ( strcasecmp( attr->value(), "box" ) == 0 )
            frame = Box;
        else if ( strcasecmp( attr->value(), "hsides" ) == 0 )
            frame = Hsides;
        else if ( strcasecmp( attr->value(), "vsides" ) == 0 )
            frame = Vsides;
        else if ( strcasecmp( attr->value(), "above" ) == 0 )
            frame = Above;
        else if ( strcasecmp( attr->value(), "below" ) == 0 )
            frame = Below;
        else if ( strcasecmp( attr->value(), "lhs" ) == 0 )
            frame = Lhs;
        else if ( strcasecmp( attr->value(), "rhs" ) == 0 )
            frame = Rhs;

        if (attached()) updateFrame();
        break;
    case ATTR_RULES:
        if ( strcasecmp( attr->value(), "none" ) == 0 )
            rules = None;
        else if ( strcasecmp( attr->value(), "groups" ) == 0 )
            rules = Groups;
        else if ( strcasecmp( attr->value(), "rows" ) == 0 )
            rules = Rows;
        else if ( strcasecmp( attr->value(), "cols" ) == 0 )
            rules = Cols;
        else if ( strcasecmp( attr->value(), "all" ) == 0 )
            rules = All;
541 542 543 544

        if (attached() && tFirstBody())
            if (setTableCellsChanged(tFirstBody()))
                setChanged();
George Staikos's avatar
George Staikos committed
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
        break;
   case ATTR_CELLSPACING:
        if (!attr->value().isEmpty())
            addCSSLength(CSS_PROP_BORDER_SPACING, attr->value(), true);
        else
            removeCSSProperty(CSS_PROP_BORDER_SPACING);
        break;
    case ATTR_CELLPADDING:
        if (!attr->value().isEmpty())
	    padding = qMax( 0, attr->value().toInt() );
        else
	    padding = 1;
        if (m_render && m_render->isTable()) {
            static_cast<RenderTable *>(m_render)->setCellPadding(padding);
	    if (!m_render->needsLayout())
	        m_render->setNeedsLayout(true);
        }
        break;
    case ATTR_COLS:
    {
        // ###
#if 0
        int c;
        c = attr->val()->toInt();
        addColumns(c-totalCols);
#endif
        break;

    }
    case ATTR_ALIGN:
        setChanged();
        break;
    case ATTR_VALIGN:
        if (!attr->value().isEmpty())
            addCSSProperty(CSS_PROP_VERTICAL_ALIGN, attr->value().lower());
        else
            removeCSSProperty(CSS_PROP_VERTICAL_ALIGN);
        break;
    case ATTR_NOSAVE:
	break;
    default:
        HTMLElementImpl::parseAttribute(attr);
    }
}

void HTMLTableElementImpl::attach()
{
    updateFrame();
    HTMLElementImpl::attach();
    if ( m_render && m_render->isTable() )
        static_cast<RenderTable *>(m_render)->setCellPadding( padding );
}

void HTMLTableElementImpl::close()
{
Maks Orlovich's avatar
Maks Orlovich committed
600
    ElementImpl* firstBody = tFirstBody();
George Staikos's avatar
George Staikos committed
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
    if (firstBody && !firstBody->closed())
        firstBody->close();
    HTMLElementImpl::close();
}

void HTMLTableElementImpl::updateFrame()
{
    int v = m_solid ? CSS_VAL_SOLID : CSS_VAL_OUTSET;
    if ( frame & Above )
         addCSSProperty(CSS_PROP_BORDER_TOP_STYLE, v);
    else
        addCSSProperty(CSS_PROP_BORDER_TOP_STYLE, CSS_VAL_NONE);
    if ( frame & Below )
        addCSSProperty(CSS_PROP_BORDER_BOTTOM_STYLE, v);
    else
        addCSSProperty(CSS_PROP_BORDER_BOTTOM_STYLE, CSS_VAL_NONE);
    if ( frame & Lhs )
        addCSSProperty(CSS_PROP_BORDER_LEFT_STYLE, v);
    else
        addCSSProperty(CSS_PROP_BORDER_LEFT_STYLE, CSS_VAL_NONE);
    if ( frame & Rhs )
        addCSSProperty(CSS_PROP_BORDER_RIGHT_STYLE, v);
    else
        addCSSProperty(CSS_PROP_BORDER_RIGHT_STYLE, CSS_VAL_NONE);
}

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

void HTMLTablePartElementImpl::parseAttribute(AttributeImpl *attr)
{
    switch(attr->id())
    {
    case ATTR_BGCOLOR:
        if (attr->val())
            addHTMLColor(CSS_PROP_BACKGROUND_COLOR, attr->value() );
        else
            removeCSSProperty(CSS_PROP_BACKGROUND_COLOR);
        break;
    case ATTR_BACKGROUND:
    {
641
        QString url = attr->value().trimSpaces().string();
642
        if (!url.isEmpty()) {
643
            url = document()->completeURL( url );
George Staikos's avatar
George Staikos committed
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
            addCSSProperty(CSS_PROP_BACKGROUND_IMAGE,  DOMString("url('"+url+"')") );
        }
        else
            removeCSSProperty(CSS_PROP_BACKGROUND_IMAGE);
        break;
    }
    case ATTR_BORDERCOLOR:
    {
        if(!attr->value().isEmpty()) {
            addHTMLColor(CSS_PROP_BORDER_COLOR, attr->value());
            addCSSProperty(CSS_PROP_BORDER_TOP_STYLE, CSS_VAL_SOLID);
            addCSSProperty(CSS_PROP_BORDER_BOTTOM_STYLE, CSS_VAL_SOLID);
            addCSSProperty(CSS_PROP_BORDER_LEFT_STYLE, CSS_VAL_SOLID);
            addCSSProperty(CSS_PROP_BORDER_RIGHT_STYLE, CSS_VAL_SOLID);
        }
        break;
    }
    case ATTR_ALIGN:
    {
        DOMString v = attr->value();
        if ( strcasecmp( attr->value(), "middle" ) == 0 || strcasecmp( attr->value(), "center" ) == 0 )
            addCSSProperty(CSS_PROP_TEXT_ALIGN, CSS_VAL__KHTML_CENTER);
        else if (strcasecmp(attr->value(), "absmiddle") == 0)
            addCSSProperty(CSS_PROP_TEXT_ALIGN, CSS_VAL_CENTER);
        else if (strcasecmp(attr->value(), "left") == 0)
            addCSSProperty(CSS_PROP_TEXT_ALIGN, CSS_VAL__KHTML_LEFT);
        else if (strcasecmp(attr->value(), "right") == 0)
            addCSSProperty(CSS_PROP_TEXT_ALIGN, CSS_VAL__KHTML_RIGHT);
        else
            addCSSProperty(CSS_PROP_TEXT_ALIGN, v);
        break;
    }
    case ATTR_VALIGN:
    {
        if (!attr->value().isEmpty())
            addCSSProperty(CSS_PROP_VERTICAL_ALIGN, attr->value().lower());
        else
            removeCSSProperty(CSS_PROP_VERTICAL_ALIGN);
        break;
    }
    case ATTR_HEIGHT:
        if (!attr->value().isEmpty())
            addCSSLength(CSS_PROP_HEIGHT, attr->value());
        else
            removeCSSProperty(CSS_PROP_HEIGHT);
        break;
    case ATTR_NOSAVE:
	break;
    default:
        HTMLElementImpl::parseAttribute(attr);
    }
}

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

Germain Garand's avatar
Germain Garand committed
699
HTMLTableSectionElementImpl::HTMLTableSectionElementImpl(DocumentImpl *doc,
George Staikos's avatar
George Staikos committed
700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720
                                                         ushort tagid, bool implicit)
    : HTMLTablePartElementImpl(doc)
{
    _id = tagid;
    m_implicit = implicit;
}

HTMLTableSectionElementImpl::~HTMLTableSectionElementImpl()
{
}

NodeImpl::Id HTMLTableSectionElementImpl::id() const
{
    return _id;
}

// these functions are rather slow, since we need to get the row at
// the index... but they aren't used during usual HTML parsing anyway
HTMLElementImpl *HTMLTableSectionElementImpl::insertRow( long index, int& exceptioncode )
{
    HTMLTableRowElementImpl *r = 0L;
Maks Orlovich's avatar
Maks Orlovich committed
721 722
    HTMLCollectionImpl rows(const_cast<HTMLTableSectionElementImpl*>(this), HTMLCollectionImpl::TSECTION_ROWS);
    int numRows = rows.length();
Thiago Macieira's avatar
Thiago Macieira committed
723
    //kDebug(6030) << "index=" << index << " numRows=" << numRows;
George Staikos's avatar
George Staikos committed
724 725 726 727 728 729 730 731 732 733 734 735 736
    if ( index < -1 || index > numRows ) {
        exceptioncode = DOMException::INDEX_SIZE_ERR; // per the DOM
    }
    else
    {
        r = new HTMLTableRowElementImpl(docPtr());
        if ( numRows == index || index == -1 )
            appendChild(r, exceptioncode);
        else {
            NodeImpl *n;
            if(index < 1)
                n = firstChild();
            else
Maks Orlovich's avatar
Maks Orlovich committed
737
                n = rows.item(index);
George Staikos's avatar
George Staikos committed
738 739 740 741 742 743 744 745
            insertBefore(r, n, exceptioncode );
        }
    }
    return r;
}

void HTMLTableSectionElementImpl::deleteRow( long index, int &exceptioncode )
{
Maks Orlovich's avatar
Maks Orlovich committed
746 747
    HTMLCollectionImpl rows(const_cast<HTMLTableSectionElementImpl*>(this), HTMLCollectionImpl::TSECTION_ROWS);
    int numRows = rows.length();
George Staikos's avatar
George Staikos committed
748 749
    if ( index == -1 ) index = numRows - 1;
    if( index >= 0 && index < numRows )
Maks Orlovich's avatar
Maks Orlovich committed
750
        HTMLElementImpl::removeChild(rows.item(index), exceptioncode);
George Staikos's avatar
George Staikos committed
751 752 753 754 755 756 757
    else
        exceptioncode = DOMException::INDEX_SIZE_ERR;
}


int HTMLTableSectionElementImpl::numRows() const
{
Maks Orlovich's avatar
Maks Orlovich committed
758 759
    HTMLCollectionImpl rows(const_cast<HTMLTableSectionElementImpl*>(this), HTMLCollectionImpl::TSECTION_ROWS);
    return rows.length();
George Staikos's avatar
George Staikos committed
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
}

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

NodeImpl::Id HTMLTableRowElementImpl::id() const
{
    return ID_TR;
}

long HTMLTableRowElementImpl::rowIndex() const
{
    int rIndex = 0;

    NodeImpl *table = parentNode();
    if ( !table )
	return -1;
    table = table->parentNode();
    if ( !table || table->id() != ID_TABLE )
	return -1;

    HTMLTableSectionElementImpl *head = static_cast<HTMLTableElementImpl *>(table)->tHead();
    HTMLTableSectionElementImpl *foot = static_cast<HTMLTableElementImpl *>(table)->tFoot();

    if ( head ) {
        const NodeImpl *row = head->firstChild();
        while ( row ) {
            if ( row == this )
                return rIndex;
788 789
            if (row->id() == ID_TR)
                rIndex++;
George Staikos's avatar
George Staikos committed
790 791 792 793 794 795 796 797 798 799 800 801
            row = row->nextSibling();
        }
    }

    NodeImpl *node = table->firstChild();
    while ( node ) {
        if ( node != foot && node != head && (node->id() == ID_THEAD || node->id() == ID_TFOOT || node->id() == ID_TBODY) ) {
	    HTMLTableSectionElementImpl* section = static_cast<HTMLTableSectionElementImpl *>(node);
	    const NodeImpl *row = section->firstChild();
	    while ( row ) {
		if ( row == this )
		    return rIndex;
802 803
                if (row->id() == ID_TR)
		    rIndex++;
George Staikos's avatar
George Staikos committed
804 805 806 807 808 809 810 811 812
		row = row->nextSibling();
	    }
	}
	node = node->nextSibling();
    }
    const NodeImpl *row = foot->firstChild();
    while ( row ) {
	if ( row == this )
	    return rIndex;
813 814
        if (row->id() == ID_TR)
	    rIndex++;
George Staikos's avatar
George Staikos committed
815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837
	row = row->nextSibling();
    }
    // should never happen
    return -1;
}

long HTMLTableRowElementImpl::sectionRowIndex() const
{
    int rIndex = 0;
    const NodeImpl *n = this;
    do {
        n = n->previousSibling();
        if (n && n->id() == ID_TR)
            rIndex++;
    }
    while (n);

    return rIndex;
}

HTMLElementImpl *HTMLTableRowElementImpl::insertCell( long index, int &exceptioncode )
{
    HTMLTableCellElementImpl *c = 0L;
838 839
    HTMLCollectionImpl children(const_cast<HTMLTableRowElementImpl*>(this), HTMLCollectionImpl::TR_CELLS);
    int numCells = children.length();
George Staikos's avatar
George Staikos committed
840 841 842 843 844 845 846 847 848 849 850 851
    if ( index < -1 || index > numCells )
        exceptioncode = DOMException::INDEX_SIZE_ERR; // per the DOM
    else
    {
        c = new HTMLTableCellElementImpl(docPtr(), ID_TD);
        if(numCells == index || index == -1)
            appendChild(c, exceptioncode);
        else {
            NodeImpl *n;
            if(index < 1)
                n = firstChild();
            else
852
                n = children.item(index);
George Staikos's avatar
George Staikos committed
853 854 855 856 857 858 859 860
            insertBefore(c, n, exceptioncode);
        }
    }
    return c;
}

void HTMLTableRowElementImpl::deleteCell( long index, int &exceptioncode )
{
861 862
    HTMLCollectionImpl children(const_cast<HTMLTableRowElementImpl*>(this), HTMLCollectionImpl::TR_CELLS);
    int numCells = children.length();
George Staikos's avatar
George Staikos committed
863 864
    if ( index == -1 ) index = numCells-1;
    if( index >= 0 && index < numCells )
865
        HTMLElementImpl::removeChild(children.item(index), exceptioncode);
George Staikos's avatar
George Staikos committed
866 867 868 869 870 871
    else
        exceptioncode = DOMException::INDEX_SIZE_ERR;
}

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

Germain Garand's avatar
Germain Garand committed
872
HTMLTableCellElementImpl::HTMLTableCellElementImpl(DocumentImpl *doc, int tag)
George Staikos's avatar
George Staikos committed
873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905
  : HTMLTablePartElementImpl(doc)
{
  _col = -1;
  _row = -1;
  cSpan = rSpan = 1;
  _id = tag;
  rowHeight = 0;
  m_solid = false;
}

HTMLTableCellElementImpl::~HTMLTableCellElementImpl()
{
}

long HTMLTableCellElementImpl::cellIndex() const
{
    int index = 0;
    for (const NodeImpl * node = previousSibling(); node; node = node->previousSibling()) {
        if (node->id() == ID_TD || node->id() == ID_TH)
            index++;
    }

    return index;
}

void HTMLTableCellElementImpl::parseAttribute(AttributeImpl *attr)
{
    switch(attr->id())
    {
    case ATTR_BORDER:
        // euhm? not supported by other browsers as far as I can see (Dirk)
        //addCSSLength(CSS_PROP_BORDER_WIDTH, attr->value());
        break;
906 907 908
    case ATTR_ROWSPAN: {
        bool Ok = true;
        rSpan = attr->val() ? attr->val()->toInt(&Ok) : 1;
George Staikos's avatar
George Staikos committed
909
        // limit this to something not causing an overflow with short int
910
        if(rSpan < 0 || rSpan > 1024 || !Ok || (!rSpan && document()->inCompatMode())) rSpan = 1;
Germain Garand's avatar
 
Germain Garand committed
911
        if (renderer())
912
            renderer()->updateFromElement();
George Staikos's avatar
George Staikos committed
913
        break;
914 915 916 917
      }
    case ATTR_COLSPAN: {
        bool Ok = true;
        cSpan = attr->val() ? attr->val()->toInt(&Ok) : 1;
George Staikos's avatar
George Staikos committed
918
        // limit this to something not causing an overflow with short int
919
        if(cSpan < 0 || cSpan > 1024 || !Ok || (!cSpan && document()->inCompatMode())) cSpan = 1;
Germain Garand's avatar
 
Germain Garand committed
920
        if (renderer())
921
            renderer()->updateFromElement();
George Staikos's avatar
George Staikos committed
922
        break;
923
     }
George Staikos's avatar
George Staikos committed
924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949
    case ATTR_NOWRAP:
        if (attr->val() != 0)
	    addCSSProperty(CSS_PROP_WHITE_SPACE, CSS_VAL__KHTML_NOWRAP);
        else
	    removeCSSProperty(CSS_PROP_WHITE_SPACE);
        break;
    case ATTR_WIDTH:
        if (!attr->value().isEmpty())
            addCSSLength( CSS_PROP_WIDTH, attr->value() );
        else
            removeCSSProperty(CSS_PROP_WIDTH);
        break;
    case ATTR_NOSAVE:
	break;
    default:
        HTMLTablePartElementImpl::parseAttribute(attr);
    }
}

void HTMLTableCellElementImpl::attach()
{
    HTMLTablePartElementImpl::attach();
}

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

Germain Garand's avatar
Germain Garand committed
950
HTMLTableColElementImpl::HTMLTableColElementImpl(DocumentImpl *doc, ushort i)
George Staikos's avatar
George Staikos committed
951 952 953
    : HTMLTablePartElementImpl(doc)
{
    _id = i;
954
    _span = 1;
George Staikos's avatar
George Staikos committed
955 956 957 958 959 960 961 962 963 964 965 966 967 968
}

NodeImpl::Id HTMLTableColElementImpl::id() const
{
    return _id;
}


void HTMLTableColElementImpl::parseAttribute(AttributeImpl *attr)
{
    switch(attr->id())
    {
    case ATTR_SPAN:
        _span = attr->val() ? attr->val()->toInt() : 1;
969
        if (_span < 1) _span = 1;
George Staikos's avatar
George Staikos committed
970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011
        break;
    case ATTR_WIDTH:
        if (!attr->value().isEmpty())
            addCSSLength(CSS_PROP_WIDTH, attr->value(), false, true );
        else
            removeCSSProperty(CSS_PROP_WIDTH);
        break;
    case ATTR_VALIGN:
        if (!attr->value().isEmpty())
            addCSSProperty(CSS_PROP_VERTICAL_ALIGN, attr->value().lower());
        else
            removeCSSProperty(CSS_PROP_VERTICAL_ALIGN);
        break;
    default:
        HTMLTablePartElementImpl::parseAttribute(attr);
    }

}

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

NodeImpl::Id HTMLTableCaptionElementImpl::id() const
{
    return ID_CAPTION;
}


void HTMLTableCaptionElementImpl::parseAttribute(AttributeImpl *attr)
{
    switch(attr->id())
    {
    case ATTR_ALIGN:
        if (!attr->value().isEmpty())
            addCSSProperty(CSS_PROP_CAPTION_SIDE, attr->value().lower());
        else
            removeCSSProperty(CSS_PROP_CAPTION_SIDE);
        break;
    default:
        HTMLElementImpl::parseAttribute(attr);
    }

}