HTMLViewSourceParser.cpp revision cad810f21b803229eb11403f9209855525a25d57
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 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 INC. OR 17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY 21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24 */ 25 26#include "config.h" 27#include "HTMLViewSourceParser.h" 28 29#include "HTMLDocumentParser.h" 30#include "HTMLNames.h" 31#include "HTMLViewSourceDocument.h" 32 33namespace WebCore { 34 35HTMLViewSourceParser::HTMLViewSourceParser(HTMLViewSourceDocument* document) 36 : DecodedDataDocumentParser(document) 37 , m_tokenizer(HTMLTokenizer::create(HTMLDocumentParser::usePreHTML5ParserQuirks(document))) 38{ 39} 40 41HTMLViewSourceParser::~HTMLViewSourceParser() 42{ 43} 44 45void HTMLViewSourceParser::insert(const SegmentedString&) 46{ 47 ASSERT_NOT_REACHED(); 48} 49 50void HTMLViewSourceParser::pumpTokenizer() 51{ 52 while (m_tokenizer->nextToken(m_input.current(), m_token)) { 53 m_token.end(m_input.current().numberOfCharactersConsumed()); 54 document()->addSource(sourceForToken(), m_token); 55 updateTokenizerState(); 56 m_token.clear(m_input.current().numberOfCharactersConsumed()); 57 } 58} 59 60void HTMLViewSourceParser::append(const SegmentedString& input) 61{ 62 m_input.appendToEnd(input); 63 m_source.append(input); 64 pumpTokenizer(); 65} 66 67String HTMLViewSourceParser::sourceForToken() 68{ 69 if (m_token.type() == HTMLToken::EndOfFile) 70 return String(); 71 72 ASSERT(m_source.numberOfCharactersConsumed() == m_token.startIndex()); 73 UChar* data = 0; 74 int length = m_token.endIndex() - m_token.startIndex(); 75 String source = String::createUninitialized(length, data); 76 for (int i = 0; i < length; ++i) { 77 data[i] = *m_source; 78 m_source.advance(); 79 } 80 return source; 81} 82 83void HTMLViewSourceParser::updateTokenizerState() 84{ 85 // FIXME: The tokenizer should do this work for us. 86 if (m_token.type() != HTMLToken::StartTag) 87 return; 88 89 AtomicString tagName(m_token.name().data(), m_token.name().size()); 90 m_tokenizer->updateStateFor(tagName, document()->frame()); 91} 92 93void HTMLViewSourceParser::finish() 94{ 95 if (!m_input.haveSeenEndOfFile()) 96 m_input.markEndOfFile(); 97 pumpTokenizer(); 98 document()->finishedParsing(); 99} 100 101bool HTMLViewSourceParser::finishWasCalled() 102{ 103 return m_input.haveSeenEndOfFile(); 104} 105 106} 107