css_valueimpl.cpp 49.3 KB
Newer Older
George Staikos's avatar
George Staikos committed
1 2 3
/**
 * This file is part of the DOM implementation for KDE.
 *
4
 * Copyright (C) 1999-2003 Lars Knoll (knoll@kde.org)
5
 *           (C) 2004-2008 Apple Computer, Inc.
6
 *           (C) 2005 Allan Sandfeld Jensen (kde@carewolf.com)
7
 *           (C) 2009 Germain Garand (germain@ebooksfrance.org)
George Staikos's avatar
George Staikos committed
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
 *
 * 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
25 26 27
#include "css_valueimpl.h"
#include "css_ruleimpl.h"
#include "css_stylesheetimpl.h"
28
#include "css/csshelper.h"
Alex Merry's avatar
Alex Merry committed
29 30 31
#include "cssparser.h"
#include "cssproperties.h"
#include "cssvalues.h"
George Staikos's avatar
George Staikos committed
32

Alex Merry's avatar
Alex Merry committed
33 34 35
#include <dom/css_value.h>
#include <dom/dom_exception.h>
#include <dom/dom_string.h>
George Staikos's avatar
George Staikos committed
36

Alex Merry's avatar
Alex Merry committed
37 38
#include <xml/dom_stringimpl.h>
#include <xml/dom_docimpl.h>
George Staikos's avatar
George Staikos committed
39

Alex Merry's avatar
Alex Merry committed
40 41
#include <rendering/font.h>
#include <rendering/render_style.h>
George Staikos's avatar
George Staikos committed
42

43 44
#include <wtf/ASCIICType.h>

George Staikos's avatar
George Staikos committed
45
#include <kdebug.h>
Dirk Mueller's avatar
Dirk Mueller committed
46 47
#include <QtCore/QRegExp>
#include <QtGui/QPaintDevice>
George Staikos's avatar
George Staikos committed
48 49 50 51 52 53 54

// Hack for debugging purposes
extern DOM::DOMString getPropertyName(unsigned short id);

using khtml::FontDef;

using namespace DOM;
55
using namespace WTF;
George Staikos's avatar
George Staikos committed
56 57 58 59 60 61 62 63 64 65 66 67 68

static int propertyID(const DOMString &s)
{
    char buffer[maxCSSPropertyNameLength];

    unsigned len = s.length();
    if (len > maxCSSPropertyNameLength)
        return 0;

    for (unsigned i = 0; i != len; ++i) {
        unsigned short c = s[i].unicode();
        if (c == 0 || c >= 0x7F)
            return 0; // illegal character
69
        buffer[i] = s[i].toLower().unicode();
George Staikos's avatar
George Staikos committed
70 71 72 73 74
    }

    return getPropertyID(buffer, len);
}

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
// "ident" from the CSS tokenizer, minus backslash-escape sequences
static bool isCSSTokenizerIdentifier(const DOMString& string)
{
    const QChar* p = string.unicode();
    const QChar* end = p + string.length();

    // -?
    if (p != end && p[0] == '-')
        ++p;

    // {nmstart}
    if (p == end || !(p[0] == '_' || p[0] >= 128 || isASCIIAlpha(p->unicode())))
        return false;
    ++p;

    // {nmchar}*
    for (; p != end; ++p) {
        if (!(p[0] == '_' || p[0] == '-' || p[0] >= 128 || isASCIIAlphanumeric(p->unicode())))
            return false;
    }

    return true;
}

static DOMString quoteString(const DOMString &string)
Maks Orlovich's avatar
Maks Orlovich committed
100 101 102 103 104
{
    // FIXME: Also need to transform control characters into \ sequences.
    QString s = string.string();
    s.replace('\\', "\\\\");
    s.replace('\'', "\\'");
105
    return QString('\'' + s + '\'');
Maks Orlovich's avatar
Maks Orlovich committed
106 107
}

108 109 110 111 112
// Quotes the string if it needs quoting.
static DOMString quoteStringIfNeeded(const DOMString &string)
{
    return isCSSTokenizerIdentifier(string) ? string : quoteString(string);
}
Maks Orlovich's avatar
Maks Orlovich committed
113

George Staikos's avatar
George Staikos committed
114 115 116 117 118 119 120
CSSStyleDeclarationImpl::CSSStyleDeclarationImpl(CSSRuleImpl *parent)
    : StyleBaseImpl(parent)
{
    m_lstValues = 0;
    m_node = 0;
}

121
CSSStyleDeclarationImpl::CSSStyleDeclarationImpl(CSSRuleImpl *parent, QList<CSSProperty*> *lstValues)
George Staikos's avatar
George Staikos committed
122 123 124 125 126 127 128 129
    : StyleBaseImpl(parent)
{
    m_lstValues = lstValues;
    m_node = 0;
}

CSSStyleDeclarationImpl&  CSSStyleDeclarationImpl::operator= (const CSSStyleDeclarationImpl& o)
{
130 131
    if (this == &o) return *this;

George Staikos's avatar
George Staikos committed
132
    // don't attach it to the same node, just leave the current m_node value
133 134
    if (m_lstValues)
        qDeleteAll(*m_lstValues);
George Staikos's avatar
George Staikos committed
135 136 137
    delete m_lstValues;
    m_lstValues = 0;
    if (o.m_lstValues) {
138 139 140 141
        m_lstValues = new QList<CSSProperty*>;
        QListIterator<CSSProperty*> lstValuesIt(*o.m_lstValues);
        while ( lstValuesIt.hasNext() )
            m_lstValues->append(new CSSProperty(*lstValuesIt.next()));
George Staikos's avatar
George Staikos committed
142 143 144 145 146 147 148
    }

    return *this;
}

CSSStyleDeclarationImpl::~CSSStyleDeclarationImpl()
{
149 150
    if (m_lstValues)
        qDeleteAll( *m_lstValues );
George Staikos's avatar
George Staikos committed
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
    delete m_lstValues;
    // we don't use refcounting for m_node, to avoid cyclic references (see ElementImpl)
}

CSSValueImpl *CSSStyleDeclarationImpl::getPropertyCSSValue(const DOMString &propertyName) const
{
    int propID = propertyID(propertyName);
    if (!propID)
        return 0;
    return getPropertyCSSValue(propID);
}

DOMString CSSStyleDeclarationImpl::getPropertyValue(const DOMString &propertyName) const
{
    int propID = propertyID(propertyName);
    if (!propID)
        return DOMString();
    return getPropertyValue(propID);
}

DOMString CSSStyleDeclarationImpl::getPropertyPriority(const DOMString &propertyName) const
{
    int propID = propertyID(propertyName);
    if (!propID)
        return DOMString();
    return getPropertyPriority(propID) ? "important" : "";
}

void CSSStyleDeclarationImpl::setProperty(const DOMString &propertyName, const DOMString &value, const DOMString &priority)
{
    int propID = propertyID(propertyName);
    if (!propID) // set exception?
        return;
184
    bool important = priority.string().indexOf("important", 0, Qt::CaseInsensitive) != -1;
George Staikos's avatar
George Staikos committed
185 186 187 188 189 190 191 192
    setProperty(propID, value, important);
}

DOMString CSSStyleDeclarationImpl::removeProperty(const DOMString &propertyName)
{
    int propID = propertyID(propertyName);
    if (!propID)
        return DOMString();
193 194 195
    DOMString old;
    removeProperty(propID, &old);
    return old;
George Staikos's avatar
George Staikos committed
196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211
}

DOMString CSSStyleDeclarationImpl::getPropertyValue( int propertyID ) const
{
    if(!m_lstValues) return DOMString();
    CSSValueImpl* value = getPropertyCSSValue( propertyID );
    if ( value )
        return value->cssText();

    // Shorthand and 4-values properties
    switch ( propertyID ) {
    case CSS_PROP_BACKGROUND_POSITION:
    {
        // ## Is this correct? The code in cssparser.cpp is confusing
        const int properties[2] = { CSS_PROP_BACKGROUND_POSITION_X,
                                    CSS_PROP_BACKGROUND_POSITION_Y };
212
        return getLayeredShortHandValue( properties, 2 );
George Staikos's avatar
George Staikos committed
213 214 215
    }
    case CSS_PROP_BACKGROUND:
    {
216 217 218 219
        const int properties[6] = { CSS_PROP_BACKGROUND_IMAGE, CSS_PROP_BACKGROUND_REPEAT,
                                    CSS_PROP_BACKGROUND_ATTACHMENT,CSS_PROP_BACKGROUND_POSITION_X,
                                    CSS_PROP_BACKGROUND_POSITION_Y, CSS_PROP_BACKGROUND_COLOR };
        return getLayeredShortHandValue( properties, 6 );
George Staikos's avatar
George Staikos committed
220 221 222
    }
    case CSS_PROP_BORDER:
    {
223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246
            const int properties[3][4] = {{ CSS_PROP_BORDER_TOP_WIDTH,
                                            CSS_PROP_BORDER_RIGHT_WIDTH,
                                            CSS_PROP_BORDER_BOTTOM_WIDTH,
                                            CSS_PROP_BORDER_LEFT_WIDTH },
                                          { CSS_PROP_BORDER_TOP_STYLE,
                                            CSS_PROP_BORDER_RIGHT_STYLE,
                                            CSS_PROP_BORDER_BOTTOM_STYLE,
                                            CSS_PROP_BORDER_LEFT_STYLE },
                                          { CSS_PROP_BORDER_TOP_COLOR,
                                            CSS_PROP_BORDER_RIGHT_COLOR,
                                            CSS_PROP_BORDER_LEFT_COLOR,
                                            CSS_PROP_BORDER_BOTTOM_COLOR }};
            DOMString res;
            const int nrprops = sizeof(properties) / sizeof(properties[0]);
            for (int i = 0; i < nrprops; ++i) {
                DOMString value = getCommonValue(properties[i], 4);
                if (!value.isNull()) {
                    if (!res.isNull())
                        res += " ";
                    res += value;
                }
            }
            return res;

George Staikos's avatar
George Staikos committed
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
    }
    case CSS_PROP_BORDER_TOP:
    {
        const int properties[3] = { CSS_PROP_BORDER_TOP_WIDTH, CSS_PROP_BORDER_TOP_STYLE,
                                    CSS_PROP_BORDER_TOP_COLOR};
        return getShortHandValue( properties, 3 );
    }
    case CSS_PROP_BORDER_RIGHT:
    {
        const int properties[3] = { CSS_PROP_BORDER_RIGHT_WIDTH, CSS_PROP_BORDER_RIGHT_STYLE,
                                    CSS_PROP_BORDER_RIGHT_COLOR};
        return getShortHandValue( properties, 3 );
    }
    case CSS_PROP_BORDER_BOTTOM:
    {
        const int properties[3] = { CSS_PROP_BORDER_BOTTOM_WIDTH, CSS_PROP_BORDER_BOTTOM_STYLE,
                                    CSS_PROP_BORDER_BOTTOM_COLOR};
        return getShortHandValue( properties, 3 );
    }
    case CSS_PROP_BORDER_LEFT:
    {
        const int properties[3] = { CSS_PROP_BORDER_LEFT_WIDTH, CSS_PROP_BORDER_LEFT_STYLE,
                                    CSS_PROP_BORDER_LEFT_COLOR};
        return getShortHandValue( properties, 3 );
    }
    case CSS_PROP_OUTLINE:
    {
        const int properties[3] = { CSS_PROP_OUTLINE_WIDTH, CSS_PROP_OUTLINE_STYLE,
                                    CSS_PROP_OUTLINE_COLOR };
        return getShortHandValue( properties, 3 );
    }
    case CSS_PROP_BORDER_COLOR:
    {
        const int properties[4] = { CSS_PROP_BORDER_TOP_COLOR, CSS_PROP_BORDER_RIGHT_COLOR,
                                    CSS_PROP_BORDER_BOTTOM_COLOR, CSS_PROP_BORDER_LEFT_COLOR };
        return get4Values( properties );
    }
    case CSS_PROP_BORDER_WIDTH:
    {
        const int properties[4] = { CSS_PROP_BORDER_TOP_WIDTH, CSS_PROP_BORDER_RIGHT_WIDTH,
                                    CSS_PROP_BORDER_BOTTOM_WIDTH, CSS_PROP_BORDER_LEFT_WIDTH };
        return get4Values( properties );
    }
    case CSS_PROP_BORDER_STYLE:
    {
        const int properties[4] = { CSS_PROP_BORDER_TOP_STYLE, CSS_PROP_BORDER_RIGHT_STYLE,
                                    CSS_PROP_BORDER_BOTTOM_STYLE, CSS_PROP_BORDER_LEFT_STYLE };
        return get4Values( properties );
    }
    case CSS_PROP_MARGIN:
    {
        const int properties[4] = { CSS_PROP_MARGIN_TOP, CSS_PROP_MARGIN_RIGHT,
                                    CSS_PROP_MARGIN_BOTTOM, CSS_PROP_MARGIN_LEFT };
        return get4Values( properties );
    }
    case CSS_PROP_PADDING:
    {
        const int properties[4] = { CSS_PROP_PADDING_TOP, CSS_PROP_PADDING_RIGHT,
                                    CSS_PROP_PADDING_BOTTOM, CSS_PROP_PADDING_LEFT };
        return get4Values( properties );
    }
    case CSS_PROP_LIST_STYLE:
    {
        const int properties[3] = { CSS_PROP_LIST_STYLE_TYPE, CSS_PROP_LIST_STYLE_POSITION,
                                    CSS_PROP_LIST_STYLE_IMAGE };
        return getShortHandValue( properties, 3 );
    }
    }
Thiago Macieira's avatar
Thiago Macieira committed
315
    //kDebug() << "property not found:" << propertyID;
George Staikos's avatar
George Staikos committed
316 317
    return DOMString();
}
318 319 320 321 322 323
 
// only returns a non-null value if all properties have the same, non-null value
DOMString CSSStyleDeclarationImpl::getCommonValue(const int* properties, int number) const
{
    DOMString res;
    for (int i = 0; i < number; ++i) {
324 325 326 327 328 329 330 331 332 333
        CSSValueImpl* value = getPropertyCSSValue(properties[i]);
        if (!value)
            return DOMString();
        DOMString text = value->cssText();
        if (text.isNull())
            return DOMString();
        if (res.isNull())
            res = text;
        else if (res != text)
            return DOMString();
334 335 336
    }
    return res;
}
George Staikos's avatar
George Staikos committed
337 338 339 340 341

DOMString CSSStyleDeclarationImpl::get4Values( const int* properties ) const
{
    DOMString res;
    for ( int i = 0 ; i < 4 ; ++i ) {
342 343 344 345 346 347 348 349
        if (!isPropertyImplicit(properties[i])) {
            CSSValueImpl* value = getPropertyCSSValue( properties[i] );
            if ( !value ) { // apparently all 4 properties must be specified.
                return DOMString();
            }
            if ( i > 0 )
                res += " ";
            res += value->cssText();
George Staikos's avatar
George Staikos committed
350 351 352 353 354
        }
    }
    return res;
}

355 356 357 358 359 360 361 362 363
DOMString CSSStyleDeclarationImpl::getLayeredShortHandValue(const int* properties, unsigned number) const
{
    DOMString res;
    unsigned i;
    unsigned j;

    // Begin by collecting the properties into an array.
    QVector<CSSValueImpl*> values(number);
    unsigned numLayers = 0;
364

365 366 367 368 369 370 371 372 373 374
    for (i = 0; i < number; ++i) {
        values[i] = getPropertyCSSValue(properties[i]);
        if (values[i]) {
            if (values[i]->isValueList()) {
                CSSValueListImpl* valueList = static_cast<CSSValueListImpl*>(values[i]);
                numLayers = qMax(valueList->length(), (unsigned long)numLayers);
            } else
                numLayers = qMax(1U, numLayers);
        }
    }
375

376 377 378 379 380 381 382 383 384 385 386
    // Now stitch the properties together.  Implicit initial values are flagged as such and
    // can safely be omitted.
    for (i = 0; i < numLayers; i++) {
        DOMString layerRes;
        for (j = 0; j < number; j++) {
            CSSValueImpl* value = 0;
            if (values[j]) {
                if (values[j]->isValueList())
                    value = static_cast<CSSValueListImpl*>(values[j])->item(i);
                else {
                    value = values[j];
387

388 389 390 391 392 393 394 395
                    // Color only belongs in the last layer.
                    if (properties[j] == CSS_PROP_BACKGROUND_COLOR) {
                        if (i != numLayers - 1)
                            value = 0;
                    } else if (i != 0) // Other singletons only belong in the first layer.
                        value = 0;
                }
            }
396

397 398 399 400 401 402
            if (value && !value->isImplicitInitialValue()) {
                if (!layerRes.isNull())
                    layerRes += " ";
                layerRes += value->cssText();
            }
        }
403

404 405 406 407 408 409 410 411 412 413
        if (!layerRes.isNull()) {
            if (!res.isNull())
                res += ", ";
            res += layerRes;
        }
    }

    return res;
}

George Staikos's avatar
George Staikos committed
414 415 416 417
DOMString CSSStyleDeclarationImpl::getShortHandValue( const int* properties, int number ) const
{
    DOMString res;
    for ( int i = 0 ; i < number ; ++i ) {
418 419 420 421 422
        CSSValueImpl* value = getPropertyCSSValue( properties[i] );
        if ( value ) { // TODO provide default value if !value
            if ( !res.isNull() )
                res += " ";
            res += value->cssText();
George Staikos's avatar
George Staikos committed
423 424 425 426 427 428 429 430 431
        }
    }
    return res;
}

 CSSValueImpl *CSSStyleDeclarationImpl::getPropertyCSSValue( int propertyID ) const
{
    if(!m_lstValues) return 0;

432
    QListIterator<CSSProperty*> lstValuesIt(*m_lstValues);
George Staikos's avatar
George Staikos committed
433
    CSSProperty *current;
434 435
    while ( lstValuesIt.hasNext() ) {
        current = lstValuesIt.next();
436
        if (current->m_id == propertyID)
George Staikos's avatar
George Staikos committed
437
            return current->value();
438
    }
George Staikos's avatar
George Staikos committed
439 440 441
    return 0;
}

442 443 444 445 446 447 448
bool CSSStyleDeclarationImpl::isPropertyImplicit(int propertyID) const
{
    QListIterator<CSSProperty*> lstValuesIt(*m_lstValues);
    CSSProperty const *current;
    while ( lstValuesIt.hasNext() ) {
        current = lstValuesIt.next();
        if (current->m_id == propertyID)
449
            return current->isImplicit();
450 451 452 453
    }
    return false;
}

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
// --------------- Shorthands mapping ----------------

// In order top be able to remove a shorthand property,
// we need a reverse mapping from the shorthands to their composing properties.

// ### Warning: keep in sync when introducing new shorthands.

struct PropertyLonghand {
    PropertyLonghand()
        : m_properties(0)
        , m_length(0)
    {
    }

    PropertyLonghand(const int* firstProperty, unsigned numProperties)
        : m_properties(firstProperty)
        , m_length(numProperties)
    {
    }

    const int* properties() const { return m_properties; }
    unsigned length() const { return m_length; }

private:
    const int* m_properties;
    unsigned m_length;
};

static void initShorthandMap(QHash<int, PropertyLonghand>& shorthandMap)
{
    #define SET_SHORTHAND_MAP_ENTRY(map, propID, array) \
        map.insert(propID, PropertyLonghand(array, sizeof(array) / sizeof(array[0])))

    // Do not change the order of the following four shorthands, and keep them together.
    static const int borderProperties[4][3] = {
        { CSS_PROP_BORDER_TOP_COLOR, CSS_PROP_BORDER_TOP_STYLE, CSS_PROP_BORDER_TOP_WIDTH },
        { CSS_PROP_BORDER_RIGHT_COLOR, CSS_PROP_BORDER_RIGHT_STYLE, CSS_PROP_BORDER_RIGHT_WIDTH },
        { CSS_PROP_BORDER_BOTTOM_COLOR, CSS_PROP_BORDER_BOTTOM_STYLE, CSS_PROP_BORDER_BOTTOM_WIDTH },
        { CSS_PROP_BORDER_LEFT_COLOR, CSS_PROP_BORDER_LEFT_STYLE, CSS_PROP_BORDER_LEFT_WIDTH }
    };
    SET_SHORTHAND_MAP_ENTRY(shorthandMap, CSS_PROP_BORDER_TOP, borderProperties[0]);
    SET_SHORTHAND_MAP_ENTRY(shorthandMap, CSS_PROP_BORDER_RIGHT, borderProperties[1]);
    SET_SHORTHAND_MAP_ENTRY(shorthandMap, CSS_PROP_BORDER_BOTTOM, borderProperties[2]);
    SET_SHORTHAND_MAP_ENTRY(shorthandMap, CSS_PROP_BORDER_LEFT, borderProperties[3]);

    shorthandMap.insert(CSS_PROP_BORDER, PropertyLonghand(borderProperties[0], sizeof(borderProperties) / sizeof(borderProperties[0][0])));

    static const int borderColorProperties[] = {
        CSS_PROP_BORDER_TOP_COLOR,
        CSS_PROP_BORDER_RIGHT_COLOR,
        CSS_PROP_BORDER_BOTTOM_COLOR,
        CSS_PROP_BORDER_LEFT_COLOR
    };
    SET_SHORTHAND_MAP_ENTRY(shorthandMap, CSS_PROP_BORDER_COLOR, borderColorProperties);

    static const int borderStyleProperties[] = {
        CSS_PROP_BORDER_TOP_STYLE,
        CSS_PROP_BORDER_RIGHT_STYLE,
        CSS_PROP_BORDER_BOTTOM_STYLE,
        CSS_PROP_BORDER_LEFT_STYLE
    };
    SET_SHORTHAND_MAP_ENTRY(shorthandMap, CSS_PROP_BORDER_STYLE, borderStyleProperties);

    static const int borderWidthProperties[] = {
        CSS_PROP_BORDER_TOP_WIDTH,
        CSS_PROP_BORDER_RIGHT_WIDTH,
        CSS_PROP_BORDER_BOTTOM_WIDTH,
        CSS_PROP_BORDER_LEFT_WIDTH
    };
    SET_SHORTHAND_MAP_ENTRY(shorthandMap, CSS_PROP_BORDER_WIDTH, borderWidthProperties);

    static const int backgroundPositionProperties[] = { CSS_PROP_BACKGROUND_POSITION_X, CSS_PROP_BACKGROUND_POSITION_Y };
    SET_SHORTHAND_MAP_ENTRY(shorthandMap, CSS_PROP_BACKGROUND_POSITION, backgroundPositionProperties);

    static const int borderSpacingProperties[] = { CSS_PROP__KHTML_BORDER_HORIZONTAL_SPACING, CSS_PROP__KHTML_BORDER_VERTICAL_SPACING };
    SET_SHORTHAND_MAP_ENTRY(shorthandMap, CSS_PROP_BORDER_SPACING, borderSpacingProperties);

    static const int listStyleProperties[] = {
        CSS_PROP_LIST_STYLE_IMAGE,
        CSS_PROP_LIST_STYLE_POSITION,
        CSS_PROP_LIST_STYLE_TYPE
    };
    SET_SHORTHAND_MAP_ENTRY(shorthandMap, CSS_PROP_LIST_STYLE, listStyleProperties);

    static const int marginProperties[] = {
        CSS_PROP_MARGIN_TOP,
        CSS_PROP_MARGIN_RIGHT,
        CSS_PROP_MARGIN_BOTTOM,
        CSS_PROP_MARGIN_LEFT
    };
    SET_SHORTHAND_MAP_ENTRY(shorthandMap, CSS_PROP_MARGIN, marginProperties);

#ifdef APPLE_CHANGES
    static const int marginCollapseProperties[] = { CSS_PROP__KHTML_MARGIN_TOP_COLLAPSE, CSS_PROP__KHTML_MARGIN_BOTTOM_COLLAPSE };
    SET_SHORTHAND_MAP_ENTRY(shorthandMap, CSS_PROP__KHTML_MARGIN_COLLAPSE, marginCollapseProperties);
#endif

    static const int marqueeProperties[] = {
        CSS_PROP__KHTML_MARQUEE_DIRECTION,
        CSS_PROP__KHTML_MARQUEE_INCREMENT,
        CSS_PROP__KHTML_MARQUEE_REPETITION,
        CSS_PROP__KHTML_MARQUEE_STYLE,
        CSS_PROP__KHTML_MARQUEE_SPEED
    };
    SET_SHORTHAND_MAP_ENTRY(shorthandMap, CSS_PROP__KHTML_MARQUEE, marqueeProperties);

    static const int outlineProperties[] = {
        CSS_PROP_OUTLINE_COLOR,
        CSS_PROP_OUTLINE_OFFSET,
        CSS_PROP_OUTLINE_STYLE,
        CSS_PROP_OUTLINE_WIDTH
    };
    SET_SHORTHAND_MAP_ENTRY(shorthandMap, CSS_PROP_OUTLINE, outlineProperties);

    static const int paddingProperties[] = {
        CSS_PROP_PADDING_TOP,
        CSS_PROP_PADDING_RIGHT,
        CSS_PROP_PADDING_BOTTOM,
        CSS_PROP_PADDING_LEFT
    };
    SET_SHORTHAND_MAP_ENTRY(shorthandMap, CSS_PROP_PADDING, paddingProperties);

#ifdef APPLE_CHANGES
    static const int textStrokeProperties[] = { CSS_PROP__KHTML_TEXT_STROKE_COLOR, CSS_PROP__KHTML_TEXT_STROKE_WIDTH };
    SET_SHORTHAND_MAP_ENTRY(shorthandMap, CSS_PROP__KHTML_TEXT_STROKE, textStrokeProperties);
#endif

    static const int backgroundProperties[] = {
        CSS_PROP_BACKGROUND_ATTACHMENT,
        CSS_PROP_BACKGROUND_COLOR,
        CSS_PROP_BACKGROUND_IMAGE,
        CSS_PROP_BACKGROUND_POSITION_X,
        CSS_PROP_BACKGROUND_POSITION_Y,
        CSS_PROP_BACKGROUND_REPEAT,
588 589
        CSS_PROP__KHTML_BACKGROUND_SIZE,
        CSS_PROP_BACKGROUND_SIZE
590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608
    };
    SET_SHORTHAND_MAP_ENTRY(shorthandMap, CSS_PROP_BACKGROUND, backgroundProperties);

#ifdef APPLE_CHANGES
    static const int columnsProperties[] = { CSS_PROP__KHTML_COLUMN_WIDTH, CSS_PROP__KHTML_COLUMN_COUNT };
    SET_SHORTHAND_MAP_ENTRY(shorthandMap, CSS_PROP__KHTML_COLUMNS, columnsProperties);

    static const int columnRuleProperties[] = {
        CSS_PROP__KHTML_COLUMN_RULE_COLOR,
        CSS_PROP__KHTML_COLUMN_RULE_STYLE,
        CSS_PROP__KHTML_COLUMN_RULE_WIDTH
    };
    SET_SHORTHAND_MAP_ENTRY(shorthandMap, CSS_PROP__KHTML_COLUMN_RULE, columnRuleProperties);
#endif

    static const int overflowProperties[] = { CSS_PROP_OVERFLOW_X, CSS_PROP_OVERFLOW_Y };
    SET_SHORTHAND_MAP_ENTRY(shorthandMap, CSS_PROP_OVERFLOW, overflowProperties);

    static const int borderRadiusProperties[] = {
609 610 611 612 613 614 615
        CSS_PROP_BORDER_TOP_RIGHT_RADIUS,
        CSS_PROP_BORDER_TOP_LEFT_RADIUS,
        CSS_PROP_BORDER_BOTTOM_LEFT_RADIUS,
        CSS_PROP_BORDER_BOTTOM_RIGHT_RADIUS
    };
    SET_SHORTHAND_MAP_ENTRY(shorthandMap, CSS_PROP_BORDER_RADIUS, borderRadiusProperties);
    static const int prefixedBorderRadiusProperties[] = {
616 617 618 619 620
        CSS_PROP__KHTML_BORDER_TOP_RIGHT_RADIUS,
        CSS_PROP__KHTML_BORDER_TOP_LEFT_RADIUS,
        CSS_PROP__KHTML_BORDER_BOTTOM_LEFT_RADIUS,
        CSS_PROP__KHTML_BORDER_BOTTOM_RIGHT_RADIUS
    };
621
    SET_SHORTHAND_MAP_ENTRY(shorthandMap, CSS_PROP__KHTML_BORDER_RADIUS, prefixedBorderRadiusProperties);
622

623
    static const int markerProperties[] = {
624
        CSS_PROP_MARKER_START,
625 626 627 628
        CSS_PROP_MARKER_MID,
        CSS_PROP_MARKER_END
    };
    SET_SHORTHAND_MAP_ENTRY(shorthandMap, CSS_PROP_MARKER, markerProperties);
629

630 631 632 633 634 635 636 637 638 639
    static const int fontProperties[] = {
        CSS_PROP_FONT_STYLE,
        CSS_PROP_FONT_VARIANT,
        CSS_PROP_FONT_WEIGHT,
        CSS_PROP_FONT_SIZE,
        CSS_PROP_LINE_HEIGHT,
        CSS_PROP_FONT_FAMILY
    };
    SET_SHORTHAND_MAP_ENTRY(shorthandMap, CSS_PROP_FONT, fontProperties);

640 641 642 643 644
    #undef SET_SHORTHAND_MAP_ENTRY
}

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

645
void CSSStyleDeclarationImpl::removeProperty(int propertyID, DOM::DOMString* old)
George Staikos's avatar
George Staikos committed
646
{
647 648
    if(!m_lstValues)
        return;
George Staikos's avatar
George Staikos committed
649

650 651
    bool changed = false;

652 653 654 655 656 657
    static QHash<int, PropertyLonghand> shorthandMap;
    if (shorthandMap.isEmpty())
        initShorthandMap(shorthandMap);

    PropertyLonghand longhand = shorthandMap.value(propertyID);
    if (longhand.length()) {
658 659
        // Remove shorthand's longhand subproperties
        changed = removePropertiesInSet(longhand.properties(), longhand.length());
660 661
    }

662 663
    // FIXME: Return an equivalent shorthand when possible.

664 665 666 667 668
    QMutableListIterator<CSSProperty*> lstValuesIt(*m_lstValues);
    CSSProperty *current;
    lstValuesIt.toBack();
    while ( lstValuesIt.hasPrevious() ) {
        current = lstValuesIt.previous();
669
        if (current->m_id == propertyID) {
670 671
            if (old)
                *old = current->value()->cssText();
672 673
            delete lstValuesIt.value();
            lstValuesIt.remove();
674
            changed = true;
675
            break;
George Staikos's avatar
George Staikos committed
676 677
        }
     }
678 679 680 681

     if (changed) {
        setChanged();
     }
George Staikos's avatar
George Staikos committed
682 683
}

684
bool CSSStyleDeclarationImpl::removePropertiesInSet(const int* set, unsigned length)
685 686 687 688 689 690 691 692
{
    bool changed = false;
    for (unsigned i = 0; i < length; i++) {
        QMutableListIterator<CSSProperty*> lstValuesIt(*m_lstValues);
        CSSProperty *current;
        lstValuesIt.toBack();
        while ( lstValuesIt.hasPrevious() ) {
            current = lstValuesIt.previous();
693
            if (current->m_id == set[i]) {
694 695 696 697 698
                delete lstValuesIt.value();
                lstValuesIt.remove();
                changed = true;
                break;
            }
699
        }
700
    }
701 702

    return changed;
703 704
}

George Staikos's avatar
George Staikos committed
705 706 707 708 709 710 711 712 713 714 715 716 717 718 719
void CSSStyleDeclarationImpl::setChanged()
{
    if (m_node) {
        m_node->setChanged();
        return;
    }

    // ### quick&dirty hack for KDE 3.0... make this MUCH better! (Dirk)
    for (StyleBaseImpl* stylesheet = this; stylesheet; stylesheet = stylesheet->parent())
        if (stylesheet->isCSSStyleSheet()) {
            static_cast<CSSStyleSheetImpl*>(stylesheet)->doc()->updateStyleSelector();
            break;
        }
}

720
void CSSStyleDeclarationImpl::clear()
George Staikos's avatar
George Staikos committed
721 722
{
    if (!m_lstValues)
723
        return;
George Staikos's avatar
George Staikos committed
724

725 726
    QMutableListIterator<CSSProperty*> it(*m_lstValues);
    while (it.hasNext()) {
727
            delete it.next();
728
            it.remove();
George Staikos's avatar
George Staikos committed
729 730 731 732 733 734
    }
}

bool CSSStyleDeclarationImpl::getPropertyPriority( int propertyID ) const
{
    if ( m_lstValues) {
735
	QListIterator<CSSProperty*> lstValuesIt(*m_lstValues);
George Staikos's avatar
George Staikos committed
736
	CSSProperty *current;
737 738
	while (lstValuesIt.hasNext()) {
            current = lstValuesIt.next();
George Staikos's avatar
George Staikos committed
739
	    if( propertyID == current->m_id )
740
		return current->m_important;
George Staikos's avatar
George Staikos committed
741 742 743 744 745
	}
    }
    return false;
}

746
bool CSSStyleDeclarationImpl::setProperty(int id, const DOMString &value, bool important, int &ec)
747 748 749 750 751 752
{
    ec = 0;

    // Setting the value to an empty string just removes the property in both IE and Gecko.
    // Setting it to null seems to produce less consistent results, but we treat it just the same.
    if (value.isEmpty()) {
753
        removeProperty(id);
754 755 756
        return true;
    }

757
    bool success = setProperty(id, value, important);
758 759 760 761 762 763 764 765 766
#if 0
    if (!success) {
        // CSS DOM requires raising SYNTAX_ERR here, but this is too dangerous for compatibility,
        // see <http://bugs.webkit.org/show_bug.cgi?id=7296>.
    }
#endif
    return success;
}

767
bool CSSStyleDeclarationImpl::setProperty(int id, const DOMString &value, bool important)
George Staikos's avatar
George Staikos committed
768 769
{
    if(!m_lstValues) {
770
	m_lstValues = new QList<CSSProperty*>;
George Staikos's avatar
George Staikos committed
771 772 773
    }

    CSSParser parser( strictParsing );
774
    bool success = parser.parseValue( this, id, value, important );
George Staikos's avatar
George Staikos committed
775
    if(!success)
Gregory S. Hayes's avatar
Gregory S. Hayes committed
776
	kDebug( 6080 ) << "CSSStyleDeclarationImpl::setProperty invalid property: [" << getPropertyName(id).string()
George Staikos's avatar
George Staikos committed
777 778 779 780 781 782
			<< "] value: [" << value.string() << "]"<< endl;
    else
        setChanged();
    return success;
}

783
void CSSStyleDeclarationImpl::setProperty(int id, int value, bool important )
George Staikos's avatar
George Staikos committed
784 785
{
    if(!m_lstValues) {
786
	m_lstValues = new QList<CSSProperty*>;
George Staikos's avatar
George Staikos committed
787
    }
788
    removeProperty(id);
George Staikos's avatar
George Staikos committed
789 790

    CSSValueImpl * cssValue = new CSSPrimitiveValueImpl(value);
791
    setParsedValue(id, cssValue, important, m_lstValues);
George Staikos's avatar
George Staikos committed
792 793 794
    setChanged();
}

795
void CSSStyleDeclarationImpl::setLengthProperty(int id, const DOM::DOMString &value, bool important, bool _multiLength )
George Staikos's avatar
George Staikos committed
796 797 798 799
{
    bool parseMode = strictParsing;
    strictParsing = false;
    multiLength = _multiLength;
800
    setProperty( id, value, important );
George Staikos's avatar
George Staikos committed
801 802 803 804 805 806 807
    strictParsing = parseMode;
    multiLength = false;
}

void CSSStyleDeclarationImpl::setProperty ( const DOMString &propertyString)
{
    if(!m_lstValues) {
808
	m_lstValues = new QList<CSSProperty*>;
George Staikos's avatar
George Staikos committed
809 810 811
    }

    CSSParser parser( strictParsing );
812
    parser.parseDeclaration( this, propertyString );
George Staikos's avatar
George Staikos committed
813 814 815 816 817 818 819 820 821 822
    setChanged();
}

unsigned long CSSStyleDeclarationImpl::length() const
{
    return m_lstValues ? m_lstValues->count() : 0;
}

DOMString CSSStyleDeclarationImpl::item( unsigned long index ) const
{
823
    if(m_lstValues && index < (unsigned)m_lstValues->count() && m_lstValues->at(index))
George Staikos's avatar
George Staikos committed
824 825 826 827 828 829 830 831 832 833 834 835 836 837
	return getPropertyName(m_lstValues->at(index)->m_id);
    return DOMString();
}

CSSRuleImpl *CSSStyleDeclarationImpl::parentRule() const
{
    return (m_parent && m_parent->isRule() ) ?
	static_cast<CSSRuleImpl *>(m_parent) : 0;
}

DOM::DOMString CSSStyleDeclarationImpl::cssText() const
{
    DOMString result;

838 839 840
    const CSSProperty* positionXProp = 0;
    const CSSProperty* positionYProp = 0;

George Staikos's avatar
George Staikos committed
841
    if ( m_lstValues) {
842 843
	QListIterator<CSSProperty*> lstValuesIt(*m_lstValues);
	while (lstValuesIt.hasNext()) {
844 845 846 847 848 849 850
	    const CSSProperty* cur = lstValuesIt.next();
            if (cur->id() == CSS_PROP_BACKGROUND_POSITION_X)
                positionXProp = cur;
            else if (cur->id() == CSS_PROP_BACKGROUND_POSITION_Y)
                positionYProp = cur;
            else
                result += cur->cssText();
George Staikos's avatar
George Staikos committed
851 852 853
	}
    }

854 855 856 857 858 859 860 861 862 863
    // FIXME: This is a not-so-nice way to turn x/y positions into single background-position in output.
    // It is required because background-position-x/y are non-standard properties and generated output
    // would not work in Firefox
    // It would be a better solution if background-position was CSS_PAIR.
    if (positionXProp && positionYProp && positionXProp->isImportant() == positionYProp->isImportant()) {
        DOMString positionValue;
        const int properties[2] = { CSS_PROP_BACKGROUND_POSITION_X, CSS_PROP_BACKGROUND_POSITION_Y };
        if (positionXProp->value()->isValueList() || positionYProp->value()->isValueList())
            positionValue = getLayeredShortHandValue(properties, 2);
        else
864
            positionValue = positionXProp->value()->cssText() + DOMString(" ") + positionYProp->value()->cssText();
865
        result += DOMString("background-position: ") + positionValue
866
                                                     + DOMString((positionXProp->isImportant() ? " !important" : ""))
867
                                                     + DOMString("; ");
868 869 870 871 872 873
    } else {
        if (positionXProp)
            result += positionXProp->cssText();
        if (positionYProp)
            result += positionYProp->cssText();
    }
George Staikos's avatar
George Staikos committed
874 875 876
    return result;
}

Harri Porten's avatar
Harri Porten committed
877
void CSSStyleDeclarationImpl::setCssText(const DOM::DOMString& text)
George Staikos's avatar
George Staikos committed
878 879
{
    if (m_lstValues) {
880
        qDeleteAll(*m_lstValues);
George Staikos's avatar
George Staikos committed
881 882
	m_lstValues->clear();
    } else {
883
	m_lstValues = new QList<CSSProperty*>;
George Staikos's avatar
George Staikos committed
884 885 886
    }

    CSSParser parser( strictParsing );
887
    parser.parseDeclaration( this, text );
George Staikos's avatar
George Staikos committed
888 889 890 891 892
    setChanged();
}

bool CSSStyleDeclarationImpl::parseString( const DOMString &/*string*/, bool )
{
Laurent Montel's avatar
Laurent Montel committed
893
    kDebug() << "WARNING: CSSStyleDeclarationImpl::parseString, unimplemented, was called";
George Staikos's avatar
George Staikos committed
894 895 896 897
    return false;
    // ###
}

