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

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

Issue 2912213002: Support serializing shadow DOM to MHTML (Closed)
Patch Set: Add .gitattributes Created 3 years, 6 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 83 matching lines...) Expand 10 before | Expand all | Expand 10 after
94 protected: 94 protected:
95 WebFrameSerializerSanitizationTest() { helper_.Initialize(); } 95 WebFrameSerializerSanitizationTest() { helper_.Initialize(); }
96 96
97 ~WebFrameSerializerSanitizationTest() override { 97 ~WebFrameSerializerSanitizationTest() override {
98 Platform::Current() 98 Platform::Current()
99 ->GetURLLoaderMockFactory() 99 ->GetURLLoaderMockFactory()
100 ->UnregisterAllURLsAndClearMemoryCache(); 100 ->UnregisterAllURLsAndClearMemoryCache();
101 } 101 }
102 102
103 String GenerateMHTMLFromHtml(const String& url, const String& file_name) { 103 String GenerateMHTMLFromHtml(const String& url, const String& file_name) {
104 return GenerateMHTML(url, file_name, "text/html", false); 104 LoadFrame(url, file_name, "text/html");
105 return GenerateMHTML(false);
105 } 106 }
106 107
107 String GenerateMHTMLPartsFromPng(const String& url, const String& file_name) { 108 String GenerateMHTMLPartsFromPng(const String& url, const String& file_name) {
108 return GenerateMHTML(url, file_name, "image/png", true); 109 LoadFrame(url, file_name, "image/png");
110 return GenerateMHTML(true);
109 } 111 }
110 112
111 String GenerateMHTML(const String& url, 113 void LoadFrame(const String& url,
112 const String& file_name, 114 const String& file_name,
113 const String& mime_type, 115 const String& mime_type) {
114 const bool only_body_parts) {
115 KURL parsed_url(kParsedURLString, url); 116 KURL parsed_url(kParsedURLString, url);
116 String file_path("frameserialization/" + file_name); 117 String file_path("frameserialization/" + file_name);
117 RegisterMockedFileURLLoad(parsed_url, file_path, mime_type); 118 RegisterMockedFileURLLoad(parsed_url, file_path, mime_type);
118 FrameTestHelpers::LoadFrame(MainFrameImpl(), url.Utf8().data()); 119 FrameTestHelpers::LoadFrame(MainFrameImpl(), url.Utf8().data());
120 MainFrameImpl()->GetFrame()->View()->UpdateAllLifecyclePhases();
121 }
122
123 String GenerateMHTML(const bool only_body_parts) {
119 // Boundaries are normally randomly generated but this one is predefined for 124 // Boundaries are normally randomly generated but this one is predefined for
120 // simplicity and as good as any other. Plus it gets used in almost all the 125 // simplicity and as good as any other. Plus it gets used in almost all the
121 // examples in the MHTML spec - RFC 2557. 126 // examples in the MHTML spec - RFC 2557.
122 const WebString boundary("boundary-example"); 127 const WebString boundary("boundary-example");
123 StringBuilder mhtml; 128 StringBuilder mhtml;
124 if (!only_body_parts) { 129 if (!only_body_parts) {
125 WebThreadSafeData header_result = WebFrameSerializer::GenerateMHTMLHeader( 130 WebThreadSafeData header_result = WebFrameSerializer::GenerateMHTMLHeader(
126 boundary, MainFrameImpl(), &mhtml_delegate_); 131 boundary, MainFrameImpl(), &mhtml_delegate_);
127 mhtml.Append(header_result.Data(), header_result.size()); 132 mhtml.Append(header_result.Data(), header_result.size());
128 } 133 }
(...skipping 13 matching lines...) Expand all
142 if (!only_body_parts) { 147 if (!only_body_parts) {
143 // Validate the generated MHTML. 148 // Validate the generated MHTML.
144 MHTMLParser parser(SharedBuffer::Create(mhtml_string.Characters8(), 149 MHTMLParser parser(SharedBuffer::Create(mhtml_string.Characters8(),
145 size_t(mhtml_string.length()))); 150 size_t(mhtml_string.length())));
146 EXPECT_FALSE(parser.ParseArchive().IsEmpty()) 151 EXPECT_FALSE(parser.ParseArchive().IsEmpty())
147 << "Generated MHTML is not well formed"; 152 << "Generated MHTML is not well formed";
148 } 153 }
149 return mhtml_string; 154 return mhtml_string;
150 } 155 }
151 156
157 ShadowRoot* SetShadowContent(TreeScope& scope,
158 const char* host,
159 ShadowRootType shadow_type,
160 const char* shadow_content,
161 bool delegates_focus = false) {
162 ShadowRoot* shadow_root =
163 scope.getElementById(AtomicString::FromUTF8(host))
164 ->CreateShadowRootInternal(shadow_type, ASSERT_NO_EXCEPTION);
165 shadow_root->SetDelegatesFocus(delegates_focus);
166 shadow_root->setInnerHTML(String::FromUTF8(shadow_content),
167 ASSERT_NO_EXCEPTION);
168 scope.GetDocument().View()->UpdateAllLifecyclePhases();
169 return shadow_root;
170 }
171
152 void SetRemovePopupOverlay(bool remove_popup_overlay) { 172 void SetRemovePopupOverlay(bool remove_popup_overlay) {
153 mhtml_delegate_.SetRemovePopupOverlay(remove_popup_overlay); 173 mhtml_delegate_.SetRemovePopupOverlay(remove_popup_overlay);
154 } 174 }
155 175
156 void RegisterMockedFileURLLoad(const KURL& url, 176 void RegisterMockedFileURLLoad(const KURL& url,
157 const String& file_path, 177 const String& file_path,
158 const String& mime_type = "image/png") { 178 const String& mime_type = "image/png") {
159 URLTestHelpers::RegisterMockedURLLoad( 179 URLTestHelpers::RegisterMockedURLLoad(
160 url, testing::WebTestDataPath(file_path.Utf8().data()), mime_type); 180 url, testing::WebTestDataPath(file_path.Utf8().data()), mime_type);
161 } 181 }
(...skipping 177 matching lines...) Expand 10 before | Expand all | Expand 10 after
339 EXPECT_EQ(WTF::kNotFound, 359 EXPECT_EQ(WTF::kNotFound,
340 mhtml.Find("<meta http-equiv=3D\"Content-Security-Policy")); 360 mhtml.Find("<meta http-equiv=3D\"Content-Security-Policy"));
341 EXPECT_NE(WTF::kNotFound, mhtml.Find("<meta name=3D\"description")); 361 EXPECT_NE(WTF::kNotFound, mhtml.Find("<meta name=3D\"description"));
342 EXPECT_NE(WTF::kNotFound, mhtml.Find("<meta http-equiv=3D\"refresh")); 362 EXPECT_NE(WTF::kNotFound, mhtml.Find("<meta http-equiv=3D\"refresh"));
343 363
344 // If an element is removed, its children should also be skipped. 364 // If an element is removed, its children should also be skipped.
345 EXPECT_EQ(WTF::kNotFound, mhtml.Find("<select")); 365 EXPECT_EQ(WTF::kNotFound, mhtml.Find("<select"));
346 EXPECT_EQ(WTF::kNotFound, mhtml.Find("<option")); 366 EXPECT_EQ(WTF::kNotFound, mhtml.Find("<option"));
347 } 367 }
348 368
369 TEST_F(WebFrameSerializerSanitizationTest, ShadowDOM) {
370 LoadFrame("http://www.test.com", "shadow_dom.html", "text/html");
371 Document* document = MainFrameImpl()->GetFrame()->GetDocument();
372 SetShadowContent(*document, "h1", ShadowRootType::V0, "V0 shadow");
373 ShadowRoot* shadowRoot =
374 SetShadowContent(*document, "h2", ShadowRootType::kOpen,
375 "Parent shadow\n<p id=\"h3\">Foo</p>", true);
376 SetShadowContent(*shadowRoot, "h3", ShadowRootType::kClosed, "Nested shadow");
377 String mhtml = GenerateMHTML(false);
378
379 // Template with special attribute should be created for each shadow DOM tree.
380 EXPECT_NE(WTF::kNotFound, mhtml.Find("<template shadowmode=3D\"v0\">"));
381 EXPECT_NE(WTF::kNotFound,
382 mhtml.Find("<template shadowmode=3D\"open\" shadowdelegatesfocus"));
383 EXPECT_NE(WTF::kNotFound, mhtml.Find("<template shadowmode=3D\"closed\">"));
384
385 // The special attribute present in the original page should be removed.
386 EXPECT_EQ(WTF::kNotFound, mhtml.Find("<template shadowmode=3D\"foo\">"));
387 }
388
349 } // namespace blink 389 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698