Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / third_party / WebKit / Source / core / html / parser / TextDocumentParser.cpp
blob2737f5a86b4811b84f5ebd1c18437294d3df418f
1 /*
2 * Copyright (C) 2010 Google 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
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.
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.
25 #include "config.h"
26 #include "core/html/parser/TextDocumentParser.h"
28 #include "core/HTMLNames.h"
29 #include "core/html/parser/HTMLTreeBuilder.h"
30 #include "core/html/parser/ParserSynchronizationPolicy.h"
32 namespace blink {
34 using namespace HTMLNames;
36 TextDocumentParser::TextDocumentParser(HTMLDocument& document, ParserSynchronizationPolicy syncPolicy)
37 : HTMLDocumentParser(document, false, syncPolicy)
38 , m_haveInsertedFakePreElement(false)
42 TextDocumentParser::~TextDocumentParser()
46 void TextDocumentParser::appendBytes(const char* data, size_t length)
48 if (!m_haveInsertedFakePreElement)
49 insertFakePreElement();
50 HTMLDocumentParser::appendBytes(data, length);
53 void TextDocumentParser::insertFakePreElement()
55 // In principle, we should create a specialized tree builder for
56 // TextDocuments, but instead we re-use the existing HTMLTreeBuilder.
57 // We create a fake token and give it to the tree builder rather than
58 // sending fake bytes through the front-end of the parser to avoid
59 // distrubing the line/column number calculations.
60 Vector<Attribute> attributes;
61 attributes.append(Attribute(styleAttr, "word-wrap: break-word; white-space: pre-wrap;"));
62 AtomicHTMLToken fakePre(HTMLToken::StartTag, preTag.localName(), attributes);
63 treeBuilder()->constructTree(&fakePre);
65 // Normally we would skip the first \n after a <pre> element, but we don't
66 // want to skip the first \n for text documents!
67 treeBuilder()->setShouldSkipLeadingNewline(false);
69 // Although Text Documents expose a "pre" element in their DOM, they
70 // act like a <plaintext> tag, so we have to force plaintext mode.
71 forcePlaintextForTextDocument();
73 m_haveInsertedFakePreElement = true;