898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917
// --------------------------------------------------------------------------------------

void CSSInlineStyleDeclarationImpl::setChanged()
{
    if (m_node)
        m_node->setNeedsStyleAttributeUpdate();
    CSSStyleDeclarationImpl::setChanged();
}

void CSSInlineStyleDeclarationImpl::updateFromAttribute(const DOMString &value)
{
    if(!m_lstValues) {
	m_lstValues = new QList<CSSProperty*>;
    } else {
        clear();
    }
    CSSParser parser( strictParsing );
    parser.parseDeclaration( this, value );
    CSSStyleDeclarationImpl::setChanged();
}
George Staikos's avatar
George Staikos committed
918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944

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

unsigned short CSSInheritedValueImpl::cssValueType() const
{
    return CSSValue::CSS_INHERIT;
}

DOM::DOMString CSSInheritedValueImpl::cssText() const
{
    return DOMString("inherit");
}

unsigned short CSSInitialValueImpl::cssValueType() const
{
    return CSSValue::CSS_INITIAL;
}

DOM::DOMString CSSInitialValueImpl::cssText() const
{
    return DOMString("initial");
}

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

CSSValueListImpl::~CSSValueListImpl()
{
945 946
    for (QListIterator<CSSValueImpl*> iterator(m_values); iterator.hasNext();)
        iterator.next()->deref();
George Staikos's avatar
George Staikos committed
947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963
}

