1/*
2 * Copyright (C) 2010 Adam Barth. All Rights Reserved.
3 * Copyright (C) 2015 Apple Inc. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
15 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
18 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
19 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
21 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
22 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27#include "config.h"
28#include "HTMLSourceTracker.h"
29
30#include "HTMLTokenizer.h"
31#include <wtf/text/StringBuilder.h>
32
33namespace WebCore {
34
35void HTMLSourceTracker::startToken(SegmentedString& currentInput, HTMLTokenizer& tokenizer)
36{
37 if (!m_started) {
38 if (tokenizer.numberOfBufferedCharacters())
39 m_previousSource = tokenizer.bufferedCharacters();
40 else
41 m_previousSource.clear();
42 m_started = true;
43 } else
44 m_previousSource.append(m_currentSource);
45
46 m_currentSource = currentInput;
47 m_tokenStart = m_currentSource.numberOfCharactersConsumed() - m_previousSource.length();
48 tokenizer.setTokenAttributeBaseOffset(m_tokenStart);
49}
50
51void HTMLSourceTracker::endToken(SegmentedString& currentInput, HTMLTokenizer& tokenizer)
52{
53 ASSERT(m_started);
54 m_started = false;
55
56 m_tokenEnd = currentInput.numberOfCharactersConsumed() - tokenizer.numberOfBufferedCharacters();
57 m_cachedSourceForToken = String();
58}
59
60String HTMLSourceTracker::source(const HTMLToken& token)
61{
62 ASSERT(!m_started);
63
64 if (token.type() == HTMLToken::EndOfFile)
65 return String(); // Hides the null character we use to mark the end of file.
66
67 if (!m_cachedSourceForToken.isEmpty())
68 return m_cachedSourceForToken;
69
70 unsigned length = m_tokenEnd - m_tokenStart;
71
72 StringBuilder source;
73 source.reserveCapacity(length);
74
75 unsigned i = 0;
76 for ( ; i < length && !m_previousSource.isEmpty(); ++i) {
77 source.append(m_previousSource.currentCharacter());
78 m_previousSource.advance();
79 }
80 for ( ; i < length; ++i) {
81 ASSERT(!m_currentSource.isEmpty());
82 source.append(m_currentSource.currentCharacter());
83 m_currentSource.advance();
84 }
85
86 m_cachedSourceForToken = source.toString();
87 return m_cachedSourceForToken;
88}
89
90String HTMLSourceTracker::source(const HTMLToken& token, unsigned attributeStart, unsigned attributeEnd)
91{
92 return source(token).substring(attributeStart, attributeEnd - attributeStart);
93}
94
95}
96