2 * Copyright (C) 2006, 2007 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
20 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
22 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 #include "TextDocument.h"
29 #include "HTMLNames.h"
30 #include "HTMLViewSourceDocument.h"
31 #include "SegmentedString.h"
33 #include "XMLTokenizer.h"
39 using namespace HTMLNames;
41 TextTokenizer::TextTokenizer(Document* doc)
48 m_buffer = static_cast<UChar*>(fastMalloc(sizeof(UChar) * m_size));
52 TextTokenizer::TextTokenizer(HTMLViewSourceDocument* doc)
60 m_buffer = static_cast<UChar*>(fastMalloc(sizeof(UChar) * m_size));
64 bool TextTokenizer::write(const SegmentedString& s, bool appendData)
70 SegmentedString str = s;
71 while (!str.isEmpty()) {
77 // possibly skip an LF in the case of an CRLF sequence
79 } else if (c == '\n') {
91 // Maybe enlarge the buffer
95 if (!m_preElement && !inViewSourceMode()) {
96 RefPtr<Element> rootElement = m_doc->createElementNS(xhtmlNamespaceURI, "html", ec);
97 m_doc->appendChild(rootElement, ec);
99 RefPtr<Element> body = m_doc->createElementNS(xhtmlNamespaceURI, "body", ec);
100 rootElement->appendChild(body, ec);
102 RefPtr<Element> preElement = m_doc->createElementNS(xhtmlNamespaceURI, "pre", ec);
103 preElement->setAttribute("style", "word-wrap: break-word; white-space: pre-wrap;", ec);
105 body->appendChild(preElement, ec);
107 m_preElement = preElement.get();
110 String string = String(m_buffer, m_dest - m_buffer);
111 if (inViewSourceMode()) {
112 static_cast<HTMLViewSourceDocument*>(m_doc)->addViewSourceText(string);
116 unsigned charsLeft = string.length();
118 // split large text to nodes of manageable size
119 RefPtr<Text> text = Text::createWithLengthLimit(m_doc, string, charsLeft);
120 m_preElement->appendChild(text, ec);
126 void TextTokenizer::finish()
131 m_doc->finishedParsing();
134 bool TextTokenizer::isWaitingForScripts() const
136 // A text document is never waiting for scripts
140 TextDocument::TextDocument(DOMImplementation* implementation, Frame* frame)
141 : HTMLDocument(implementation, frame)
145 Tokenizer* TextDocument::createTokenizer()
147 return new TextTokenizer(this);