Skip to content
This repository was archived by the owner on Apr 10, 2025. It is now read-only.

Commit c1827bb

Browse files
morlovichcrowell
authored andcommitted
Make dedup_image_filter work in cases where we generate IDs:
don't produce duplicate IDs for all the duplicates. Should address #1369
1 parent 2a3b127 commit c1827bb

File tree

2 files changed

+29
-26
lines changed

2 files changed

+29
-26
lines changed

net/instaweb/rewriter/dedup_inlined_images_filter.cc

+2-1
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,8 @@ void DedupInlinedImagesFilter::EndElementImpl(HtmlElement* element) {
116116
GoogleString element_id;
117117
const char* id = element->AttributeValue(HtmlName::kId);
118118
if (id == NULL || id[0] == '\0') {
119-
element_id = StrCat("pagespeed_img_", hash);
119+
element_id = StrCat("pagespeed_img_", hash,
120+
IntegerToString(++snippet_id_));
120121
driver()->AddAttribute(element, HtmlName::kId, element_id);
121122
} else {
122123
element_id = id;

net/instaweb/rewriter/dedup_inlined_images_filter_test.cc

+27-25
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ const char kInlinedScriptFormat[] =
8080
"<script type=\"text/javascript\""
8181
" id=\"pagespeed_script_%d\" data-pagespeed-no-defer>"
8282
"pagespeed.dedupInlinedImages.inlineImg("
83-
"'pagespeed_img_0','pagespeed_img_0','pagespeed_script_%d'"
83+
"'pagespeed_img_0%d','pagespeed_img_0%d','pagespeed_script_%d'"
8484
");</script>";
8585

8686
const char kHtmlWrapperFormat[] =
@@ -162,7 +162,7 @@ TEST_F(DedupInlinedImagesTest, InlineSingleSmallImage) {
162162
TestDedupImages("inline_single_small_image", "", "",
163163
StrCat("<img src='", kCuppaPngFilename, "'>"),
164164
StrCat("<img src='", kCuppaPngInlineData,
165-
"' id=\"pagespeed_img_0\">"));
165+
"' id=\"pagespeed_img_01\">"));
166166
}
167167

168168
TEST_F(DedupInlinedImagesTest, DontInlineLargeImage) {
@@ -178,10 +178,10 @@ TEST_F(DedupInlinedImagesTest, DedupSecondSmallImage) {
178178
StrCat("<img src='", kCuppaPngFilename, "'>\n",
179179
"<img src='", kCuppaPngFilename, "'>"),
180180
StrCat("<img src='", kCuppaPngInlineData,
181-
"' id=\"pagespeed_img_0\">\n",
181+
"' id=\"pagespeed_img_01\">\n",
182182
InsertScriptBefore(
183-
StrCat("<img id=\"pagespeed_img_0\">",
184-
StringPrintf(kInlinedScriptFormat, 1, 1)))));
183+
StrCat("<img id=\"pagespeed_img_02\">",
184+
StringPrintf(kInlinedScriptFormat, 3, 1, 2, 3)))));
185185
}
186186

187187
TEST_F(DedupInlinedImagesTest, DedupManySmallImages) {
@@ -190,12 +190,12 @@ TEST_F(DedupInlinedImagesTest, DedupManySmallImages) {
190190
TestDedupImages(
191191
"dedup_many_small_images", "", "",
192192
StrCat(image, "\n", image, "\n", image),
193-
StrCat("<img src='", kCuppaPngInlineData, "' id=\"pagespeed_img_0\">\n",
193+
StrCat("<img src='", kCuppaPngInlineData, "' id=\"pagespeed_img_01\">\n",
194194
InsertScriptBefore(
195-
StrCat("<img id=\"pagespeed_img_0\">",
196-
StringPrintf(kInlinedScriptFormat, 1, 1), "\n",
197-
"<img id=\"pagespeed_img_0\">",
198-
StringPrintf(kInlinedScriptFormat, 2, 2)))));
195+
StrCat("<img id=\"pagespeed_img_02\">",
196+
StringPrintf(kInlinedScriptFormat, 3, 1, 2, 3), "\n",
197+
"<img id=\"pagespeed_img_04\">",
198+
StringPrintf(kInlinedScriptFormat, 5, 1, 4, 5)))));
199199
}
200200

