Skip to content

Commit 29d5207

Browse files
authored
Merge pull request #144 from ampproject/update/tiny-image-detection
2 parents 8d28ed1 + 40144be commit 29d5207

File tree

5 files changed

+40
-17
lines changed

5 files changed

+40
-17
lines changed

src/Dom/Document.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2081,7 +2081,7 @@ public function createElementWithAttributes($name, $attributes, $value = null)
20812081
return false;
20822082
}
20832083

2084-
$element->addAttributes($attributes);
2084+
$element->setAttributes($attributes);
20852085

20862086
return $element;
20872087
}

src/Dom/Element.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ public function getAttributesAsAssocArray()
240240
*
241241
* @param string[] $attributes One or more attributes for the node's HTML element.
242242
*/
243-
public function addAttributes($attributes)
243+
public function setAttributes($attributes)
244244
{
245245
foreach ($attributes as $name => $value) {
246246
try {

src/Optimizer/ImageDimensions.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,11 @@ public function getDimensionsFromParent()
113113
continue;
114114
}
115115

116+
// If layout is responsive, consider dimensions to be unbounded.
117+
if (Layout::RESPONSIVE === $element->getAttribute(Attribute::LAYOUT)) {
118+
return [PHP_INT_MAX, PHP_INT_MAX];
119+
}
120+
116121
return [(int)$width, (int)$height];
117122
}
118123

tests/Dom/ElementTest.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -393,27 +393,27 @@ public function testCopyAttributes($attributes, Element $from, Element $to, $exp
393393
/**
394394
* Test adding no attributes to node.
395395
*
396-
* @covers \AmpProject\Dom\Element::addAttributes()
396+
* @covers \AmpProject\Dom\Element::setAttributes()
397397
*/
398398
public function testAddAttributesToNodeForNoAttributes()
399399
{
400400
$dom = Document::fromHtml('<p>Hello World</p>');
401401
$element = $dom->createElement('b');
402-
$element->addAttributes([]);
402+
$element->setAttributes([]);
403403
$this->assertFalse($element->hasAttributes());
404404
}
405405

406406
/**
407407
* Test adding attribute with no value to node.
408408
*
409-
* @covers \AmpProject\Dom\Element::addAttributes()
409+
* @covers \AmpProject\Dom\Element::setAttributes()
410410
*/
411411
public function testAddAttributesToNodeForAttributeWithoutValue()
412412
{
413413
$dom = Document::fromHtml('<p>Hello World</p>');
414414
$element = $dom->createElement('div');
415415
$attributes = [ 'placeholder' => '' ];
416-
$element->addAttributes($attributes);
416+
$element->setAttributes($attributes);
417417

418418
$this->assertTrue($element->hasAttributes());
419419
$this->checkElementHasAttributes($element, $attributes);
@@ -422,7 +422,7 @@ public function testAddAttributesToNodeForAttributeWithoutValue()
422422
/**
423423
* Test adding attribute with value to node.
424424
*
425-
* @covers \AmpProject\Dom\Element::addAttributes()
425+
* @covers \AmpProject\Dom\Element::setAttributes()
426426
*/
427427
public function testAddAttributesToNodeForAttributeWithValue()
428428
{
@@ -432,7 +432,7 @@ public function testAddAttributesToNodeForAttributeWithValue()
432432
'class' => 'myClass',
433433
'id' => 'myId',
434434
];
435-
$element->addAttributes($attributes);
435+
$element->setAttributes($attributes);
436436

437437
$this->assertTrue($element->hasAttributes());
438438
$this->checkElementHasAttributes($element, $attributes);

tests/Optimizer/ImageDimensionsTest.php

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -410,28 +410,46 @@ public function dataItCanCheckIfAnImageIsTiny()
410410
public function testItCanCheckIfAnImageIsTiny($width, $height, $layout, $threshold, $expected)
411411
{
412412
$dom = new Document();
413-
$image = $dom->createElement(Tag::IMG);
413+
$attrs = [];
414414

415415
if ($width !== null) {
416-
$image->setAttribute(Attribute::WIDTH, $width);
416+
$attrs[Attribute::WIDTH] = $width;
417417
}
418418

419419
if ($height !== null) {
420-
$image->setAttribute(Attribute::HEIGHT, $height);
420+
$attrs[Attribute::HEIGHT] = $height;
421421
}
422422

423423
if ($layout !== null) {
424-
$image->setAttribute(Attribute::LAYOUT, $layout);
424+
$attrs[Attribute::LAYOUT] = $layout;
425425
}
426426

427-
$parent = $dom->createElement(Tag::FIGURE);
428-
$parent->setAttribute(Attribute::WIDTH, 400);
429-
$parent->setAttribute(Attribute::HEIGHT, 200);
430-
427+
// Check in fixed layout parent.
428+
$parent = $dom->createElementWithAttributes(
429+
'amp-layout',
430+
[
431+
Attribute::LAYOUT => Layout::FIXED,
432+
Attribute::WIDTH => 400,
433+
Attribute::HEIGHT => 200,
434+
]
435+
);
436+
$image = $dom->createElementWithAttributes(Tag::IMG, $attrs);
431437
$parent->appendChild($image);
432-
433438
$imageDimensions = new ImageDimensions($image);
439+
$this->assertEquals($expected, $imageDimensions->isTiny($threshold));
434440

441+
// Check in responsive layout parent.
442+
$parent = $dom->createElementWithAttributes(
443+
'amp-layout',
444+
[
445+
Attribute::LAYOUT => Layout::RESPONSIVE,
446+
Attribute::WIDTH => 3,
447+
Attribute::HEIGHT => 4,
448+
]
449+
);
450+
$image = $dom->createElementWithAttributes(Tag::IMG, $attrs);
451+
$parent->appendChild($image);
452+
$imageDimensions = new ImageDimensions($image);
435453
$this->assertEquals($expected, $imageDimensions->isTiny($threshold));
436454
}
437455
}

0 commit comments

Comments
 (0)