mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 14:05:37 +02:00
ENHANCEMENT: use image file name (without file path and extension) for alt attribute when title is not provided
git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@77668 467b73ca-7a2a-4603-9d3b-597d59a354a9
This commit is contained in:
parent
9f7bbc57d0
commit
ce6402f8d0
@ -102,6 +102,15 @@ class Image extends File {
|
|||||||
$url = $this->URL();
|
$url = $this->URL();
|
||||||
$title = ($this->Title) ? $this->Title : $this->Filename;
|
$title = ($this->Title) ? $this->Title : $this->Filename;
|
||||||
|
|
||||||
|
if ($this->Title) {
|
||||||
|
$title = $this->Title;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
// use the file name (without path and extension) for alt atttribute when the title is not defined
|
||||||
|
$title = $this->Filename;
|
||||||
|
if (preg_match("/([^\/]*)\.[a-zA-Z0-9]{1,6}$/", $title, $matches)) $title = $matches[1];
|
||||||
|
}
|
||||||
|
|
||||||
return "<img src=\"$url\" alt=\"$title\" />";
|
return "<img src=\"$url\" alt=\"$title\" />";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
29
tests/model/ImageTest.php
Normal file
29
tests/model/ImageTest.php
Normal file
@ -0,0 +1,29 @@
|
|||||||
|
<?php
|
||||||
|
class ImageTest extends SapphireTest {
|
||||||
|
static $fixture_file = 'sapphire/tests/model/ImageTest.yml';
|
||||||
|
|
||||||
|
function testGetTagWithTitle() {
|
||||||
|
$image = $this->fixture->objFromFixture('Image', 'imageWithTitle');
|
||||||
|
$expected = '<img src="' . Director::baseUrl() . 'sapphire/tests/model/testimages/test_image.png" alt="This is a image Title" />';
|
||||||
|
$actual = $image->getTag();
|
||||||
|
|
||||||
|
$this->assertEquals($expected, $actual);
|
||||||
|
}
|
||||||
|
|
||||||
|
function testGetTagWithoutTitle() {
|
||||||
|
$image = $this->fixture->objFromFixture('Image', 'imageWithoutTitle');
|
||||||
|
$expected = '<img src="' . Director::baseUrl() . 'sapphire/tests/model/testimages/test_image.png" alt="test_image" />';
|
||||||
|
$actual = $image->getTag();
|
||||||
|
|
||||||
|
$this->assertEquals($expected, $actual);
|
||||||
|
}
|
||||||
|
|
||||||
|
function testGetTagWithoutTitleContainingDots() {
|
||||||
|
$image = $this->fixture->objFromFixture('Image', 'imageWithoutTitleContainingDots');
|
||||||
|
$expected = '<img src="' . Director::baseUrl() . 'sapphire/tests/model/testimages/test.image.with.dots.png" alt="test.image.with.dots" />';
|
||||||
|
$actual = $image->getTag();
|
||||||
|
|
||||||
|
$this->assertEquals($expected, $actual);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
?>
|
8
tests/model/ImageTest.yml
Normal file
8
tests/model/ImageTest.yml
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
Image:
|
||||||
|
imageWithTitle:
|
||||||
|
Title: This is a image Title
|
||||||
|
Filename: sapphire/tests/model/testimages/test_image.png
|
||||||
|
imageWithoutTitle:
|
||||||
|
Filename: sapphire/tests/model/testimages/test_image.png
|
||||||
|
imageWithoutTitleContainingDots:
|
||||||
|
Filename: sapphire/tests/model/testimages/test.image.with.dots.png
|
BIN
tests/model/testimages/test.image.with.dots.png
Normal file
BIN
tests/model/testimages/test.image.with.dots.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 4.3 KiB |
BIN
tests/model/testimages/test_image.png
Normal file
BIN
tests/model/testimages/test_image.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 4.3 KiB |
Loading…
Reference in New Issue
Block a user