📄 styledelement.cpp
字号:
/* * 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"#include <wtf/HashFunctions.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 = CSSMutableStyleDeclaration::create(); 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::classAttributeChanged(const AtomicString& newClassString){ const UChar* characters = newClassString.characters(); unsigned length = newClassString.length(); unsigned i; for (i = 0; i < length; ++i) { if (!isClassWhitespace(characters[i])) break; } setHasClass(i < length); if (namedAttrMap) { if (i < length) mappedAttributes()->setClass(newClassString); else mappedAttributes()->clearClass(); } setChanged(); dispatchSubtreeModifiedEvent();}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) classAttributeChanged(attr->value()); else if (attr->name() == styleAttr) { if (attr->isNull()) destroyInlineStyleDecl(); else
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -