Skip to content

Commit 7a87b1f

Browse files
Avoid duplicate auto generated ids (#1251)
Fixes a problem in which the same auto-generated id is assigned to multiple elements. Fixes #1250
1 parent 1ce81d6 commit 7a87b1f

File tree

3 files changed

+17
-17
lines changed

3 files changed

+17
-17
lines changed

packages/optimizer/lib/transformers/ApplyCommonAttributes.js

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -164,13 +164,11 @@ class ApplyCommonAttributes {
164164
this.log = log;
165165
this.canRemoveBoilerplate = true;
166166
// node counter for id generation
167-
this.counter = 0;
167+
this.transformedNodesCounter = 0;
168168
// nodes to check for attributes
169169
this.nodesToTransform = [];
170170
// existing ids in the document
171171
this.ids = new Set();
172-
// nodes that have been transformed
173-
this.transformedNodes = [];
174172
this.attributeTransformations = {
175173
media: new MediaTransformer(),
176174
sizes: new SizesTransformer(),
@@ -201,19 +199,12 @@ class ApplyCommonAttributes {
201199
*/
202200
apply() {
203201
for (const node of this.nodesToTransform) {
202+
const id = this.getOrCreateId(node);
203+
let nodeHasBeenTransformed = false;
204204
for (const [attribute, transformer] of Object.entries(this.attributeTransformations)) {
205205
if (hasAttribute(node, attribute)) {
206206
try {
207-
const id = this.getOrCreateId(node);
208-
const nodeHasBeenTransformed = transformer.transform(node, id);
209-
this.transformedNodes.push(node);
210-
if (nodeHasBeenTransformed && !node.attribs.id) {
211-
// Only update id if it's needed...
212-
node.attribs.id = id;
213-
} else {
214-
// Decrease counter otherwise
215-
this.counter--;
216-
}
207+
nodeHasBeenTransformed = nodeHasBeenTransformed || transformer.transform(node, id);
217208
} catch (e) {
218209
this.log.debug(
219210
`Cannot remove boilerplate. Failed transforming ${attribute}="${node.attribs[attribute]}".`,
@@ -223,6 +214,11 @@ class ApplyCommonAttributes {
223214
}
224215
}
225216
}
217+
if (nodeHasBeenTransformed) {
218+
node.attribs.id = id;
219+
} else {
220+
this.transformedNodesCounter--;
221+
}
226222
}
227223
}
228224

@@ -247,7 +243,7 @@ class ApplyCommonAttributes {
247243
insertText(customStyles, '');
248244
}
249245
customStyles.children[0].data += styles;
250-
for (const node of this.transformedNodes) {
246+
for (const node of this.nodesToTransform) {
251247
for (const attribute of Object.keys(this.attributeTransformations)) {
252248
delete node.attribs[attribute];
253249
}
@@ -264,8 +260,8 @@ class ApplyCommonAttributes {
264260
return node.attribs.id;
265261
}
266262
node.attribs = node.attribs || [];
267-
const id = ID_PREFIX + this.counter;
268-
this.counter++;
263+
const id = ID_PREFIX + this.transformedNodesCounter;
264+
this.transformedNodesCounter++;
269265
if (this.ids.has(id)) {
270266
// generate a new id if this one already exists
271267
return this.getOrCreateId(node);

packages/optimizer/spec/transformers/valid/ServerSideRendering/converts_media_attribute_to_css/expected_output.html

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66

77
<link href="https://example.com/favicon.ico" rel="icon">
8-
<style amp-custom>@media not all and (min-width: 650px){#i-amp-0,#my-image,#i-amp-4{display:none}}@media not screen and (min-width: 650px){#i-amp-1{display:none}}@media screen and (min-width: 650px){#i-amp-2{display:none}}@media (min-width: 650px){#i-amp-3,#i-amp-5,#i-amp-6{display:none}}</style>
8+
<style amp-custom>@media not all and (min-width: 650px){#i-amp-0,#my-image,#i-amp-4{display:none}}@media not screen and (min-width: 650px){#i-amp-1{display:none}}@media screen and (min-width: 650px){#i-amp-2{display:none}}@media (min-width: 650px){#i-amp-3,#i-amp-5,#i-amp-6{display:none}}@media not all and (min-width: 999px){#i-amp-7{display:none}}</style>
99
</head>
1010
<body>
1111
<!-- 1. empty -->
@@ -26,5 +26,7 @@
2626
<amp-img id="my-image" src="image7.jpg" height="355" width="466" class="i-amphtml-layout-fixed i-amphtml-layout-size-defined" style="width:466px;height:355px;" i-amphtml-layout="fixed"></amp-img>
2727
<!-- 8. avoids id conflicts -->
2828
<amp-img id="i-amp-4" src="image8.jpg" height="355" width="466" class="i-amphtml-layout-fixed i-amphtml-layout-size-defined" style="width:466px;height:355px;" i-amphtml-layout="fixed"></amp-img>
29+
<!-- 9. media attribute empty sizes -->
30+
<amp-img src="image2.jpg" height="355" width="466" class="i-amphtml-layout-fixed i-amphtml-layout-size-defined" style="width:466px;height:355px;" i-amphtml-layout="fixed" id="i-amp-7"></amp-img>
2931
</body>
3032
</html>

packages/optimizer/spec/transformers/valid/ServerSideRendering/converts_media_attribute_to_css/input.html

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,5 +27,7 @@
2727
<amp-img id="my-image" media="(min-width: 650px)" src="image7.jpg" height="355" width="466"></amp-img>
2828
<!-- 8. avoids id conflicts -->
2929
<amp-img id="i-amp-4" media="(min-width: 650px)" src="image8.jpg" height="355" width="466"></amp-img>
30+
<!-- 9. media attribute empty sizes -->
31+
<amp-img media="(min-width: 999px)" sizes="" heights="" src="image2.jpg" height="355" width="466"></amp-img>
3032
</body>
3133
</html>

0 commit comments

Comments
 (0)