unsigned short CSSValueListImpl::cssValueType() const
{
    return CSSValue::CSS_VALUE_LIST;
}

void CSSValueListImpl::append(CSSValueImpl *val)
{
    m_values.append(val);
    val->ref();
}

DOM::DOMString CSSValueListImpl::cssText() const
{
    DOMString result = "";

964
    for (QListIterator<CSSValueImpl*> iterator(m_values); iterator.hasNext();) {
965 966 967 968 969 970 971
        if (!result.isEmpty()) {
            if (m_separator == Comma)
                result += ", ";
            else if (m_separator == Space)
                result += " ";
        }
        result += iterator.next()->cssText();
George Staikos's avatar
George Staikos committed
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 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026
    }

    return result;
}

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

CSSPrimitiveValueImpl::CSSPrimitiveValueImpl()
    : CSSValueImpl()
{
    m_type = 0;
}

CSSPrimitiveValueImpl::CSSPrimitiveValueImpl(int ident)
    : CSSValueImpl()
{
    m_value.ident = ident;
    m_type = CSSPrimitiveValue::CSS_IDENT;
}

CSSPrimitiveValueImpl::CSSPrimitiveValueImpl(double num, CSSPrimitiveValue::UnitTypes type)
{
    m_value.num = num;
    m_type = type;
}

