Home | History | Annotate | Download | only in dom
      1 /*
      2  * Copyright (C) 2011 Google Inc. All rights reserved.
      3  *
      4  * Redistribution and use in source and binary forms, with or without
      5  * modification, are permitted provided that the following conditions are
      6  * met:
      7  *
      8  *     * Redistributions of source code must retain the above copyright
      9  * notice, this list of conditions and the following disclaimer.
     10  *     * Redistributions in binary form must reproduce the above
     11  * copyright notice, this list of conditions and the following disclaimer
     12  * in the documentation and/or other materials provided with the
     13  * distribution.
     14  *     * Neither the name of Google Inc. nor the names of its
     15  * contributors may be used to endorse or promote products derived from
     16  * this software without specific prior written permission.
     17  *
     18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     19  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
     20  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
     21  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
     22  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
     23  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
     24  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     28  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     29  */
     30 
     31 #include "config.h"
     32 
     33 #include "core/dom/MutationRecord.h"
     34 
     35 #include "core/dom/Node.h"
     36 #include "core/dom/NodeList.h"
     37 #include "core/dom/QualifiedName.h"
     38 #include "core/dom/StaticNodeList.h"
     39 #include "wtf/StdLibExtras.h"
     40 
     41 namespace WebCore {
     42 
     43 namespace {
     44 
     45 class ChildListRecord : public MutationRecord {
     46 public:
     47     ChildListRecord(PassRefPtrWillBeRawPtr<Node> target, PassRefPtrWillBeRawPtr<StaticNodeList> added, PassRefPtrWillBeRawPtr<StaticNodeList> removed, PassRefPtrWillBeRawPtr<Node> previousSibling, PassRefPtrWillBeRawPtr<Node> nextSibling)
     48         : m_target(target)
     49         , m_addedNodes(added)
     50         , m_removedNodes(removed)
     51         , m_previousSibling(previousSibling)
     52         , m_nextSibling(nextSibling)
     53     {
     54     }
     55 
     56     virtual void trace(Visitor* visitor) OVERRIDE
     57     {
     58         visitor->trace(m_target);
     59         visitor->trace(m_addedNodes);
     60         visitor->trace(m_removedNodes);
     61         visitor->trace(m_previousSibling);
     62         visitor->trace(m_nextSibling);
     63         MutationRecord::trace(visitor);
     64     }
     65 
     66 private:
     67     virtual const AtomicString& type() OVERRIDE;
     68     virtual Node* target() OVERRIDE { return m_target.get(); }
     69     virtual StaticNodeList* addedNodes() OVERRIDE { return m_addedNodes.get(); }
     70     virtual StaticNodeList* removedNodes() OVERRIDE { return m_removedNodes.get(); }
     71     virtual Node* previousSibling() OVERRIDE { return m_previousSibling.get(); }
     72     virtual Node* nextSibling() OVERRIDE { return m_nextSibling.get(); }
     73 
     74     RefPtrWillBeMember<Node> m_target;
     75     RefPtrWillBeMember<StaticNodeList> m_addedNodes;
     76     RefPtrWillBeMember<StaticNodeList> m_removedNodes;
     77     RefPtrWillBeMember<Node> m_previousSibling;
     78     RefPtrWillBeMember<Node> m_nextSibling;
     79 };
     80 
     81 class RecordWithEmptyNodeLists : public MutationRecord {
     82 public:
     83     RecordWithEmptyNodeLists(PassRefPtrWillBeRawPtr<Node> target, const String& oldValue)
     84         : m_target(target)
     85         , m_oldValue(oldValue)
     86     {
     87     }
     88 
     89     virtual void trace(Visitor* visitor) OVERRIDE
     90     {
     91         visitor->trace(m_target);
     92         visitor->trace(m_addedNodes);
     93         visitor->trace(m_removedNodes);
     94         MutationRecord::trace(visitor);
     95     }
     96 
     97 private:
     98     virtual Node* target() OVERRIDE { return m_target.get(); }
     99     virtual String oldValue() OVERRIDE { return m_oldValue; }
    100     virtual StaticNodeList* addedNodes() OVERRIDE { return lazilyInitializeEmptyNodeList(m_addedNodes); }
    101     virtual StaticNodeList* removedNodes() OVERRIDE { return lazilyInitializeEmptyNodeList(m_removedNodes); }
    102 
    103     static StaticNodeList* lazilyInitializeEmptyNodeList(RefPtrWillBeMember<StaticNodeList>& nodeList)
    104     {
    105         if (!nodeList)
    106             nodeList = StaticNodeList::createEmpty();
    107         return nodeList.get();
    108     }
    109 
    110     RefPtrWillBeMember<Node> m_target;
    111     String m_oldValue;
    112     RefPtrWillBeMember<StaticNodeList> m_addedNodes;
    113     RefPtrWillBeMember<StaticNodeList> m_removedNodes;
    114 };
    115 
    116 class AttributesRecord : public RecordWithEmptyNodeLists {
    117 public:
    118     AttributesRecord(PassRefPtrWillBeRawPtr<Node> target, const QualifiedName& name, const AtomicString& oldValue)
    119         : RecordWithEmptyNodeLists(target, oldValue)
    120         , m_attributeName(name.localName())
    121         , m_attributeNamespace(name.namespaceURI())
    122     {
    123     }
    124 
    125 private:
    126     virtual const AtomicString& type() OVERRIDE;
    127     virtual const AtomicString& attributeName() OVERRIDE { return m_attributeName; }
    128     virtual const AtomicString& attributeNamespace() OVERRIDE { return m_attributeNamespace; }
    129 
    130     AtomicString m_attributeName;
    131     AtomicString m_attributeNamespace;
    132 };
    133 
    134 class CharacterDataRecord : public RecordWithEmptyNodeLists {
    135 public:
    136     CharacterDataRecord(PassRefPtrWillBeRawPtr<Node> target, const String& oldValue)
    137         : RecordWithEmptyNodeLists(target, oldValue)
    138     {
    139     }
    140 
    141 private:
    142     virtual const AtomicString& type() OVERRIDE;
    143 };
    144 
    145 class MutationRecordWithNullOldValue : public MutationRecord {
    146 public:
    147     MutationRecordWithNullOldValue(PassRefPtrWillBeRawPtr<MutationRecord> record)
    148         : m_record(record)
    149     {
    150     }
    151 
    152     virtual void trace(Visitor* visitor) OVERRIDE
    153     {
    154         visitor->trace(m_record);
    155         MutationRecord::trace(visitor);
    156     }
    157 
    158 private:
    159     virtual const AtomicString& type() OVERRIDE { return m_record->type(); }
    160     virtual Node* target() OVERRIDE { return m_record->target(); }
    161     virtual StaticNodeList* addedNodes() OVERRIDE { return m_record->addedNodes(); }
    162     virtual StaticNodeList* removedNodes() OVERRIDE { return m_record->removedNodes(); }
    163     virtual Node* previousSibling() OVERRIDE { return m_record->previousSibling(); }
    164     virtual Node* nextSibling() OVERRIDE { return m_record->nextSibling(); }
    165     virtual const AtomicString& attributeName() OVERRIDE { return m_record->attributeName(); }
    166     virtual const AtomicString& attributeNamespace() OVERRIDE { return m_record->attributeNamespace(); }
    167 
    168     virtual String oldValue() OVERRIDE { return String(); }
    169 
    170     RefPtrWillBeMember<MutationRecord> m_record;
    171 };
    172 
    173 const AtomicString& ChildListRecord::type()
    174 {
    175     DEFINE_STATIC_LOCAL(AtomicString, childList, ("childList", AtomicString::ConstructFromLiteral));
    176     return childList;
    177 }
    178 
    179 const AtomicString& AttributesRecord::type()
    180 {
    181     DEFINE_STATIC_LOCAL(AtomicString, attributes, ("attributes", AtomicString::ConstructFromLiteral));
    182     return attributes;
    183 }
    184 
    185 const AtomicString& CharacterDataRecord::type()
    186 {
    187     DEFINE_STATIC_LOCAL(AtomicString, characterData, ("characterData", AtomicString::ConstructFromLiteral));
    188     return characterData;
    189 }
    190 
    191 } // namespace
    192 
    193 PassRefPtrWillBeRawPtr<MutationRecord> MutationRecord::createChildList(PassRefPtrWillBeRawPtr<Node> target, PassRefPtrWillBeRawPtr<StaticNodeList> added, PassRefPtrWillBeRawPtr<StaticNodeList> removed, PassRefPtrWillBeRawPtr<Node> previousSibling, PassRefPtrWillBeRawPtr<Node> nextSibling)
    194 {
    195     return adoptRefWillBeNoop(new ChildListRecord(target, added, removed, previousSibling, nextSibling));
    196 }
    197 
    198 PassRefPtrWillBeRawPtr<MutationRecord> MutationRecord::createAttributes(PassRefPtrWillBeRawPtr<Node> target, const QualifiedName& name, const AtomicString& oldValue)
    199 {
    200     return adoptRefWillBeNoop(new AttributesRecord(target, name, oldValue));
    201 }
    202 
    203 PassRefPtrWillBeRawPtr<MutationRecord> MutationRecord::createCharacterData(PassRefPtrWillBeRawPtr<Node> target, const String& oldValue)
    204 {
    205     return adoptRefWillBeNoop(new CharacterDataRecord(target, oldValue));
    206 }
    207 
    208 PassRefPtrWillBeRawPtr<MutationRecord> MutationRecord::createWithNullOldValue(PassRefPtrWillBeRawPtr<MutationRecord> record)
    209 {
    210     return adoptRefWillBeNoop(new MutationRecordWithNullOldValue(record));
    211 }
    212 
    213 MutationRecord::~MutationRecord()
    214 {
    215 }
    216 
    217 } // namespace WebCore
    218