diff --git a/forms/HtmlEditorField.php b/forms/HtmlEditorField.php
index 2d2171aa3..e088b8401 100644
--- a/forms/HtmlEditorField.php
+++ b/forms/HtmlEditorField.php
@@ -582,11 +582,13 @@ class HtmlEditorField_Toolbar extends RequestHandler {
// Similar to the regex found in HtmlEditorField.js / getAnchors method.
if (preg_match_all(
- "/\s+(name|id)\s*=\s*([\"'])([^\\2\s>]*?)\\2|\s+(name|id)\s*=\s*([^\"']+)[\s +>]/im",
+ "/\\s+(name|id)\\s*=\\s*([\"'])([^\\2\\s>]*?)\\2|\\s+(name|id)\\s*=\\s*([^\"']+)[\\s +>]/im",
$page->Content,
$matches
)) {
- $anchors = array_filter(array_merge($matches[3], $matches[5]));
+ $anchors = array_values(array_unique(array_filter(
+ array_merge($matches[3], $matches[5]))
+ ));
}
} else {
diff --git a/tests/forms/HtmlEditorFieldTest.php b/tests/forms/HtmlEditorFieldTest.php
index 067e71210..726666cfd 100644
--- a/tests/forms/HtmlEditorFieldTest.php
+++ b/tests/forms/HtmlEditorFieldTest.php
@@ -133,8 +133,7 @@ class HtmlEditorFieldTest extends FunctionalTest {
'bar',
'baz',
'bam',
- "some'id",
- 'bar',
+ "some'id",
);
$page = new Page();
$page->Title = 'Test';
@@ -142,15 +141,15 @@ class HtmlEditorFieldTest extends FunctionalTest {
$page->write();
$this->useDraftSite(true);
- $controller = new Controller();
- $controller->setRequest(new SS_HTTPRequest('GET', '/', array(
+ $request = new SS_HTTPRequest('GET', '/', array(
'PageID' => $page->ID,
- )));
- $controller->init();
+ ));
- $toolBar = new HtmlEditorField_Toolbar($controller, 'test');
+ $toolBar = new HtmlEditorField_Toolbar(new Controller(), 'test');
+ $toolBar->setRequest($request);
- $this->assertEquals(json_encode($expected), $toolBar->getanchors());
+ $results = json_decode($toolBar->getanchors(), true);
+ $this->assertEquals($expected, $results);
}
public function testHtmlEditorFieldFileLocal() {