Commit 27c34e42 authored by Mason Freed's avatar Mason Freed Committed by Commit Bot

Remove the HTMLSourceTracker

This class was previously only (really) used in HTMLViewSourceParser,
so this CL folds it into that class.

This CL should not change any functionality.

Bug: 901056
Change-Id: Ia47e5da05b586c542c4ff93a94dafdb439f22dd0
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2225124
Commit-Queue: Mason Freed <masonfreed@chromium.org>
Reviewed-by: default avatarRichard Townsend <richard.townsend@arm.com>
Cr-Commit-Position: refs/heads/master@{#773785}
parent 9d518a68
...@@ -47,8 +47,6 @@ blink_core_sources("parser") { ...@@ -47,8 +47,6 @@ blink_core_sources("parser") {
"html_preload_scanner.h", "html_preload_scanner.h",
"html_resource_preloader.cc", "html_resource_preloader.cc",
"html_resource_preloader.h", "html_resource_preloader.h",
"html_source_tracker.cc",
"html_source_tracker.h",
"html_srcset_parser.cc", "html_srcset_parser.cc",
"html_srcset_parser.h", "html_srcset_parser.h",
"html_stack_item.h", "html_stack_item.h",
......
...@@ -37,7 +37,6 @@ ...@@ -37,7 +37,6 @@
#include "third_party/blink/renderer/core/html/parser/compact_html_token.h" #include "third_party/blink/renderer/core/html/parser/compact_html_token.h"
#include "third_party/blink/renderer/core/html/parser/html_parser_options.h" #include "third_party/blink/renderer/core/html/parser/html_parser_options.h"
#include "third_party/blink/renderer/core/html/parser/html_preload_scanner.h" #include "third_party/blink/renderer/core/html/parser/html_preload_scanner.h"
#include "third_party/blink/renderer/core/html/parser/html_source_tracker.h"
#include "third_party/blink/renderer/core/html/parser/html_tree_builder_simulator.h" #include "third_party/blink/renderer/core/html/parser/html_tree_builder_simulator.h"
#include "third_party/blink/renderer/core/html/parser/text_resource_decoder.h" #include "third_party/blink/renderer/core/html/parser/text_resource_decoder.h"
#include "third_party/blink/renderer/core/page/viewport_description.h" #include "third_party/blink/renderer/core/page/viewport_description.h"
...@@ -108,7 +107,6 @@ class BackgroundHTMLParser { ...@@ -108,7 +107,6 @@ class BackgroundHTMLParser {
void UpdateDocument(const String& decoded_data); void UpdateDocument(const String& decoded_data);
BackgroundHTMLInputStream input_; BackgroundHTMLInputStream input_;
HTMLSourceTracker source_tracker_;
std::unique_ptr<HTMLToken> token_; std::unique_ptr<HTMLToken> token_;
std::unique_ptr<HTMLTokenizer> tokenizer_; std::unique_ptr<HTMLTokenizer> tokenizer_;
HTMLTreeBuilderSimulator tree_builder_simulator_; HTMLTreeBuilderSimulator tree_builder_simulator_;
......
/*
* Copyright (C) 2010 Adam Barth. All Rights Reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "third_party/blink/renderer/core/html/parser/html_source_tracker.h"
#include "third_party/blink/renderer/core/html/parser/html_tokenizer.h"
#include "third_party/blink/renderer/platform/wtf/text/string_builder.h"
namespace blink {
HTMLSourceTracker::HTMLSourceTracker() : is_started_(false) {}
void HTMLSourceTracker::Start(SegmentedString& current_input,
HTMLTokenizer* tokenizer,
HTMLToken& token) {
if (token.GetType() == HTMLToken::kUninitialized && !is_started_) {
previous_source_.Clear();
if (NeedToCheckTokenizerBuffer(tokenizer) &&
tokenizer->NumberOfBufferedCharacters())
previous_source_ = tokenizer->BufferedCharacters();
} else {
previous_source_.Append(current_source_);
}
is_started_ = true;
current_source_ = current_input;
token.SetBaseOffset(current_source_.NumberOfCharactersConsumed() -
previous_source_.length());
}
void HTMLSourceTracker::end(SegmentedString& current_input,
HTMLTokenizer* tokenizer,
HTMLToken& token) {
is_started_ = false;
cached_source_for_token_ = String();
// FIXME: This work should really be done by the HTMLTokenizer.
wtf_size_t number_of_buffered_characters = 0u;
if (NeedToCheckTokenizerBuffer(tokenizer)) {
number_of_buffered_characters = tokenizer->NumberOfBufferedCharacters();
}
token.end(current_input.NumberOfCharactersConsumed() -
number_of_buffered_characters);
}
String HTMLSourceTracker::SourceForToken(const HTMLToken& token) {
if (!cached_source_for_token_.IsEmpty())
return cached_source_for_token_;
wtf_size_t length;
if (token.GetType() == HTMLToken::kEndOfFile) {
// Consume the remainder of the input, omitting the null character we use to
// mark the end of the file.
length = previous_source_.length() + current_source_.length() - 1;
} else {
DCHECK(!token.StartIndex());
length = static_cast<wtf_size_t>(token.EndIndex() - token.StartIndex());
}
StringBuilder source;
source.ReserveCapacity(length);
size_t i = 0;
for (; i < length && !previous_source_.IsEmpty(); ++i) {
source.Append(previous_source_.CurrentChar());
previous_source_.Advance();
}
for (; i < length; ++i) {
DCHECK(!current_source_.IsEmpty());
source.Append(current_source_.CurrentChar());
current_source_.Advance();
}
cached_source_for_token_ = source.ToString();
return cached_source_for_token_;
}
bool HTMLSourceTracker::NeedToCheckTokenizerBuffer(HTMLTokenizer* tokenizer) {
HTMLTokenizer::State state = tokenizer->GetState();
// The temporary buffer must not be used unconditionally, because in some
// states (e.g. ScriptDataDoubleEscapedStartState), data is appended to
// both the temporary buffer and the token itself.
return state == HTMLTokenizer::kDataState ||
HTMLTokenizer::IsEndTagBufferingState(state);
}
} // namespace blink
/*
* Copyright (C) 2010 Adam Barth. All Rights Reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef THIRD_PARTY_BLINK_RENDERER_CORE_HTML_PARSER_HTML_SOURCE_TRACKER_H_
#define THIRD_PARTY_BLINK_RENDERER_CORE_HTML_PARSER_HTML_SOURCE_TRACKER_H_
#include "base/macros.h"
#include "third_party/blink/renderer/core/html/parser/html_token.h"
#include "third_party/blink/renderer/platform/text/segmented_string.h"
#include "third_party/blink/renderer/platform/wtf/allocator/allocator.h"
namespace blink {
class HTMLTokenizer;
class HTMLSourceTracker {
DISALLOW_NEW();
public:
HTMLSourceTracker();
// FIXME: Once we move "end" into HTMLTokenizer, rename "start" to
// something that makes it obvious that this method can be called multiple
// times.
void Start(SegmentedString&, HTMLTokenizer*, HTMLToken&);
void end(SegmentedString&, HTMLTokenizer*, HTMLToken&);
String SourceForToken(const HTMLToken&);
private:
bool NeedToCheckTokenizerBuffer(HTMLTokenizer*);
SegmentedString previous_source_;
SegmentedString current_source_;
String cached_source_for_token_;
bool is_started_;
DISALLOW_COPY_AND_ASSIGN(HTMLSourceTracker);
};
} // namespace blink
#endif
...@@ -44,12 +44,12 @@ HTMLViewSourceParser::HTMLViewSourceParser(HTMLViewSourceDocument& document, ...@@ -44,12 +44,12 @@ HTMLViewSourceParser::HTMLViewSourceParser(HTMLViewSourceDocument& document,
void HTMLViewSourceParser::PumpTokenizer() { void HTMLViewSourceParser::PumpTokenizer() {
while (true) { while (true) {
source_tracker_.Start(input_.Current(), tokenizer_.get(), token_); StartTracker(input_.Current(), tokenizer_.get(), token_);
if (!tokenizer_->NextToken(input_.Current(), token_)) if (!tokenizer_->NextToken(input_.Current(), token_))
return; return;
source_tracker_.end(input_.Current(), tokenizer_.get(), token_); EndTracker(input_.Current(), tokenizer_.get(), token_);
GetDocument()->AddSource(source_tracker_.SourceForToken(token_), token_); GetDocument()->AddSource(SourceForToken(token_), token_);
// FIXME: The tokenizer should do this work for us. // FIXME: The tokenizer should do this work for us.
if (token_.GetType() == HTMLToken::kStartTag) if (token_.GetType() == HTMLToken::kStartTag)
...@@ -75,4 +75,80 @@ void HTMLViewSourceParser::Finish() { ...@@ -75,4 +75,80 @@ void HTMLViewSourceParser::Finish() {
} }
} }
void HTMLViewSourceParser::StartTracker(SegmentedString& current_input,
HTMLTokenizer* tokenizer,
HTMLToken& token) {
if (token.GetType() == HTMLToken::kUninitialized && !tracker_is_started_) {
previous_source_.Clear();
if (NeedToCheckTokenizerBuffer(tokenizer) &&
tokenizer->NumberOfBufferedCharacters())
previous_source_ = tokenizer->BufferedCharacters();
} else {
previous_source_.Append(current_source_);
}
tracker_is_started_ = true;
current_source_ = current_input;
token.SetBaseOffset(current_source_.NumberOfCharactersConsumed() -
previous_source_.length());
}
void HTMLViewSourceParser::EndTracker(SegmentedString& current_input,
HTMLTokenizer* tokenizer,
HTMLToken& token) {
tracker_is_started_ = false;
cached_source_for_token_ = String();
// FIXME: This work should really be done by the HTMLTokenizer.
wtf_size_t number_of_buffered_characters = 0u;
if (NeedToCheckTokenizerBuffer(tokenizer)) {
number_of_buffered_characters = tokenizer->NumberOfBufferedCharacters();
}
token.end(current_input.NumberOfCharactersConsumed() -
number_of_buffered_characters);
}
String HTMLViewSourceParser::SourceForToken(const HTMLToken& token) {
if (!cached_source_for_token_.IsEmpty())
return cached_source_for_token_;
wtf_size_t length;
if (token.GetType() == HTMLToken::kEndOfFile) {
// Consume the remainder of the input, omitting the null character we use to
// mark the end of the file.
length = previous_source_.length() + current_source_.length() - 1;
} else {
DCHECK(!token.StartIndex());
length = static_cast<wtf_size_t>(token.EndIndex() - token.StartIndex());
}
StringBuilder source;
source.ReserveCapacity(length);
size_t i = 0;
for (; i < length && !previous_source_.IsEmpty(); ++i) {
source.Append(previous_source_.CurrentChar());
previous_source_.Advance();
}
for (; i < length; ++i) {
DCHECK(!current_source_.IsEmpty());
source.Append(current_source_.CurrentChar());
current_source_.Advance();
}
cached_source_for_token_ = source.ToString();
return cached_source_for_token_;
}
bool HTMLViewSourceParser::NeedToCheckTokenizerBuffer(
HTMLTokenizer* tokenizer) {
HTMLTokenizer::State state = tokenizer->GetState();
// The temporary buffer must not be used unconditionally, because in some
// states (e.g. ScriptDataDoubleEscapedStartState), data is appended to
// both the temporary buffer and the token itself.
return state == HTMLTokenizer::kDataState ||
HTMLTokenizer::IsEndTagBufferingState(state);
}
} // namespace blink } // namespace blink
...@@ -31,7 +31,6 @@ ...@@ -31,7 +31,6 @@
#include "third_party/blink/renderer/core/dom/decoded_data_document_parser.h" #include "third_party/blink/renderer/core/dom/decoded_data_document_parser.h"
#include "third_party/blink/renderer/core/html/html_view_source_document.h" #include "third_party/blink/renderer/core/html/html_view_source_document.h"
#include "third_party/blink/renderer/core/html/parser/html_input_stream.h" #include "third_party/blink/renderer/core/html/parser/html_input_stream.h"
#include "third_party/blink/renderer/core/html/parser/html_source_tracker.h"
#include "third_party/blink/renderer/core/html/parser/html_tokenizer.h" #include "third_party/blink/renderer/core/html/parser/html_tokenizer.h"
namespace blink { namespace blink {
...@@ -56,10 +55,20 @@ class CORE_EXPORT HTMLViewSourceParser final ...@@ -56,10 +55,20 @@ class CORE_EXPORT HTMLViewSourceParser final
void PumpTokenizer(); void PumpTokenizer();
void UpdateTokenizerState(); void UpdateTokenizerState();
void StartTracker(SegmentedString&, HTMLTokenizer*, HTMLToken&);
void EndTracker(SegmentedString&, HTMLTokenizer*, HTMLToken&);
String SourceForToken(const HTMLToken&);
bool NeedToCheckTokenizerBuffer(HTMLTokenizer*);
HTMLInputStream input_; HTMLInputStream input_;
HTMLToken token_; HTMLToken token_;
HTMLSourceTracker source_tracker_;
std::unique_ptr<HTMLTokenizer> tokenizer_; std::unique_ptr<HTMLTokenizer> tokenizer_;
bool tracker_is_started_;
SegmentedString previous_source_;
SegmentedString current_source_;
String cached_source_for_token_;
}; };
} // namespace blink } // namespace blink
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment