Home | History | Annotate | Download | only in parser
      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 
     33 namespace WebCore {
     34 
     35 HTMLViewSourceParser::HTMLViewSourceParser(HTMLViewSourceDocument* document)
     36     : DecodedDataDocumentParser(document)
     37     , m_tokenizer(HTMLTokenizer::create(HTMLDocumentParser::usePreHTML5ParserQuirks(document)))
     38 {
     39 }
     40 
     41 HTMLViewSourceParser::~HTMLViewSourceParser()
     42 {
     43 }
     44 
     45 void HTMLViewSourceParser::insert(const SegmentedString&)
     46 {
     47     ASSERT_NOT_REACHED();
     48 }
     49 
     50 void HTMLViewSourceParser::pumpTokenizer()
     51 {
     52     while (true) {
     53         m_sourceTracker.start(m_input, m_token);
     54         if (!m_tokenizer->nextToken(m_input.current(), m_token))
     55             break;
     56         m_sourceTracker.end(m_input, m_token);
     57 
     58         document()->addSource(sourceForToken(), m_token);
     59         updateTokenizerState();
     60         m_token.clear();
     61     }
     62 }
     63 
     64 void HTMLViewSourceParser::append(const SegmentedString& input)
     65 {
     66     m_input.appendToEnd(input);
     67     pumpTokenizer();
     68 }
     69 
     70 String HTMLViewSourceParser::sourceForToken()
     71 {
     72     return m_sourceTracker.sourceForToken(m_token);
     73 }
     74 
     75 void HTMLViewSourceParser::updateTokenizerState()
     76 {
     77     // FIXME: The tokenizer should do this work for us.
     78     if (m_token.type() != HTMLToken::StartTag)
     79         return;
     80 
     81     AtomicString tagName(m_token.name().data(), m_token.name().size());
     82     m_tokenizer->updateStateFor(tagName, document()->frame());
     83 }
     84 
     85 void HTMLViewSourceParser::finish()
     86 {
     87     if (!m_input.haveSeenEndOfFile())
     88         m_input.markEndOfFile();
     89     pumpTokenizer();
     90     document()->finishedParsing();
     91 }
     92 
     93 bool HTMLViewSourceParser::finishWasCalled()
     94 {
     95     return m_input.haveSeenEndOfFile();
     96 }
     97 
     98 }
     99