CSSPrimitiveValueImpl::CSSPrimitiveValueImpl(const DOMString &str, CSSPrimitiveValue::UnitTypes type)
{
    m_value.string = str.implementation();
    if(m_value.string) m_value.string->ref();
    m_type = type;
}

CSSPrimitiveValueImpl::CSSPrimitiveValueImpl(CounterImpl *c)
{
    m_value.counter = c;
    if (m_value.counter)
	m_value.counter->ref();
    m_type = CSSPrimitiveValue::CSS_COUNTER;
}

CSSPrimitiveValueImpl::CSSPrimitiveValueImpl( RectImpl *r)
{
    m_value.rect = r;
    if (m_value.rect)
	m_value.rect->ref();
    m_type = CSSPrimitiveValue::CSS_RECT;
}

CSSPrimitiveValueImpl::CSSPrimitiveValueImpl(QRgb color)
{
    m_value.rgbcolor = color;
    m_type = CSSPrimitiveValue::CSS_RGBCOLOR;
}

1027 1028 1029 1030 1031 1032 1033 1034 1035
CSSPrimitiveValueImpl::CSSPrimitiveValueImpl(PairImpl *p)
{
    m_value.pair = p;
    if (m_value.pair)
	m_value.pair->ref();
    m_type = CSSPrimitiveValue::CSS_PAIR;
}


