1/*
2 * Copyright (C) 1999 Lars Knoll (knoll@kde.org)
3 *           (C) 1999 Antti Koivisto (koivisto@kde.org)
4 *           (C) 2001 Peter Kelly (pmk@post.com)
5 *           (C) 2001 Dirk Mueller (mueller@kde.org)
6 * Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008, 2009 Apple Inc. All rights reserved.
7 *
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Library General Public
10 * License as published by the Free Software Foundation; either
11 * version 2 of the License, or (at your option) any later version.
12 *
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16 * Library General Public License for more details.
17 *
18 * You should have received a copy of the GNU Library General Public License
19 * along with this library; see the file COPYING.LIB.  If not, write to
20 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21 * Boston, MA 02110-1301, USA.
22 *
23 */
24
25#ifndef Attr_h
26#define Attr_h
27
28#include "ContainerNode.h"
29#include "QualifiedName.h"
30
31namespace WebCore {
32
33class CSSStyleDeclaration;
34class MutableStyleProperties;
35
36// Attr can have Text and EntityReference children
37// therefore it has to be a fullblown Node. The plan
38// is to dynamically allocate a textchild and store the
39// resulting nodevalue in the attribute upon
40// destruction. however, this is not yet implemented.
41
42class Attr final : public ContainerNode {
43public:
44    static PassRefPtr<Attr> create(Element*, const QualifiedName&);
45    static PassRefPtr<Attr> create(Document&, const QualifiedName&, const AtomicString& value);
46    virtual ~Attr();
47
48    String name() const { return qualifiedName().toString(); }
49    bool specified() const { return true; }
50    Element* ownerElement() const { return m_element; }
51
52    const AtomicString& value() const;
53    void setValue(const AtomicString&, ExceptionCode&);
54    void setValue(const AtomicString&);
55
56    const QualifiedName& qualifiedName() const { return m_name; }
57
58    bool isId() const;
59
60    CSSStyleDeclaration* style();
61
62    void attachToElement(Element*);
63    void detachFromElementWithValue(const AtomicString&);
64
65    virtual const AtomicString& namespaceURI() const override { return m_name.namespaceURI(); }
66
67private:
68    Attr(Element*, const QualifiedName&);
69    Attr(Document&, const QualifiedName&, const AtomicString& value);
70
71    void createTextChild();
72
73    virtual String nodeName() const override { return name(); }
74    virtual NodeType nodeType() const override { return ATTRIBUTE_NODE; }
75
76    virtual const AtomicString& localName() const override { return m_name.localName(); }
77    virtual const AtomicString& prefix() const override { return m_name.prefix(); }
78
79    virtual void setPrefix(const AtomicString&, ExceptionCode&) override;
80
81    virtual String nodeValue() const override { return value(); }
82    virtual void setNodeValue(const String&, ExceptionCode&) override;
83    virtual PassRefPtr<Node> cloneNode(bool deep) override;
84
85    virtual bool isAttributeNode() const override { return true; }
86    virtual bool childTypeAllowed(NodeType) const override;
87
88    virtual void childrenChanged(const ChildChange&) override;
89
90    Attribute& elementAttribute();
91
92    // Attr wraps either an element/name, or a name/value pair (when it's a standalone Node.)
93    // Note that m_name is always set, but m_element/m_standaloneValue may be null.
94    Element* m_element;
95    QualifiedName m_name;
96    AtomicString m_standaloneValue;
97
98    RefPtr<MutableStyleProperties> m_style;
99    unsigned m_ignoreChildrenChanged;
100};
101
102inline bool isAttr(const Node& node) { return node.isAttributeNode(); }
103void isAttr(const Attr&); // Catch unnecessary runtime check of type known at compile time.
104
105NODE_TYPE_CASTS(Attr)
106
107} // namespace WebCore
108
109#endif // Attr_h
110