Fix for bugzilla bug 4129, font size wrong for <tt> elements that
[WebKit-https.git] / WebCore / css / cssstyleselector.cpp
1 /**
2  * This file is part of the CSS implementation for KDE.
3  *
4  * Copyright (C) 1999 Lars Knoll (knoll@kde.org)
5  *           (C) 2004-2005 Allan Sandfeld Jensen (kde@carewolf.com)
6  *           (C) 2006 Nicholas Shanks (webkit@nickshanks.com)
7  * Copyright (C) 2005, 2006 Apple Computer, Inc.
8  *
9  * This library is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Library General Public
11  * License as published by the Free Software Foundation; either
12  * version 2 of the License, or (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  * Library General Public License for more details.
18  *
19  * You should have received a copy of the GNU Library General Public License
20  * along with this library; see the file COPYING.LIB.  If not, write to
21  * the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22  * Boston, MA 02111-1307, USA.
23  */
24
25 #include "config.h"
26 #include "cssstyleselector.h"
27
28 #include "CSSBorderImageValue.h"
29 #include "CSSImageValue.h"
30 #include "CSSImportRule.h"
31 #include "CSSMediaRule.h"
32 #include "CSSProperty.h"
33 #include "CSSPropertyNames.h"
34 #include "CSSRuleList.h"
35 #include "CSSSelector.h"
36 #include "CSSStyleRule.h"
37 #include "CSSStyleSheet.h"
38 #include "CSSValueKeywords.h"
39 #include "CSSValueList.h"
40 #include "CachedImage.h"
41 #include "DashboardRegion.h"
42 #include "FontFamilyValue.h"
43 #include "FontValue.h"
44 #include "Frame.h"
45 #include "HTMLDocument.h"
46 #include "HTMLElement.h"
47 #include "HTMLInputElement.h"
48 #include "HTMLNames.h"
49 #include "History.h"
50 #include "Settings.h"
51 #include "MediaList.h"
52 #include "MediaQueryEvaluator.h"
53 #include "Pair.h"
54 #include "RectImpl.h"
55 #include "RenderTheme.h"
56 #include "StyleSheetList.h"
57 #include "UserAgentStyleSheets.h"
58 #include "loader.h"
59 #include "ShadowValue.h"
60
61 using namespace std;
62
63 namespace WebCore {
64
65 using namespace HTMLNames;
66
67 // #define STYLE_SHARING_STATS 1
68
69 #define HANDLE_INHERIT(prop, Prop) \
70 if (isInherit) { \
71     style->set##Prop(parentStyle->prop()); \
72     return; \
73 }
74
75 #define HANDLE_INHERIT_AND_INITIAL(prop, Prop) \
76 HANDLE_INHERIT(prop, Prop) \
77 if (isInitial) { \
78     style->set##Prop(RenderStyle::initial##Prop()); \
79     return; \
80 }
81
82 #define HANDLE_INHERIT_AND_INITIAL_WITH_VALUE(prop, Prop, Value) \
83 HANDLE_INHERIT(prop, Prop) \
84 if (isInitial) { \
85     style->set##Prop(RenderStyle::initial##Value());\
86     return;\
87 }
88
89 #define HANDLE_BACKGROUND_INHERIT_AND_INITIAL(prop, Prop) \
90 if (isInherit) { \
91     BackgroundLayer* currChild = style->accessBackgroundLayers(); \
92     BackgroundLayer* prevChild = 0; \
93     const BackgroundLayer* currParent = parentStyle->backgroundLayers(); \
94     while (currParent && currParent->is##Prop##Set()) { \
95         if (!currChild) { \
96             /* Need to make a new layer.*/ \
97             currChild = new BackgroundLayer(); \
98             prevChild->setNext(currChild); \
99         } \
100         currChild->set##Prop(currParent->prop()); \
101         prevChild = currChild; \
102         currChild = prevChild->next(); \
103         currParent = currParent->next(); \
104     } \
105     \
106     while (currChild) { \
107         /* Reset any remaining layers to not have the property set. */ \
108         currChild->clear##Prop(); \
109         currChild = currChild->next(); \
110     } \
111     return; \
112 } \
113 if (isInitial) { \
114     BackgroundLayer* currChild = style->accessBackgroundLayers(); \
115     currChild->set##Prop(RenderStyle::initial##Prop()); \
116     for (currChild = currChild->next(); currChild; currChild = currChild->next()) \
117         currChild->clear##Prop(); \
118     return; \
119 }
120
121 #define HANDLE_BACKGROUND_VALUE(prop, Prop, value) { \
122 HANDLE_BACKGROUND_INHERIT_AND_INITIAL(prop, Prop) \
123 if (!value->isPrimitiveValue() && !value->isValueList()) \
124     return; \
125 BackgroundLayer* currChild = style->accessBackgroundLayers(); \
126 BackgroundLayer* prevChild = 0; \
127 if (value->isPrimitiveValue()) { \
128     map##Prop(currChild, value); \
129     currChild = currChild->next(); \
130 } \
131 else { \
132     /* Walk each value and put it into a layer, creating new layers as needed. */ \
133     CSSValueList* valueList = static_cast<CSSValueList*>(value); \
134     for (unsigned int i = 0; i < valueList->length(); i++) { \
135         if (!currChild) { \
136             /* Need to make a new layer to hold this value */ \
137             currChild = new BackgroundLayer(); \
138             prevChild->setNext(currChild); \
139         } \
140         map##Prop(currChild, valueList->item(i)); \
141         prevChild = currChild; \
142         currChild = currChild->next(); \
143     } \
144 } \
145 while (currChild) { \
146     /* Reset all remaining layers to not have the property set. */ \
147     currChild->clear##Prop(); \
148     currChild = currChild->next(); \
149 } }
150
151 #define HANDLE_INHERIT_COND(propID, prop, Prop) \
152 if (id == propID) { \
153     style->set##Prop(parentStyle->prop()); \
154     return; \
155 }
156
157 #define HANDLE_INITIAL_COND(propID, Prop) \
158 if (id == propID) { \
159     style->set##Prop(RenderStyle::initial##Prop()); \
160     return; \
161 }
162
163 #define HANDLE_INITIAL_COND_WITH_VALUE(propID, Prop, Value) \
164 if (id == propID) { \
165     style->set##Prop(RenderStyle::initial##Value()); \
166     return; \
167 }
168
169 class CSSRuleSet
170 {
171 public:
172     CSSRuleSet();
173     ~CSSRuleSet();
174     
175     typedef HashMap<AtomicStringImpl*, CSSRuleDataList*> AtomRuleMap;
176     
177     void addRulesFromSheet(CSSStyleSheet* sheet, MediaQueryEvaluator* medium);
178     
179     void addRule(CSSStyleRule* rule, CSSSelector* sel);
180     void addToRuleSet(AtomicStringImpl* key, AtomRuleMap& map,
181                       CSSStyleRule* rule, CSSSelector* sel);
182     
183     CSSRuleDataList* getIDRules(AtomicStringImpl* key) { return m_idRules.get(key); }
184     CSSRuleDataList* getClassRules(AtomicStringImpl* key) { return m_classRules.get(key); }
185     CSSRuleDataList* getTagRules(AtomicStringImpl* key) { return m_tagRules.get(key); }
186     CSSRuleDataList* getUniversalRules() { return m_universalRules; }
187     
188 public:
189     AtomRuleMap m_idRules;
190     AtomRuleMap m_classRules;
191     AtomRuleMap m_tagRules;
192     CSSRuleDataList* m_universalRules;
193     
194     unsigned m_ruleCount;
195 };
196
197 CSSRuleSet* CSSStyleSelector::defaultStyle = 0;
198 CSSRuleSet* CSSStyleSelector::defaultQuirksStyle = 0;
199 CSSRuleSet* CSSStyleSelector::defaultPrintStyle = 0;
200 CSSRuleSet* CSSStyleSelector::defaultViewSourceStyle = 0;
201
202 CSSStyleSheet* CSSStyleSelector::defaultSheet = 0;
203 RenderStyle* CSSStyleSelector::styleNotYetAvailable = 0;
204 CSSStyleSheet* CSSStyleSelector::quirksSheet = 0;
205 CSSStyleSheet* CSSStyleSelector::viewSourceSheet = 0;
206
207 #ifdef SVG_SUPPORT
208 CSSStyleSheet *CSSStyleSelector::svgSheet = 0;
209 #endif
210
211 static CSSStyleSelector::Encodedurl *currentEncodedURL = 0;
212 static PseudoState pseudoState;
213
214 CSSStyleSelector::CSSStyleSelector(Document* doc, const String& userStyleSheet, StyleSheetList *styleSheets, bool _strictParsing)
215 {
216     init();
217
218     view = doc->view();
219     strictParsing = _strictParsing;
220     settings = view ? view->frame()->settings() : 0;
221     if (!defaultStyle)
222         loadDefaultStyle();
223
224     m_userStyle = 0;
225     m_userSheet = 0;
226
227     // construct document root element default style. this is needed
228     // to evaluate media queries that contain relative constraints, like "screen and (max-width: 10em)"
229     // This is here instead of constructor, because when constructor is run,
230     // document doesn't have documentElement
231     // NOTE: this assumes that element that gets passed to styleForElement -call
232     // is always from the document that owns the style selector
233     if (view)
234         m_medium = new MediaQueryEvaluator(view->mediaType());
235     else
236         m_medium = new MediaQueryEvaluator("all");
237
238     Element* root = doc->documentElement();
239
240     if (root)
241         m_rootDefaultStyle = styleForElement(root, 0, false, true); // dont ref, because the RenderStyle is allocated from global heap
242
243     if (m_rootDefaultStyle && view) {
244         delete m_medium;
245         m_medium = new MediaQueryEvaluator(view->mediaType(), view->frame()->page(), m_rootDefaultStyle);
246     }
247
248     // FIXME: This sucks! The user sheet is reparsed every time!
249     if (!userStyleSheet.isEmpty()) {
250         m_userSheet = new CSSStyleSheet(doc);
251         m_userSheet->parseString(userStyleSheet, strictParsing);
252
253         m_userStyle = new CSSRuleSet();
254         m_userStyle->addRulesFromSheet(m_userSheet, m_medium);
255     }
256
257     // add stylesheets from document
258     m_authorStyle = new CSSRuleSet();
259
260     DeprecatedPtrListIterator<StyleSheet> it(styleSheets->styleSheets);
261     for (; it.current(); ++it)
262         if (it.current()->isCSSStyleSheet() && !it.current()->disabled())
263             m_authorStyle->addRulesFromSheet(static_cast<CSSStyleSheet*>(it.current()), m_medium);
264
265 }
266
267 CSSStyleSelector::CSSStyleSelector(CSSStyleSheet *sheet)
268 {
269     init();
270
271     if (!defaultStyle)
272         loadDefaultStyle();
273     FrameView *view = sheet->doc()->view();
274
275     if (view)
276         m_medium = new MediaQueryEvaluator(view->mediaType());
277     else
278         m_medium = new MediaQueryEvaluator("all");
279
280     Element* root = sheet->doc()->documentElement();
281     if (root)
282         m_rootDefaultStyle = styleForElement(root, 0, false, true);
283
284     if (m_rootDefaultStyle && view) {
285         delete m_medium;
286         m_medium = new MediaQueryEvaluator(view->mediaType(), view->frame()->page(), m_rootDefaultStyle);
287     }
288
289     m_authorStyle = new CSSRuleSet();
290     m_authorStyle->addRulesFromSheet(sheet, m_medium);
291 }
292
293 void CSSStyleSelector::init()
294 {
295     element = 0;
296     settings = 0;
297     m_matchedRules.clear();
298     m_matchedDecls.clear();
299     m_ruleList = 0;
300     m_collectRulesOnly = false;
301     m_rootDefaultStyle = 0;
302     m_medium = 0;
303 }
304
305 void CSSStyleSelector::setEncodedURL(const KURL& url)
306 {
307     KURL u = url;
308
309     u.setQuery(DeprecatedString::null);
310     u.setRef(DeprecatedString::null);
311     encodedurl.file = u.url();
312     int pos = encodedurl.file.findRev('/');
313     encodedurl.path = encodedurl.file;
314     if (pos > 0) {
315         encodedurl.path.truncate(pos);
316         encodedurl.path += '/';
317     }
318     u.setPath(DeprecatedString::null);
319     encodedurl.host = u.url();
320 }
321
322 CSSStyleSelector::~CSSStyleSelector()
323 {
324     delete m_medium;
325     ::delete m_rootDefaultStyle;
326
327     delete m_authorStyle;
328     delete m_userStyle;
329     delete m_userSheet;
330 }
331
332 static CSSStyleSheet* parseUASheet(const char* characters, unsigned size)
333 {
334     CSSStyleSheet* const parent = 0;
335     CSSStyleSheet* sheet = new CSSStyleSheet(parent);
336     sheet->ref(); // leak the sheet on purpose since it will be stored in a global variable
337     sheet->parseString(String(characters, size));
338     return sheet;
339 }
340
341 template<typename T> CSSStyleSheet* parseUASheet(const T& array)
342 {
343     return parseUASheet(array, sizeof(array));
344 }
345
346 void CSSStyleSelector::loadDefaultStyle()
347 {
348     if (defaultStyle)
349         return;
350
351     defaultStyle = new CSSRuleSet;
352     defaultPrintStyle = new CSSRuleSet;
353     defaultQuirksStyle = new CSSRuleSet;
354     defaultViewSourceStyle = new CSSRuleSet;
355
356     MediaQueryEvaluator screenEval("screen");
357     MediaQueryEvaluator printEval("print");
358
359     // Strict-mode rules.
360     defaultSheet = parseUASheet(html4UserAgentStyleSheet);
361     defaultStyle->addRulesFromSheet(defaultSheet, &screenEval);
362     defaultPrintStyle->addRulesFromSheet(defaultSheet, &printEval);
363
364 #ifdef SVG_SUPPORT
365     // SVG rules.
366     svgSheet = parseUASheet(svgUserAgentStyleSheet);
367     defaultStyle->addRulesFromSheet(svgSheet, &screenEval);
368     defaultPrintStyle->addRulesFromSheet(svgSheet, &printEval);
369 #endif
370
371     // Quirks-mode rules.
372     quirksSheet = parseUASheet(quirksUserAgentStyleSheet);
373     defaultQuirksStyle->addRulesFromSheet(quirksSheet, &screenEval);
374     
375     // View source rules.
376     viewSourceSheet = parseUASheet(sourceUserAgentStyleSheet);
377     defaultViewSourceStyle->addRulesFromSheet(viewSourceSheet, &screenEval);
378 }
379
380 void CSSStyleSelector::matchRules(CSSRuleSet* rules, int& firstRuleIndex, int& lastRuleIndex)
381 {
382     m_matchedRules.clear();
383
384     if (!rules || !element)
385         return;
386     
387     // We need to collect the rules for id, class, tag, and everything else into a buffer and
388     // then sort the buffer.
389     if (element->hasID())
390         matchRulesForList(rules->getIDRules(element->getIDAttribute().impl()), firstRuleIndex, lastRuleIndex);
391     if (element->hasClass()) {
392         for (const AtomicStringList* singleClass = element->getClassList(); singleClass; singleClass = singleClass->next())
393             matchRulesForList(rules->getClassRules(singleClass->string().impl()), firstRuleIndex, lastRuleIndex);
394     }
395     matchRulesForList(rules->getTagRules(element->localName().impl()), firstRuleIndex, lastRuleIndex);
396     matchRulesForList(rules->getUniversalRules(), firstRuleIndex, lastRuleIndex);
397     
398     // If we didn't match any rules, we're done.
399     if (m_matchedRules.isEmpty())
400         return;
401     
402     // Sort the set of matched rules.
403     sortMatchedRules(0, m_matchedRules.size());
404     
405     // Now transfer the set of matched rules over to our list of decls.
406     if (!m_collectRulesOnly) {
407         for (unsigned i = 0; i < m_matchedRules.size(); i++)
408             addMatchedDeclaration(m_matchedRules[i]->rule()->declaration());
409     } else {
410         for (unsigned i = 0; i < m_matchedRules.size(); i++) {
411             if (!m_ruleList)
412                 m_ruleList = new CSSRuleList();
413             m_ruleList->append(m_matchedRules[i]->rule());
414         }
415     }
416 }
417
418 void CSSStyleSelector::matchRulesForList(CSSRuleDataList* rules, int& firstRuleIndex, int& lastRuleIndex)
419 {
420     if (!rules)
421         return;
422
423     for (CSSRuleData* d = rules->first(); d; d = d->next()) {
424         CSSStyleRule* rule = d->rule();
425         const AtomicString& localName = element->localName();
426         const AtomicString& selectorLocalName = d->selector()->tag.localName();
427         if ((localName == selectorLocalName || selectorLocalName == starAtom) && checkSelector(d->selector(), element)) {
428             // If the rule has no properties to apply, then ignore it.
429             CSSMutableStyleDeclaration* decl = rule->declaration();
430             if (!decl || !decl->length())
431                 continue;
432             
433             // If we're matching normal rules, set a pseudo bit if 
434             // we really just matched a pseudo-element.
435             if (dynamicPseudo != RenderStyle::NOPSEUDO && pseudoStyle == RenderStyle::NOPSEUDO) {
436                 if (m_collectRulesOnly)
437                     return;
438                 style->setHasPseudoStyle(dynamicPseudo);
439             } else {
440                 // Update our first/last rule indices in the matched rules array.
441                 lastRuleIndex = m_matchedDecls.size() + m_matchedRules.size();
442                 if (firstRuleIndex == -1)
443                     firstRuleIndex = lastRuleIndex;
444
445                 // Add this rule to our list of matched rules.
446                 addMatchedRule(d);
447             }
448         }
449     }
450 }
451
452 bool operator >(CSSRuleData& r1, CSSRuleData& r2)
453 {
454     int spec1 = r1.selector()->specificity();
455     int spec2 = r2.selector()->specificity();
456     return (spec1 == spec2) ? r1.position() > r2.position() : spec1 > spec2; 
457 }
458 bool operator <=(CSSRuleData& r1, CSSRuleData& r2)
459 {
460     return !(r1 > r2);
461 }
462
463 void CSSStyleSelector::sortMatchedRules(unsigned start, unsigned end)
464 {
465     if (start >= end || (end - start == 1))
466         return; // Sanity check.
467
468     if (end - start <= 6) {
469         // Apply a bubble sort for smaller lists.
470         for (unsigned i = end - 1; i > start; i--) {
471             bool sorted = true;
472             for (unsigned j = start; j < i; j++) {
473                 CSSRuleData* elt = m_matchedRules[j];
474                 CSSRuleData* elt2 = m_matchedRules[j + 1];
475                 if (*elt > *elt2) {
476                     sorted = false;
477                     m_matchedRules[j] = elt2;
478                     m_matchedRules[j + 1] = elt;
479                 }
480             }
481             if (sorted)
482                 return;
483         }
484         return;
485     }
486
487     // Peform a merge sort for larger lists.
488     unsigned mid = (start + end) / 2;
489     sortMatchedRules(start, mid);
490     sortMatchedRules(mid, end);
491     
492     CSSRuleData* elt = m_matchedRules[mid - 1];
493     CSSRuleData* elt2 = m_matchedRules[mid];
494     
495     // Handle the fast common case (of equal specificity).  The list may already
496     // be completely sorted.
497     if (*elt <= *elt2)
498         return;
499     
500     // We have to merge sort.  Ensure our merge buffer is big enough to hold
501     // all the items.
502     Vector<CSSRuleData*> rulesMergeBuffer;
503     rulesMergeBuffer.reserveCapacity(end - start); 
504
505     unsigned i1 = start;
506     unsigned i2 = mid;
507     
508     elt = m_matchedRules[i1];
509     elt2 = m_matchedRules[i2];
510     
511     while (i1 < mid || i2 < end) {
512         if (i1 < mid && (i2 == end || *elt <= *elt2)) {
513             rulesMergeBuffer.append(elt);
514             if (++i1 < mid)
515                 elt = m_matchedRules[i1];
516         } else {
517             rulesMergeBuffer.append(elt2);
518             if (++i2 < end)
519                 elt2 = m_matchedRules[i2];
520         }
521     }
522     
523     for (unsigned i = start; i < end; i++)
524         m_matchedRules[i] = rulesMergeBuffer[i - start];
525 }
526
527 void CSSStyleSelector::initElementAndPseudoState(Element* e)
528 {
529     element = e;
530     if (element && element->isStyledElement())
531         styledElement = static_cast<StyledElement*>(element);
532     else
533         styledElement = 0;
534     currentEncodedURL = &encodedurl;
535     pseudoState = PseudoUnknown;
536 }
537
538 void CSSStyleSelector::initForStyleResolve(Element* e, RenderStyle* defaultParent)
539 {
540     // set some variables we will need
541     pseudoStyle = RenderStyle::NOPSEUDO;
542
543     parentNode = e->parentNode();
544     if (defaultParent)
545         parentStyle = defaultParent;
546     else
547         parentStyle = parentNode ? parentNode->renderStyle() : 0;
548     view = element->document()->view();
549     isXMLDoc = !element->document()->isHTMLDocument();
550     frame = element->document()->frame();
551     settings = frame ? frame->settings() : 0;
552
553     style = 0;
554     
555     m_matchedRules.clear();
556     m_matchedDecls.clear();
557
558     m_ruleList = 0;
559
560     fontDirty = false;
561 }
562
563 // modified version of the one in kurl.cpp
564 static void cleanpath(DeprecatedString &path)
565 {
566     int pos;
567     while ((pos = path.find("/../")) != -1) {
568         int prev = 0;
569         if (pos > 0)
570             prev = path.findRev("/", pos -1);
571         // don't remove the host, i.e. http://foo.org/../foo.html
572         if (prev < 0 || (prev > 3 && path.findRev("://", prev-1) == prev-2))
573             path.remove(pos, 3);
574         else
575             // matching directory found ?
576             path.remove(prev, pos- prev + 3);
577     }
578     pos = 0;
579     
580     // Don't remove "//" from an anchor identifier. -rjw
581     // Set refPos to -2 to mean "I haven't looked for the anchor yet".
582     // We don't want to waste a function call on the search for the the anchor
583     // in the vast majority of cases where there is no "//" in the path.
584     int refPos = -2;
585     while ((pos = path.find("//", pos)) != -1) {
586         if (refPos == -2)
587             refPos = path.find("#");
588         if (refPos > 0 && pos >= refPos)
589             break;
590         
591         if (pos == 0 || path[pos-1] != ':')
592             path.remove(pos, 1);
593         else
594             pos += 2;
595     }
596     while ((pos = path.find("/./")) != -1)
597         path.remove(pos, 2);
598 }
599
600 static void checkPseudoState(Element *e, bool checkVisited = true)
601 {
602     if (!e->isLink()) {
603         pseudoState = PseudoNone;
604         return;
605     }
606     
607     const AtomicString& attr = e->getAttribute(hrefAttr);
608     if (attr.isNull()) {
609         pseudoState = PseudoNone;
610         return;
611     }
612     
613     if (!checkVisited) {
614         pseudoState = PseudoAnyLink;
615         return;
616     }
617     
618     DeprecatedConstString cu(reinterpret_cast<const DeprecatedChar*>(attr.characters()), attr.length());
619     DeprecatedString u = cu.string();
620     if (!u.contains("://")) {
621         if (u[0] == '/')
622             u.prepend(currentEncodedURL->host);
623         else if (u[0] == '#')
624             u.prepend(currentEncodedURL->file);
625         else
626             u.prepend(currentEncodedURL->path);
627         cleanpath(u);
628     }
629     pseudoState = historyContains(u) ? PseudoVisited : PseudoLink;
630 }
631
632 #ifdef STYLE_SHARING_STATS
633 static int fraction = 0;
634 static int total = 0;
635 #endif
636
637 const int siblingThreshold = 10;
638
639 Node* CSSStyleSelector::locateCousinList(Element* parent)
640 {
641     if (parent && parent->isStyledElement()) {
642         StyledElement* p = static_cast<StyledElement*>(parent);
643         if (!p->inlineStyleDecl() && !p->hasID()) {
644             Node* r = p->previousSibling();
645             int subcount = 0;
646             RenderStyle* st = p->renderStyle();
647             while (r) {
648                 if (r->renderStyle() == st)
649                     return r->lastChild();
650                 if (subcount++ == siblingThreshold)
651                     return 0;
652                 r = r->previousSibling();
653             }
654             if (!r)
655                 r = locateCousinList(static_cast<Element*>(parent->parentNode()));
656             while (r) {
657                 if (r->renderStyle() == st)
658                     return r->lastChild();
659                 if (subcount++ == siblingThreshold)
660                     return 0;
661                 r = r->previousSibling();
662             }
663         }
664     }
665     return 0;
666 }
667
668 bool CSSStyleSelector::canShareStyleWithElement(Node* n)
669 {
670     if (n->isStyledElement()) {
671         StyledElement* s = static_cast<StyledElement*>(n);
672         RenderStyle* style = s->renderStyle();
673         if (style && !style->unique() &&
674             (s->tagQName() == element->tagQName()) && !s->hasID() &&
675             (s->hasClass() == element->hasClass()) && !s->inlineStyleDecl() &&
676             (s->hasMappedAttributes() == styledElement->hasMappedAttributes()) &&
677             (s->isLink() == element->isLink()) && 
678             !style->affectedByAttributeSelectors() &&
679             (s->hovered() == element->hovered()) &&
680             (s->active() == element->active()) &&
681             (s->focused() == element->focused()) &&
682             (s->isEnabled() == element->isEnabled()) &&
683             (s->isIndeterminate() == element->isIndeterminate()) &&
684             (s->isChecked() == element->isChecked()) &&
685             (s != s->document()->getCSSTarget() && element != element->document()->getCSSTarget()) &&
686             (s->getAttribute(typeAttr) == element->getAttribute(typeAttr))) {
687             bool classesMatch = true;
688             if (s->hasClass()) {
689                 const AtomicString& class1 = element->getAttribute(classAttr);
690                 const AtomicString& class2 = s->getAttribute(classAttr);
691                 classesMatch = (class1 == class2);
692             }
693             
694             if (classesMatch) {
695                 bool mappedAttrsMatch = true;
696                 if (s->hasMappedAttributes())
697                     mappedAttrsMatch = s->mappedAttributes()->mapsEquivalent(styledElement->mappedAttributes());
698                 if (mappedAttrsMatch) {
699                     bool linksMatch = true;
700                     if (s->isLink()) {
701                         // We need to check to see if the visited state matches.
702                         Color linkColor = element->document()->linkColor();
703                         Color visitedColor = element->document()->visitedLinkColor();
704                         if (pseudoState == PseudoUnknown)
705                             checkPseudoState(element, style->pseudoState() != PseudoAnyLink ||
706                                              linkColor != visitedColor);
707                         linksMatch = (pseudoState == style->pseudoState());
708                     }
709                     
710                     if (linksMatch)
711                         return true;
712                 }
713             }
714         }
715     }
716     return false;
717 }
718
719 RenderStyle* CSSStyleSelector::locateSharedStyle()
720 {
721     if (styledElement && !styledElement->inlineStyleDecl() && !styledElement->hasID() &&
722         !styledElement->document()->usesSiblingRules()) {
723         // Check previous siblings.
724         int count = 0;
725         Node* n;
726         for (n = element->previousSibling(); n && !n->isElementNode(); n = n->previousSibling());
727         while (n) {
728             if (canShareStyleWithElement(n))
729                 return n->renderStyle();
730             if (count++ == siblingThreshold)
731                 return 0;
732             for (n = n->previousSibling(); n && !n->isElementNode(); n = n->previousSibling());
733         }
734         if (!n) 
735             n = locateCousinList(static_cast<Element*>(element->parentNode()));
736         while (n) {
737             if (canShareStyleWithElement(n))
738                 return n->renderStyle();
739             if (count++ == siblingThreshold)
740                 return 0;
741             for (n = n->previousSibling(); n && !n->isElementNode(); n = n->previousSibling());
742         }        
743     }
744     return 0;
745 }
746
747 void CSSStyleSelector::matchUARules(int& firstUARule, int& lastUARule)
748 {
749     // 1. First we match rules from the user agent sheet.
750     matchRules(defaultStyle, firstUARule, lastUARule);
751
752     // 2. In quirks mode, we match rules from the quirks user agent sheet.
753     if (!strictParsing)
754         matchRules(defaultQuirksStyle, firstUARule, lastUARule);
755
756     // 3. If our medium is print, then we match rules from the print sheet.
757     if (m_medium->mediaTypeMatch("print"))
758         matchRules(defaultPrintStyle, firstUARule, lastUARule);
759         
760     // 4. If we're in view source mode, then we match rules from the view source style sheet.
761     if (view && view->frame() && view->frame()->inViewSourceMode())
762         matchRules(defaultViewSourceStyle, firstUARule, lastUARule);
763 }
764
765 // If resolveForRootDefault is true, style based on user agent style sheet only. This is used in media queries, where
766 // relative units are interpreted according to document root element style, styled only with UA stylesheet
767
768 RenderStyle* CSSStyleSelector::styleForElement(Element* e, RenderStyle* defaultParent, bool allowSharing, bool resolveForRootDefault)
769 {
770     // Once an element has a renderer, we don't try to destroy it, since otherwise the renderer
771     // will vanish if a style recalc happens during loading.
772     if (allowSharing && !e->document()->haveStylesheetsLoaded() && !e->renderer()) {
773         if (!styleNotYetAvailable) {
774             styleNotYetAvailable = ::new RenderStyle();
775             styleNotYetAvailable->setDisplay(NONE);
776             styleNotYetAvailable->ref();
777         }
778         styleNotYetAvailable->ref();
779         return styleNotYetAvailable;
780     }
781     
782     initElementAndPseudoState(e);
783     if (allowSharing) {
784         style = locateSharedStyle();
785 #ifdef STYLE_SHARING_STATS
786         fraction += style != 0;
787         total++;
788         printf("Sharing %d out of %d\n", fraction, total);
789 #endif
790         if (style) {
791             style->ref();
792             return style;
793         }
794     }
795     initForStyleResolve(e, defaultParent);
796
797     if (resolveForRootDefault) {
798         style = ::new RenderStyle();
799         // don't ref, because we want to delete this, but we cannot unref it
800     } else {
801         style = new (e->document()->renderArena()) RenderStyle();
802         style->ref();
803     }
804     if (parentStyle)
805         style->inheritFrom(parentStyle);
806     else
807         parentStyle = style;
808
809     int firstUARule = -1, lastUARule = -1;
810     int firstUserRule = -1, lastUserRule = -1;
811     int firstAuthorRule = -1, lastAuthorRule = -1;
812     matchUARules(firstUARule, lastUARule);
813
814     if (!resolveForRootDefault) {
815         // 4. Now we check user sheet rules.
816         matchRules(m_userStyle, firstUserRule, lastUserRule);
817
818         // 5. Now check author rules, beginning first with presentational attributes
819         // mapped from HTML.
820         if (styledElement) {
821             // Ask if the HTML element has mapped attributes.
822             if (styledElement->hasMappedAttributes()) {
823                 // Walk our attribute list and add in each decl.
824                 const NamedMappedAttrMap* map = styledElement->mappedAttributes();
825                 for (unsigned i = 0; i < map->length(); i++) {
826                     MappedAttribute* attr = map->attributeItem(i);
827                     if (attr->decl()) {
828                         lastAuthorRule = m_matchedDecls.size();
829                         if (firstAuthorRule == -1)
830                             firstAuthorRule = lastAuthorRule;
831                         addMatchedDeclaration(attr->decl());
832                     }
833                 }
834             }
835
836             // Now we check additional mapped declarations.
837             // Tables and table cells share an additional mapped rule that must be applied
838             // after all attributes, since their mapped style depends on the values of multiple attributes.
839             CSSMutableStyleDeclaration* attributeDecl = styledElement->additionalAttributeStyleDecl();
840             if (attributeDecl) {
841                 lastAuthorRule = m_matchedDecls.size();
842                 if (firstAuthorRule == -1)
843                     firstAuthorRule = lastAuthorRule;
844                 addMatchedDeclaration(attributeDecl);
845             }
846         }
847     
848         // 6. Check the rules in author sheets next.
849         matchRules(m_authorStyle, firstAuthorRule, lastAuthorRule);
850     
851         // 7. Now check our inline style attribute.
852         if (styledElement) {
853             CSSMutableStyleDeclaration* inlineDecl = styledElement->inlineStyleDecl();
854             if (inlineDecl) {
855                 lastAuthorRule = m_matchedDecls.size();
856                 if (firstAuthorRule == -1)
857                     firstAuthorRule = lastAuthorRule;
858                 addMatchedDeclaration(inlineDecl);
859             }
860         }
861     }
862
863     // Now we have all of the matched rules in the appropriate order.  Walk the rules and apply
864     // high-priority properties first, i.e., those properties that other properties depend on.
865     // The order is (1) high-priority not important, (2) high-priority important, (3) normal not important
866     // and (4) normal important.
867     applyDeclarations(true, false, 0, m_matchedDecls.size() - 1);
868     if (!resolveForRootDefault) {
869         applyDeclarations(true, true, firstAuthorRule, lastAuthorRule);
870         applyDeclarations(true, true, firstUserRule, lastUserRule);
871     }
872     applyDeclarations(true, true, firstUARule, lastUARule);
873     
874     // If our font got dirtied, go ahead and update it now.
875     if (fontDirty)
876         updateFont();
877     
878     // Now do the normal priority UA properties.
879     applyDeclarations(false, false, firstUARule, lastUARule);
880     
881     // Cache our border and background so that we can examine them later.
882     cacheBorderAndBackground();
883     
884     // Now do the author and user normal priority properties and all the !important properties.
885     if (!resolveForRootDefault) {
886         applyDeclarations(false, false, lastUARule + 1, m_matchedDecls.size() - 1);
887         applyDeclarations(false, true, firstAuthorRule, lastAuthorRule);
888         applyDeclarations(false, true, firstUserRule, lastUserRule);
889     }
890     applyDeclarations(false, true, firstUARule, lastUARule);
891     
892     // If our font got dirtied by one of the non-essential font props, 
893     // go ahead and update it a second time.
894     if (fontDirty)
895         updateFont();
896     
897     // Clean up our style object's display and text decorations (among other fixups).
898     adjustRenderStyle(style, e);
899
900     // If we are a link, cache the determined pseudo-state.
901     if (e->isLink())
902         style->setPseudoState(pseudoState);
903
904     // Now return the style.
905     return style;
906 }
907
908 RenderStyle* CSSStyleSelector::pseudoStyleForElement(RenderStyle::PseudoId pseudo, Element* e, RenderStyle* parentStyle)
909 {
910     if (!e)
911         return 0;
912
913     initElementAndPseudoState(e);
914     initForStyleResolve(e, parentStyle);
915     pseudoStyle = pseudo;
916     
917     // Since we don't use pseudo-elements in any of our quirk/print user agent rules, don't waste time walking
918     // those rules.
919     
920     // Check UA, user and author rules.
921     int firstUARule = -1, lastUARule = -1, firstUserRule = -1, lastUserRule = -1, firstAuthorRule = -1, lastAuthorRule = -1;
922     matchRules(defaultStyle, firstUARule, lastUARule);
923     matchRules(m_userStyle, firstUserRule, lastUserRule);
924     matchRules(m_authorStyle, firstAuthorRule, lastAuthorRule);
925     
926     if (m_matchedDecls.isEmpty())
927         return 0;
928     
929     style = new (e->document()->renderArena()) RenderStyle();
930     style->ref();
931     if (parentStyle)
932         style->inheritFrom(parentStyle);
933     else
934         parentStyle = style;
935     style->noninherited_flags._styleType = pseudoStyle;
936     
937     // High-priority properties.
938     applyDeclarations(true, false, 0, m_matchedDecls.size() - 1);
939     applyDeclarations(true, true, firstAuthorRule, lastAuthorRule);
940     applyDeclarations(true, true, firstUserRule, lastUserRule);
941     applyDeclarations(true, true, firstUARule, lastUARule);
942     
943     // If our font got dirtied, go ahead and update it now.
944     if (fontDirty)
945         updateFont();
946     
947     // Now do the normal priority properties.
948     applyDeclarations(false, false, firstUARule, lastUARule);
949     
950     // Cache our border and background so that we can examine them later.
951     cacheBorderAndBackground();
952     
953     applyDeclarations(false, false, lastUARule + 1, m_matchedDecls.size() - 1);
954     applyDeclarations(false, true, firstAuthorRule, lastAuthorRule);
955     applyDeclarations(false, true, firstUserRule, lastUserRule);
956     applyDeclarations(false, true, firstUARule, lastUARule);
957     
958     // If our font got dirtied by one of the non-essential font props, 
959     // go ahead and update it a second time.
960     if (fontDirty)
961         updateFont();
962     // Clean up our style object's display and text decorations (among other fixups).
963     adjustRenderStyle(style, 0);
964
965     // Now return the style.
966     return style;
967 }
968
969 static void addIntrinsicMargins(RenderStyle* style)
970 {
971     // Intrinsic margin value.
972     const int intrinsicMargin = 2;
973     
974     // FIXME: Using width/height alone and not also dealing with min-width/max-width is flawed.
975     // FIXME: Using "quirk" to decide the margin wasn't set is kind of lame.
976     if (style->width().isIntrinsicOrAuto()) {
977         if (style->marginLeft().quirk())
978             style->setMarginLeft(Length(intrinsicMargin, Fixed));
979         if (style->marginRight().quirk())
980             style->setMarginRight(Length(intrinsicMargin, Fixed));
981     }
982
983     if (style->height().isAuto()) {
984         if (style->marginTop().quirk())
985             style->setMarginTop(Length(intrinsicMargin, Fixed));
986         if (style->marginBottom().quirk())
987             style->setMarginBottom(Length(intrinsicMargin, Fixed));
988     }
989 }
990
991 void CSSStyleSelector::adjustRenderStyle(RenderStyle* style, Element *e)
992 {
993     // Cache our original display.
994     style->setOriginalDisplay(style->display());
995
996     if (style->display() != NONE) {
997         // If we have a <td> that specifies a float property, in quirks mode we just drop the float
998         // property.
999         // Sites also commonly use display:inline/block on <td>s and <table>s.  In quirks mode we force
1000         // these tags to retain their display types.
1001         if (!strictParsing && e) {
1002             if (e->hasTagName(tdTag)) {
1003                 style->setDisplay(TABLE_CELL);
1004                 style->setFloating(FNONE);
1005             }
1006             else if (e->hasTagName(tableTag))
1007                 style->setDisplay(style->isDisplayInlineType() ? INLINE_TABLE : TABLE);
1008         }
1009
1010         // Frames and framesets never honor position:relative or position:absolute.  This is necessary to
1011         // fix a crash where a site tries to position these objects.  They also never honor display.
1012         if (e && (e->hasTagName(frameTag) || e->hasTagName(framesetTag))) {
1013             style->setPosition(StaticPosition);
1014             style->setDisplay(BLOCK);
1015         }
1016
1017         // Table headers with a text-align of auto will change the text-align to center.
1018         if (e && e->hasTagName(thTag) && style->textAlign() == TAAUTO)
1019             style->setTextAlign(CENTER);
1020         
1021         // Mutate the display to BLOCK or TABLE for certain cases, e.g., if someone attempts to
1022         // position or float an inline, compact, or run-in.  Cache the original display, since it
1023         // may be needed for positioned elements that have to compute their static normal flow
1024         // positions.  We also force inline-level roots to be block-level.
1025         if (style->display() != BLOCK && style->display() != TABLE && style->display() != BOX &&
1026             (style->position() == AbsolutePosition || style->position() == FixedPosition || style->floating() != FNONE ||
1027              (e && e->document()->documentElement() == e))) {
1028             if (style->display() == INLINE_TABLE)
1029                 style->setDisplay(TABLE);
1030             else if (style->display() == INLINE_BOX)
1031                 style->setDisplay(BOX);
1032             else if (style->display() == LIST_ITEM) {
1033                 // It is a WinIE bug that floated list items lose their bullets, so we'll emulate the quirk,
1034                 // but only in quirks mode.
1035                 if (!strictParsing && style->floating() != FNONE)
1036                     style->setDisplay(BLOCK);
1037             }
1038             else
1039                 style->setDisplay(BLOCK);
1040         }
1041         
1042         // After performing the display mutation, check table rows.  We do not honor position:relative on
1043         // table rows or cells.  This has been established in CSS2.1 (and caused a crash in containingBlock()
1044         // on some sites).
1045         if ((style->display() == TABLE_HEADER_GROUP || style->display() == TABLE_ROW_GROUP ||
1046              style->display() == TABLE_FOOTER_GROUP || style->display() == TABLE_ROW || style->display() == TABLE_CELL) &&
1047              style->position() == RelativePosition)
1048             style->setPosition(StaticPosition);
1049     }
1050
1051     // Make sure our z-index value is only applied if the object is positioned,
1052     // relatively positioned, or transparent.
1053     if (style->position() == StaticPosition && style->opacity() == 1.0f)
1054         style->setHasAutoZIndex();
1055
1056     // Auto z-index becomes 0 for the root element and transparent objects.  This prevents
1057     // cases where objects that should be blended as a single unit end up with a non-transparent
1058     // object wedged in between them.
1059     if (style->hasAutoZIndex() && ((e && e->document()->documentElement() == e) || style->opacity() < 1.0f))
1060         style->setZIndex(0);
1061     
1062     // Button, legend, input, select and textarea all consider width values of 'auto' to be 'intrinsic'.
1063     // This will be important when we use block flows for all form controls.
1064     if (e && (e->hasTagName(legendTag) || e->hasTagName(buttonTag) || e->hasTagName(inputTag) ||
1065               e->hasTagName(selectTag) || e->hasTagName(textareaTag))) {
1066         if (style->width().isAuto())
1067             style->setWidth(Length(Intrinsic));
1068     }
1069
1070     // Finally update our text decorations in effect, but don't allow text-decoration to percolate through
1071     // tables, inline blocks, inline tables, or run-ins.
1072     if (style->display() == TABLE || style->display() == INLINE_TABLE || style->display() == RUN_IN
1073         || style->display() == INLINE_BLOCK || style->display() == INLINE_BOX)
1074         style->setTextDecorationsInEffect(style->textDecoration());
1075     else
1076         style->addToTextDecorationsInEffect(style->textDecoration());
1077     
1078     // If either overflow value is not visible, change to auto.
1079     if (style->overflowX() == OMARQUEE && style->overflowY() != OMARQUEE)
1080         style->setOverflowY(OMARQUEE);
1081     else if (style->overflowY() == OMARQUEE && style->overflowX() != OMARQUEE)
1082         style->setOverflowX(OMARQUEE);
1083     else if (style->overflowX() == OVISIBLE && style->overflowY() != OVISIBLE)
1084         style->setOverflowX(OAUTO);
1085     else if (style->overflowY() == OVISIBLE && style->overflowX() != OVISIBLE)
1086         style->setOverflowY(OAUTO);
1087
1088     // Table rows, sections and the table itself will support overflow:hidden and will ignore scroll/auto.
1089     // FIXME: Eventually table sections will support auto and scroll.
1090     if (style->display() == TABLE || style->display() == INLINE_TABLE ||
1091         style->display() == TABLE_ROW_GROUP || style->display() == TABLE_ROW) {
1092         if (style->overflowX() != OVISIBLE && style->overflowX() != OHIDDEN) 
1093             style->setOverflowX(OVISIBLE);
1094         if (style->overflowY() != OVISIBLE && style->overflowY() != OHIDDEN) 
1095             style->setOverflowY(OVISIBLE);
1096     }
1097
1098     // Links should be user selectable when content editable
1099     if (e && e->isLink() && (style->userModify() == READ_WRITE || style->userModify() == READ_WRITE_PLAINTEXT_ONLY))
1100         style->setUserSelect(SELECT_AUTO);
1101
1102     // Cull out any useless layers and also repeat patterns into additional layers.
1103     style->adjustBackgroundLayers();
1104
1105     // Important: Intrinsic margins get added to controls before the theme has adjusted the style, since the theme will
1106     // alter fonts and heights/widths.
1107     if (e && e->isControl() && style->fontSize() >= 11) {
1108         // Don't apply intrinsic margins to image buttons.  The designer knows how big the images are,
1109         // so we have to treat all image buttons as though they were explicitly sized.
1110         if (!e->hasTagName(inputTag) || static_cast<HTMLInputElement*>(e)->inputType() != HTMLInputElement::IMAGE)
1111             addIntrinsicMargins(style);
1112     }
1113
1114     // Let the theme also have a crack at adjusting the style.
1115     if (style->hasAppearance())
1116         theme()->adjustStyle(this, style, e, m_hasUAAppearance, m_borderData, m_backgroundData, m_backgroundColor);
1117
1118 #ifdef SVG_SUPPORT
1119     if (e && e->isSVGElement()) {
1120         // Spec: http://www.w3.org/TR/SVG/masking.html#OverflowProperty
1121         if (style->overflowY() == OSCROLL)
1122             style->setOverflowY(OHIDDEN);
1123         else if (style->overflowY() == OAUTO)
1124             style->setOverflowY(OVISIBLE);
1125         
1126         if (style->overflowX() == OSCROLL)
1127             style->setOverflowX(OHIDDEN);
1128         else if (style->overflowX() == OAUTO)
1129             style->setOverflowX(OVISIBLE);
1130     }
1131 #endif
1132 }
1133
1134 void CSSStyleSelector::updateFont()
1135 {
1136     checkForTextSizeAdjust();
1137     checkForGenericFamilyChange(style, parentStyle);
1138     style->font().update();
1139     fontDirty = false;
1140 }
1141
1142 void CSSStyleSelector::cacheBorderAndBackground()
1143 {
1144     m_hasUAAppearance = style->hasAppearance();
1145     if (m_hasUAAppearance) {
1146         m_borderData = style->border();
1147         m_backgroundData = *style->backgroundLayers();
1148         m_backgroundColor = style->backgroundColor();
1149     }
1150 }
1151
1152 RefPtr<CSSRuleList> CSSStyleSelector::styleRulesForElement(Element* e, bool authorOnly)
1153 {
1154     if (!e->document()->haveStylesheetsLoaded())
1155         return 0;
1156
1157     m_collectRulesOnly = true;
1158     
1159     initElementAndPseudoState(e);
1160     initForStyleResolve(e, 0);
1161     
1162     if (!authorOnly) {
1163         int firstUARule = -1, lastUARule = -1;
1164         // First we match rules from the user agent sheet.
1165         matchUARules(firstUARule, lastUARule);
1166
1167         // Now we check user sheet rules.
1168         int firstUserRule = -1, lastUserRule = -1;
1169         matchRules(m_userStyle, firstUserRule, lastUserRule);
1170     }
1171
1172     // Check the rules in author sheets.
1173     int firstAuthorRule = -1, lastAuthorRule = -1;
1174     matchRules(m_authorStyle, firstAuthorRule, lastAuthorRule);
1175     
1176     m_collectRulesOnly = false;
1177     
1178     return m_ruleList;
1179 }
1180
1181 RefPtr<CSSRuleList> CSSStyleSelector::pseudoStyleRulesForElement(Element* e, StringImpl* pseudoStyle, bool authorOnly)
1182 {
1183     // FIXME: Implement this.
1184     return 0;
1185 }
1186
1187 static bool subject;
1188
1189 bool CSSStyleSelector::checkSelector(CSSSelector* sel, Element *e)
1190 {
1191     dynamicPseudo = RenderStyle::NOPSEUDO;
1192     
1193     Node *n = e;
1194
1195     // we have the subject part of the selector
1196     subject = true;
1197
1198     // We track whether or not the rule contains only :hover and :active in a simple selector. If
1199     // so, we can't allow that to apply to every element on the page.  We assume the author intended
1200     // to apply the rules only to links.
1201     bool onlyHoverActive = (!sel->hasTag() &&
1202                             (sel->match == CSSSelector::PseudoClass &&
1203                               (sel->pseudoType() == CSSSelector::PseudoHover ||
1204                                sel->pseudoType() == CSSSelector::PseudoActive)));
1205     bool affectedByHover = style ? style->affectedByHoverRules() : false;
1206     bool affectedByActive = style ? style->affectedByActiveRules() : false;
1207     bool havePseudo = pseudoStyle != RenderStyle::NOPSEUDO;
1208     
1209     // first selector has to match
1210     if (!checkOneSelector(sel, e))
1211         return false;
1212
1213     // check the subselectors
1214     CSSSelector::Relation relation = sel->relation();
1215     while((sel = sel->tagHistory)) {
1216         if (!n->isElementNode())
1217             return false;
1218         if (relation != CSSSelector::SubSelector) {
1219             subject = false;
1220             if (havePseudo && dynamicPseudo != pseudoStyle)
1221                 return false;
1222         }
1223         
1224         switch(relation)
1225         {
1226         case CSSSelector::Descendant:
1227             // FIXME: This match needs to know how to backtrack and be non-deterministic.
1228             do {
1229                 n = n->parentNode();
1230                 if (!n || !n->isElementNode())
1231                     return false;
1232             } while (!checkOneSelector(sel, static_cast<Element*>(n)));
1233             break;
1234         case CSSSelector::Child:
1235         {
1236             n = n->parentNode();
1237             if (!strictParsing)
1238                 while (n && n->implicitNode())
1239                     n = n->parentNode();
1240             if (!n || !n->isElementNode())
1241                 return false;
1242             if (!checkOneSelector(sel, static_cast<Element*>(n)))
1243                 return false;
1244             break;
1245         }
1246         case CSSSelector::DirectAdjacent:
1247         {
1248             n = n->previousSibling();
1249             while (n && !n->isElementNode())
1250                 n = n->previousSibling();
1251             if (!n)
1252                 return false;
1253             if (!checkOneSelector(sel, static_cast<Element*>(n)))
1254                 return false;
1255             break;
1256         }
1257         case CSSSelector::IndirectAdjacent:
1258             // FIXME: This match needs to know how to backtrack and be non-deterministic.
1259             do {
1260                 n = n->previousSibling();
1261                 while (n && !n->isElementNode())
1262                     n = n->previousSibling();
1263                 if (!n)
1264                     return false;
1265             } while (!checkOneSelector(sel, static_cast<Element*>(n)));
1266             break;
1267        case CSSSelector::SubSelector:
1268        {
1269             if (onlyHoverActive)
1270                 onlyHoverActive = (sel->match == CSSSelector::PseudoClass &&
1271                                    (sel->pseudoType() == CSSSelector::PseudoHover ||
1272                                     sel->pseudoType() == CSSSelector::PseudoActive));
1273             
1274             Element *elem = static_cast<Element*>(n);
1275             // a selector is invalid if something follows :first-xxx
1276             if (elem == element && dynamicPseudo != RenderStyle::NOPSEUDO)
1277                 return false;
1278             if (!checkOneSelector(sel, elem, true))
1279                 return false;
1280             break;
1281         }
1282         }
1283         relation = sel->relation();
1284     }
1285
1286     if (subject && havePseudo && dynamicPseudo != pseudoStyle)
1287         return false;
1288     
1289     // disallow *:hover, *:active, and *:hover:active except for links
1290     if (!strictParsing && onlyHoverActive && subject) {
1291         if (pseudoState == PseudoUnknown)
1292             checkPseudoState(e);
1293
1294         if (pseudoState == PseudoNone) {
1295             if (!affectedByHover && style->affectedByHoverRules())
1296                 style->setAffectedByHoverRules(false);
1297             if (!affectedByActive && style->affectedByActiveRules())
1298                 style->setAffectedByActiveRules(false);
1299             return false;
1300         }
1301     }
1302
1303     return true;
1304 }
1305
1306 bool CSSStyleSelector::checkOneSelector(CSSSelector* sel, Element* e, bool isSubSelector)
1307 {
1308     if(!e)
1309         return false;
1310
1311     if (sel->hasTag()) {
1312         const AtomicString& localName = e->localName();
1313         const AtomicString& ns = e->namespaceURI();
1314         const AtomicString& selLocalName = sel->tag.localName();
1315         const AtomicString& selNS = sel->tag.namespaceURI();
1316     
1317         if ((selLocalName != starAtom && localName != selLocalName) ||
1318             (selNS != starAtom && ns != selNS))
1319             return false;
1320     }
1321
1322     if (sel->hasAttribute()) {
1323         if (sel->match == CSSSelector::Class) {
1324             if (!e->hasClass())
1325                 return false;
1326             for (const AtomicStringList* c = e->getClassList(); c; c = c->next())
1327                 if (c->string() == sel->value)
1328                     return true;
1329             return false;
1330         }
1331         else if (sel->match == CSSSelector::Id)
1332             return e->hasID() && e->getIDAttribute() == sel->value;
1333         else if (style && (e != element || !styledElement || (!styledElement->isMappedAttribute(sel->attr) && sel->attr != typeAttr))) {
1334             style->setAffectedByAttributeSelectors(); // Special-case the "type" attribute so input form controls can share style.
1335             m_selectorAttrs.add(sel->attr.localName().impl());
1336         }
1337
1338         const AtomicString& value = e->getAttribute(sel->attr);
1339         if (value.isNull())
1340             return false; // attribute is not set
1341
1342         switch(sel->match) {
1343         case CSSSelector::Exact:
1344             if ((isXMLDoc && sel->value != value) || (!isXMLDoc && !equalIgnoringCase(sel->value, value)))
1345                 return false;
1346             break;
1347         case CSSSelector::List:
1348         {
1349             // The selector's value can't contain a space, or it's totally bogus.
1350             if (sel->value.contains(' '))
1351                 return false;
1352
1353             int startSearchAt = 0;
1354             while (true) {
1355                 int foundPos = value.find(sel->value, startSearchAt, isXMLDoc);
1356                 if (foundPos == -1)
1357                     return false;
1358                 if (foundPos == 0 || value[foundPos-1] == ' ') {
1359                     unsigned endStr = foundPos + sel->value.length();
1360                     if (endStr == value.length() || value[endStr] == ' ')
1361                         break; // We found a match.
1362                 }
1363                 
1364                 // No match.  Keep looking.
1365                 startSearchAt = foundPos + 1;
1366             }
1367             break;
1368         }
1369         case CSSSelector::Contain:
1370             if (!value.contains(sel->value, isXMLDoc))
1371                 return false;
1372             break;
1373         case CSSSelector::Begin:
1374             if (!value.startsWith(sel->value, isXMLDoc))
1375                 return false;
1376             break;
1377         case CSSSelector::End:
1378             if (!value.endsWith(sel->value, isXMLDoc))
1379                 return false;
1380             break;
1381         case CSSSelector::Hyphen:
1382             if (value.length() < sel->value.length())
1383                 return false;
1384             if (!value.startsWith(sel->value, isXMLDoc))
1385                 return false;
1386             // It they start the same, check for exact match or following '-':
1387             if (value.length() != sel->value.length() && value[sel->value.length()] != '-')
1388                 return false;
1389             break;
1390         case CSSSelector::PseudoClass:
1391         case CSSSelector::PseudoElement:
1392         default:
1393             break;
1394         }
1395     }
1396     if(sel->match == CSSSelector::PseudoClass || sel->match == CSSSelector::PseudoElement)
1397     {
1398         // Pseudo elements. We need to check first child here. No dynamic pseudo
1399         // elements for the moment
1400             switch (sel->pseudoType()) {
1401                 // Pseudo classes:
1402             case CSSSelector::PseudoEmpty:
1403                 if (!e->firstChild())
1404                     return true;
1405                 break;
1406             case CSSSelector::PseudoFirstChild: {
1407                 // first-child matches the first child that is an element!
1408                 if (e->parentNode() && e->parentNode()->isElementNode()) {
1409                     Node *n = e->previousSibling();
1410                     while (n && !n->isElementNode())
1411                         n = n->previousSibling();
1412                     if (!n)
1413                         return true;
1414                 }
1415                 break;
1416             }
1417             case CSSSelector::PseudoFirstOfType: {
1418                 // first-of-type matches the first element of its type!
1419                 if (e->parentNode() && e->parentNode()->isElementNode()) {
1420                     const QualifiedName& type = e->tagQName();
1421                     Node *n = e->previousSibling();
1422                     while (n) {
1423                         if (n->isElementNode() && static_cast<Element*>(n)->hasTagName(type))
1424                             break;
1425                         n = n->previousSibling();
1426                     }
1427                     if (!n)
1428                         return true;
1429                 }
1430                 break;
1431             }
1432             case CSSSelector::PseudoLastChild: {
1433                 // last-child matches the last child that is an element!
1434                 if (e->parentNode() && e->parentNode()->isElementNode()) {
1435                     Node *n = e->nextSibling();
1436                     while (n && !n->isElementNode())
1437                         n = n->nextSibling();
1438                     if (!n)
1439                         return true;
1440                 }
1441                 break;
1442             }
1443             case CSSSelector::PseudoLastOfType: {
1444                 // last-of-type matches the last element of its type!
1445                 if (e->parentNode() && e->parentNode()->isElementNode()) {
1446                     const QualifiedName& type = e->tagQName();
1447                     Node *n = e->nextSibling();
1448                     while (n) {
1449                         if (n->isElementNode() && static_cast<Element*>(n)->hasTagName(type))
1450                             break;
1451                         n = n->nextSibling();
1452                     }
1453                     if (!n)
1454                         return true;
1455                 }
1456                 break;
1457             }
1458             case CSSSelector::PseudoOnlyChild: {
1459                 // If both first-child and last-child apply, then only-child applies.
1460                 if (e->parentNode() && e->parentNode()->isElementNode()) {
1461                     Node *n = e->previousSibling();
1462                     while (n && !n->isElementNode())
1463                         n = n->previousSibling();
1464                     if (!n) {
1465                         n = e->nextSibling();
1466                         while (n && !n->isElementNode())
1467                             n = n->nextSibling();
1468                         if (!n)
1469                             return true;
1470                     }
1471                 }
1472                 break;
1473             }
1474             case CSSSelector::PseudoOnlyOfType: {
1475                 // If both first-of-type and last-of-type apply, then only-of-type applies.
1476                 if (e->parentNode() && e->parentNode()->isElementNode()) {
1477                     const QualifiedName& type = e->tagQName();
1478                     Node *n = e->previousSibling();
1479                     while (n && !static_cast<Element*>(n)->hasTagName(type))
1480                         n = n->previousSibling();
1481                     if (!n) {
1482                         n = e->nextSibling();
1483                         while (n && !static_cast<Element*>(n)->hasTagName(type))
1484                             n = n->nextSibling();
1485                         if (!n)
1486                             return true;
1487                     }
1488                 }
1489                 break;
1490             }
1491             case CSSSelector::PseudoTarget:
1492                 if (e == e->document()->getCSSTarget())
1493                     return true;
1494                 break;
1495             case CSSSelector::PseudoAnyLink:
1496                 if (pseudoState == PseudoUnknown)
1497                     checkPseudoState(e, false);
1498                 if (pseudoState == PseudoAnyLink || pseudoState == PseudoLink || pseudoState == PseudoVisited)
1499                     return true;
1500                 break;
1501             case CSSSelector::PseudoAutofill:
1502                 if (e && e->hasTagName(inputTag))
1503                     return static_cast<HTMLInputElement*>(e)->autofilled();
1504                 break;
1505             case CSSSelector::PseudoLink:
1506                 if (pseudoState == PseudoUnknown || pseudoState == PseudoAnyLink)
1507                     checkPseudoState(e);
1508                 if (pseudoState == PseudoLink)
1509                     return true;
1510                 break;
1511             case CSSSelector::PseudoVisited:
1512                 if (pseudoState == PseudoUnknown || pseudoState == PseudoAnyLink)
1513                     checkPseudoState(e);
1514                 if (pseudoState == PseudoVisited)
1515                     return true;
1516                 break;
1517             case CSSSelector::PseudoHover: {
1518                 // If we're in quirks mode, then hover should never match anchors with no
1519                 // href and *:hover should not match anything.  This is important for sites like wsj.com.
1520                 if (strictParsing || isSubSelector || sel->relation() == CSSSelector::SubSelector || (sel->hasTag() && !e->hasTagName(aTag)) || e->isLink()) {
1521                     if (element == e && style)
1522                         style->setAffectedByHoverRules(true);
1523                         if (element != e && e->renderStyle())
1524                             e->renderStyle()->setAffectedByHoverRules(true);
1525                         if (e->hovered())
1526                             return true;
1527                 }
1528                 break;
1529             }
1530             case CSSSelector::PseudoDrag: {
1531                 if (element == e && style)
1532                     style->setAffectedByDragRules(true);
1533                     if (element != e && e->renderStyle())
1534                         e->renderStyle()->setAffectedByDragRules(true);
1535                     if (e->renderer() && e->renderer()->isDragging())
1536                         return true;
1537                 break;
1538             }
1539             case CSSSelector::PseudoFocus:
1540                 if (e && e->focused() && e->document()->frame()->isActive())
1541                     return true;
1542                 break;
1543             case CSSSelector::PseudoActive:
1544                 // If we're in quirks mode, then :active should never match anchors with no
1545                 // href and *:active should not match anything. 
1546                 if (strictParsing || isSubSelector || sel->relation() == CSSSelector::SubSelector || (sel->hasTag() && !e->hasTagName(aTag)) || e->isLink()) {
1547                     if (element == e && style)
1548                         style->setAffectedByActiveRules(true);
1549                     else if (e->renderStyle())
1550                         e->renderStyle()->setAffectedByActiveRules(true);
1551                     if (e->active())
1552                         return true;
1553                 }
1554                 break;
1555             case CSSSelector::PseudoEnabled:
1556                 if (e && e->isControl())
1557                     // The UI spec states that you can't match :enabled unless you are an object that can
1558                     // "receive focus and be activated."  We will limit matching of this pseudo-class to elements
1559                     // that are controls.
1560                     return e->isEnabled();                    
1561                 break;
1562             case CSSSelector::PseudoDisabled:
1563                 if (e && e->isControl())
1564                     // The UI spec states that you can't match :enabled unless you are an object that can
1565                     // "receive focus and be activated."  We will limit matching of this pseudo-class to elements
1566                     // that are controls.
1567                     return !e->isEnabled();                    
1568                 break;
1569             case CSSSelector::PseudoChecked:
1570                 // Even though WinIE allows checked and indeterminate to co-exist, the CSS selector spec says that
1571                 // you can't be both checked and indeterminate.  We will behave like WinIE behind the scenes and just
1572                 // obey the CSS spec here in the test for matching the pseudo.
1573                 if (e && e->isChecked() && !e->isIndeterminate())
1574                     return true;
1575                 break;
1576             case CSSSelector::PseudoIndeterminate:
1577                 if (e && e->isIndeterminate())
1578                     return true;
1579                 break;
1580             case CSSSelector::PseudoRoot:
1581                 if (e == e->document()->documentElement())
1582                     return true;
1583                 break;
1584             case CSSSelector::PseudoLang: {
1585                 const AtomicString& value = e->getAttribute(langAttr);
1586                 if (value.isEmpty() || !value.startsWith(sel->argument, false))
1587                     break;
1588                 if (value.length() != sel->argument.length() && value[sel->argument.length()] != '-')
1589                     break;
1590                 return true;
1591             }
1592             case CSSSelector::PseudoNot: {
1593                 // check the simple selector
1594                 for (CSSSelector* subSel = sel->simpleSelector; subSel; subSel = subSel->tagHistory) {
1595                     // :not cannot nest. I don't really know why this is a
1596                     // restriction in CSS3, but it is, so let's honour it.
1597                     if (subSel->simpleSelector)
1598                         break;
1599                     if (!checkOneSelector(subSel, e))
1600                         return true;
1601                 }
1602                 break;
1603             }
1604             case CSSSelector::PseudoOther:
1605                 break;
1606             
1607             // Pseudo-elements:
1608             case CSSSelector::PseudoFirstLine:
1609                 if (subject) {
1610                     dynamicPseudo=RenderStyle::FIRST_LINE;
1611                     return true;
1612                 }
1613                 break;
1614             case CSSSelector::PseudoFirstLetter:
1615                 if (subject) {
1616                     dynamicPseudo=RenderStyle::FIRST_LETTER;
1617                     return true;
1618                 }
1619                 break;
1620             case CSSSelector::PseudoSelection:
1621                 dynamicPseudo = RenderStyle::SELECTION;
1622                 return true;
1623             case CSSSelector::PseudoBefore:
1624                 dynamicPseudo = RenderStyle::BEFORE;
1625                 return true;
1626             case CSSSelector::PseudoAfter:
1627                 dynamicPseudo = RenderStyle::AFTER;
1628                 return true;
1629             case CSSSelector::PseudoFileUploadButton:
1630                 dynamicPseudo = RenderStyle::FILE_UPLOAD_BUTTON;
1631                 return true;
1632                 
1633             case CSSSelector::PseudoNotParsed:
1634                 assert(false);
1635                 break;
1636         }
1637         return false;
1638     }
1639     // ### add the rest of the checks...
1640     return true;
1641 }
1642
1643 // -----------------------------------------------------------------
1644
1645 CSSRuleSet::CSSRuleSet()
1646 {
1647     m_universalRules = 0;
1648     m_ruleCount = 0;
1649 }
1650
1651 CSSRuleSet::~CSSRuleSet()
1652
1653     deleteAllValues(m_idRules);
1654     deleteAllValues(m_classRules);
1655     deleteAllValues(m_tagRules);
1656
1657     delete m_universalRules; 
1658 }
1659
1660
1661 void CSSRuleSet::addToRuleSet(AtomicStringImpl* key, AtomRuleMap& map,
1662                               CSSStyleRule* rule, CSSSelector* sel)
1663 {
1664     if (!key) return;
1665     CSSRuleDataList* rules = map.get(key);
1666     if (!rules) {
1667         rules = new CSSRuleDataList(m_ruleCount++, rule, sel);
1668         map.set(key, rules);
1669     } else
1670         rules->append(m_ruleCount++, rule, sel);
1671 }
1672
1673 void CSSRuleSet::addRule(CSSStyleRule* rule, CSSSelector* sel)
1674 {
1675     if (sel->match == CSSSelector::Id) {
1676         addToRuleSet(sel->value.impl(), m_idRules, rule, sel);
1677         return;
1678     }
1679     if (sel->match == CSSSelector::Class) {
1680         addToRuleSet(sel->value.impl(), m_classRules, rule, sel);
1681         return;
1682     }
1683      
1684     const AtomicString& localName = sel->tag.localName();
1685     if (localName != starAtom) {
1686         addToRuleSet(localName.impl(), m_tagRules, rule, sel);
1687         return;
1688     }
1689     
1690     // Just put it in the universal rule set.
1691     if (!m_universalRules)
1692         m_universalRules = new CSSRuleDataList(m_ruleCount++, rule, sel);
1693     else
1694         m_universalRules->append(m_ruleCount++, rule, sel);
1695 }
1696
1697 void CSSRuleSet::addRulesFromSheet(CSSStyleSheet* sheet,  MediaQueryEvaluator* medium)
1698 {
1699     if (!sheet || !sheet->isCSSStyleSheet())
1700         return;
1701
1702     // No media implies "all", but if a media list exists it must
1703     // contain our current medium
1704     if (sheet->media() && !medium->eval(sheet->media()))
1705         return; // the style sheet doesn't apply
1706
1707     int len = sheet->length();
1708
1709     for (int i = 0; i < len; i++) {
1710         StyleBase* item = sheet->item(i);
1711         if (item->isStyleRule()) {
1712             CSSStyleRule* rule = static_cast<CSSStyleRule*>(item);
1713             for (CSSSelector* s = rule->selector(); s; s = s->next())
1714                 addRule(rule, s);
1715         }
1716         else if(item->isImportRule()) {
1717             CSSImportRule* import = static_cast<CSSImportRule*>(item);
1718             if (!import->media() || medium->eval(import->media()))
1719                 addRulesFromSheet(import->styleSheet(), medium);
1720         }
1721         else if(item->isMediaRule()) {
1722             CSSMediaRule* r = static_cast<CSSMediaRule*>(item);
1723             CSSRuleList* rules = r->cssRules();
1724
1725             if ((!r->media() || medium->eval(r->media())) && rules) {
1726                 // Traverse child elements of the @media rule.
1727                 for (unsigned j = 0; j < rules->length(); j++) {
1728                     CSSRule *childItem = rules->item(j);
1729                     if (childItem->isStyleRule()) {
1730                         // It is a StyleRule, so append it to our list
1731                         CSSStyleRule* rule = static_cast<CSSStyleRule*>(childItem);
1732                         for (CSSSelector* s = rule->selector(); s; s = s->next())
1733                             addRule(rule, s);
1734                         
1735                     }
1736                 }   // for rules
1737             }   // if rules
1738         }
1739     }
1740 }
1741
1742 // -------------------------------------------------------------------------------------
1743 // this is mostly boring stuff on how to apply a certain rule to the renderstyle...
1744
1745 static Length convertToLength(CSSPrimitiveValue *primitiveValue, RenderStyle *style, bool *ok = 0)
1746 {
1747     Length l;
1748     if (!primitiveValue) {
1749         if (ok)
1750             *ok = false;
1751     } else {
1752         int type = primitiveValue->primitiveType();
1753         if(type > CSSPrimitiveValue::CSS_PERCENTAGE && type < CSSPrimitiveValue::CSS_DEG)
1754             l = Length(primitiveValue->computeLengthIntForLength(style), Fixed);
1755         else if(type == CSSPrimitiveValue::CSS_PERCENTAGE)
1756             l = Length(int(primitiveValue->getFloatValue()), Percent);
1757         else if(type == CSSPrimitiveValue::CSS_NUMBER)
1758             l = Length(int(primitiveValue->getFloatValue() * 100), Percent);
1759         else if (ok)
1760             *ok = false;
1761     }
1762     return l;
1763 }
1764
1765 void CSSStyleSelector::applyDeclarations(bool applyFirst, bool isImportant,
1766                                          int startIndex, int endIndex)
1767 {
1768     if (startIndex == -1) return;
1769     for (int i = startIndex; i <= endIndex; i++) {
1770         CSSMutableStyleDeclaration* decl = m_matchedDecls[i];
1771         DeprecatedValueListConstIterator<CSSProperty> end;
1772         for (DeprecatedValueListConstIterator<CSSProperty> it = decl->valuesIterator(); it != end; ++it) {
1773             const CSSProperty& current = *it;
1774             // give special priority to font-xxx, color properties
1775             if (isImportant == current.isImportant()) {
1776                 bool first;
1777                 switch(current.id()) {
1778                     case CSS_PROP_COLOR:
1779                     case CSS_PROP_DIRECTION:
1780                     case CSS_PROP_DISPLAY:
1781                     case CSS_PROP_FONT:
1782                     case CSS_PROP_FONT_SIZE:
1783                     case CSS_PROP_FONT_STYLE:
1784                     case CSS_PROP_FONT_FAMILY:
1785                     case CSS_PROP_FONT_WEIGHT:
1786                     case CSS_PROP__WEBKIT_TEXT_SIZE_ADJUST:
1787                     case CSS_PROP_FONT_VARIANT:
1788                         // these have to be applied first, because other properties use the computed
1789                         // values of these porperties.
1790                         first = true;
1791                         break;
1792                     default:
1793                         first = false;
1794                         break;
1795                 }
1796                 
1797                 if (first == applyFirst)
1798                     applyProperty(current.id(), current.value());
1799             }
1800         }
1801     }
1802 }
1803
1804 void CSSStyleSelector::applyProperty(int id, CSSValue *value)
1805 {
1806     CSSPrimitiveValue *primitiveValue = 0;
1807     if(value->isPrimitiveValue()) primitiveValue = static_cast<CSSPrimitiveValue*>(value);
1808
1809     Length l;
1810     bool apply = false;
1811
1812     unsigned short valueType = value->cssValueType();
1813
1814     bool isInherit = parentNode && valueType == CSSValue::CSS_INHERIT;
1815     bool isInitial = valueType == CSSValue::CSS_INITIAL || (!parentNode && valueType == CSSValue::CSS_INHERIT);
1816
1817     // These properties are used to set the correct margins/padding on RTL lists.
1818     if (id == CSS_PROP__WEBKIT_MARGIN_START)
1819         id = style->direction() == LTR ? CSS_PROP_MARGIN_LEFT : CSS_PROP_MARGIN_RIGHT;
1820     else if (id == CSS_PROP__WEBKIT_PADDING_START)
1821         id = style->direction() == LTR ? CSS_PROP_PADDING_LEFT : CSS_PROP_PADDING_RIGHT;
1822
1823     // What follows is a list that maps the CSS properties into their corresponding front-end
1824     // RenderStyle values.  Shorthands (e.g. border, background) occur in this list as well and
1825     // are only hit when mapping "inherit" or "initial" into front-end values.
1826     switch(id)
1827     {
1828 // ident only properties
1829     case CSS_PROP_BACKGROUND_ATTACHMENT:
1830         HANDLE_BACKGROUND_VALUE(backgroundAttachment, BackgroundAttachment, value)
1831         break;
1832     case CSS_PROP__WEBKIT_BACKGROUND_CLIP:
1833         HANDLE_BACKGROUND_VALUE(backgroundClip, BackgroundClip, value)
1834         break;
1835     case CSS_PROP__WEBKIT_BACKGROUND_COMPOSITE:
1836         HANDLE_BACKGROUND_VALUE(backgroundComposite, BackgroundComposite, value)
1837         break;
1838     case CSS_PROP__WEBKIT_BACKGROUND_ORIGIN:
1839         HANDLE_BACKGROUND_VALUE(backgroundOrigin, BackgroundOrigin, value)
1840         break;
1841     case CSS_PROP_BACKGROUND_REPEAT:
1842         HANDLE_BACKGROUND_VALUE(backgroundRepeat, BackgroundRepeat, value)
1843         break;
1844     case CSS_PROP__WEBKIT_BACKGROUND_SIZE:
1845         HANDLE_BACKGROUND_VALUE(backgroundSize, BackgroundSize, value)
1846         break;
1847     case CSS_PROP_BORDER_COLLAPSE:
1848         HANDLE_INHERIT_AND_INITIAL(borderCollapse, BorderCollapse)
1849         if(!primitiveValue) break;
1850         switch(primitiveValue->getIdent())
1851         {
1852         case CSS_VAL_COLLAPSE:
1853             style->setBorderCollapse(true);
1854             break;
1855         case CSS_VAL_SEPARATE:
1856             style->setBorderCollapse(false);
1857             break;
1858         default:
1859             return;
1860         }
1861         break;
1862         
1863     case CSS_PROP_BORDER_TOP_STYLE:
1864         HANDLE_INHERIT_AND_INITIAL_WITH_VALUE(borderTopStyle, BorderTopStyle, BorderStyle)
1865         if (!primitiveValue) return;
1866         style->setBorderTopStyle((EBorderStyle)(primitiveValue->getIdent() - CSS_VAL_NONE));
1867         break;
1868     case CSS_PROP_BORDER_RIGHT_STYLE:
1869         HANDLE_INHERIT_AND_INITIAL_WITH_VALUE(borderRightStyle, BorderRightStyle, BorderStyle)
1870         if (!primitiveValue) return;
1871         style->setBorderRightStyle((EBorderStyle)(primitiveValue->getIdent() - CSS_VAL_NONE));
1872         break;
1873     case CSS_PROP_BORDER_BOTTOM_STYLE:
1874         HANDLE_INHERIT_AND_INITIAL_WITH_VALUE(borderBottomStyle, BorderBottomStyle, BorderStyle)
1875         if (!primitiveValue) return;
1876         style->setBorderBottomStyle((EBorderStyle)(primitiveValue->getIdent() - CSS_VAL_NONE));
1877         break;
1878     case CSS_PROP_BORDER_LEFT_STYLE:
1879         HANDLE_INHERIT_AND_INITIAL_WITH_VALUE(borderLeftStyle, BorderLeftStyle, BorderStyle)
1880         if (!primitiveValue) return;
1881         style->setBorderLeftStyle((EBorderStyle)(primitiveValue->getIdent() - CSS_VAL_NONE));
1882         break;
1883     case CSS_PROP_OUTLINE_STYLE:
1884         HANDLE_INHERIT_AND_INITIAL_WITH_VALUE(outlineStyle, OutlineStyle, BorderStyle)
1885         if (!primitiveValue) return;
1886         if (primitiveValue->getIdent() == CSS_VAL_AUTO)
1887             style->setOutlineStyle(DOTTED, true);
1888         else
1889             style->setOutlineStyle((EBorderStyle)(primitiveValue->getIdent() - CSS_VAL_NONE));
1890         break;
1891     case CSS_PROP_CAPTION_SIDE:
1892     {
1893         HANDLE_INHERIT_AND_INITIAL(captionSide, CaptionSide)
1894         if(!primitiveValue) break;
1895         ECaptionSide c = RenderStyle::initialCaptionSide();
1896         switch(primitiveValue->getIdent())
1897         {
1898         case CSS_VAL_LEFT:
1899             c = CAPLEFT; break;
1900         case CSS_VAL_RIGHT:
1901             c = CAPRIGHT; break;
1902         case CSS_VAL_TOP:
1903             c = CAPTOP; break;
1904         case CSS_VAL_BOTTOM:
1905             c = CAPBOTTOM; break;
1906         default:
1907             return;
1908         }
1909         style->setCaptionSide(c);
1910         return;
1911     }
1912     case CSS_PROP_CLEAR:
1913     {
1914         HANDLE_INHERIT_AND_INITIAL(clear, Clear)
1915         if(!primitiveValue) break;
1916         EClear c;
1917         switch(primitiveValue->getIdent())
1918         {
1919         case CSS_VAL_NONE:
1920             c = CNONE; break;
1921         case CSS_VAL_LEFT:
1922             c = CLEFT; break;
1923         case CSS_VAL_RIGHT:
1924             c = CRIGHT; break;
1925         case CSS_VAL_BOTH:
1926             c = CBOTH; break;
1927         default:
1928             return;
1929         }
1930         style->setClear(c);
1931         return;
1932     }
1933     case CSS_PROP_DIRECTION:
1934     {
1935         HANDLE_INHERIT_AND_INITIAL(direction, Direction)
1936         if(!primitiveValue) break;
1937         style->setDirection(primitiveValue->getIdent() == CSS_VAL_LTR ? LTR : RTL);
1938         return;
1939     }
1940     case CSS_PROP_DISPLAY:
1941     {
1942         HANDLE_INHERIT_AND_INITIAL(display, Display)
1943         if(!primitiveValue) break;
1944         int id = primitiveValue->getIdent();
1945         EDisplay d;
1946         if (id == CSS_VAL_NONE)
1947             d = NONE;
1948         else
1949             d = EDisplay(primitiveValue->getIdent() - CSS_VAL_INLINE);
1950
1951         style->setDisplay(d);
1952         break;
1953     }
1954
1955     case CSS_PROP_EMPTY_CELLS:
1956     {
1957         HANDLE_INHERIT_AND_INITIAL(emptyCells, EmptyCells)
1958         if (!primitiveValue) break;
1959         int id = primitiveValue->getIdent();
1960         if (id == CSS_VAL_SHOW)
1961             style->setEmptyCells(SHOW);
1962         else if (id == CSS_VAL_HIDE)
1963             style->setEmptyCells(HIDE);
1964         break;
1965     }
1966     case CSS_PROP_FLOAT:
1967     {
1968         HANDLE_INHERIT_AND_INITIAL(floating, Floating)
1969         if(!primitiveValue) return;
1970         EFloat f;
1971         switch(primitiveValue->getIdent())
1972         {
1973         case CSS_VAL_LEFT:
1974             f = FLEFT; break;
1975         case CSS_VAL_RIGHT:
1976             f = FRIGHT; break;
1977         case CSS_VAL_NONE:
1978         case CSS_VAL_CENTER:  //Non standart CSS-Value
1979             f = FNONE; break;
1980         default:
1981             return;
1982         }
1983         
1984         style->setFloating(f);
1985         break;
1986     }
1987
1988         break;
1989     case CSS_PROP_FONT_STRETCH:
1990         break; /* Not supported. */
1991
1992     case CSS_PROP_FONT_STYLE:
1993     {
1994         FontDescription fontDescription = style->fontDescription();
1995         if (isInherit)
1996             fontDescription.setItalic(parentStyle->fontDescription().italic());
1997         else if (isInitial)
1998             fontDescription.setItalic(false);
1999         else {
2000             if (!primitiveValue) return;
2001             switch (primitiveValue->getIdent()) {
2002                 case CSS_VAL_OBLIQUE:
2003                 // FIXME: oblique is the same as italic for the moment...
2004                 case CSS_VAL_ITALIC:
2005                     fontDescription.setItalic(true);
2006                     break;
2007                 case CSS_VAL_NORMAL:
2008                     fontDescription.setItalic(false);
2009                     break;
2010                 default:
2011                     return;
2012             }
2013         }
2014         if (style->setFontDescription(fontDescription))
2015             fontDirty = true;
2016         break;
2017     }
2018
2019     case CSS_PROP_FONT_VARIANT:
2020     {
2021         FontDescription fontDescription = style->fontDescription();
2022         if (isInherit) 
2023             fontDescription.setSmallCaps(parentStyle->fontDescription().smallCaps());
2024         else if (isInitial)
2025             fontDescription.setSmallCaps(false);
2026         else {
2027             if (!primitiveValue) return;
2028             int id = primitiveValue->getIdent();
2029             if (id == CSS_VAL_NORMAL)
2030                 fontDescription.setSmallCaps(false);
2031             else if (id == CSS_VAL_SMALL_CAPS)
2032                 fontDescription.setSmallCaps(true);
2033             else
2034                 return;
2035         }
2036         if (style->setFontDescription(fontDescription))
2037             fontDirty = true;
2038         break;        
2039     }
2040
2041     case CSS_PROP_FONT_WEIGHT:
2042     {
2043         FontDescription fontDescription = style->fontDescription();
2044         if (isInherit)
2045             fontDescription.setWeight(parentStyle->fontDescription().weight());
2046         else if (isInitial)
2047             fontDescription.setWeight(cNormalWeight);
2048         else {
2049             if (!primitiveValue) return;
2050             if (primitiveValue->getIdent()) {
2051                 switch (primitiveValue->getIdent()) {
2052                     // FIXME: We aren't genuinely supporting specific weight values.
2053                     case CSS_VAL_BOLD:
2054                     case CSS_VAL_BOLDER:
2055                     case CSS_VAL_600:
2056                     case CSS_VAL_700:
2057                     case CSS_VAL_800:
2058                     case CSS_VAL_900:
2059                         fontDescription.setWeight(cBoldWeight);
2060                         break;
2061                     case CSS_VAL_NORMAL:
2062                     case CSS_VAL_LIGHTER:
2063                     case CSS_VAL_100:
2064                     case CSS_VAL_200:
2065                     case CSS_VAL_300:
2066                     case CSS_VAL_400:
2067                     case CSS_VAL_500:
2068                         fontDescription.setWeight(cNormalWeight);
2069                         break;
2070                     default:
2071                         return;
2072                 }
2073             }
2074             else
2075             {
2076                 // ### fix parsing of 100-900 values in parser, apply them here
2077             }
2078         }
2079         if (style->setFontDescription(fontDescription))
2080             fontDirty = true;
2081         break;
2082     }
2083         
2084     case CSS_PROP_LIST_STYLE_POSITION:
2085     {
2086         HANDLE_INHERIT_AND_INITIAL(listStylePosition, ListStylePosition)
2087         if (!primitiveValue) return;
2088         if (primitiveValue->getIdent())
2089             style->setListStylePosition((EListStylePosition) (primitiveValue->getIdent() - CSS_VAL_OUTSIDE));
2090         return;
2091     }
2092
2093     case CSS_PROP_LIST_STYLE_TYPE:
2094     {
2095         HANDLE_INHERIT_AND_INITIAL(listStyleType, ListStyleType)
2096         if (!primitiveValue) return;
2097         if (primitiveValue->getIdent())
2098         {
2099             EListStyleType t;
2100             int id = primitiveValue->getIdent();
2101             if (id == CSS_VAL_NONE) { // important!!
2102               t = LNONE;
2103             } else {
2104               t = EListStyleType(id - CSS_VAL_DISC);
2105             }
2106             style->setListStyleType(t);
2107         }
2108         return;
2109     }
2110
2111     case CSS_PROP_OVERFLOW:
2112     {
2113         if (isInherit) {
2114             style->setOverflowX(parentStyle->overflowX());
2115             style->setOverflowY(parentStyle->overflowY());
2116             return;
2117         }
2118         
2119         if (isInitial) {
2120             style->setOverflowX(RenderStyle::initialOverflowX());
2121             style->setOverflowY(RenderStyle::initialOverflowY());
2122             return;
2123         }
2124             
2125         EOverflow o;
2126         switch(primitiveValue->getIdent()) {
2127             case CSS_VAL_VISIBLE:
2128                 o = OVISIBLE; break;
2129             case CSS_VAL_HIDDEN:
2130                 o = OHIDDEN; break;
2131             case CSS_VAL_SCROLL:
2132                 o = OSCROLL; break;
2133             case CSS_VAL_AUTO:
2134                 o = OAUTO; break;
2135             case CSS_VAL__WEBKIT_MARQUEE:
2136                 o = OMARQUEE; break;
2137             case CSS_VAL_OVERLAY:
2138                 o = OOVERLAY; break;
2139             default:
2140                 return;
2141         }
2142         style->setOverflowX(o);
2143         style->setOverflowY(o);
2144         return;
2145     }
2146
2147     case CSS_PROP_OVERFLOW_X:
2148     {
2149         HANDLE_INHERIT_AND_INITIAL(overflowX, OverflowX)
2150         EOverflow o;
2151         switch(primitiveValue->getIdent())
2152         {
2153         case CSS_VAL_VISIBLE:
2154             o = OVISIBLE; break;
2155         case CSS_VAL_HIDDEN:
2156             o = OHIDDEN; break;
2157         case CSS_VAL_SCROLL:
2158             o = OSCROLL; break;
2159         case CSS_VAL_AUTO:
2160             o = OAUTO; break;
2161         case CSS_VAL_OVERLAY:
2162             o = OOVERLAY; break;
2163         default:
2164             return;
2165         }
2166         style->setOverflowX(o);
2167         return;
2168     }
2169
2170     case CSS_PROP_OVERFLOW_Y:
2171     {
2172         HANDLE_INHERIT_AND_INITIAL(overflowY, OverflowY)
2173         EOverflow o;
2174         switch(primitiveValue->getIdent())
2175         {
2176         case CSS_VAL_VISIBLE:
2177             o = OVISIBLE; break;
2178         case CSS_VAL_HIDDEN:
2179             o = OHIDDEN; break;
2180         case CSS_VAL_SCROLL:
2181             o = OSCROLL; break;
2182         case CSS_VAL_AUTO:
2183             o = OAUTO; break;
2184         case CSS_VAL_OVERLAY:
2185             o = OOVERLAY; break;
2186         default:
2187             return;
2188         }
2189         style->setOverflowY(o);
2190         return;
2191     }
2192
2193     case CSS_PROP_PAGE_BREAK_BEFORE:
2194     {
2195         HANDLE_INHERIT_AND_INITIAL_WITH_VALUE(pageBreakBefore, PageBreakBefore, PageBreak)
2196         if (!primitiveValue) return;
2197         switch (primitiveValue->getIdent()) {
2198             case CSS_VAL_AUTO:
2199                 style->setPageBreakBefore(PBAUTO);
2200                 break;
2201             case CSS_VAL_LEFT:
2202             case CSS_VAL_RIGHT:
2203             case CSS_VAL_ALWAYS:
2204                 style->setPageBreakBefore(PBALWAYS); // CSS2.1: "Conforming user agents may map left/right to always."
2205                 break;
2206             case CSS_VAL_AVOID:
2207                 style->setPageBreakBefore(PBAVOID);
2208                 break;
2209         }
2210         break;
2211     }
2212
2213     case CSS_PROP_PAGE_BREAK_AFTER:
2214     {
2215         HANDLE_INHERIT_AND_INITIAL_WITH_VALUE(pageBreakAfter, PageBreakAfter, PageBreak)
2216         if (!primitiveValue) return;
2217         switch (primitiveValue->getIdent()) {
2218             case CSS_VAL_AUTO:
2219                 style->setPageBreakAfter(PBAUTO);
2220                 break;
2221             case CSS_VAL_LEFT:
2222             case CSS_VAL_RIGHT:
2223             case CSS_VAL_ALWAYS:
2224                 style->setPageBreakAfter(PBALWAYS); // CSS2.1: "Conforming user agents may map left/right to always."
2225                 break;
2226             case CSS_VAL_AVOID:
2227                 style->setPageBreakAfter(PBAVOID);
2228                 break;
2229         }
2230         break;
2231     }
2232
2233     case CSS_PROP_PAGE_BREAK_INSIDE: {
2234         HANDLE_INHERIT_AND_INITIAL_WITH_VALUE(pageBreakInside, PageBreakInside, PageBreak)
2235         if (!primitiveValue) return;
2236         if (primitiveValue->getIdent() == CSS_VAL_AUTO)
2237             style->setPageBreakInside(PBAUTO);
2238         else if (primitiveValue->getIdent() == CSS_VAL_AVOID)
2239             style->setPageBreakInside(PBAVOID);
2240         return;
2241     }
2242         
2243     case CSS_PROP_PAGE:
2244         break; /* FIXME: Not even sure what this is...  -dwh */
2245
2246     case CSS_PROP_POSITION:
2247     {
2248         HANDLE_INHERIT_AND_INITIAL(position, Position)
2249         if(!primitiveValue) return;
2250         EPosition p;
2251         switch(primitiveValue->getIdent())
2252         {
2253         case CSS_VAL_STATIC:
2254             p = StaticPosition;
2255             break;
2256         case CSS_VAL_RELATIVE:
2257             p = RelativePosition;
2258             break;
2259         case CSS_VAL_ABSOLUTE:
2260             p = AbsolutePosition;
2261             break;
2262         case CSS_VAL_FIXED:
2263             p = FixedPosition;
2264             break;
2265         default:
2266             return;
2267         }
2268         style->setPosition(p);
2269         return;
2270     }
2271
2272     case CSS_PROP_TABLE_LAYOUT: {
2273         HANDLE_INHERIT_AND_INITIAL(tableLayout, TableLayout)
2274
2275         if (!primitiveValue->getIdent())
2276             return;
2277
2278         ETableLayout l = RenderStyle::initialTableLayout();
2279         switch(primitiveValue->getIdent()) {
2280             case CSS_VAL_FIXED:
2281                 l = TFIXED;
2282                 // fall through
2283             case CSS_VAL_AUTO:
2284                 style->setTableLayout(l);
2285             default:
2286                 break;
2287         }
2288         break;
2289     }
2290         
2291     case CSS_PROP_UNICODE_BIDI: {
2292         HANDLE_INHERIT_AND_INITIAL(unicodeBidi, UnicodeBidi)
2293         switch (primitiveValue->getIdent()) {
2294             case CSS_VAL_NORMAL:
2295                 style->setUnicodeBidi(UBNormal); 
2296                 break;
2297             case CSS_VAL_EMBED:
2298                 style->setUnicodeBidi(Embed); 
2299                 break;
2300             case CSS_VAL_BIDI_OVERRIDE:
2301                 style->setUnicodeBidi(Override);
2302                 break;
2303             default:
2304                 return;
2305         }
2306         break;
2307     }
2308     case CSS_PROP_TEXT_TRANSFORM: {
2309         HANDLE_INHERIT_AND_INITIAL(textTransform, TextTransform)
2310         
2311         if(!primitiveValue->getIdent()) return;
2312
2313         ETextTransform tt;
2314         switch(primitiveValue->getIdent()) {
2315         case CSS_VAL_CAPITALIZE:  tt = CAPITALIZE;  break;
2316         case CSS_VAL_UPPERCASE:   tt = UPPERCASE;   break;
2317         case CSS_VAL_LOWERCASE:   tt = LOWERCASE;   break;
2318         case CSS_VAL_NONE:
2319         default:                  tt = TTNONE;      break;
2320         }
2321         style->setTextTransform(tt);
2322         break;
2323         }
2324
2325     case CSS_PROP_VISIBILITY:
2326     {
2327         HANDLE_INHERIT_AND_INITIAL(visibility, Visibility)
2328
2329         switch(primitiveValue->getIdent()) {
2330         case CSS_VAL_HIDDEN:
2331             style->setVisibility(HIDDEN);
2332             break;
2333         case CSS_VAL_VISIBLE:
2334             style->setVisibility(VISIBLE);
2335             break;
2336         case CSS_VAL_COLLAPSE:
2337             style->setVisibility(COLLAPSE);
2338         default:
2339             break;
2340         }
2341         break;
2342     }
2343     case CSS_PROP_WHITE_SPACE:
2344         HANDLE_INHERIT_AND_INITIAL(whiteSpace, WhiteSpace)
2345
2346         if(!primitiveValue->getIdent()) return;
2347
2348         EWhiteSpace s;
2349         switch(primitiveValue->getIdent()) {
2350         case CSS_VAL__WEBKIT_NOWRAP:
2351             s = KHTML_NOWRAP;
2352             break;
2353         case CSS_VAL_NOWRAP:
2354             s = NOWRAP;
2355             break;
2356         case CSS_VAL_PRE:
2357             s = PRE;
2358             break;
2359         case CSS_VAL_PRE_WRAP:
2360             s = PRE_WRAP;
2361             break;
2362         case CSS_VAL_PRE_LINE:
2363             s = PRE_LINE;
2364             break;
2365         case CSS_VAL_NORMAL:
2366         default:
2367             s = NORMAL;
2368             break;
2369         }
2370         style->setWhiteSpace(s);
2371         break;
2372
2373     case CSS_PROP_BACKGROUND_POSITION:
2374         HANDLE_BACKGROUND_INHERIT_AND_INITIAL(backgroundXPosition, BackgroundXPosition);
2375         HANDLE_BACKGROUND_INHERIT_AND_INITIAL(backgroundYPosition, BackgroundYPosition);
2376         break;
2377     case CSS_PROP_BACKGROUND_POSITION_X: {
2378         HANDLE_BACKGROUND_VALUE(backgroundXPosition, BackgroundXPosition, value)
2379         break;
2380     }
2381     case CSS_PROP_BACKGROUND_POSITION_Y: {
2382         HANDLE_BACKGROUND_VALUE(backgroundYPosition, BackgroundYPosition, value)
2383         break;
2384     }
2385     case CSS_PROP_BORDER_SPACING: {
2386         if (isInherit) {
2387             style->setHorizontalBorderSpacing(parentStyle->horizontalBorderSpacing());
2388             style->setVerticalBorderSpacing(parentStyle->verticalBorderSpacing());
2389         }
2390         else if (isInitial) {
2391             style->setHorizontalBorderSpacing(0);
2392             style->setVerticalBorderSpacing(0);
2393         }
2394         break;
2395     }
2396     case CSS_PROP__WEBKIT_BORDER_HORIZONTAL_SPACING: {
2397         HANDLE_INHERIT_AND_INITIAL(horizontalBorderSpacing, HorizontalBorderSpacing)
2398         if (!primitiveValue) break;
2399         short spacing =  primitiveValue->computeLengthShort(style);
2400         style->setHorizontalBorderSpacing(spacing);
2401         break;
2402     }
2403     case CSS_PROP__WEBKIT_BORDER_VERTICAL_SPACING: {
2404         HANDLE_INHERIT_AND_INITIAL(verticalBorderSpacing, VerticalBorderSpacing)
2405         if (!primitiveValue) break;
2406         short spacing =  primitiveValue->computeLengthShort(style);
2407         style->setVerticalBorderSpacing(spacing);
2408         break;
2409     }
2410     case CSS_PROP_CURSOR:
2411         if (isInherit) {
2412             style->setCursor(parentStyle->cursor());
2413             style->setCursorImage(parentStyle->cursorImage());
2414             return;
2415         }
2416         if (isInitial) {
2417             style->setCursor(RenderStyle::initialCursor());
2418             style->setCursorImage(0);
2419             return;
2420         }
2421         if (primitiveValue) {
2422             int type = primitiveValue->primitiveType();
2423             if (type == CSSPrimitiveValue::CSS_IDENT) {
2424                 style->setCursor((ECursor)(primitiveValue->getIdent() - CSS_VAL_AUTO));
2425                 style->setCursorImage(0);
2426             } else if (type == CSSPrimitiveValue::CSS_URI) {
2427                 CSSImageValue* image = static_cast<CSSImageValue*>(primitiveValue);
2428                 style->setCursor(CURSOR_AUTO);
2429                 style->setCursorImage(image->image(element->document()->docLoader()));
2430             }
2431         }
2432         break;
2433 // colors || inherit
2434     case CSS_PROP_BACKGROUND_COLOR:
2435     case CSS_PROP_BORDER_TOP_COLOR:
2436     case CSS_PROP_BORDER_RIGHT_COLOR:
2437     case CSS_PROP_BORDER_BOTTOM_COLOR:
2438     case CSS_PROP_BORDER_LEFT_COLOR:
2439     case CSS_PROP_COLOR:
2440     case CSS_PROP_OUTLINE_COLOR:
2441         // this property is an extension used to get HTML4 <font> right.
2442     {
2443         Color col;
2444         if (isInherit) {
2445             HANDLE_INHERIT_COND(CSS_PROP_BACKGROUND_COLOR, backgroundColor, BackgroundColor)
2446             HANDLE_INHERIT_COND(CSS_PROP_BORDER_TOP_COLOR, borderTopColor, BorderTopColor)
2447             HANDLE_INHERIT_COND(CSS_PROP_BORDER_BOTTOM_COLOR, borderBottomColor, BorderBottomColor)
2448             HANDLE_INHERIT_COND(CSS_PROP_BORDER_RIGHT_COLOR, borderRightColor, BorderRightColor)
2449             HANDLE_INHERIT_COND(CSS_PROP_BORDER_LEFT_COLOR, borderLeftColor, BorderLeftColor)
2450             HANDLE_INHERIT_COND(CSS_PROP_COLOR, color, Color)
2451             HANDLE_INHERIT_COND(CSS_PROP_OUTLINE_COLOR, outlineColor, OutlineColor)
2452             return;
2453         }
2454         if (isInitial) {
2455             // The border/outline colors will just map to the invalid color |col| above.  This will have the
2456             // effect of forcing the use of the currentColor when it comes time to draw the borders (and of
2457             // not painting the background since the color won't be valid).
2458             if (id == CSS_PROP_COLOR)
2459                 col = RenderStyle::initialColor();
2460         } else {
2461             if (!primitiveValue)
2462                 return;
2463             col = getColorFromPrimitiveValue(primitiveValue);
2464         }
2465
2466         switch(id) {
2467         case CSS_PROP_BACKGROUND_COLOR:
2468             style->setBackgroundColor(col); break;
2469         case CSS_PROP_BORDER_TOP_COLOR:
2470             style->setBorderTopColor(col); break;
2471         case CSS_PROP_BORDER_RIGHT_COLOR:
2472             style->setBorderRightColor(col); break;
2473         case CSS_PROP_BORDER_BOTTOM_COLOR:
2474             style->setBorderBottomColor(col); break;
2475         case CSS_PROP_BORDER_LEFT_COLOR:
2476             style->setBorderLeftColor(col); break;
2477         case CSS_PROP_COLOR:
2478             style->setColor(col); break;
2479         case CSS_PROP_OUTLINE_COLOR:
2480             style->setOutlineColor(col); break;
2481         }
2482         return;
2483     }
2484     break;
2485     
2486 // uri || inherit
2487     case CSS_PROP_BACKGROUND_IMAGE:
2488         HANDLE_BACKGROUND_VALUE(backgroundImage, BackgroundImage, value)
2489         break;
2490     case CSS_PROP_LIST_STYLE_IMAGE:
2491     {
2492         HANDLE_INHERIT_AND_INITIAL(listStyleImage, ListStyleImage)
2493         if (!primitiveValue)
2494             return;
2495         style->setListStyleImage(static_cast<CSSImageValue*>(primitiveValue)->image(element->document()->docLoader()));
2496         break;
2497     }
2498
2499 // length
2500     case CSS_PROP_BORDER_TOP_WIDTH:
2501     case CSS_PROP_BORDER_RIGHT_WIDTH:
2502     case CSS_PROP_BORDER_BOTTOM_WIDTH:
2503     case CSS_PROP_BORDER_LEFT_WIDTH:
2504     case CSS_PROP_OUTLINE_WIDTH:
2505     {
2506         if (isInherit) {
2507             HANDLE_INHERIT_COND(CSS_PROP_BORDER_TOP_WIDTH, borderTopWidth, BorderTopWidth)
2508             HANDLE_INHERIT_COND(CSS_PROP_BORDER_RIGHT_WIDTH, borderRightWidth, BorderRightWidth)
2509             HANDLE_INHERIT_COND(CSS_PROP_BORDER_BOTTOM_WIDTH, borderBottomWidth, BorderBottomWidth)
2510             HANDLE_INHERIT_COND(CSS_PROP_BORDER_LEFT_WIDTH, borderLeftWidth, BorderLeftWidth)
2511             HANDLE_INHERIT_COND(CSS_PROP_OUTLINE_WIDTH, outlineWidth, OutlineWidth)
2512             return;
2513         }
2514         else if (isInitial) {
2515             HANDLE_INITIAL_COND_WITH_VALUE(CSS_PROP_BORDER_TOP_WIDTH, BorderTopWidth, BorderWidth)
2516             HANDLE_INITIAL_COND_WITH_VALUE(CSS_PROP_BORDER_RIGHT_WIDTH, BorderRightWidth, BorderWidth)
2517             HANDLE_INITIAL_COND_WITH_VALUE(CSS_PROP_BORDER_BOTTOM_WIDTH, BorderBottomWidth, BorderWidth)
2518             HANDLE_INITIAL_COND_WITH_VALUE(CSS_PROP_BORDER_LEFT_WIDTH, BorderLeftWidth, BorderWidth)
2519             HANDLE_INITIAL_COND_WITH_VALUE(CSS_PROP_OUTLINE_WIDTH, OutlineWidth, BorderWidth)
2520             return;
2521         }
2522
2523         if(!primitiveValue) break;
2524         short width = 3;
2525         switch(primitiveValue->getIdent())
2526         {
2527         case CSS_VAL_THIN:
2528             width = 1;
2529             break;
2530         case CSS_VAL_MEDIUM:
2531             width = 3;
2532             break;
2533         case CSS_VAL_THICK:
2534             width = 5;
2535             break;
2536         case CSS_VAL_INVALID:
2537             width = primitiveValue->computeLengthShort(style);
2538             break;
2539         default:
2540             return;
2541         }
2542
2543         if(width < 0) return;
2544         switch(id)
2545         {
2546         case CSS_PROP_BORDER_TOP_WIDTH:
2547             style->setBorderTopWidth(width);
2548             break;
2549         case CSS_PROP_BORDER_RIGHT_WIDTH:
2550             style->setBorderRightWidth(width);
2551             break;
2552         case CSS_PROP_BORDER_BOTTOM_WIDTH:
2553             style->setBorderBottomWidth(width);
2554             break;
2555         case CSS_PROP_BORDER_LEFT_WIDTH:
2556             style->setBorderLeftWidth(width);
2557             break;
2558         case CSS_PROP_OUTLINE_WIDTH:
2559             style->setOutlineWidth(width);
2560             break;
2561         default:
2562             return;
2563         }
2564         return;
2565     }
2566
2567     case CSS_PROP_LETTER_SPACING:
2568     case CSS_PROP_WORD_SPACING:
2569     {
2570         
2571         if (isInherit) {
2572             HANDLE_INHERIT_COND(CSS_PROP_LETTER_SPACING, letterSpacing, LetterSpacing)
2573             HANDLE_INHERIT_COND(CSS_PROP_WORD_SPACING, wordSpacing, WordSpacing)
2574             return;
2575         }
2576         else if (isInitial) {
2577             HANDLE_INITIAL_COND_WITH_VALUE(CSS_PROP_LETTER_SPACING, LetterSpacing, LetterWordSpacing)
2578             HANDLE_INITIAL_COND_WITH_VALUE(CSS_PROP_WORD_SPACING, WordSpacing, LetterWordSpacing)
2579             return;
2580         }
2581         
2582         int width = 0;
2583         if (primitiveValue && primitiveValue->getIdent() == CSS_VAL_NORMAL){
2584             width = 0;
2585         } else {
2586             if(!primitiveValue) return;
2587             width = primitiveValue->computeLengthInt(style);
2588         }
2589         switch(id)
2590         {
2591         case CSS_PROP_LETTER_SPACING:
2592             style->setLetterSpacing(width);
2593             break;
2594         case CSS_PROP_WORD_SPACING:
2595             style->setWordSpacing(width);
2596             break;
2597             // ### needs the definitions in renderstyle
2598         default: break;
2599         }
2600         return;
2601     }
2602
2603     case CSS_PROP_WORD_WRAP:
2604     {
2605         HANDLE_INHERIT_AND_INITIAL(wordWrap, WordWrap)
2606
2607         if(!primitiveValue->getIdent()) return;
2608
2609         EWordWrap s;
2610         switch(primitiveValue->getIdent()) {
2611         case CSS_VAL_BREAK_WORD:
2612             s = BREAK_WORD;
2613             break;
2614         case CSS_VAL_NORMAL:
2615         default:
2616             s = WBNORMAL;
2617             break;
2618         }
2619         style->setWordWrap(s);
2620         break;
2621     }
2622
2623     case CSS_PROP__WEBKIT_NBSP_MODE:
2624     {
2625         HANDLE_INHERIT_AND_INITIAL(nbspMode, NBSPMode)
2626
2627         if (!primitiveValue->getIdent()) return;
2628
2629         ENBSPMode m;
2630         switch(primitiveValue->getIdent()) {
2631         case CSS_VAL_SPACE:
2632             m = SPACE;
2633             break;
2634         case CSS_VAL_NORMAL:
2635         default:
2636             m = NBNORMAL;
2637             break;
2638         }
2639         style->setNBSPMode(m);
2640         break;
2641     }
2642
2643     case CSS_PROP__WEBKIT_LINE_BREAK:
2644     {
2645         HANDLE_INHERIT_AND_INITIAL(khtmlLineBreak, KHTMLLineBreak)
2646
2647         if (!primitiveValue->getIdent()) return;
2648
2649         EKHTMLLineBreak b;
2650         switch(primitiveValue->getIdent()) {
2651         case CSS_VAL_AFTER_WHITE_SPACE:
2652             b = AFTER_WHITE_SPACE;
2653             break;
2654         case CSS_VAL_NORMAL:
2655         default:
2656             b = LBNORMAL;
2657             break;
2658         }
2659         style->setKHTMLLineBreak(b);
2660         break;
2661     }
2662
2663     case CSS_PROP__WEBKIT_MATCH_NEAREST_MAIL_BLOCKQUOTE_COLOR:
2664     {
2665         HANDLE_INHERIT_AND_INITIAL(matchNearestMailBlockquoteColor, MatchNearestMailBlockquoteColor)
2666
2667         if (!primitiveValue->getIdent()) return;
2668
2669         EMatchNearestMailBlockquoteColor c;
2670         switch(primitiveValue->getIdent()) {
2671         case CSS_VAL_NORMAL:
2672             c = BCNORMAL;
2673             break;
2674         case CSS_VAL_MATCH:
2675         default:
2676             c = MATCH;
2677             break;
2678         }
2679         style->setMatchNearestMailBlockquoteColor(c);
2680         break;
2681     }
2682
2683     case CSS_PROP_RESIZE:
2684     {
2685         HANDLE_INHERIT_AND_INITIAL(resize, Resize)
2686
2687         if (!primitiveValue->getIdent()) return;
2688
2689         EResize r;
2690         switch(primitiveValue->getIdent()) {
2691         case CSS_VAL_BOTH:
2692             r = RESIZE_BOTH;
2693             break;
2694         case CSS_VAL_HORIZONTAL:
2695             r = RESIZE_HORIZONTAL;
2696             break;
2697         case CSS_VAL_VERTICAL:
2698             r = RESIZE_VERTICAL;
2699             break;
2700         case CSS_VAL_AUTO:
2701             r = settings->textAreasAreResizable() ? RESIZE_BOTH : RESIZE_NONE;
2702             break;
2703         case CSS_VAL_NONE:
2704         default:
2705             r = RESIZE_NONE;
2706             break;
2707         }
2708         style->setResize(r);
2709         break;
2710     }
2711     
2712     // length, percent
2713     case CSS_PROP_MAX_WIDTH:
2714         // +none +inherit
2715         if(primitiveValue && primitiveValue->getIdent() == CSS_VAL_NONE)
2716             apply = true;
2717     case CSS_PROP_TOP:
2718     case CSS_PROP_LEFT:
2719     case CSS_PROP_RIGHT:
2720     case CSS_PROP_BOTTOM:
2721     case CSS_PROP_WIDTH:
2722     case CSS_PROP_MIN_WIDTH:
2723     case CSS_PROP_MARGIN_TOP:
2724     case CSS_PROP_MARGIN_RIGHT:
2725     case CSS_PROP_MARGIN_BOTTOM:
2726     case CSS_PROP_MARGIN_LEFT:
2727         // +inherit +auto
2728         if (id == CSS_PROP_WIDTH || id == CSS_PROP_MIN_WIDTH || id == CSS_PROP_MAX_WIDTH) {
2729             if (primitiveValue && primitiveValue->getIdent() == CSS_VAL_INTRINSIC) {
2730                 l = Length(Intrinsic);
2731                 apply = true;
2732             }
2733             else if (primitiveValue && primitiveValue->getIdent() == CSS_VAL_MIN_INTRINSIC) {
2734                 l = Length(MinIntrinsic);
2735                 apply = true;
2736             }
2737         }
2738         if (id != CSS_PROP_MAX_WIDTH && primitiveValue && primitiveValue->getIdent() == CSS_VAL_AUTO)
2739             apply = true;
2740     case CSS_PROP_PADDING_TOP:
2741     case CSS_PROP_PADDING_RIGHT:
2742     case CSS_PROP_PADDING_BOTTOM:
2743     case CSS_PROP_PADDING_LEFT:
2744     case CSS_PROP_TEXT_INDENT:
2745         // +inherit
2746     {
2747         if (isInherit) {
2748             HANDLE_INHERIT_COND(CSS_PROP_MAX_WIDTH, maxWidth, MaxWidth)
2749             HANDLE_INHERIT_COND(CSS_PROP_BOTTOM, bottom, Bottom)
2750             HANDLE_INHERIT_COND(CSS_PROP_TOP, top, Top)
2751             HANDLE_INHERIT_COND(CSS_PROP_LEFT, left, Left)
2752             HANDLE_INHERIT_COND(CSS_PROP_RIGHT, right, Right)
2753             HANDLE_INHERIT_COND(CSS_PROP_WIDTH, width, Width)
2754             HANDLE_INHERIT_COND(CSS_PROP_MIN_WIDTH, minWidth, MinWidth)
2755             HANDLE_INHERIT_COND(CSS_PROP_PADDING_TOP, paddingTop, PaddingTop)
2756             HANDLE_INHERIT_COND(CSS_PROP_PADDING_RIGHT, paddingRight, PaddingRight)
2757             HANDLE_INHERIT_COND(CSS_PROP_PADDING_BOTTOM, paddingBottom, PaddingBottom)
2758             HANDLE_INHERIT_COND(CSS_PROP_PADDING_LEFT, paddingLeft, PaddingLeft)
2759             HANDLE_INHERIT_COND(CSS_PROP_MARGIN_TOP, marginTop, MarginTop)
2760             HANDLE_INHERIT_COND(CSS_PROP_MARGIN_RIGHT, marginRight, MarginRight)
2761             HANDLE_INHERIT_COND(CSS_PROP_MARGIN_BOTTOM, marginBottom, MarginBottom)
2762             HANDLE_INHERIT_COND(CSS_PROP_MARGIN_LEFT, marginLeft, MarginLeft)
2763             HANDLE_INHERIT_COND(CSS_PROP_TEXT_INDENT, textIndent, TextIndent)
2764             return;
2765         }
2766         else if (isInitial) {
2767             HANDLE_INITIAL_COND_WITH_VALUE(CSS_PROP_MAX_WIDTH, MaxWidth, MaxSize)
2768             HANDLE_INITIAL_COND_WITH_VALUE(CSS_PROP_BOTTOM, Bottom, Offset)
2769             HANDLE_INITIAL_COND_WITH_VALUE(CSS_PROP_TOP, Top, Offset)
2770             HANDLE_INITIAL_COND_WITH_VALUE(CSS_PROP_LEFT, Left, Offset)
2771             HANDLE_INITIAL_COND_WITH_VALUE(CSS_PROP_RIGHT, Right, Offset)
2772             HANDLE_INITIAL_COND_WITH_VALUE(CSS_PROP_WIDTH, Width, Size)
2773             HANDLE_INITIAL_COND_WITH_VALUE(CSS_PROP_MIN_WIDTH, MinWidth, MinSize)
2774             HANDLE_INITIAL_COND_WITH_VALUE(CSS_PROP_PADDING_TOP, PaddingTop, Padding)
2775             HANDLE_INITIAL_COND_WITH_VALUE(CSS_PROP_PADDING_RIGHT, PaddingRight, Padding)
2776             HANDLE_INITIAL_COND_WITH_VALUE(CSS_PROP_PADDING_BOTTOM, PaddingBottom, Padding)
2777             HANDLE_INITIAL_COND_WITH_VALUE(CSS_PROP_PADDING_LEFT, PaddingLeft, Padding)
2778             HANDLE_INITIAL_COND_WITH_VALUE(CSS_PROP_MARGIN_TOP, MarginTop, Margin)
2779             HANDLE_INITIAL_COND_WITH_VALUE(CSS_PROP_MARGIN_RIGHT, MarginRight, Margin)
2780             HANDLE_INITIAL_COND_WITH_VALUE(CSS_PROP_MARGIN_BOTTOM, MarginBottom, Margin)
2781             HANDLE_INITIAL_COND_WITH_VALUE(CSS_PROP_MARGIN_LEFT, MarginLeft, Margin)
2782             HANDLE_INITIAL_COND(CSS_PROP_TEXT_INDENT, TextIndent)
2783             return;
2784         } 
2785
2786         if (primitiveValue && !apply) {
2787             int type = primitiveValue->primitiveType();
2788             if(type > CSSPrimitiveValue::CSS_PERCENTAGE && type < CSSPrimitiveValue::CSS_DEG)
2789                 // Handle our quirky margin units if we have them.
2790                 l = Length(primitiveValue->computeLengthIntForLength(style), Fixed, 
2791                            primitiveValue->isQuirkValue());
2792             else if(type == CSSPrimitiveValue::CSS_PERCENTAGE)
2793                 l = Length((int)primitiveValue->getFloatValue(), Percent);
2794             else
2795                 return;
2796             if (id == CSS_PROP_PADDING_LEFT || id == CSS_PROP_PADDING_RIGHT ||
2797                 id == CSS_PROP_PADDING_TOP || id == CSS_PROP_PADDING_BOTTOM)
2798                 // Padding can't be negative
2799                 apply = !((l.isFixed() || l.isPercent()) && l.calcValue(100) < 0);
2800             else
2801                 apply = true;
2802         }
2803         if(!apply) return;
2804         switch(id)
2805             {
2806             case CSS_PROP_MAX_WIDTH:
2807                 style->setMaxWidth(l); break;
2808             case CSS_PROP_BOTTOM:
2809                 style->setBottom(l); break;
2810             case CSS_PROP_TOP:
2811                 style->setTop(l); break;
2812             case CSS_PROP_LEFT:
2813                 style->setLeft(l); break;
2814             case CSS_PROP_RIGHT:
2815                 style->setRight(l); break;
2816             case CSS_PROP_WIDTH:
2817                 style->setWidth(l); break;
2818             case CSS_PROP_MIN_WIDTH:
2819                 style->setMinWidth(l); break;
2820             case CSS_PROP_PADDING_TOP:
2821                 style->setPaddingTop(l); break;
2822             case CSS_PROP_PADDING_RIGHT:
2823                 style->setPaddingRight(l); break;
2824             case CSS_PROP_PADDING_BOTTOM:
2825                 style->setPaddingBottom(l); break;
2826             case CSS_PROP_PADDING_LEFT:
2827                 style->setPaddingLeft(l); break;
2828             case CSS_PROP_MARGIN_TOP:
2829                 style->setMarginTop(l); break;
2830             case CSS_PROP_MARGIN_RIGHT:
2831                 style->setMarginRight(l); break;
2832             case CSS_PROP_MARGIN_BOTTOM:
2833                 style->setMarginBottom(l); break;
2834             case CSS_PROP_MARGIN_LEFT:
2835                 style->setMarginLeft(l); break;
2836             case CSS_PROP_TEXT_INDENT:
2837                 style->setTextIndent(l); break;
2838             default: break;
2839             }
2840         return;
2841     }
2842
2843     case CSS_PROP_MAX_HEIGHT:
2844         if (primitiveValue && primitiveValue->getIdent() == CSS_VAL_NONE) {
2845             l = Length(undefinedLength, Fixed);
2846             apply = true;
2847         }
2848     case CSS_PROP_HEIGHT:
2849     case CSS_PROP_MIN_HEIGHT:
2850         if (primitiveValue && primitiveValue->getIdent() == CSS_VAL_INTRINSIC) {
2851             l = Length(Intrinsic);
2852             apply = true;
2853         } else if (primitiveValue && primitiveValue->getIdent() == CSS_VAL_MIN_INTRINSIC) {
2854             l = Length(MinIntrinsic);
2855             apply = true;
2856         } else if (id != CSS_PROP_MAX_HEIGHT && primitiveValue && primitiveValue->getIdent() == CSS_VAL_AUTO)
2857             apply = true;
2858         if (isInherit) {
2859             HANDLE_INHERIT_COND(CSS_PROP_MAX_HEIGHT, maxHeight, MaxHeight)
2860             HANDLE_INHERIT_COND(CSS_PROP_HEIGHT, height, Height)
2861             HANDLE_INHERIT_COND(CSS_PROP_MIN_HEIGHT, minHeight, MinHeight)
2862             return;
2863         }
2864         if (isInitial) {
2865             HANDLE_INITIAL_COND_WITH_VALUE(CSS_PROP_MAX_HEIGHT, MaxHeight, MaxSize)
2866             HANDLE_INITIAL_COND_WITH_VALUE(CSS_PROP_HEIGHT, Height, Size)
2867             HANDLE_INITIAL_COND_WITH_VALUE(CSS_PROP_MIN_HEIGHT, MinHeight, MinSize)
2868             return;
2869         }
2870
2871         if (primitiveValue && !apply) {
2872             unsigned short type = primitiveValue->primitiveType();
2873             if (type > CSSPrimitiveValue::CSS_PERCENTAGE && type < CSSPrimitiveValue::CSS_DEG)
2874                 l = Length(primitiveValue->computeLengthIntForLength(style), Fixed);
2875             else if (type == CSSPrimitiveValue::CSS_PERCENTAGE)
2876                 l = Length((int)primitiveValue->getFloatValue(), Percent);
2877             else
2878                 return;
2879             apply = true;
2880         }
2881         if (apply)
2882             switch (id) {
2883                 case CSS_PROP_MAX_HEIGHT:
2884                     style->setMaxHeight(l);
2885                     break;
2886                 case CSS_PROP_HEIGHT:
2887                     style->setHeight(l);
2888                     break;
2889                 case CSS_PROP_MIN_HEIGHT:
2890                     style->setMinHeight(l);
2891                     break;
2892             }
2893         break;
2894
2895     case CSS_PROP_VERTICAL_ALIGN:
2896         HANDLE_INHERIT_AND_INITIAL(verticalAlign, VerticalAlign)
2897         if (!primitiveValue) return;
2898         if (primitiveValue->getIdent()) {
2899           EVerticalAlign align;
2900
2901           switch(primitiveValue->getIdent())
2902             {
2903                 case CSS_VAL_TOP:
2904                     align = TOP; break;
2905                 case CSS_VAL_BOTTOM:
2906                     align = BOTTOM; break;
2907                 case CSS_VAL_MIDDLE:
2908                     align = MIDDLE; break;
2909                 case CSS_VAL_BASELINE:
2910                     align = BASELINE; break;
2911                 case CSS_VAL_TEXT_BOTTOM:
2912                     align = TEXT_BOTTOM; break;
2913                 case CSS_VAL_TEXT_TOP:
2914                     align = TEXT_TOP; break;
2915                 case CSS_VAL_SUB:
2916                     align = SUB; break;
2917                 case CSS_VAL_SUPER:
2918                     align = SUPER; break;
2919                 case CSS_VAL__WEBKIT_BASELINE_MIDDLE:
2920                     align = BASELINE_MIDDLE; break;
2921                 default:
2922                     return;
2923             }
2924           style->setVerticalAlign(align);
2925           return;
2926         } else {
2927           int type = primitiveValue->primitiveType();
2928           Length l;
2929           if(type > CSSPrimitiveValue::CSS_PERCENTAGE && type < CSSPrimitiveValue::CSS_DEG)
2930             l = Length(primitiveValue->computeLengthIntForLength(style), Fixed);
2931           else if(type == CSSPrimitiveValue::CSS_PERCENTAGE)
2932             l = Length(int(primitiveValue->getFloatValue()), Percent);
2933
2934           style->setVerticalAlign(LENGTH);
2935           style->setVerticalAlignLength(l);
2936         }
2937         break;
2938
2939     case CSS_PROP_FONT_SIZE:
2940     {
2941         FontDescription fontDescription = style->fontDescription();
2942         float oldSize = 0;
2943         float size = 0;
2944         
2945         bool parentIsAbsoluteSize = false;
2946         if (parentNode) {
2947             oldSize = parentStyle->fontDescription().specifiedSize();
2948             parentIsAbsoluteSize = parentStyle->fontDescription().isAbsoluteSize();
2949         }
2950
2951         if (isInherit)
2952             size = oldSize;
2953         else if (isInitial)
2954             size = fontSizeForKeyword(CSS_VAL_MEDIUM, style->htmlHacks());
2955         else if (primitiveValue->getIdent()) {
2956             // Keywords are being used.
2957             switch (primitiveValue->getIdent()) {
2958             case CSS_VAL_XX_SMALL:
2959             case CSS_VAL_X_SMALL:
2960             case CSS_VAL_SMALL:
2961             case CSS_VAL_MEDIUM:
2962             case CSS_VAL_LARGE:
2963             case CSS_VAL_X_LARGE:
2964             case CSS_VAL_XX_LARGE:
2965             case CSS_VAL__WEBKIT_XXX_LARGE:
2966                 size = fontSizeForKeyword(primitiveValue->getIdent(), style->htmlHacks());
2967                 break;
2968             case CSS_VAL_LARGER:
2969                 size = largerFontSize(oldSize, style->htmlHacks());
2970                 break;
2971             case CSS_VAL_SMALLER:
2972                 size = smallerFontSize(oldSize, style->htmlHacks());
2973                 break;
2974             default:
2975                 return;
2976             }
2977
2978             fontDescription.setIsAbsoluteSize(parentIsAbsoluteSize && 
2979                                               (primitiveValue->getIdent() == CSS_VAL_LARGER ||
2980                                                primitiveValue->getIdent() == CSS_VAL_SMALLER));
2981         } else {
2982             int type = primitiveValue->primitiveType();
2983             fontDescription.setIsAbsoluteSize(parentIsAbsoluteSize ||
2984                                               (type != CSSPrimitiveValue::CSS_PERCENTAGE &&
2985                                                type != CSSPrimitiveValue::CSS_EMS && 
2986                                                type != CSSPrimitiveValue::CSS_EXS));
2987             if (type > CSSPrimitiveValue::CSS_PERCENTAGE && type < CSSPrimitiveValue::CSS_DEG)
2988                 size = primitiveValue->computeLengthFloat(parentStyle, false);
2989             else if(type == CSSPrimitiveValue::CSS_PERCENTAGE)
2990                 size = (primitiveValue->getFloatValue() * oldSize) / 100;
2991             else
2992                 return;
2993         }
2994
2995         if (size <= 0)
2996             return;
2997
2998         setFontSize(fontDescription, size);
2999         if (style->setFontDescription(fontDescription))
3000             fontDirty = true;
3001         return;
3002     }
3003         break;
3004     case CSS_PROP_Z_INDEX:
3005     {
3006         HANDLE_INHERIT(zIndex, ZIndex)
3007         else if (isInitial) {
3008             style->setHasAutoZIndex();
3009             return;
3010         }
3011         
3012         if (!primitiveValue)
3013             return;
3014
3015         if (primitiveValue->getIdent() == CSS_VAL_AUTO) {
3016             style->setHasAutoZIndex();
3017             return;
3018         }
3019         
3020         if (primitiveValue->primitiveType() != CSSPrimitiveValue::CSS_NUMBER)
3021             return; // Error case.
3022         
3023         style->setZIndex((int)primitiveValue->getFloatValue());
3024         
3025         return;
3026     }
3027         
3028     case CSS_PROP_WIDOWS:
3029     {
3030         HANDLE_INHERIT_AND_INITIAL(widows, Widows)
3031         if (!primitiveValue || primitiveValue->primitiveType() != CSSPrimitiveValue::CSS_NUMBER)
3032             return;
3033         style->setWidows((int)primitiveValue->getFloatValue());
3034         break;
3035     }
3036         
3037     case CSS_PROP_ORPHANS:
3038     {
3039         HANDLE_INHERIT_AND_INITIAL(orphans, Orphans)
3040         if (!primitiveValue || primitiveValue->primitiveType() != CSSPrimitiveValue::CSS_NUMBER)
3041             return;
3042         style->setOrphans((int)primitiveValue->getFloatValue());
3043         break;
3044     }        
3045
3046 // length, percent, number
3047     case CSS_PROP_LINE_HEIGHT:
3048     {
3049         HANDLE_INHERIT_AND_INITIAL(lineHeight, LineHeight)
3050         if(!primitiveValue) return;
3051         Length lineHeight;
3052         int type = primitiveValue->primitiveType();
3053         if (primitiveValue->getIdent() == CSS_VAL_NORMAL)
3054             lineHeight = Length(-100, Percent);
3055         else if (type > CSSPrimitiveValue::CSS_PERCENTAGE && type < CSSPrimitiveValue::CSS_DEG) {
3056             double multiplier = 1.0;
3057             // Scale for the font zoom factor only for types other than "em" and "ex", since those are
3058             // already based on the font size.
3059             if (type != CSSPrimitiveValue::CSS_EMS && type != CSSPrimitiveValue::CSS_EXS && view && view->frame()) {
3060                 multiplier = view->frame()->zoomFactor() / 100.0;
3061             }
3062             lineHeight = Length(primitiveValue->computeLengthIntForLength(style, multiplier), Fixed);
3063         } else if (type == CSSPrimitiveValue::CSS_PERCENTAGE)
3064             lineHeight = Length((style->fontSize() * int(primitiveValue->getFloatValue())) / 100, Fixed);
3065         else if (type == CSSPrimitiveValue::CSS_NUMBER)
3066             lineHeight = Length(int(primitiveValue->getFloatValue() * 100), Percent);
3067         else
3068             return;
3069         style->setLineHeight(lineHeight);
3070         return;
3071     }
3072
3073 // string
3074     case CSS_PROP_TEXT_ALIGN:
3075     {
3076         HANDLE_INHERIT_AND_INITIAL(textAlign, TextAlign)
3077         if (!primitiveValue) return;
3078         if (primitiveValue->getIdent())
3079             style->setTextAlign((ETextAlign) (primitiveValue->getIdent() - CSS_VAL__WEBKIT_AUTO));
3080         return;
3081     }
3082
3083 // rect
3084     case CSS_PROP_CLIP:
3085     {
3086         Length top;
3087         Length right;
3088         Length bottom;
3089         Length left;
3090         bool hasClip = true;
3091         if (isInherit) {
3092             if (parentStyle->hasClip()) {
3093                 top = parentStyle->clipTop();
3094                 right = parentStyle->clipRight();
3095                 bottom = parentStyle->clipBottom();
3096                 left = parentStyle->clipLeft();
3097             }
3098             else {
3099                 hasClip = false;
3100                 top = right = bottom = left = Length();
3101             }
3102         } else if (isInitial) {
3103             hasClip = false;
3104             top = right = bottom = left = Length();
3105         } else if (!primitiveValue) {
3106             break;
3107         } else if (primitiveValue->primitiveType() == CSSPrimitiveValue::CSS_RECT) {
3108             RectImpl* rect = primitiveValue->getRectValue();
3109             if (!rect)
3110                 break;
3111             top = convertToLength(rect->top(), style);
3112             right = convertToLength(rect->right(), style);
3113             bottom = convertToLength(rect->bottom(), style);
3114             left = convertToLength(rect->left(), style);
3115
3116         } else if (primitiveValue->getIdent() != CSS_VAL_AUTO) {
3117             break;
3118         }
3119         style->setClip(top, right, bottom, left);
3120         style->setHasClip(hasClip);
3121     
3122         // rect, ident
3123         break;
3124     }
3125
3126 // lists
3127     case CSS_PROP_CONTENT:
3128         // list of string, uri, counter, attr, i
3129     {
3130         // FIXME: In CSS3, it will be possible to inherit content.  In CSS2 it is not.  This
3131         // note is a reminder that eventually "inherit" needs to be supported.
3132
3133         if (isInitial) {
3134             if (style->contentData())
3135                 style->contentData()->clearContent();
3136             return;
3137         }
3138         
3139         if (!value->isValueList()) return;
3140         CSSValueList *list = static_cast<CSSValueList*>(value);
3141         int len = list->length();
3142
3143         for (int i = 0; i < len; i++) {
3144             CSSValue *item = list->item(i);
3145             if (!item->isPrimitiveValue()) continue;
3146             CSSPrimitiveValue *val = static_cast<CSSPrimitiveValue*>(item);
3147             if (val->primitiveType()==CSSPrimitiveValue::CSS_STRING)
3148                 style->setContent(val->getStringValue().impl(), i != 0);
3149             else if (val->primitiveType()==CSSPrimitiveValue::CSS_ATTR) {
3150                 // FIXME: Can a namespace be specified for an attr(foo)?
3151                 if (style->styleType() == RenderStyle::NOPSEUDO)
3152                     style->setUnique();
3153                 else
3154                     parentStyle->setUnique();
3155                 QualifiedName attr(nullAtom, val->getStringValue().impl(), nullAtom);
3156                 style->setContent(element->getAttribute(attr).impl(), i != 0);
3157                 // register the fact that the attribute value affects the style
3158                 m_selectorAttrs.add(attr.localName().impl());
3159             }
3160             else if (val->primitiveType()==CSSPrimitiveValue::CSS_URI) {
3161                 CSSImageValue *image = static_cast<CSSImageValue*>(val);
3162                 style->setContent(image->image(element->document()->docLoader()), i != 0);
3163             }
3164         }
3165         break;
3166     }
3167
3168     case CSS_PROP_COUNTER_INCREMENT:
3169         // list of CSS2CounterIncrement
3170     case CSS_PROP_COUNTER_RESET:
3171         // list of CSS2CounterReset
3172         break;
3173     case CSS_PROP_FONT_FAMILY: {
3174         // list of strings and ids
3175         if (isInherit) {
3176             FontDescription parentFontDescription = parentStyle->fontDescription();
3177             FontDescription fontDescription = style->fontDescription();
3178             fontDescription.setGenericFamily(parentFontDescription.genericFamily());
3179             fontDescription.setFamily(parentFontDescription.firstFamily());
3180             if (style->setFontDescription(fontDescription))
3181                 fontDirty = true;
3182             return;
3183         }
3184         else if (isInitial) {
3185             FontDescription initialDesc = FontDescription();
3186             FontDescription fontDescription = style->fontDescription();
3187             fontDescription.setGenericFamily(initialDesc.genericFamily());
3188             fontDescription.setFamily(initialDesc.firstFamily());
3189             if (style->setFontDescription(fontDescription))
3190                 fontDirty = true;
3191             return;
3192         }
3193         
3194         if (!value->isValueList()) return;
3195         FontDescription fontDescription = style->fontDescription();
3196         CSSValueList *list = static_cast<CSSValueList*>(value);
3197         int len = list->length();
3198         FontFamily& firstFamily = fontDescription.firstFamily();
3199         FontFamily *currFamily = 0;
3200         
3201         // Before mapping in a new font-family property, we should reset the generic family.
3202         fontDescription.setGenericFamily(FontDescription::NoFamily);
3203
3204         for(int i = 0; i < len; i++) {
3205             CSSValue *item = list->item(i);
3206             if(!item->isPrimitiveValue()) continue;
3207             CSSPrimitiveValue *val = static_cast<CSSPrimitiveValue*>(item);
3208             AtomicString face;
3209             if(val->primitiveType() == CSSPrimitiveValue::CSS_STRING)
3210                 face = static_cast<FontFamilyValue*>(val)->fontName();
3211             else if (val->primitiveType() == CSSPrimitiveValue::CSS_IDENT) {
3212                 switch (val->getIdent()) {
3213                     case CSS_VAL__WEBKIT_BODY:
3214                         face = settings->stdFontName();
3215                         break;
3216                     case CSS_VAL_SERIF:
3217                         face = settings->serifFontName();
3218                         fontDescription.setGenericFamily(FontDescription::SerifFamily);
3219                         break;
3220                     case CSS_VAL_SANS_SERIF:
3221                         face = settings->sansSerifFontName();
3222                         fontDescription.setGenericFamily(FontDescription::SansSerifFamily);
3223                         break;
3224                     case CSS_VAL_CURSIVE:
3225                         face = settings->cursiveFontName();
3226                         fontDescription.setGenericFamily(FontDescription::CursiveFamily);
3227                         break;
3228                     case CSS_VAL_FANTASY:
3229                         face = settings->fantasyFontName();
3230                         fontDescription.setGenericFamily(FontDescription::FantasyFamily);
3231                         break;
3232                     case CSS_VAL_MONOSPACE:
3233                         face = settings->fixedFontName();
3234                         fontDescription.setGenericFamily(FontDescription::MonospaceFamily);
3235                         break;
3236                 }
3237             }
3238     
3239             if (!face.isEmpty()) {
3240                 if (!currFamily) {
3241                     // Filling in the first family.
3242                     firstFamily.setFamily(face);
3243                     currFamily = &firstFamily;
3244                 }
3245                 else {
3246                     FontFamily *newFamily = new FontFamily;
3247                     newFamily->setFamily(face);
3248                     currFamily->appendFamily(newFamily);
3249                     currFamily = newFamily;
3250                 }
3251                 
3252                 if (style->setFontDescription(fontDescription))
3253                     fontDirty = true;
3254             }
3255         }
3256       break;
3257     }
3258     case CSS_PROP_QUOTES:
3259         // list of strings or i
3260     case CSS_PROP_SIZE:
3261         // ### look up
3262       break;
3263     case CSS_PROP_TEXT_DECORATION: {
3264         // list of ident
3265         HANDLE_INHERIT_AND_INITIAL(textDecoration, TextDecoration)
3266         int t = RenderStyle::initialTextDecoration();
3267         if(primitiveValue && primitiveValue->getIdent() == CSS_VAL_NONE) {
3268             // do nothing
3269         } else {
3270             if(!value->isValueList()) return;
3271             CSSValueList *list = static_cast<CSSValueList*>(value);
3272             int len = list->length();
3273             for(int i = 0; i < len; i++)
3274             {
3275                 CSSValue *item = list->item(i);
3276                 if(!item->isPrimitiveValue()) continue;
3277                 primitiveValue = static_cast<CSSPrimitiveValue*>(item);
3278                 switch(primitiveValue->getIdent())
3279                 {
3280                     case CSS_VAL_NONE:
3281                         t = TDNONE; break;
3282                     case CSS_VAL_UNDERLINE:
3283                         t |= UNDERLINE; break;
3284                     case CSS_VAL_OVERLINE:
3285                         t |= OVERLINE; break;
3286                     case CSS_VAL_LINE_THROUGH:
3287                         t |= LINE_THROUGH; break;
3288                     case CSS_VAL_BLINK:
3289                         t |= BLINK; break;
3290                     default:
3291                         return;
3292                 }
3293             }
3294         }
3295
3296         style->setTextDecoration(t);
3297         break;
3298     }
3299
3300 // shorthand properties
3301     case CSS_PROP_BACKGROUND: