summaryrefslogtreecommitdiffstats
path: root/chrome
diff options
context:
space:
mode:
authorcira@chromium.org <cira@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2010-06-03 22:34:26 +0000
committercira@chromium.org <cira@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2010-06-03 22:34:26 +0000
commitb085fa9b1f77c3fdad7f50cf66733c9cf820caf8 (patch)
treea188ca457f4a646b1008ee21502a934fde535929 /chrome
parentb39171b47da84c18b4dfabfaebe0a6318c96bc68 (diff)
downloadchromium_src-b085fa9b1f77c3fdad7f50cf66733c9cf820caf8.zip
chromium_src-b085fa9b1f77c3fdad7f50cf66733c9cf820caf8.tar.gz
chromium_src-b085fa9b1f77c3fdad7f50cf66733c9cf820caf8.tar.bz2
Remove BOM from js and css files in content script.
BUG=38152 TEST=Inject css file with BOM using an extension. It should be injected without BOM. Review URL: http://codereview.chromium.org/2453005 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@48885 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome')
-rw-r--r--chrome/browser/extensions/user_script_master.cc15
-rw-r--r--chrome/browser/extensions/user_script_master_unittest.cc57
2 files changed, 51 insertions, 21 deletions
diff --git a/chrome/browser/extensions/user_script_master.cc b/chrome/browser/extensions/user_script_master.cc
index bedc7a8..960bc49 100644
--- a/chrome/browser/extensions/user_script_master.cc
+++ b/chrome/browser/extensions/user_script_master.cc
@@ -53,11 +53,6 @@ bool UserScriptMaster::ScriptReloader::ParseMetadataHeader(
// http://wiki.greasespot.net/Metadata_block
base::StringPiece line;
size_t line_start = 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;
@@ -165,11 +160,19 @@ static bool LoadScriptContent(UserScript::File* script_file) {
return false;
}
- script_file->set_content(content);
+ // Remove BOM from the content.
+ std::string::size_type index = content.find(kUtf8ByteOrderMark);
+ if (index == 0) {
+ script_file->set_content(content.substr(strlen(kUtf8ByteOrderMark)));
+ } else {
+ script_file->set_content(content);
+ }
+
LOG(INFO) << "Loaded user script file: " << path.value();
return true;
}
+// static
void UserScriptMaster::ScriptReloader::LoadScriptsFromDirectory(
const FilePath& script_dir, UserScriptList* result) {
// Clear the list. We will populate it with the scripts found in script_dir.
diff --git a/chrome/browser/extensions/user_script_master_unittest.cc b/chrome/browser/extensions/user_script_master_unittest.cc
index ec8c728..3760941 100644
--- a/chrome/browser/extensions/user_script_master_unittest.cc
+++ b/chrome/browser/extensions/user_script_master_unittest.cc
@@ -203,21 +203,6 @@ TEST_F(UserScriptMasterTest, Parse6) {
}
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());
-}
-
-TEST_F(UserScriptMasterTest, Parse8) {
// Greasemonkey allows there to be any leading text before the comment marker.
const std::string text(
"// ==UserScript==\n"
@@ -235,3 +220,45 @@ TEST_F(UserScriptMasterTest, Parse8) {
EXPECT_EQ("http://mail.yahoo.com/*",
script.url_patterns()[0].GetAsString());
}
+
+TEST_F(UserScriptMasterTest, SkipBOMAtTheBeginning) {
+ FilePath path = script_dir_.AppendASCII("script.user.js");
+
+ const std::string content(
+ "\xEF\xBB\xBF// ==UserScript==\n"
+ "// @match http://*.mail.google.com/*\n"
+ "// ==/UserScript==\n");
+ size_t written = file_util::WriteFile(path, content.c_str(), content.size());
+ ASSERT_EQ(written, content.size());
+
+ UserScriptList script_list;
+ UserScriptMaster::ScriptReloader::LoadScriptsFromDirectory(
+ script_dir_, &script_list);
+ ASSERT_EQ(1U, script_list.size());
+
+ EXPECT_EQ(content.substr(3),
+ script_list[0].js_scripts()[0].GetContent().as_string());
+ EXPECT_EQ("http://*.mail.google.com/*",
+ script_list[0].url_patterns()[0].GetAsString());
+}
+
+TEST_F(UserScriptMasterTest, LeaveBOMNotAtTheBeginning) {
+ FilePath path = script_dir_.AppendASCII("script.user.js");
+
+ const std::string content(
+ "// ==UserScript==\n"
+ "// @match http://*.mail.google.com/*\n"
+ "// ==/UserScript==\n"
+ "// @bom \xEF\xBB\xBF");
+ size_t written = file_util::WriteFile(path, content.c_str(), content.size());
+ ASSERT_EQ(written, content.size());
+
+ UserScriptList script_list;
+ UserScriptMaster::ScriptReloader::LoadScriptsFromDirectory(
+ script_dir_, &script_list);
+ ASSERT_EQ(1U, script_list.size());
+
+ EXPECT_EQ(content, script_list[0].js_scripts()[0].GetContent().as_string());
+ EXPECT_EQ("http://*.mail.google.com/*",
+ script_list[0].url_patterns()[0].GetAsString());
+}