File: ElementAttributeData.cpp

package info (click to toggle)
webkit 1.8.1-3.4
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 86,872 kB
  • sloc: cpp: 748,063; ansic: 17,151; sh: 11,084; perl: 10,883; yacc: 3,678; python: 3,440; lex: 559; makefile: 168; xml: 91
file content (184 lines) | stat: -rw-r--r-- 6,093 bytes parent folder | download
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
/*
 * 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) 2003, 2004, 2005, 2006, 2008, 2010 Apple Inc. All rights reserved.
 * Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
 *
 * 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 "ElementAttributeData.h"

#include "Attr.h"
#include "StyledElement.h"

namespace WebCore {

ElementAttributeData::~ElementAttributeData()
{
    detachAttributesFromElement();
}

void ElementAttributeData::setClass(const String& className, bool shouldFoldCase)
{
    m_classNames.set(className, shouldFoldCase);
}

StylePropertySet* ElementAttributeData::ensureInlineStyleDecl(StyledElement* element)
{
    if (!m_inlineStyleDecl) {
        ASSERT(element->isStyledElement());
        m_inlineStyleDecl = StylePropertySet::create(element->document()->elementSheet());
        m_inlineStyleDecl->setStrictParsing(element->isHTMLElement() && !element->document()->inQuirksMode());
    }
    return m_inlineStyleDecl.get();
}

void ElementAttributeData::destroyInlineStyleDecl(StyledElement* element)
{
    if (!m_inlineStyleDecl)
        return;
    m_inlineStyleDecl->clearParentElement(element);
    m_inlineStyleDecl = 0;
}

void ElementAttributeData::addAttribute(PassRefPtr<Attribute> prpAttribute, Element* element)
{
    RefPtr<Attribute> attribute = prpAttribute;

    if (element)
        element->willModifyAttribute(attribute->name(), nullAtom, attribute->value());

    m_attributes.append(attribute);
    if (Attr* attr = attribute->attr())
        attr->m_element = element;

    if (element)
        element->didAddAttribute(attribute.get());
}

void ElementAttributeData::removeAttribute(size_t index, Element* element)
{
    ASSERT(index < length());

    RefPtr<Attribute> attribute = m_attributes[index];

    if (element)
        element->willRemoveAttribute(attribute->name(), attribute->value());

    if (Attr* attr = attribute->attr())
        attr->m_element = 0;
    m_attributes.remove(index);

    if (element)
        element->didRemoveAttribute(attribute.get());
}

void ElementAttributeData::detachAttributesFromElement()
{
    size_t size = m_attributes.size();
    for (size_t i = 0; i < size; i++) {
        if (Attr* attr = m_attributes[i]->attr())
            attr->m_element = 0;
    }
}

void ElementAttributeData::copyAttributesToVector(Vector<RefPtr<Attribute> >& copy)
{
    copy = m_attributes;
}

size_t ElementAttributeData::getAttributeItemIndexSlowCase(const String& name, bool shouldIgnoreAttributeCase) const
{
    unsigned len = length();

    // Continue to checking case-insensitively and/or full namespaced names if necessary:
    for (unsigned i = 0; i < len; ++i) {
        const QualifiedName& attrName = m_attributes[i]->name();
        if (!attrName.hasPrefix()) {
            if (shouldIgnoreAttributeCase && equalIgnoringCase(name, attrName.localName()))
                return i;
        } else {
            // FIXME: Would be faster to do this comparison without calling toString, which
            // generates a temporary string by concatenation. But this branch is only reached
            // if the attribute name has a prefix, which is rare in HTML.
            if (equalPossiblyIgnoringCase(name, attrName.toString(), shouldIgnoreAttributeCase))
                return i;
        }
    }
    return notFound;
}

void ElementAttributeData::setAttributes(const ElementAttributeData& other, Element* element)
{
    ASSERT(element);

    // If assigning the map changes the id attribute, we need to call
    // updateId.
    Attribute* oldId = getAttributeItem(element->document()->idAttributeName());
    Attribute* newId = other.getAttributeItem(element->document()->idAttributeName());

    if (oldId || newId)
        element->updateId(oldId ? oldId->value() : nullAtom, newId ? newId->value() : nullAtom);

    Attribute* oldName = getAttributeItem(HTMLNames::nameAttr);
    Attribute* newName = other.getAttributeItem(HTMLNames::nameAttr);

    if (oldName || newName)
        element->updateName(oldName ? oldName->value() : nullAtom, newName ? newName->value() : nullAtom);

    clearAttributes();
    unsigned newLength = other.length();
    m_attributes.resize(newLength);

    // FIXME: These loops can probably be combined.
    for (unsigned i = 0; i < newLength; i++)
        m_attributes[i] = other.m_attributes[i]->clone();
    for (unsigned i = 0; i < newLength; i++)
        element->attributeChanged(m_attributes[i].get());
}

void ElementAttributeData::clearAttributes()
{
    clearClass();
    detachAttributesFromElement();
    m_attributes.clear();
}

void ElementAttributeData::replaceAttribute(size_t index, PassRefPtr<Attribute> prpAttribute, Element* element)
{
    ASSERT(element);
    ASSERT(index < length());

    RefPtr<Attribute> attribute = prpAttribute;
    Attribute* old = m_attributes[index].get();

    element->willModifyAttribute(attribute->name(), old->value(), attribute->value());

    if (Attr* attr = old->attr())
        attr->m_element = 0;
    m_attributes[index] = attribute;
    if (Attr* attr = attribute->attr())
        attr->m_element = element;

    element->didModifyAttribute(attribute.get());
}

}