1 /*
2 * Copyright (C) 2010 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
6 * are met:
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.
12 *
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.
23 */
24
25 #include "config.h"
26 #include "TextDocumentParser.h"
27
28 #include "HTMLDocument.h"
29 #include "HTMLNames.h"
30 #include "HTMLTokenizer.h"
31 #include "HTMLTreeBuilder.h"
32
33 namespace WebCore {
34
35 using namespace HTMLNames;
36
TextDocumentParser(HTMLDocument * document)37 TextDocumentParser::TextDocumentParser(HTMLDocument* document)
38 : HTMLDocumentParser(document, false)
39 , m_haveInsertedFakePreElement(false)
40 {
41 tokenizer()->setState(HTMLTokenizer::PLAINTEXTState);
42 }
43
~TextDocumentParser()44 TextDocumentParser::~TextDocumentParser()
45 {
46 }
47
append(const SegmentedString & text)48 void TextDocumentParser::append(const SegmentedString& text)
49 {
50 if (!m_haveInsertedFakePreElement)
51 insertFakePreElement();
52 HTMLDocumentParser::append(text);
53 }
54
insertFakePreElement()55 void TextDocumentParser::insertFakePreElement()
56 {
57 // In principle, we should create a specialized tree builder for
58 // TextDocuments, but instead we re-use the existing HTMLTreeBuilder.
59 // We create a fake token and give it to the tree builder rather than
60 // sending fake bytes through the front-end of the parser to avoid
61 // distrubing the line/column number calculations.
62
63 RefPtr<Attribute> styleAttribute = Attribute::createMapped("style", "word-wrap: break-word; white-space: pre-wrap;");
64 RefPtr<NamedNodeMap> attributes = NamedNodeMap::create();
65 attributes->insertAttribute(styleAttribute.release(), false);
66 AtomicHTMLToken fakePre(HTMLToken::StartTag, preTag.localName(), attributes.release());
67
68 treeBuilder()->constructTreeFromAtomicToken(fakePre);
69 m_haveInsertedFakePreElement = true;
70 }
71
72 }
73