1/*
2 * Copyright (C) 1999 Lars Knoll ([email protected])
3 * (C) 1999 Antti Koivisto ([email protected])
4 * Copyright (C) 2003-2018 Apple Inc. All rights reserved.
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
15 *
16 * You should have received a copy of the GNU Library General Public License
17 * along with this library; see the file COPYING.LIB. If not, write to
18 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19 * Boston, MA 02110-1301, USA.
20 *
21 */
22
23#pragma once
24
25#include "ContainerNode.h"
26
27namespace WebCore {
28
29class CharacterData : public Node {
30 WTF_MAKE_ISO_ALLOCATED(CharacterData);
31public:
32 const String& data() const { return m_data; }
33 static ptrdiff_t dataMemoryOffset() { return OBJECT_OFFSETOF(CharacterData, m_data); }
34
35 WEBCORE_EXPORT void setData(const String&);
36 unsigned length() const { return m_data.length(); }
37 WEBCORE_EXPORT ExceptionOr<String> substringData(unsigned offset, unsigned count);
38 WEBCORE_EXPORT void appendData(const String&);
39 WEBCORE_EXPORT ExceptionOr<void> insertData(unsigned offset, const String&);
40 WEBCORE_EXPORT ExceptionOr<void> deleteData(unsigned offset, unsigned count);
41 WEBCORE_EXPORT ExceptionOr<void> replaceData(unsigned offset, unsigned count, const String&);
42
43 // Like appendData, but optimized for the parser (e.g., no mutation events).
44 // Returns how much could be added before length limit was met.
45 unsigned parserAppendData(const String& string, unsigned offset, unsigned lengthLimit);
46
47protected:
48 CharacterData(Document& document, const String& text, ConstructionType type)
49 : Node(document, type)
50 , m_data(!text.isNull() ? text : emptyString())
51 {
52 ASSERT(type == CreateOther || type == CreateText || type == CreateEditingText);
53 }
54
55 void setDataWithoutUpdate(const String& data)
56 {
57 ASSERT(!data.isNull());
58 m_data = data;
59 }
60 void dispatchModifiedEvent(const String& oldValue);
61
62private:
63 String nodeValue() const final;
64 ExceptionOr<void> setNodeValue(const String&) final;
65 bool isCharacterDataNode() const final { return true; }
66 int maxCharacterOffset() const final;
67 void setDataAndUpdate(const String&, unsigned offsetOfReplacedData, unsigned oldLength, unsigned newLength);
68 void notifyParentAfterChange(ContainerNode::ChildChangeSource);
69
70 String m_data;
71};
72
73} // namespace WebCore
74
75SPECIALIZE_TYPE_TRAITS_BEGIN(WebCore::CharacterData)
76 static bool isType(const WebCore::Node& node) { return node.isCharacterDataNode(); }
77SPECIALIZE_TYPE_TRAITS_END()
78