Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(88)

Side by Side Diff: third_party/WebKit/Source/web/tests/WebFrameSerializerTest.cpp

Issue 2538953002: Remove hidden elements from MHTML (Closed)
Patch Set: Fix trybots Created 3 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 /* 1 /*
2 * Copyright (C) 2011 Google Inc. All rights reserved. 2 * Copyright (C) 2011 Google Inc. All rights reserved.
3 * 3 *
4 * Redistribution and use in source and binary forms, with or without 4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are 5 * modification, are permitted provided that the following conditions are
6 * met: 6 * met:
7 * 7 *
8 * * Redistributions of source code must retain the above copyright 8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer. 9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above 10 * * Redistributions in binary form must reproduce the above
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after
68 68
69 WebString getContentID(WebFrame*) final { return WebString("<cid>"); } 69 WebString getContentID(WebFrame*) final { return WebString("<cid>"); }
70 70
71 WebFrameSerializerCacheControlPolicy cacheControlPolicy() final { 71 WebFrameSerializerCacheControlPolicy cacheControlPolicy() final {
72 return WebFrameSerializerCacheControlPolicy::None; 72 return WebFrameSerializerCacheControlPolicy::None;
73 } 73 }
74 74
75 bool useBinaryEncoding() final { return false; } 75 bool useBinaryEncoding() final { return false; }
76 }; 76 };
77 77
78 // Returns the count of match for substring |pattern| in string |str|.
79 int matchSubstring(const String& str, const char* pattern, size_t size) {
80 int matches = 0;
81 size_t start = 0;
82 while (true) {
83 size_t pos = str.find(pattern, start);
84 if (pos == WTF::kNotFound)
85 break;
86 matches++;
87 start = pos + size;
88 }
89 return matches;
90 }
91
78 } // namespace 92 } // namespace
79 93
80 class WebFrameSerializerTest : public testing::Test { 94 class WebFrameSerializerTest : public testing::Test {
81 protected: 95 protected:
82 WebFrameSerializerTest() { m_helper.initialize(); } 96 WebFrameSerializerTest() { m_helper.initialize(); }
83 97
84 ~WebFrameSerializerTest() override { 98 ~WebFrameSerializerTest() override {
85 Platform::current()->getURLLoaderMockFactory()->unregisterAllURLs(); 99 Platform::current()->getURLLoaderMockFactory()->unregisterAllURLs();
86 WebCache::clear(); 100 WebCache::clear();
87 } 101 }
(...skipping 128 matching lines...) Expand 10 before | Expand all | Expand 10 after
216 EXPECT_NE(WTF::kNotFound, mhtml.find("id=")); 230 EXPECT_NE(WTF::kNotFound, mhtml.find("id="));
217 231
218 // srcdoc attribute of frame element should be replaced with src attribute. 232 // srcdoc attribute of frame element should be replaced with src attribute.
219 EXPECT_EQ(WTF::kNotFound, mhtml.find("srcdoc=")); 233 EXPECT_EQ(WTF::kNotFound, mhtml.find("srcdoc="));
220 EXPECT_NE(WTF::kNotFound, mhtml.find("src=")); 234 EXPECT_NE(WTF::kNotFound, mhtml.find("src="));
221 } 235 }
222 236
223 TEST_F(WebFrameSerializerSanitizationTest, DisableFormElements) { 237 TEST_F(WebFrameSerializerSanitizationTest, DisableFormElements) {
224 String mhtml = generateMHTMLParts("http://www.test.com", "form.html"); 238 String mhtml = generateMHTMLParts("http://www.test.com", "form.html");
225 239
226 int matches = 0; 240 const char kDisabledAttr[] = "disabled=3D\"\"";
227 size_t start = 0; 241 int matches =
228 while (true) { 242 matchSubstring(mhtml, kDisabledAttr, arraysize(kDisabledAttr) - 1);
229 const char kDisabledAttr[] = "disabled=3D\"\"";
230 size_t pos = mhtml.find(kDisabledAttr, start);
231 if (pos == WTF::kNotFound)
232 break;
233 matches++;
234 start = pos + arraysize(kDisabledAttr) - 1;
235 }
236 EXPECT_EQ(21, matches); 243 EXPECT_EQ(21, matches);
237 } 244 }
238 245
246 TEST_F(WebFrameSerializerSanitizationTest, RemoveHiddenElements) {
247 String mhtml =
248 generateMHTMLParts("http://www.test.com", "hidden_elements.html");
249
250 // These hidden elements that do not affect layout should be removed.
251 EXPECT_EQ(WTF::kNotFound, mhtml.find("<h1"));
252 EXPECT_EQ(WTF::kNotFound, mhtml.find("<p id=3D\"hidden_id\""));
253 EXPECT_EQ(WTF::kNotFound, mhtml.find("<input type=3D\"hidden\""));
254
255 // These default-hidden elements should not be removed.
256 EXPECT_NE(WTF::kNotFound, mhtml.find("<html"));
257 EXPECT_NE(WTF::kNotFound, mhtml.find("<head"));
258 EXPECT_NE(WTF::kNotFound, mhtml.find("<style"));
259 EXPECT_NE(WTF::kNotFound, mhtml.find("<title"));
260 EXPECT_NE(WTF::kNotFound, mhtml.find("<link"));
261 EXPECT_NE(WTF::kNotFound, mhtml.find("<datalist"));
262 EXPECT_NE(WTF::kNotFound, mhtml.find("<option"));
263 // One for meta in head and another for meta in body.
264 EXPECT_EQ(2, matchSubstring(mhtml, "<meta", 5));
265
266 // These hidden elements that affect layout should remain intact.
267 EXPECT_NE(WTF::kNotFound, mhtml.find("<h2"));
268
269 // These visible elements should remain intact.
270 EXPECT_NE(WTF::kNotFound, mhtml.find("<h2"));
271 EXPECT_NE(WTF::kNotFound, mhtml.find("<p id=3D\"visible_id\""));
272 EXPECT_NE(WTF::kNotFound, mhtml.find("<form"));
273 EXPECT_NE(WTF::kNotFound, mhtml.find("<input type=3D\"text\""));
274 EXPECT_NE(WTF::kNotFound, mhtml.find("<div"));
275 }
276
239 } // namespace blink 277 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698