diff options
-rw-r--r-- | chrome_frame/html_utils.cc | 562 | ||||
-rw-r--r-- | chrome_frame/html_utils.h | 240 |
2 files changed, 401 insertions, 401 deletions
diff --git a/chrome_frame/html_utils.cc b/chrome_frame/html_utils.cc index 60fdd7e..944ea11 100644 --- a/chrome_frame/html_utils.cc +++ b/chrome_frame/html_utils.cc @@ -1,281 +1,281 @@ -// Copyright (c) 2009 The Chromium Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style license that can be
-// found in the LICENSE file.
-//
-#include "chrome_frame/html_utils.h"
-
-#include "base/string_util.h"
-#include "base/string_tokenizer.h"
-
-const wchar_t* kQuotes = L"\"'";
-
-HTMLScanner::StringRange::StringRange() {
-}
-
-HTMLScanner::StringRange::StringRange(StrPos start, StrPos end)
- : start_(start), end_(end) {
-}
-
-bool HTMLScanner::StringRange::LowerCaseEqualsASCII(const char* other) const {
- return ::LowerCaseEqualsASCII(start_, end_, other);
-}
-
-bool HTMLScanner::StringRange::Equals(const wchar_t* other) const {
- int ret = wcsncmp(&start_[0], other, end_ - start_);
- if (ret == 0)
- ret = (other[end_ - start_] == L'\0') ? 0 : -1;
- return ret == 0;
-}
-
-std::wstring HTMLScanner::StringRange::Copy() const {
- return std::wstring(start_, end_);
-}
-
-bool HTMLScanner::StringRange::GetTagName(std::wstring* tag_name) const {
- if (*start_ != L'<') {
- LOG(ERROR) << "Badly formatted tag found";
- return false;
- }
-
- StrPos name_start = start_;
- name_start++;
- while (name_start < end_ && IsWhitespace(*name_start))
- name_start++;
-
- if (name_start >= end_) {
- // We seem to have a degenerate tag (i.e. < >). Return false here.
- return false;
- }
-
- StrPos name_end = name_start + 1;
- while (name_end < end_ && !IsWhitespace(*name_end))
- name_end++;
-
- if (name_end > end_) {
- // This looks like an improperly formatted tab ('<foo'). Return false here.
- return false;
- }
-
- tag_name->assign(name_start, name_end);
- return true;
-}
-
-
-bool HTMLScanner::StringRange::GetTagAttribute(const wchar_t* attribute_name,
- StringRange* attribute_value) const {
- if (NULL == attribute_name || NULL == attribute_value) {
- NOTREACHED();
- return false;
- }
-
- // Use this so we can use the convenience method LowerCaseEqualsASCII()
- // from string_util.h.
- std::string search_name_ascii(WideToASCII(attribute_name));
-
- WStringTokenizer tokenizer(start_, end_, L" =/");
- tokenizer.set_options(WStringTokenizer::RETURN_DELIMS);
-
- // Set up the quote chars so that we get quoted attribute values as single
- // tokens.
- tokenizer.set_quote_chars(L"\"'");
-
- const bool PARSE_STATE_NAME = true;
- const bool PARSE_STATE_VALUE = false;
- bool parse_state = PARSE_STATE_NAME;
-
- // Used to skip the first token, which is the tag name.
- bool first_token_skipped = false;
-
- // This is set during a loop iteration in which an '=' sign was spotted.
- // It is used to filter out degenerate tags such as:
- // <meta foo==bar>
- bool last_token_was_delim = false;
-
- // Set this if the attribute name has been found that we might then
- // pick up the value in the next loop iteration.
- bool attribute_name_found = false;
-
- while (tokenizer.GetNext()) {
- // If we have a whitespace delimiter, just keep going. Cases of this should
- // be reduced by the CollapseWhitespace call. If we have an '=' character,
- // we update our state and reiterate.
- if (tokenizer.token_is_delim()) {
- if (*tokenizer.token_begin() == L'=') {
- if (last_token_was_delim) {
- // Looks like we have a badly formed tag, just stop parsing now.
- return false;
- }
- parse_state = !parse_state;
- last_token_was_delim = true;
- }
- continue;
- }
-
- last_token_was_delim = false;
-
- // The first non-delimiter token is the tag name, which we don't want.
- if (!first_token_skipped) {
- first_token_skipped = true;
- continue;
- }
-
- if (PARSE_STATE_NAME == parse_state) {
- // We have a tag name, check to see if it matches our target name:
- if (::LowerCaseEqualsASCII(tokenizer.token_begin(), tokenizer.token_end(),
- search_name_ascii.c_str())) {
- attribute_name_found = true;
- continue;
- }
- } else if (PARSE_STATE_VALUE == parse_state && attribute_name_found) {
- attribute_value->start_ = tokenizer.token_begin();
- attribute_value->end_ = tokenizer.token_end();
-
- // Unquote the attribute value if need be.
- attribute_value->UnQuote();
-
- return true;
- } else if (PARSE_STATE_VALUE == parse_state) {
- // If we haven't found the attribute name we want yet, ignore this token
- // and go back to looking for our name.
- parse_state = PARSE_STATE_NAME;
- }
- }
-
- return false;
-}
-
-bool HTMLScanner::StringRange::UnQuote() {
- if (start_ + 2 > end_) {
- // String's too short to be quoted, bail.
- return false;
- }
-
- if ((*start_ == L'\'' && *(end_ - 1) == L'\'') ||
- (*start_ == L'"' && *(end_ - 1) == L'"')) {
- start_ = start_ + 1;
- end_ = end_ - 1;
- return true;
- }
-
- return false;
-}
-
-HTMLScanner::HTMLScanner(const wchar_t* html_string)
- : html_string_(CollapseWhitespace(html_string, true)),
- quotes_(kQuotes) {
-}
-
-void HTMLScanner::GetTagsByName(const wchar_t* name, StringRangeList* tag_list,
- const wchar_t* stop_tag) {
- DCHECK(NULL != name);
- DCHECK(NULL != tag_list);
- DCHECK(NULL != stop_tag);
-
- StringRange remaining_html(html_string_.begin(), html_string_.end());
-
- std::wstring search_name(name);
- TrimWhitespace(search_name, TRIM_ALL, &search_name);
-
- // Use this so we can use the convenience method LowerCaseEqualsASCII()
- // from string_util.h.
- std::string search_name_ascii(WideToASCII(search_name));
- std::string stop_tag_ascii(WideToASCII(stop_tag));
-
- StringRange current_tag;
- std::wstring current_name;
- while (NextTag(&remaining_html, ¤t_tag)) {
- if (current_tag.GetTagName(¤t_name)) {
- if (LowerCaseEqualsASCII(current_name, search_name_ascii.c_str())) {
- tag_list->push_back(current_tag);
- } else if (LowerCaseEqualsASCII(current_name, stop_tag_ascii.c_str())) {
- // We hit the stop tag so it's time to go home.
- break;
- }
- }
- }
-}
-
-struct ScanState {
- bool in_quote;
- bool in_escape;
- wchar_t quote_char;
- ScanState() : in_quote(false), in_escape(false) {}
-};
-
-bool HTMLScanner::IsQuote(wchar_t c) {
- return quotes_.find(c) != std::wstring::npos;
-}
-
-bool HTMLScanner::IsHTMLCommentClose(StringRange* html_string, StrPos pos) {
- if (pos < html_string->end_ && pos > html_string->start_ + 2 &&
- *pos == L'>') {
- return *(pos-1) == L'-' && *(pos-2) == L'-';
- }
- return false;
-}
-
-bool HTMLScanner::NextTag(StringRange* html_string, StringRange* tag) {
- DCHECK(NULL != html_string);
- DCHECK(NULL != tag);
-
- tag->start_ = html_string->start_;
- while (tag->start_ < html_string->end_ && *tag->start_ != L'<') {
- tag->start_++;
- }
-
- // we went past the end of the string.
- if (tag->start_ >= html_string->end_) {
- return false;
- }
-
- tag->end_ = tag->start_ + 1;
-
- // Get the tag name to see if we are in an HTML comment. If we are, then
- // don't consider quotes. This should work for example:
- // <!-- foo ' --> <meta foo='bar'>
- std::wstring tag_name;
- StringRange start_range(tag->start_, html_string->end_);
- start_range.GetTagName(&tag_name);
- if (StartsWith(tag_name, L"!--", true)) {
- // We're inside a comment tag, keep going until we get out of it.
- while (tag->end_ < html_string->end_ &&
- !IsHTMLCommentClose(html_string, tag->end_)) {
- tag->end_++;
- }
- } else {
- // Properly handle quoted strings within non-comment tags by maintaining
- // some state while scanning. Specifically, we have to maintain state on
- // whether we are inside a string, what the string terminating character
- // will be and whether we are inside an escape sequence.
- ScanState state;
- while (tag->end_ < html_string->end_) {
- if (state.in_quote) {
- if (state.in_escape) {
- state.in_escape = false;
- } else if (*tag->end_ == '\\') {
- state.in_escape = true;
- } else if (*tag->end_ == state.quote_char) {
- state.in_quote = false;
- }
- } else {
- state.in_quote = IsQuote(state.quote_char = *tag->end_);
- }
-
- if (!state.in_quote && *tag->end_ == L'>') {
- break;
- }
- tag->end_++;
- }
- }
-
- // We hit the end_ but found no matching tag closure. Consider this an
- // incomplete tag and do not report it.
- if (tag->end_ >= html_string->end_)
- return false;
-
- // Modify html_string to point to just beyond the end_ of the current tag.
- html_string->start_ = tag->end_ + 1;
-
- return true;
-}
-
+// Copyright (c) 2009 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. +// +#include "chrome_frame/html_utils.h" + +#include "base/string_util.h" +#include "base/string_tokenizer.h" + +const wchar_t* kQuotes = L"\"'"; + +HTMLScanner::StringRange::StringRange() { +} + +HTMLScanner::StringRange::StringRange(StrPos start, StrPos end) + : start_(start), end_(end) { +} + +bool HTMLScanner::StringRange::LowerCaseEqualsASCII(const char* other) const { + return ::LowerCaseEqualsASCII(start_, end_, other); +} + +bool HTMLScanner::StringRange::Equals(const wchar_t* other) const { + int ret = wcsncmp(&start_[0], other, end_ - start_); + if (ret == 0) + ret = (other[end_ - start_] == L'\0') ? 0 : -1; + return ret == 0; +} + +std::wstring HTMLScanner::StringRange::Copy() const { + return std::wstring(start_, end_); +} + +bool HTMLScanner::StringRange::GetTagName(std::wstring* tag_name) const { + if (*start_ != L'<') { + LOG(ERROR) << "Badly formatted tag found"; + return false; + } + + StrPos name_start = start_; + name_start++; + while (name_start < end_ && IsWhitespace(*name_start)) + name_start++; + + if (name_start >= end_) { + // We seem to have a degenerate tag (i.e. < >). Return false here. + return false; + } + + StrPos name_end = name_start + 1; + while (name_end < end_ && !IsWhitespace(*name_end)) + name_end++; + + if (name_end > end_) { + // This looks like an improperly formatted tab ('<foo'). Return false here. + return false; + } + + tag_name->assign(name_start, name_end); + return true; +} + + +bool HTMLScanner::StringRange::GetTagAttribute(const wchar_t* attribute_name, + StringRange* attribute_value) const { + if (NULL == attribute_name || NULL == attribute_value) { + NOTREACHED(); + return false; + } + + // Use this so we can use the convenience method LowerCaseEqualsASCII() + // from string_util.h. + std::string search_name_ascii(WideToASCII(attribute_name)); + + WStringTokenizer tokenizer(start_, end_, L" =/"); + tokenizer.set_options(WStringTokenizer::RETURN_DELIMS); + + // Set up the quote chars so that we get quoted attribute values as single + // tokens. + tokenizer.set_quote_chars(L"\"'"); + + const bool PARSE_STATE_NAME = true; + const bool PARSE_STATE_VALUE = false; + bool parse_state = PARSE_STATE_NAME; + + // Used to skip the first token, which is the tag name. + bool first_token_skipped = false; + + // This is set during a loop iteration in which an '=' sign was spotted. + // It is used to filter out degenerate tags such as: + // <meta foo==bar> + bool last_token_was_delim = false; + + // Set this if the attribute name has been found that we might then + // pick up the value in the next loop iteration. + bool attribute_name_found = false; + + while (tokenizer.GetNext()) { + // If we have a whitespace delimiter, just keep going. Cases of this should + // be reduced by the CollapseWhitespace call. If we have an '=' character, + // we update our state and reiterate. + if (tokenizer.token_is_delim()) { + if (*tokenizer.token_begin() == L'=') { + if (last_token_was_delim) { + // Looks like we have a badly formed tag, just stop parsing now. + return false; + } + parse_state = !parse_state; + last_token_was_delim = true; + } + continue; + } + + last_token_was_delim = false; + + // The first non-delimiter token is the tag name, which we don't want. + if (!first_token_skipped) { + first_token_skipped = true; + continue; + } + + if (PARSE_STATE_NAME == parse_state) { + // We have a tag name, check to see if it matches our target name: + if (::LowerCaseEqualsASCII(tokenizer.token_begin(), tokenizer.token_end(), + search_name_ascii.c_str())) { + attribute_name_found = true; + continue; + } + } else if (PARSE_STATE_VALUE == parse_state && attribute_name_found) { + attribute_value->start_ = tokenizer.token_begin(); + attribute_value->end_ = tokenizer.token_end(); + + // Unquote the attribute value if need be. + attribute_value->UnQuote(); + + return true; + } else if (PARSE_STATE_VALUE == parse_state) { + // If we haven't found the attribute name we want yet, ignore this token + // and go back to looking for our name. + parse_state = PARSE_STATE_NAME; + } + } + + return false; +} + +bool HTMLScanner::StringRange::UnQuote() { + if (start_ + 2 > end_) { + // String's too short to be quoted, bail. + return false; + } + + if ((*start_ == L'\'' && *(end_ - 1) == L'\'') || + (*start_ == L'"' && *(end_ - 1) == L'"')) { + start_ = start_ + 1; + end_ = end_ - 1; + return true; + } + + return false; +} + +HTMLScanner::HTMLScanner(const wchar_t* html_string) + : html_string_(CollapseWhitespace(html_string, true)), + quotes_(kQuotes) { +} + +void HTMLScanner::GetTagsByName(const wchar_t* name, StringRangeList* tag_list, + const wchar_t* stop_tag) { + DCHECK(NULL != name); + DCHECK(NULL != tag_list); + DCHECK(NULL != stop_tag); + + StringRange remaining_html(html_string_.begin(), html_string_.end()); + + std::wstring search_name(name); + TrimWhitespace(search_name, TRIM_ALL, &search_name); + + // Use this so we can use the convenience method LowerCaseEqualsASCII() + // from string_util.h. + std::string search_name_ascii(WideToASCII(search_name)); + std::string stop_tag_ascii(WideToASCII(stop_tag)); + + StringRange current_tag; + std::wstring current_name; + while (NextTag(&remaining_html, ¤t_tag)) { + if (current_tag.GetTagName(¤t_name)) { + if (LowerCaseEqualsASCII(current_name, search_name_ascii.c_str())) { + tag_list->push_back(current_tag); + } else if (LowerCaseEqualsASCII(current_name, stop_tag_ascii.c_str())) { + // We hit the stop tag so it's time to go home. + break; + } + } + } +} + +struct ScanState { + bool in_quote; + bool in_escape; + wchar_t quote_char; + ScanState() : in_quote(false), in_escape(false) {} +}; + +bool HTMLScanner::IsQuote(wchar_t c) { + return quotes_.find(c) != std::wstring::npos; +} + +bool HTMLScanner::IsHTMLCommentClose(StringRange* html_string, StrPos pos) { + if (pos < html_string->end_ && pos > html_string->start_ + 2 && + *pos == L'>') { + return *(pos-1) == L'-' && *(pos-2) == L'-'; + } + return false; +} + +bool HTMLScanner::NextTag(StringRange* html_string, StringRange* tag) { + DCHECK(NULL != html_string); + DCHECK(NULL != tag); + + tag->start_ = html_string->start_; + while (tag->start_ < html_string->end_ && *tag->start_ != L'<') { + tag->start_++; + } + + // we went past the end of the string. + if (tag->start_ >= html_string->end_) { + return false; + } + + tag->end_ = tag->start_ + 1; + + // Get the tag name to see if we are in an HTML comment. If we are, then + // don't consider quotes. This should work for example: + // <!-- foo ' --> <meta foo='bar'> + std::wstring tag_name; + StringRange start_range(tag->start_, html_string->end_); + start_range.GetTagName(&tag_name); + if (StartsWith(tag_name, L"!--", true)) { + // We're inside a comment tag, keep going until we get out of it. + while (tag->end_ < html_string->end_ && + !IsHTMLCommentClose(html_string, tag->end_)) { + tag->end_++; + } + } else { + // Properly handle quoted strings within non-comment tags by maintaining + // some state while scanning. Specifically, we have to maintain state on + // whether we are inside a string, what the string terminating character + // will be and whether we are inside an escape sequence. + ScanState state; + while (tag->end_ < html_string->end_) { + if (state.in_quote) { + if (state.in_escape) { + state.in_escape = false; + } else if (*tag->end_ == '\\') { + state.in_escape = true; + } else if (*tag->end_ == state.quote_char) { + state.in_quote = false; + } + } else { + state.in_quote = IsQuote(state.quote_char = *tag->end_); + } + + if (!state.in_quote && *tag->end_ == L'>') { + break; + } + tag->end_++; + } + } + + // We hit the end_ but found no matching tag closure. Consider this an + // incomplete tag and do not report it. + if (tag->end_ >= html_string->end_) + return false; + + // Modify html_string to point to just beyond the end_ of the current tag. + html_string->start_ = tag->end_ + 1; + + return true; +} + diff --git a/chrome_frame/html_utils.h b/chrome_frame/html_utils.h index c670b00..4b89a2d 100644 --- a/chrome_frame/html_utils.h +++ b/chrome_frame/html_utils.h @@ -1,120 +1,120 @@ -// Copyright (c) 2009 The Chromium Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style license that can be
-// found in the LICENSE file.
-
-// This file defines utility functions for working with html.
-
-#ifndef CHROME_FRAME_HTML_UTILS_H_
-#define CHROME_FRAME_HTML_UTILS_H_
-
-#include <string>
-#include <vector>
-
-#include "base/basictypes.h"
-#include "testing/gtest/include/gtest/gtest_prod.h"
-
-// Forward declarations
-class HtmlUtilUnittest;
-
-//
-// Class designed to take a string of HTML and extract from it named
-// attribute values from named tags.
-//
-// Caveat: this class currently doesn't handle multi-word UTF-16 encoded
-// characters. Doesn't handle implies that any data following such a
-// character could possibly be misinterpreted.
-//
-class HTMLScanner {
- public:
- typedef std::wstring::const_iterator StrPos;
-
- // Structure maintaining const_iterators into html_string_.
- class StringRange {
- friend class HTMLScanner;
- public:
- StringRange();
- StringRange(StrPos start, StrPos end);
-
- bool LowerCaseEqualsASCII(const char* other) const;
- bool Equals(const wchar_t* other) const;
-
- // Copies the data described by StringRange into destination.
- std::wstring Copy() const;
-
- // If this StringRange represents a tag, this method extracts the name of
- // the tag and sticks it in tag_name.
- // Returns true if the tag name was successfully extracted.
- // Returns false if this string doesn't look like a valid tag.
- bool GetTagName(std::wstring* tag_name) const;
-
- // From a given string range, uses a string tokenizer to extract the value
- // of the named attribute if a simple scan finds that the attribute name is
- // present.
- //
- // Returns true if the named attribute can be located and it has a value
- // which has been placed in attribute_value.
- //
- // Note that the attribute value is unquoted here as well, so that
- // GetTagAttribute(*<foo bar="baz">*, L"bar", *out_value*) will stick
- // 'bar' in out_value and not '"bar"'.
- //
- // Returns false if the named attribute is not present in the tag or if it
- // did not have a value.
- //
- bool GetTagAttribute(const wchar_t* attribute_name,
- StringRange* attribute_value) const;
-
- // Unquotes a StringRange by removing a matching pair of either ' or "
- // characters from the beginning and end of the string if present.
- // Returns true if string was modified, false otherwise.
- bool UnQuote();
- private:
- StrPos start_;
- StrPos end_;
- };
-
- typedef std::vector<StringRange> StringRangeList;
-
- // html_string must be a null-terminated string containing the HTML
- // to be scanned.
- explicit HTMLScanner(const wchar_t* html_string);
-
- // Returns the set of ranges denoting HTML tags that match the given name.
- // If stop_tag_name is given, then as soon as a tag with this name is
- // encountered this method will return.
- void GetTagsByName(const wchar_t* name, StringRangeList* tag_list,
- const wchar_t* stop_tag_name);
-
- private:
- friend class HtmlUtilUnittest;
- FRIEND_TEST(HtmlUtilUnittest, BasicTest);
-
- // Given html_string which represents the remaining html range, this method
- // returns the next tag in tag and advances html_string to one character after
- // the end of tag. This method is intended to be called repeatedly to extract
- // all of the tags in sequence.
- //
- // Returns true if another tag was found and 'tag' was populated with a valid
- // range.
- // Returns false if we have reached the end of the html data.
- bool NextTag(StringRange* html_string, StringRange* tag);
-
- // Returns true if c can be found in quotes_, false otherwise
- bool IsQuote(wchar_t c);
-
- // Returns true if pos refers to the last character in an HTML comment in a
- // string described by html_string, false otherwise.
- // For example with html_string describing <!-- foo> -->, pos must refer to
- // the last > for this method to return true.
- bool IsHTMLCommentClose(StringRange* html_string, StrPos pos);
-
- // We store a (CollapsedWhitespace'd) copy of the html data.
- const std::wstring html_string_;
-
- // Store the string of quote characters to avoid repeated construction.
- const std::wstring quotes_;
-
- DISALLOW_COPY_AND_ASSIGN(HTMLScanner);
-};
-
-#endif // CHROME_FRAME_HTML_UTILS_H_
+// Copyright (c) 2009 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// This file defines utility functions for working with html. + +#ifndef CHROME_FRAME_HTML_UTILS_H_ +#define CHROME_FRAME_HTML_UTILS_H_ + +#include <string> +#include <vector> + +#include "base/basictypes.h" +#include "testing/gtest/include/gtest/gtest_prod.h" + +// Forward declarations +class HtmlUtilUnittest; + +// +// Class designed to take a string of HTML and extract from it named +// attribute values from named tags. +// +// Caveat: this class currently doesn't handle multi-word UTF-16 encoded +// characters. Doesn't handle implies that any data following such a +// character could possibly be misinterpreted. +// +class HTMLScanner { + public: + typedef std::wstring::const_iterator StrPos; + + // Structure maintaining const_iterators into html_string_. + class StringRange { + friend class HTMLScanner; + public: + StringRange(); + StringRange(StrPos start, StrPos end); + + bool LowerCaseEqualsASCII(const char* other) const; + bool Equals(const wchar_t* other) const; + + // Copies the data described by StringRange into destination. + std::wstring Copy() const; + + // If this StringRange represents a tag, this method extracts the name of + // the tag and sticks it in tag_name. + // Returns true if the tag name was successfully extracted. + // Returns false if this string doesn't look like a valid tag. + bool GetTagName(std::wstring* tag_name) const; + + // From a given string range, uses a string tokenizer to extract the value + // of the named attribute if a simple scan finds that the attribute name is + // present. + // + // Returns true if the named attribute can be located and it has a value + // which has been placed in attribute_value. + // + // Note that the attribute value is unquoted here as well, so that + // GetTagAttribute(*<foo bar="baz">*, L"bar", *out_value*) will stick + // 'bar' in out_value and not '"bar"'. + // + // Returns false if the named attribute is not present in the tag or if it + // did not have a value. + // + bool GetTagAttribute(const wchar_t* attribute_name, + StringRange* attribute_value) const; + + // Unquotes a StringRange by removing a matching pair of either ' or " + // characters from the beginning and end of the string if present. + // Returns true if string was modified, false otherwise. + bool UnQuote(); + private: + StrPos start_; + StrPos end_; + }; + + typedef std::vector<StringRange> StringRangeList; + + // html_string must be a null-terminated string containing the HTML + // to be scanned. + explicit HTMLScanner(const wchar_t* html_string); + + // Returns the set of ranges denoting HTML tags that match the given name. + // If stop_tag_name is given, then as soon as a tag with this name is + // encountered this method will return. + void GetTagsByName(const wchar_t* name, StringRangeList* tag_list, + const wchar_t* stop_tag_name); + + private: + friend class HtmlUtilUnittest; + FRIEND_TEST(HtmlUtilUnittest, BasicTest); + + // Given html_string which represents the remaining html range, this method + // returns the next tag in tag and advances html_string to one character after + // the end of tag. This method is intended to be called repeatedly to extract + // all of the tags in sequence. + // + // Returns true if another tag was found and 'tag' was populated with a valid + // range. + // Returns false if we have reached the end of the html data. + bool NextTag(StringRange* html_string, StringRange* tag); + + // Returns true if c can be found in quotes_, false otherwise + bool IsQuote(wchar_t c); + + // Returns true if pos refers to the last character in an HTML comment in a + // string described by html_string, false otherwise. + // For example with html_string describing <!-- foo> -->, pos must refer to + // the last > for this method to return true. + bool IsHTMLCommentClose(StringRange* html_string, StrPos pos); + + // We store a (CollapsedWhitespace'd) copy of the html data. + const std::wstring html_string_; + + // Store the string of quote characters to avoid repeated construction. + const std::wstring quotes_; + + DISALLOW_COPY_AND_ASSIGN(HTMLScanner); +}; + +#endif // CHROME_FRAME_HTML_UTILS_H_ |