diff options
-rw-r--r-- | base/string_util.cc | 2 | ||||
-rw-r--r-- | base/string_util.h | 2 | ||||
-rw-r--r-- | chrome/browser/extensions/user_script_master.cc | 7 | ||||
-rw-r--r-- | chrome/browser/extensions/user_script_master_unittest.cc | 15 |
4 files changed, 25 insertions, 1 deletions
diff --git a/base/string_util.cc b/base/string_util.cc index 5a45f27..01a6907 100644 --- a/base/string_util.cc +++ b/base/string_util.cc @@ -390,6 +390,8 @@ const char kWhitespaceASCII[] = { 0 }; +const char kUtf8ByteOrderMark[] = "\xEF\xBB\xBF"; + template<typename STR> TrimPositions TrimStringT(const STR& input, const typename STR::value_type trim_chars[], diff --git a/base/string_util.h b/base/string_util.h index 6d4a4d9..dec39a6 100644 --- a/base/string_util.h +++ b/base/string_util.h @@ -133,6 +133,8 @@ extern const wchar_t kWhitespaceWide[]; extern const char16 kWhitespaceUTF16[]; extern const char kWhitespaceASCII[]; +extern const char kUtf8ByteOrderMark[]; + // Removes characters in trim_chars from the beginning and end of input. // NOTE: Safe to use the same variable for both input and output. bool TrimString(const std::wstring& input, diff --git a/chrome/browser/extensions/user_script_master.cc b/chrome/browser/extensions/user_script_master.cc index c9228ff..a485a41 100644 --- a/chrome/browser/extensions/user_script_master.cc +++ b/chrome/browser/extensions/user_script_master.cc @@ -46,7 +46,12 @@ bool UserScriptMaster::ScriptReloader::ParseMetadataHeader( // http://wiki.greasespot.net/Metadata_block base::StringPiece line; size_t line_start = 0; - size_t line_end = 0; + + // Skip UTF-8's BOM. + if (script_text.starts_with(kUtf8ByteOrderMark)) + line_start += strlen(kUtf8ByteOrderMark); + + size_t line_end = line_start; bool in_metadata = false; static const base::StringPiece kUserScriptBegin("// ==UserScript=="); diff --git a/chrome/browser/extensions/user_script_master_unittest.cc b/chrome/browser/extensions/user_script_master_unittest.cc index 25ef7fc..6c01939 100644 --- a/chrome/browser/extensions/user_script_master_unittest.cc +++ b/chrome/browser/extensions/user_script_master_unittest.cc @@ -218,3 +218,18 @@ TEST_F(UserScriptMasterTest, Parse6) { EXPECT_TRUE(UserScriptMaster::ScriptReloader::ParseMetadataHeader( text, &script)); } + +TEST_F(UserScriptMasterTest, Parse7) { + const std::string text( + "\xEF\xBB\xBF// ==UserScript==\n" + "// @match http://*.mail.google.com/*\n" + "// ==/UserScript==\n"); + + // Should Ignore UTF-8's BOM. + UserScript script; + EXPECT_TRUE(UserScriptMaster::ScriptReloader::ParseMetadataHeader( + text, &script)); + ASSERT_EQ(1U, script.url_patterns().size()); + EXPECT_EQ("http://*.mail.google.com/*", + script.url_patterns()[0].GetAsString()); +} |