1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498
|
/*
* Copyright (C) 1999 Lars Knoll (knoll@kde.org)
* (C) 1999 Antti Koivisto (koivisto@kde.org)
* (C) 2001 Peter Kelly (pmk@post.com)
* (C) 2001 Dirk Mueller (mueller@kde.org)
* Copyright (C) 2004, 2005, 2006, 2008 Apple Inc. All rights reserved.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public License
* along with this library; see the file COPYING.LIB. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#include "config.h"
#include "StyledElement.h"
#include "CSSStyleSelector.h"
#include "CSSStyleSheet.h"
#include "CSSValueKeywords.h"
#include "Document.h"
#include "HTMLNames.h"
using namespace std;
namespace WebCore {
using namespace HTMLNames;
struct MappedAttributeKey {
uint16_t type;
StringImpl* name;
StringImpl* value;
MappedAttributeKey(MappedAttributeEntry t = eNone, StringImpl* n = 0, StringImpl* v = 0)
: type(t), name(n), value(v) { }
};
static inline bool operator==(const MappedAttributeKey& a, const MappedAttributeKey& b)
{ return a.type == b.type && a.name == b.name && a.value == b.value; }
struct MappedAttributeKeyTraits : WTF::GenericHashTraits<MappedAttributeKey> {
static const bool emptyValueIsZero = true;
static const bool needsDestruction = false;
static void constructDeletedValue(MappedAttributeKey* slot) { slot->type = eLastEntry; }
static bool isDeletedValue(const MappedAttributeKey& value) { return value.type == eLastEntry; }
};
struct MappedAttributeHash {
static unsigned hash(const MappedAttributeKey&);
static bool equal(const MappedAttributeKey& a, const MappedAttributeKey& b) { return a == b; }
static const bool safeToCompareToEmptyOrDeleted = true;
};
typedef HashMap<MappedAttributeKey, CSSMappedAttributeDeclaration*, MappedAttributeHash, MappedAttributeKeyTraits> MappedAttributeDecls;
static MappedAttributeDecls* mappedAttributeDecls = 0;
CSSMappedAttributeDeclaration* StyledElement::getMappedAttributeDecl(MappedAttributeEntry entryType, Attribute* attr)
{
if (!mappedAttributeDecls)
return 0;
return mappedAttributeDecls->get(MappedAttributeKey(entryType, attr->name().localName().impl(), attr->value().impl()));
}
CSSMappedAttributeDeclaration* StyledElement::getMappedAttributeDecl(MappedAttributeEntry type, const QualifiedName& name, const AtomicString& value)
{
if (!mappedAttributeDecls)
return 0;
return mappedAttributeDecls->get(MappedAttributeKey(type, name.localName().impl(), value.impl()));
}
void StyledElement::setMappedAttributeDecl(MappedAttributeEntry entryType, Attribute* attr, CSSMappedAttributeDeclaration* decl)
{
if (!mappedAttributeDecls)
mappedAttributeDecls = new MappedAttributeDecls;
mappedAttributeDecls->set(MappedAttributeKey(entryType, attr->name().localName().impl(), attr->value().impl()), decl);
}
void StyledElement::setMappedAttributeDecl(MappedAttributeEntry entryType, const QualifiedName& name, const AtomicString& value, CSSMappedAttributeDeclaration* decl)
{
if (!mappedAttributeDecls)
mappedAttributeDecls = new MappedAttributeDecls;
mappedAttributeDecls->set(MappedAttributeKey(entryType, name.localName().impl(), value.impl()), decl);
}
void StyledElement::removeMappedAttributeDecl(MappedAttributeEntry entryType, const QualifiedName& attrName, const AtomicString& attrValue)
{
if (!mappedAttributeDecls)
return;
mappedAttributeDecls->remove(MappedAttributeKey(entryType, attrName.localName().impl(), attrValue.impl()));
}
void StyledElement::invalidateStyleAttribute()
{
m_isStyleAttributeValid = false;
}
void StyledElement::updateStyleAttribute() const
{
ASSERT(!m_isStyleAttributeValid);
m_isStyleAttributeValid = true;
m_synchronizingStyleAttribute = true;
if (m_inlineStyleDecl)
const_cast<StyledElement*>(this)->setAttribute(styleAttr, m_inlineStyleDecl->cssText());
m_synchronizingStyleAttribute = false;
}
StyledElement::StyledElement(const QualifiedName& name, Document *doc)
: Element(name, doc)
{
}
StyledElement::~StyledElement()
{
destroyInlineStyleDecl();
}
PassRefPtr<Attribute> StyledElement::createAttribute(const QualifiedName& name, const AtomicString& value)
{
return MappedAttribute::create(name, value);
}
void StyledElement::createInlineStyleDecl()
{
m_inlineStyleDecl = new CSSMutableStyleDeclaration;
m_inlineStyleDecl->setParent(document()->elementSheet());
m_inlineStyleDecl->setNode(this);
m_inlineStyleDecl->setStrictParsing(isHTMLElement() && !document()->inCompatMode());
}
void StyledElement::destroyInlineStyleDecl()
{
if (m_inlineStyleDecl) {
m_inlineStyleDecl->setNode(0);
m_inlineStyleDecl->setParent(0);
m_inlineStyleDecl = 0;
}
}
void StyledElement::attributeChanged(Attribute* attr, bool preserveDecls)
{
if (!attr->isMappedAttribute()) {
Element::attributeChanged(attr, preserveDecls);
return;
}
MappedAttribute* mappedAttr = static_cast<MappedAttribute*>(attr);
if (mappedAttr->decl() && !preserveDecls) {
mappedAttr->setDecl(0);
setChanged();
if (namedAttrMap)
mappedAttributes()->declRemoved();
}
bool checkDecl = true;
MappedAttributeEntry entry;
bool needToParse = mapToEntry(attr->name(), entry);
if (preserveDecls) {
if (mappedAttr->decl()) {
setChanged();
if (namedAttrMap)
mappedAttributes()->declAdded();
checkDecl = false;
}
}
else if (!attr->isNull() && entry != eNone) {
CSSMappedAttributeDeclaration* decl = getMappedAttributeDecl(entry, attr);
if (decl) {
mappedAttr->setDecl(decl);
setChanged();
if (namedAttrMap)
mappedAttributes()->declAdded();
checkDecl = false;
} else
needToParse = true;
}
if (needToParse)
parseMappedAttribute(mappedAttr);
if (entry == eNone && ownerDocument()->attached() && ownerDocument()->styleSelector()->hasSelectorForAttribute(attr->name().localName()))
setChanged();
if (checkDecl && mappedAttr->decl()) {
// Add the decl to the table in the appropriate spot.
setMappedAttributeDecl(entry, attr, mappedAttr->decl());
mappedAttr->decl()->setMappedState(entry, attr->name(), attr->value());
mappedAttr->decl()->setParent(0);
mappedAttr->decl()->setNode(0);
if (namedAttrMap)
mappedAttributes()->declAdded();
}
Element::attributeChanged(attr, preserveDecls);
}
bool StyledElement::mapToEntry(const QualifiedName& attrName, MappedAttributeEntry& result) const
{
result = eNone;
if (attrName == styleAttr)
return !m_synchronizingStyleAttribute;
return true;
}
void StyledElement::parseMappedAttribute(MappedAttribute *attr)
{
if (attr->name() == idAttr) {
// unique id
setHasID(!attr->isNull());
if (namedAttrMap) {
if (attr->isNull())
namedAttrMap->setID(nullAtom);
else if (document()->inCompatMode() && !attr->value().impl()->isLower())
namedAttrMap->setID(AtomicString(attr->value().string().lower()));
else
namedAttrMap->setID(attr->value());
}
setChanged();
} else if (attr->name() == classAttr) {
// class
const AtomicString& value = attr->value();
const UChar* characters = value.characters();
unsigned length = value.length();
unsigned i;
for (i = 0; i < length; ++i) {
if (!isClassWhitespace(characters[i]))
break;
}
setHasClass(i < length);
if (namedAttrMap) {
if (i < length)
mappedAttributes()->setClass(value);
else
mappedAttributes()->clearClass();
}
setChanged();
} else if (attr->name() == styleAttr) {
if (attr->isNull())
destroyInlineStyleDecl();
else
getInlineStyleDecl()->parseDeclaration(attr->value());
m_isStyleAttributeValid = true;
setChanged();
}
}
void StyledElement::createAttributeMap() const
{
namedAttrMap = NamedMappedAttrMap::create(const_cast<StyledElement*>(this));
}
CSSMutableStyleDeclaration* StyledElement::getInlineStyleDecl()
{
if (!m_inlineStyleDecl)
createInlineStyleDecl();
return m_inlineStyleDecl.get();
}
CSSStyleDeclaration* StyledElement::style()
{
return getInlineStyleDecl();
}
static inline int toHex(UChar c) {
return ((c >= '0' && c <= '9') ? (c - '0')
: ((c >= 'a' && c <= 'f') ? (c - 'a' + 10)
: (( c >= 'A' && c <= 'F') ? (c - 'A' + 10)
: -1)));
}
void StyledElement::addCSSProperty(MappedAttribute* attr, int id, const String &value)
{
if (!attr->decl()) createMappedDecl(attr);
attr->decl()->setProperty(id, value, false);
}
void StyledElement::addCSSProperty(MappedAttribute* attr, int id, int value)
{
if (!attr->decl()) createMappedDecl(attr);
attr->decl()->setProperty(id, value, false);
}
void StyledElement::addCSSStringProperty(MappedAttribute* attr, int id, const String &value, CSSPrimitiveValue::UnitTypes type)
{
if (!attr->decl()) createMappedDecl(attr);
attr->decl()->setStringProperty(id, value, type, false);
}
void StyledElement::addCSSImageProperty(MappedAttribute* attr, int id, const String& url)
{
if (!attr->decl()) createMappedDecl(attr);
attr->decl()->setImageProperty(id, url, false);
}
void StyledElement::addCSSLength(MappedAttribute* attr, int id, const String &value)
{
// FIXME: This function should not spin up the CSS parser, but should instead just figure out the correct
// length unit and make the appropriate parsed value.
if (!attr->decl())
createMappedDecl(attr);
// strip attribute garbage..
StringImpl* v = value.impl();
if (v) {
unsigned int l = 0;
while (l < v->length() && (*v)[l] <= ' ')
l++;
for (; l < v->length(); l++) {
UChar cc = (*v)[l];
if (cc > '9')
break;
if (cc < '0') {
if (cc == '%' || cc == '*')
l++;
if (cc != '.')
break;
}
}
if (l != v->length()) {
attr->decl()->setLengthProperty(id, v->substring(0, l), false);
return;
}
}
attr->decl()->setLengthProperty(id, value, false);
}
/* color parsing that tries to match as close as possible IE 6. */
void StyledElement::addCSSColor(MappedAttribute* attr, int id, const String& c)
{
// this is the only case no color gets applied in IE.
if (!c.length())
return;
if (!attr->decl())
createMappedDecl(attr);
if (attr->decl()->setProperty(id, c, false))
return;
String color = c;
// not something that fits the specs.
// we're emulating IEs color parser here. It maps transparent to black, otherwise it tries to build a rgb value
// out of everyhting you put in. The algorithm is experimentally determined, but seems to work for all test cases I have.
// the length of the color value is rounded up to the next
// multiple of 3. each part of the rgb triple then gets one third
// of the length.
//
// Each triplet is parsed byte by byte, mapping
// each number to a hex value (0-9a-fA-F to their values
// everything else to 0).
//
// The highest non zero digit in all triplets is remembered, and
// used as a normalization point to normalize to values between 0
// and 255.
if (color.lower() != "transparent") {
if (color[0] == '#')
color.remove(0, 1);
int basicLength = (color.length() + 2) / 3;
if (basicLength > 1) {
// IE ignores colors with three digits or less
int colors[3] = { 0, 0, 0 };
int component = 0;
int pos = 0;
int maxDigit = basicLength-1;
while (component < 3) {
// search forward for digits in the string
int numDigits = 0;
while (pos < (int)color.length() && numDigits < basicLength) {
int hex = toHex(color[pos]);
colors[component] = (colors[component] << 4);
if (hex > 0) {
colors[component] += hex;
maxDigit = min(maxDigit, numDigits);
}
numDigits++;
pos++;
}
while (numDigits++ < basicLength)
colors[component] <<= 4;
component++;
}
maxDigit = basicLength - maxDigit;
// normalize to 00-ff. The highest filled digit counts, minimum is 2 digits
maxDigit -= 2;
colors[0] >>= 4*maxDigit;
colors[1] >>= 4*maxDigit;
colors[2] >>= 4*maxDigit;
// ASSERT(colors[0] < 0x100 && colors[1] < 0x100 && colors[2] < 0x100);
color = String::format("#%02x%02x%02x", colors[0], colors[1], colors[2]);
if (attr->decl()->setProperty(id, color, false))
return;
}
}
attr->decl()->setProperty(id, CSSValueBlack, false);
}
void StyledElement::createMappedDecl(MappedAttribute* attr)
{
CSSMappedAttributeDeclaration* decl = new CSSMappedAttributeDeclaration(0);
attr->setDecl(decl);
decl->setParent(document()->elementSheet());
decl->setNode(this);
decl->setStrictParsing(false); // Mapped attributes are just always quirky.
}
// Golden ratio - arbitrary start value to avoid mapping all 0's to all 0's
// or anything like that.
const unsigned PHI = 0x9e3779b9U;
// Paul Hsieh's SuperFastHash
// http://www.azillionmonkeys.com/qed/hash.html
unsigned MappedAttributeHash::hash(const MappedAttributeKey& key)
{
uint32_t hash = PHI;
uint32_t tmp;
const uint16_t* p;
p = reinterpret_cast<const uint16_t*>(&key.name);
hash += p[0];
tmp = (p[1] << 11) ^ hash;
hash = (hash << 16) ^ tmp;
hash += hash >> 11;
ASSERT(sizeof(key.name) == 4 || sizeof(key.name) == 8);
if (sizeof(key.name) == 8) {
p += 2;
hash += p[0];
tmp = (p[1] << 11) ^ hash;
hash = (hash << 16) ^ tmp;
hash += hash >> 11;
}
p = reinterpret_cast<const uint16_t*>(&key.value);
hash += p[0];
tmp = (p[1] << 11) ^ hash;
hash = (hash << 16) ^ tmp;
hash += hash >> 11;
ASSERT(sizeof(key.value) == 4 || sizeof(key.value) == 8);
if (sizeof(key.value) == 8) {
p += 2;
hash += p[0];
tmp = (p[1] << 11) ^ hash;
hash = (hash << 16) ^ tmp;
hash += hash >> 11;
}
// Handle end case
hash += key.type;
hash ^= hash << 11;
hash += hash >> 17;
// Force "avalanching" of final 127 bits
hash ^= hash << 3;
hash += hash >> 5;
hash ^= hash << 2;
hash += hash >> 15;
hash ^= hash << 10;
// This avoids ever returning a hash code of 0, since that is used to
// signal "hash not computed yet", using a value that is likely to be
// effectively the same as 0 when the low bits are masked
if (hash == 0)
hash = 0x80000000;
return hash;
}
void StyledElement::copyNonAttributeProperties(const Element *sourceElement)
{
const StyledElement* source = static_cast<const StyledElement*>(sourceElement);
if (!source->m_inlineStyleDecl)
return;
*getInlineStyleDecl() = *source->m_inlineStyleDecl;
m_isStyleAttributeValid = source->m_isStyleAttributeValid;
m_synchronizingStyleAttribute = source->m_synchronizingStyleAttribute;
Element::copyNonAttributeProperties(sourceElement);
}
}
|