diff --git a/code/model/SiteTreeLinkTracking.php b/code/model/SiteTreeLinkTracking.php index fead777c..9e3638e1 100644 --- a/code/model/SiteTreeLinkTracking.php +++ b/code/model/SiteTreeLinkTracking.php @@ -217,14 +217,18 @@ class SiteTreeLinkTracking_Parser { $matches = array(); if(preg_match('/\[sitetree_link(?:\s*|%20|,)?id=([0-9]+)\](#(.*))?/i', $href, $matches)) { $page = DataObject::get_by_id('SiteTree', $matches[1]); + $broken = false; + if (!$page) { // Page doesn't exist. $broken = true; - } else if (!empty($matches[3]) && !preg_match("/(name|id)=\"{$matches[3]}\"/", $page->Content)) { - // Broken anchor on the target page. - $broken = true; - } else { - $broken = false; + } else if (!empty($matches[3])) { + $anchor = preg_quote($matches[3], '/'); + + if (!preg_match("/(name|id)=\"{$anchor}\"/", $page->Content)) { + // Broken anchor on the target page. + $broken = true; + } } $results[] = array( diff --git a/tests/behat/features/insert-a-link.feature b/tests/behat/features/insert-a-link.feature index 84015b03..f2334406 100644 --- a/tests/behat/features/insert-a-link.feature +++ b/tests/behat/features/insert-a-link.feature @@ -57,6 +57,10 @@ So that I can link to a external website or a page on my site Then the "Content" HTML field should contain "awesome" # Required to avoid "unsaved changes" browser dialog Then I press the "Save draft" button + # Check that the field is reset when adding another new link + Given I select "content" in the "Content" HTML field + When I press the "Insert Link" button + Then I should not see a ".ss-uploadfield-files .ss-uploadfield-item" element Scenario: I can link to an anchor Given I fill in the "Content" HTML field with "

My awesome content

" diff --git a/tests/model/SiteTreeLinkTrackingTest.php b/tests/model/SiteTreeLinkTrackingTest.php index 98ccb71f..e5967631 100644 --- a/tests/model/SiteTreeLinkTrackingTest.php +++ b/tests/model/SiteTreeLinkTrackingTest.php @@ -32,6 +32,7 @@ class SiteTreeLinkTrackingTest extends SapphireTest { $this->assertFalse($this->isBroken("ID]#yes-name-anchor\">link")); $this->assertFalse($this->isBroken("ID]#yes-id-anchor\">link")); $this->assertFalse($this->isBroken("ID]\">link")); + $this->assertTrue($this->isBroken("ID]#http://invalid-anchor.com\">")); } function highlight($content) {