George Staikos's avatar
George Staikos committed
1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053
CSSPrimitiveValueImpl::~CSSPrimitiveValueImpl()
{
    cleanup();
}

void CSSPrimitiveValueImpl::cleanup()
{
    switch(m_type) {
    case CSSPrimitiveValue::CSS_STRING:
    case CSSPrimitiveValue::CSS_URI:
    case CSSPrimitiveValue::CSS_ATTR:
	if(m_value.string) m_value.string->deref();
        break;
    case CSSPrimitiveValue::CSS_COUNTER:
	m_value.counter->deref();
        break;
    case CSSPrimitiveValue::CSS_RECT:
	m_value.rect->deref();
1054 1055 1056 1057
        break;
    case CSSPrimitiveValue::CSS_PAIR:
        m_value.pair->deref();
        break;
George Staikos's avatar
George Staikos committed
1058 1059 1060 1061 1062 1063 1064
    default:
        break;
    }

    m_type = 0;
}

1065
int CSSPrimitiveValueImpl::computeLength( khtml::RenderStyle *style, int logicalDpiY)
George Staikos's avatar
George Staikos committed
1066
{
1067
    return snapValue( computeLengthFloat( style, logicalDpiY ) );
George Staikos's avatar
George Staikos committed
1068 1069
}

