| @@ -17,48 +17,48 @@ | ||
| 17 | 17 | class RichLinksExtension extends Extension | 
| 18 | 18 |  { | 
| 19 | 19 | |
| 20 | - /** | |
| 21 | - * @var array | |
| 22 | - */ | |
| 23 | - private static $casting = [ | |
| 24 | - 'RichLinks' => 'HTMLText' | |
| 25 | - ]; | |
| 20 | + /** | |
| 21 | + * @var array | |
| 22 | + */ | |
| 23 | + private static $casting = [ | |
| 24 | + 'RichLinks' => 'HTMLText' | |
| 25 | + ]; | |
| 26 | 26 | |
| 27 | - /** | |
| 28 | - * @return string | |
| 29 | - */ | |
| 30 | - public function RichLinks() | |
| 31 | -    { | |
| 32 | - // Note: | |
| 33 | - // Assume we can use Regexes because the link will always be formatted | |
| 34 | - // in the same way coming from the CMS. | |
| 27 | + /** | |
| 28 | + * @return string | |
| 29 | + */ | |
| 30 | + public function RichLinks() | |
| 31 | +	{ | |
| 32 | + // Note: | |
| 33 | + // Assume we can use Regexes because the link will always be formatted | |
| 34 | + // in the same way coming from the CMS. | |
| 35 | 35 | |
| 36 | - $content = $this->owner->value; | |
| 36 | + $content = $this->owner->value; | |
| 37 | 37 | |
| 38 | - // Find all file links for processing. | |
| 39 | -        preg_match_all('/<a.*href="\[file_link,id=([0-9]+)\].*".*>.*<\/a>/U', $content, $matches); | |
| 38 | + // Find all file links for processing. | |
| 39 | +		preg_match_all('/<a.*href="\[file_link,id=([0-9]+)\].*".*>.*<\/a>/U', $content, $matches); | |
| 40 | 40 | |
| 41 | - // Attach the file type and size to each of the links. | |
| 42 | -        for ($i = 0; $i < count($matches[0]); $i++) { | |
| 43 | - $file = DataObject::get_by_id(File::class, $matches[1][$i]); | |
| 44 | -            if ($file) { | |
| 45 | - $size = $file->getSize(); | |
| 46 | - $ext = strtoupper($file->getExtension()); | |
| 47 | - // Replace the closing </a> tag with the size span (and reattach the closing tag). | |
| 48 | - $newLink = substr($matches[0][$i], 0, strlen($matches[0][$i]) - 4) | |
| 49 | - . "<span class='fileExt'> [$ext, $size]</span></a>"; | |
| 50 | - $content = str_replace($matches[0][$i], $newLink, $content); | |
| 51 | - } | |
| 52 | - } | |
| 41 | + // Attach the file type and size to each of the links. | |
| 42 | +		for ($i = 0; $i < count($matches[0]); $i++) { | |
| 43 | + $file = DataObject::get_by_id(File::class, $matches[1][$i]); | |
| 44 | +			if ($file) { | |
| 45 | + $size = $file->getSize(); | |
| 46 | + $ext = strtoupper($file->getExtension()); | |
| 47 | + // Replace the closing </a> tag with the size span (and reattach the closing tag). | |
| 48 | + $newLink = substr($matches[0][$i], 0, strlen($matches[0][$i]) - 4) | |
| 49 | + . "<span class='fileExt'> [$ext, $size]</span></a>"; | |
| 50 | + $content = str_replace($matches[0][$i], $newLink, $content); | |
| 51 | + } | |
| 52 | + } | |
| 53 | 53 | |
| 54 | - // Inject extra attributes into the external links. | |
| 55 | - $pattern = '/(<a.*)(href=\"https?:\/\/[^\"]*\"[^>]*>.*)(<\/a>)/iU'; | |
| 56 | - $replacement = sprintf( | |
| 57 | - '$1class="external" rel="external" $2<span class="nonvisual-indicator">(%s)</span>$3', | |
| 58 | - _t(__CLASS__ . '.ExternalLink', 'external link') | |
| 59 | - ); | |
| 60 | - $content = preg_replace($pattern, $replacement, $content, -1); | |
| 54 | + // Inject extra attributes into the external links. | |
| 55 | + $pattern = '/(<a.*)(href=\"https?:\/\/[^\"]*\"[^>]*>.*)(<\/a>)/iU'; | |
| 56 | + $replacement = sprintf( | |
| 57 | + '$1class="external" rel="external" $2<span class="nonvisual-indicator">(%s)</span>$3', | |
| 58 | + _t(__CLASS__ . '.ExternalLink', 'external link') | |
| 59 | + ); | |
| 60 | + $content = preg_replace($pattern, $replacement, $content, -1); | |
| 61 | 61 | |
| 62 | - return $content; | |
| 63 | - } | |
| 62 | + return $content; | |
| 63 | + } | |
| 64 | 64 | } | 
| @@ -7,35 +7,35 @@ | ||
| 7 | 7 | |
| 8 | 8 | class RichLinksExtensionTest extends SapphireTest | 
| 9 | 9 |  { | 
| 10 | - public function testContentLinkInjections() | |
| 11 | -    { | |
| 12 | - $field = new DBText(); | |
| 10 | + public function testContentLinkInjections() | |
| 11 | +	{ | |
| 12 | + $field = new DBText(); | |
| 13 | 13 | |
| 14 | - // External links injection. | |
| 15 | -        $field->setValue('<a href="http://newzealand.govt.nz">New Zealand Government</a>'); | |
| 16 | - $this->assertEquals( | |
| 17 | - $field->RichLinks(), | |
| 18 | - '<a class="external" rel="external" ' | |
| 19 | - . 'href="http://newzealand.govt.nz">New Zealand Government' | |
| 20 | - . '<span class="nonvisual-indicator">(external link)</span></a>', | |
| 21 | - 'Injects attributes to external link without target.' | |
| 22 | - ); | |
| 14 | + // External links injection. | |
| 15 | +		$field->setValue('<a href="http://newzealand.govt.nz">New Zealand Government</a>'); | |
| 16 | + $this->assertEquals( | |
| 17 | + $field->RichLinks(), | |
| 18 | + '<a class="external" rel="external" ' | |
| 19 | + . 'href="http://newzealand.govt.nz">New Zealand Government' | |
| 20 | + . '<span class="nonvisual-indicator">(external link)</span></a>', | |
| 21 | + 'Injects attributes to external link without target.' | |
| 22 | + ); | |
| 23 | 23 | |
| 24 | -        $field->setValue('<a href="http://newzealand.govt.nz" target="_blank">New Zealand Government</a>'); | |
| 25 | - $this->assertEquals( | |
| 26 | - $field->RichLinks(), | |
| 27 | - '<a class="external" rel="external" ' | |
| 28 | - . 'href="http://newzealand.govt.nz" target="_blank">New Zealand Government' | |
| 29 | - . '<span class="nonvisual-indicator">(external link)</span></a>', | |
| 30 | - 'Injects attributes to external link with target, while keeping the existing attributes.' | |
| 31 | - ); | |
| 24 | +		$field->setValue('<a href="http://newzealand.govt.nz" target="_blank">New Zealand Government</a>'); | |
| 25 | + $this->assertEquals( | |
| 26 | + $field->RichLinks(), | |
| 27 | + '<a class="external" rel="external" ' | |
| 28 | + . 'href="http://newzealand.govt.nz" target="_blank">New Zealand Government' | |
| 29 | + . '<span class="nonvisual-indicator">(external link)</span></a>', | |
| 30 | + 'Injects attributes to external link with target, while keeping the existing attributes.' | |
| 31 | + ); | |
| 32 | 32 | |
| 33 | - // Check the normal links are not affected. | |
| 34 | -        $field->setValue('<a href="[sitetree_link,id=1]">Internal</a>'); | |
| 35 | - $this->assertEquals( | |
| 36 | - $field->RichLinks(), | |
| 37 | - '<a href="[sitetree_link,id=1]">Internal</a>', | |
| 38 | - 'Regular link is not modified.' | |
| 39 | - ); | |
| 40 | - } | |
| 33 | + // Check the normal links are not affected. | |
| 34 | +		$field->setValue('<a href="[sitetree_link,id=1]">Internal</a>'); | |
| 35 | + $this->assertEquals( | |
| 36 | + $field->RichLinks(), | |
| 37 | + '<a href="[sitetree_link,id=1]">Internal</a>', | |
| 38 | + 'Regular link is not modified.' | |
| 39 | + ); | |
| 40 | + } | |
| 41 | 41 | } |