201201
TEST_F(DedupInlinedImagesTest, DedupSecondSmallImageWithId) {
@@ -205,14 +205,14 @@ TEST_F(DedupInlinedImagesTest, DedupSecondSmallImageWithId) {
205205
"<img src='", kCuppaPngFilename, "'>"),
206206
StrCat("<img src='", kCuppaPngInlineData, "' id='xyzzy'>\n",
207207
InsertScriptBefore(
208-
"<img id=\"pagespeed_img_0\">"
208+
"<img id=\"pagespeed_img_01\">"
209209
"<script type=\"text/javascript\""
210-
" id=\"pagespeed_script_1\""
210+
" id=\"pagespeed_script_2\""
211211
" data-pagespeed-no-defer>"
212212
"pagespeed.dedupInlinedImages.inlineImg("
213213
"'xyzzy',"
214-
"'pagespeed_img_0',"
215-
"'pagespeed_script_1');"
214+
"'pagespeed_img_01',"
215+
"'pagespeed_script_2');"
216216
"</script>")));
217217
}
218218

@@ -223,16 +223,16 @@ TEST_F(DedupInlinedImagesTest, DedupSecondSmallImageWithAttributes) {
223223
"<img src='", kCuppaPngFilename,
224224
"' alt='xyzzy' id='plugh'>"),
225225
StrCat("<img src='", kCuppaPngInlineData,
226-
"' id=\"pagespeed_img_0\">\n",
226+
"' id=\"pagespeed_img_01\">\n",
227227
InsertScriptBefore(
228228
"<img alt='xyzzy' id='plugh'>"
229229
"<script type=\"text/javascript\""
230-
" id=\"pagespeed_script_1\""
230+
" id=\"pagespeed_script_2\""
231231
" data-pagespeed-no-defer>"
232232
"pagespeed.dedupInlinedImages.inlineImg("
233-
"'pagespeed_img_0',"
233+
"'pagespeed_img_01',"
234234
"'plugh',"
235-
"'pagespeed_script_1');"
235+
"'pagespeed_script_2');"
236236
"</script>")));
237237
}
238238

@@ -293,15 +293,17 @@ TEST_F(DedupInlinePreviewImagesTest, DedupInlinePreviewImages) {
293293
"\" onload=\"", DelayImagesFilter::kImageOnloadCode,
294294
"\" onerror=\"this.onerror=null;",
295295
DelayImagesFilter::kImageOnloadCode,
296-
"\" id=\"pagespeed_img_0\"/>");
297-
GoogleString scripted_img =
296+
"\" id=\"pagespeed_img_01\"/>");
297+
GoogleString scripted_img_fmt =
298298
StrCat("<img data-pagespeed-high-res-src='", image_filename,
299299
"' onload=\"", DelayImagesFilter::kImageOnloadCode,
300300
"\" onerror=\"this.onerror=null;",
301301
DelayImagesFilter::kImageOnloadCode,
302-
"\" id=\"pagespeed_img_0\"/>");
303-
GoogleString script_1 = StringPrintf(kInlinedScriptFormat, 1, 1);
304-
GoogleString script_2 = StringPrintf(kInlinedScriptFormat, 2, 2);
302+
"\" id=\"pagespeed_img_0%d\"/>");
303+
GoogleString scripted_img_1 = StringPrintf(scripted_img_fmt.c_str(), 2);
304+
GoogleString scripted_img_2 = StringPrintf(scripted_img_fmt.c_str(), 4);
305+
GoogleString script_1 = StringPrintf(kInlinedScriptFormat, 3, 1, 2, 3);
306+
GoogleString script_2 = StringPrintf(kInlinedScriptFormat, 5, 1, 4, 5);
305307
GoogleString input_html = StrCat("<head></head>"
306308
"<body>",
307309
input_img, input_img, input_img,
@@ -312,8 +314,8 @@ TEST_F(DedupInlinePreviewImagesTest, DedupInlinePreviewImages) {
312314
GetImageOnloadScriptBlock(),
313315
inlined_img,
314316
InsertScriptBefore(
315-
StrCat(scripted_img, script_1,
316-
scripted_img, script_2)),
317+
StrCat(scripted_img_1, script_1,
318+
scripted_img_2, script_2)),
317319
"</body>");
318320

319321
// Since the preview image has been resized use a wildcard to match it.

0 commit comments

Comments
 (0)