1070
double CSSPrimitiveValueImpl::computeLengthFloat( khtml::RenderStyle *style, int logicalDpiY)
George Staikos's avatar
George Staikos committed
1071 1072 1073 1074
{
    unsigned short type = primitiveType();

    double dpiY = 72.; // fallback
1075 1076
    if ( logicalDpiY )
        dpiY = logicalDpiY;
George Staikos's avatar
George Staikos committed
1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116
    if ( !khtml::printpainter && dpiY < 96 )
        dpiY = 96.;

    double factor = 1.;
    switch(type)
    {
    case CSSPrimitiveValue::CSS_EMS:
        factor = style->font().pixelSize();
        break;
    case CSSPrimitiveValue::CSS_EXS:
	{
        QFontMetrics fm = style->fontMetrics();
        factor = fm.xHeight();
        break;
	}
    case CSSPrimitiveValue::CSS_PX:
        break;
    case CSSPrimitiveValue::CSS_CM:
	factor = dpiY/2.54; //72dpi/(2.54 cm/in)
        break;
    case CSSPrimitiveValue::CSS_MM:
	factor = dpiY/25.4;
        break;
    case CSSPrimitiveValue::CSS_IN:
        factor = dpiY;
        break;
    case CSSPrimitiveValue::CSS_PT:
        factor = dpiY/72.;
        break;
    case CSSPrimitiveValue::CSS_PC:
        // 1 pc == 12 pt
        factor = dpiY*12./72.;
        break;
    default:
        return -1;
    }

    return floatValue(type)*factor;
}

