1 /*
2  * This file is part of the DOM implementation for KDE.
3  *
4  * Copyright (C) 1999-2003 Lars Knoll (knoll@kde.org)
5  *           (C) 2001-2003 Dirk Mueller (mueller@kde.org)
6  *           (C) 1999 Antti Koivisto (koivisto@kde.org)
7  *           (C) 2003 Apple Computer, Inc
8  *
9  * This library is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Library General Public
11  * License as published by the Free Software Foundation; either
12  * version 2 of the License, or (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  * Library General Public License for more details.
18  *
19  * You should have received a copy of the GNU Library General Public License
20  * along with this library; see the file COPYING.LIB.  If not, write to
21  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
22  * Boston, MA 02110-1301, USA.
23  *
24  */
25 #ifndef _DOM_CharacterDataImpl_h_
26 #define _DOM_CharacterDataImpl_h_
27 
28 #include "xml/dom_nodeimpl.h"
29 #include "dom/dom_string.h"
30 
31 namespace DOM
32 {
33 
34 class DocumentImpl;
35 class CharacterData;
36 class Text;
37 
38 class CharacterDataImpl : public NodeImpl
39 {
40 public:
41     CharacterDataImpl(DocumentImpl *doc, DOMStringImpl *_text);
42 
43     virtual ~CharacterDataImpl();
44 
45     // DOM methods & attributes for CharacterData
46 
47     virtual void setData(const DOMString &_data, int &exceptioncode);
48     virtual unsigned long length() const;
49     virtual DOMString substringData(const unsigned long offset, const unsigned long count, int &exceptioncode);
50     virtual void appendData(const DOMString &arg, int &exceptioncode);
51     virtual void insertData(const unsigned long offset, const DOMString &arg, int &exceptioncode);
52     virtual void deleteData(const unsigned long offset, const unsigned long count, int &exceptioncode);
53     virtual void replaceData(const unsigned long offset, const unsigned long count, const DOMString &arg, int &exceptioncode);
54 
55     bool containsOnlyWhitespace() const override;
56 
57     // DOM methods overridden from  parent classes
58 
59     DOMString nodeValue() const override;
60     void setNodeValue(const DOMString &_nodeValue, int &exceptioncode) override;
61 
62     // Other methods (not part of DOM)
63 
string()64     DOMStringImpl *string() const
65     {
66         return str;
67     }
data()68     DOMString data() const
69     {
70         return str;
71     }
72 
73     virtual void checkCharDataOperation(const unsigned long offset, int &exceptioncode);
74 
offsetInCharacters()75     bool offsetInCharacters() const override
76     {
77         return true;
78     }
maxCharacterOffset()79     int maxCharacterOffset() const override
80     {
81         return static_cast<int>(length());
82     }
83 
84     long maxOffset() const override;
85     long caretMinOffset() const override;
86     long caretMaxOffset() const override;
87     unsigned long caretMaxRenderedOffset() const override;
88 
89     bool rendererIsNeeded(khtml::RenderStyle *) override;
90 
91 protected:
92     // note: since DOMStrings are shared, str should always be copied when making
93     // a change or returning a string
94     DOMStringImpl *str;
95 
96     void dispatchModifiedEvent(DOMStringImpl *prevValue);
97 };
98 
99 // ----------------------------------------------------------------------------
100 
101 class CommentImpl : public CharacterDataImpl
102 {
103 public:
CommentImpl(DocumentImpl * doc,DOMStringImpl * _text)104     CommentImpl(DocumentImpl *doc, DOMStringImpl *_text)
105         : CharacterDataImpl(doc, _text) {}
CommentImpl(DocumentImpl * doc)106     CommentImpl(DocumentImpl *doc)
107         : CharacterDataImpl(doc, nullptr) {}
108     // DOM methods overridden from  parent classes
109     DOMString nodeName() const override;
110     unsigned short nodeType() const override;
111     WTF::PassRefPtr<NodeImpl> cloneNode(bool deep) override;
112 
113     // Other methods (not part of DOM)
114 
115     Id id() const override;
116     bool childTypeAllowed(unsigned short type) override;
117 
118     DOMString toString() const override;
119 };
120 
121 // ----------------------------------------------------------------------------
122 
123 class TextImpl : public CharacterDataImpl
124 {
125 public:
TextImpl(DocumentImpl * impl,DOMStringImpl * _text)126     TextImpl(DocumentImpl *impl, DOMStringImpl *_text)
127         : CharacterDataImpl(impl, _text) {}
TextImpl(DocumentImpl * impl)128     TextImpl(DocumentImpl *impl)
129         : CharacterDataImpl(impl, nullptr) {}
130 
131     // DOM methods & attributes for CharacterData
132 
133     TextImpl *splitText(const unsigned long offset, int &exceptioncode);
134 
135     // DOM Level 3: https://www.w3.org/TR/DOM-Level-3-Core/core.html#ID-1312295772
136     DOMString wholeText() const;
137     TextImpl *replaceWholeText(const DOMString &newText, int &ec);
138 
139     // DOM methods overridden from  parent classes
140     DOMString nodeName() const override;
141     unsigned short nodeType() const override;
142     WTF::PassRefPtr<NodeImpl> cloneNode(bool deep) override;
143 
144     // Other methods (not part of DOM)
145 
isTextNode()146     bool isTextNode() const override
147     {
148         return true;
149     }
150     Id id() const override;
151     void attach() override;
152     bool rendererIsNeeded(khtml::RenderStyle *) override;
153     khtml::RenderObject *createRenderer(khtml::RenderArena *, khtml::RenderStyle *) override;
154     void recalcStyle(StyleChange = NoChange) override;
affectedByNoInherit()155     bool affectedByNoInherit() const override
156     {
157         return true;
158     }
159     bool childTypeAllowed(unsigned short type) override;
160 
161     DOMStringImpl *renderString() const;
162 
163     DOMString toString() const override;
164     /** Return the text for the node, with < replaced with &lt; and so on.
165      *  @param startOffset The number of characters counted from the left, zero indexed, counting "<" as one character, to start from.  Use -1 to start from 0.
166      *  @param endOffset The number of characters counted from the left, zero indexed, counting "<" as one character, to end on.  Use -1 to end at the end of the string.
167      *  @return An html escaped version of the substring.
168      */
169     DOMString toString(long long startOffset, long long endOffset) const;
170 protected:
171     virtual TextImpl *createNew(DOMStringImpl *_str);
172 };
173 
174 // ----------------------------------------------------------------------------
175 
176 class CDATASectionImpl : public TextImpl
177 {
178 public:
CDATASectionImpl(DocumentImpl * impl,DOMStringImpl * _text)179     CDATASectionImpl(DocumentImpl *impl, DOMStringImpl *_text)
180         : TextImpl(impl, _text) {}
CDATASectionImpl(DocumentImpl * impl)181     CDATASectionImpl(DocumentImpl *impl)
182         : TextImpl(impl) {}
183 
184     // DOM methods overridden from  parent classes
185     DOMString nodeName() const override;
186     unsigned short nodeType() const override;
187     WTF::PassRefPtr<NodeImpl> cloneNode(bool deep) override;
188 
189     // Other methods (not part of DOM)
190 
191     bool childTypeAllowed(unsigned short type) override;
192 
193     DOMString toString() const override;
194 
195 protected:
196     TextImpl *createNew(DOMStringImpl *_str) override;
197 };
198 
199 // ----------------------------------------------------------------------------
200 
201 class EditingTextImpl : public TextImpl
202 {
203 public:
204     EditingTextImpl(DocumentImpl *impl, const DOMString &text);
205     EditingTextImpl(DocumentImpl *impl);
206     virtual ~EditingTextImpl();
207 
208     bool rendererIsNeeded(khtml::RenderStyle *) override;
209 };
210 
211 } //namespace
212 #endif
213