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:
Saophalkun Ponlu 2009-05-24 12:16:17 +00:00
parent 9f7bbc57d0
commit ce6402f8d0
5 changed files with 46 additions and 0 deletions

View File

@ -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
View 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);
}
}
?>

View 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

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.3 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.3 KiB