Germain Garand's avatar
Germain Garand committed
1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134
int CSSPrimitiveValueImpl::getDPIResolution() const
{
    unsigned short type = primitiveType();
    double factor = 1.;
    switch(type)
    {
    case CSSPrimitiveValue::CSS_DPI:
        break;
    case CSSPrimitiveValue::CSS_DPCM:
        factor = 2.54;
        break;
    default:
        return -1;
    }

    return (int)(0.01+floatValue(type)*factor);
}

George Staikos's avatar
George Staikos committed
1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175
void CSSPrimitiveValueImpl::setFloatValue( unsigned short unitType, double floatValue, int &exceptioncode )
{
    exceptioncode = 0;
    cleanup();
    // ### check if property supports this type
    if(m_type > CSSPrimitiveValue::CSS_DIMENSION) {
	exceptioncode = CSSException::SYNTAX_ERR + CSSException::_EXCEPTION_OFFSET;
	return;
    }
    //if(m_type > CSSPrimitiveValue::CSS_DIMENSION) throw DOMException(DOMException::INVALID_ACCESS_ERR);
    m_value.num = floatValue;
    m_type = unitType;
}

void CSSPrimitiveValueImpl::setStringValue( unsigned short stringType, const DOMString &stringValue, int &exceptioncode )
{
    exceptioncode = 0;
    cleanup();
    //if(m_type < CSSPrimitiveValue::CSS_STRING) throw DOMException(DOMException::INVALID_ACCESS_ERR);
    //if(m_type > CSSPrimitiveValue::CSS_ATTR) throw DOMException(DOMException::INVALID_ACCESS_ERR);
    if(m_type < CSSPrimitiveValue::CSS_STRING || m_type > CSSPrimitiveValue::CSS_ATTR) {
	exceptioncode = CSSException::SYNTAX_ERR + CSSException::_EXCEPTION_OFFSET;
	return;
    }
    if(stringType != CSSPrimitiveValue::CSS_IDENT)
    {
	m_value.string = stringValue.implementation();
	m_value.string->ref();
	m_type = stringType;
    }
    // ### parse ident
}

unsigned short CSSPrimitiveValueImpl::cssValueType() const
{
    return CSSValue::CSS_PRIMITIVE_VALUE;
}

bool CSSPrimitiveValueImpl::parseString( const DOMString &/*string*/, bool )
{
    // ###
Laurent Montel's avatar
Laurent Montel committed
1176
    kDebug() << "WARNING: CSSPrimitiveValueImpl::parseString, unimplemented, was called";
George Staikos's avatar
George Staikos committed
1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195
    return false;
}

int CSSPrimitiveValueImpl::getIdent()
{
    if(m_type != CSSPrimitiveValue::CSS_IDENT) return 0;
    return m_value.ident;
}

DOM::DOMString CSSPrimitiveValueImpl::cssText() const
{
    // ### return the original value instead of a generated one (e.g. color
    // name if it was specified) - check what spec says about this
    DOMString text;
    switch ( m_type ) {
	case CSSPrimitiveValue::CSS_UNKNOWN:
	    // ###
	    break;
	case CSSPrimitiveValue::CSS_NUMBER:
1196 1197 1198 1199 1200
	    // We want to output integral values w/o a period, but others as-is
	    if ( m_value.num == (int)m_value.num )
		text = DOMString(QString::number( (int)m_value.num ));
	    else
		text = DOMString(QString::number( m_value.num ));
George Staikos's avatar
George Staikos committed
1201 1202
	    break;
	case CSSPrimitiveValue::CSS_PERCENTAGE:
1203
	    text = DOMString(QString::number( m_value.num ) + "%");
George Staikos's avatar
George Staikos committed
1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241
	    break;
	case CSSPrimitiveValue::CSS_EMS:
	    text = DOMString(QString::number( m_value.num ) + "em");
	    break;
	case CSSPrimitiveValue::CSS_EXS:
	    text = DOMString(QString::number( m_value.num ) + "ex");
	    break;
	case CSSPrimitiveValue::CSS_PX:
	    text = DOMString(QString::number( m_value.num ) + "px");
	    break;
	case CSSPrimitiveValue::CSS_CM:
	    text = DOMString(QString::number( m_value.num ) + "cm");
	    break;
	case CSSPrimitiveValue::CSS_MM:
	    text = DOMString(QString::number( m_value.num ) + "mm");
	    break;
	case CSSPrimitiveValue::CSS_IN:
	    text = DOMString(QString::number( m_value.num ) + "in");
	    break;
	case CSSPrimitiveValue::CSS_PT:
	    text = DOMString(QString::number( m_value.num ) + "pt");
	    break;
	case CSSPrimitiveValue::CSS_PC:
	    text = DOMString(QString::number( m_value.num ) + "pc");
	    break;
	case CSSPrimitiveValue::CSS_DEG:
	    text = DOMString(QString::number( m_value.num ) + "deg");
	    break;
	case CSSPrimitiveValue::CSS_RAD:
	    text = DOMString(QString::number( m_value.num ) + "rad");
	    break;
	case CSSPrimitiveValue::CSS_GRAD:
	    text = DOMString(QString::number( m_value.num ) + "grad");
	    break;
	case CSSPrimitiveValue::CSS_MS:
	    text = DOMString(QString::number( m_value.num ) + "ms");
	    break;
	case CSSPrimitiveValue::CSS_S:
1242
	    text = DOMString(QString::number( m_value.num ) + "s");
George Staikos's avatar
George Staikos committed
1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253
	    break;
	case CSSPrimitiveValue::CSS_HZ:
	    text = DOMString(QString::number( m_value.num ) + "hz");
	    break;
	case CSSPrimitiveValue::CSS_KHZ:
	    text = DOMString(QString::number( m_value.num ) + "khz");
	    break;
	case CSSPrimitiveValue::CSS_DIMENSION:
	    // ###
	    break;
	case CSSPrimitiveValue::CSS_STRING:
Maks Orlovich's avatar
Maks Orlovich committed
1254
	    text = quoteStringIfNeeded(m_value.string);
George Staikos's avatar
George Staikos committed
1255 1256 1257 1258
	    break;
	case CSSPrimitiveValue::CSS_URI:
            text  = "url(";
	    text += DOMString( m_value.string );
1259
            text += ")";
George Staikos's avatar
George Staikos committed
1260 1261 1262 1263 1264
	    break;
	case CSSPrimitiveValue::CSS_IDENT:
	    text = getValueName(m_value.ident);
	    break;
	case CSSPrimitiveValue::CSS_ATTR:
1265 1266 1267
            text = "attr(";
            text += DOMString( m_value.string );
            text += ")";
George Staikos's avatar
George Staikos committed
1268 1269 1270 1271
	    break;
	case CSSPrimitiveValue::CSS_COUNTER:
            text = "counter(";
            text += m_value.counter->m_identifier;
1272
            text += ")";
George Staikos's avatar
George Staikos committed
1273 1274 1275 1276 1277 1278
            // ### add list-style and separator
	    break;
	case CSSPrimitiveValue::CSS_RECT:
        {
            RectImpl* rectVal = getRectValue();
            text = "rect(";
1279 1280 1281 1282
            text += rectVal->top()->cssText() + DOMString(" ");
            text += rectVal->right()->cssText() + DOMString(" ");
            text += rectVal->bottom()->cssText() + DOMString(" ");
            text += rectVal->left()->cssText() + DOMString(")");
1283
            break;
George Staikos's avatar
George Staikos committed
1284 1285
        }
	case CSSPrimitiveValue::CSS_RGBCOLOR:
1286 1287 1288 1289
	    if (qAlpha(m_value.rgbcolor) != 0xFF) {
		if (m_value.rgbcolor == khtml::transparentColor)
		    text = "transparent";
		else
1290
                    text = QString("rgba(" + QString::number(qRed  (m_value.rgbcolor)) + ", "
1291 1292
				   + QString::number(qGreen(m_value.rgbcolor)) + ", "
				   + QString::number(qBlue (m_value.rgbcolor)) + ", "
1293
                                   + QString::number(qAlpha(m_value.rgbcolor)/255.0) + ")");
1294
	    } else {
1295
                text = QString("rgb(" + QString::number(qRed  (m_value.rgbcolor)) + ", "
1296
                              + QString::number(qGreen(m_value.rgbcolor)) + ", "
1297
                              + QString::number(qBlue (m_value.rgbcolor)) + ")");
1298
	    }
George Staikos's avatar
George Staikos committed
1299
	    break;
1300 1301
        case CSSPrimitiveValue::CSS_PAIR:
            text = m_value.pair->first()->cssText();
1302
            text += " ";
1303 1304
            text += m_value.pair->second()->cssText();
            break;
George Staikos's avatar
George Staikos committed
1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358
	default:
	    break;
    }
    return text;
}

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

RectImpl::RectImpl()
{
    m_top = 0;
    m_right = 0;
    m_bottom = 0;
    m_left = 0;
}

RectImpl::~RectImpl()
{
    if (m_top) m_top->deref();
    if (m_right) m_right->deref();
    if (m_bottom) m_bottom->deref();
    if (m_left) m_left->deref();
}

void RectImpl::setTop( CSSPrimitiveValueImpl *top )
{
    if( top ) top->ref();
    if ( m_top ) m_top->deref();
    m_top = top;
}

void RectImpl::setRight( CSSPrimitiveValueImpl *right )
{
    if( right ) right->ref();
    if ( m_right ) m_right->deref();
    m_right = right;
}

void RectImpl::setBottom( CSSPrimitiveValueImpl *bottom )
{
    if( bottom ) bottom->ref();
    if ( m_bottom ) m_bottom->deref();
    m_bottom = bottom;
}

void RectImpl::setLeft( CSSPrimitiveValueImpl *left )
{
    if( left ) left->ref();
    if ( m_left ) m_left->deref();
    m_left = left;
}

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

1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381
PairImpl::~PairImpl()
{
    if (m_first) m_first->deref(); if (m_second) m_second->deref();
}

void PairImpl::setFirst(CSSPrimitiveValueImpl* first)
{
    if (first == m_first) return;
    if (m_first) m_first->deref();
    m_first = first;
    if (m_first) m_first->ref();
}

void PairImpl::setSecond(CSSPrimitiveValueImpl* second)
{
    if (second == m_second) return;
    if (m_second) m_second->deref();
    m_second = second;
    if (m_second) m_second->ref();
}

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

1382
CSSImageValueImpl::CSSImageValueImpl(const DOMString &url, StyleBaseImpl* style)
George Staikos's avatar
George Staikos committed
1383 1384
    : CSSPrimitiveValueImpl(url, CSSPrimitiveValue::CSS_URI)
{
1385
    m_image = 0;
1386
    const DOMString imgUrl = url.trimSpaces();
1387
    if (!imgUrl.isEmpty()) {
1388
        m_fullImageUrl = KUrl(style->baseURL(), imgUrl.string()).url();
1389 1390
    } else {
        m_fullImageUrl.clear();
1391
    }
1392
}
George Staikos's avatar
George Staikos committed
1393 1394 1395 1396 1397

CSSImageValueImpl::CSSImageValueImpl()
    : CSSPrimitiveValueImpl(CSS_VAL_NONE)
{
    m_image = 0;
1398
    m_fullImageUrl.clear();
George Staikos's avatar
George Staikos committed
1399 1400 1401 1402 1403 1404 1405
}

CSSImageValueImpl::~CSSImageValueImpl()
{
    if(m_image) m_image->deref(this);
}

1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416
khtml::CachedImage *CSSImageValueImpl::requestCssImage(DocumentImpl* doc)
{
    if (!m_image && !m_fullImageUrl.isEmpty()) {
        m_image = doc->docLoader()->requestImage(m_fullImageUrl);
        if (m_image) {
            m_image->ref(this);
        }
    }
    return m_image;
}

George Staikos's avatar
George Staikos committed
1417 1418 1419 1420 1421 1422
// ------------------------------------------------------------------------

FontFamilyValueImpl::FontFamilyValueImpl( const QString &string)
: CSSPrimitiveValueImpl( DOMString(string), CSSPrimitiveValue::CSS_STRING)
{
    static const QRegExp parenReg(" \\(.*\\)$");
Germain Garand's avatar
Germain Garand committed
1423
//  static const QRegExp braceReg(" \\[.*\\]$");
George Staikos's avatar
George Staikos committed
1424 1425

    parsedFontName = string;
1426

George Staikos's avatar
George Staikos committed
1427 1428
    // a language tag is often added in braces at the end. Remove it.
    parsedFontName.replace(parenReg, QString());
1429

Germain Garand's avatar
Germain Garand committed
1430 1431 1432 1433 1434
#if 0
    // cannot use such early checks against the font database anymore,
    // as the font subsystem might not contain the requested font yet
    // (case of downloadable font faces)

George Staikos's avatar
George Staikos committed
1435 1436 1437 1438 1439 1440
    // remove [Xft] qualifiers
    parsedFontName.replace(braceReg, QString());

    const QString &available = KHTMLSettings::availableFamilies();

    parsedFontName = parsedFontName.toLower();
Laurent Montel's avatar
Laurent Montel committed
1441
    // kDebug(0) << "searching for face '" << parsedFontName << "'";
George Staikos's avatar
George Staikos committed
1442

1443
    int pos = available.indexOf( ',' + parsedFontName + ',', 0, Qt::CaseInsensitive );
George Staikos's avatar
George Staikos committed
1444 1445 1446 1447 1448 1449
    if ( pos == -1 ) {
        // many pages add extra MSs to make sure it's windows only ;(
        if ( parsedFontName.startsWith( "ms " ) )
            parsedFontName = parsedFontName.mid( 3 );
        if ( parsedFontName.endsWith( " ms" ) )
            parsedFontName.truncate( parsedFontName.length() - 3 );