diff --git a/forms/HtmlEditorField.php b/forms/HtmlEditorField.php index 8960a8d7a..2d2171aa3 100644 --- a/forms/HtmlEditorField.php +++ b/forms/HtmlEditorField.php @@ -581,8 +581,12 @@ class HtmlEditorField_Toolbar extends RequestHandler { } // Similar to the regex found in HtmlEditorField.js / getAnchors method. - if (preg_match_all("/\s(name|id)=\"([^\"]+?)\"|\s(name|id)='([^']+?)'/im", $page->Content, $matches)) { - $anchors = array_filter(array_merge($matches[2], $matches[4])); + if (preg_match_all( + "/\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])); } } else { diff --git a/javascript/HtmlEditorField.js b/javascript/HtmlEditorField.js index 2152a2273..21f4a8d5d 100644 --- a/javascript/HtmlEditorField.js +++ b/javascript/HtmlEditorField.js @@ -716,7 +716,8 @@ ss.editorWrappers['default'] = ss.editorWrappers.tinyMCE; // http://www.w3.org/TR/1999/REC-html401-19991224/struct/links.html#h-12.2 if(ed) { - var raw = ed.getContent().match(/\s(name|id)="([^"]+?)"|\s(name|id)='([^']+?)'/gim); + var raw = ed.getContent() + .match(/\s+(name|id)\s*=\s*(["'])([^\2\s>]*?)\2|\s+(name|id)\s*=\s*([^"']+)[\s +>]/gim); if (raw && raw.length) { for(var i = 0; i < raw.length; i++) { var indexStart = (raw[i].indexOf('id=') == -1) ? 7 : 5; diff --git a/tests/forms/HtmlEditorFieldTest.php b/tests/forms/HtmlEditorFieldTest.php index a3586a690..067e71210 100644 --- a/tests/forms/HtmlEditorFieldTest.php +++ b/tests/forms/HtmlEditorFieldTest.php @@ -116,6 +116,43 @@ class HtmlEditorFieldTest extends FunctionalTest { ); } + public function testGetAnchors() { + if (!class_exists('Page')) { + $this->markTestSkipped(); + } + $html = '
+
+
+
+
+
+
+
'; + $expected = array( + 'foo', + 'bar', + 'baz', + 'bam', + "some'id", + 'bar', + ); + $page = new Page(); + $page->Title = 'Test'; + $page->Content = $html; + $page->write(); + $this->useDraftSite(true); + + $controller = new Controller(); + $controller->setRequest(new SS_HTTPRequest('GET', '/', array( + 'PageID' => $page->ID, + ))); + $controller->init(); + + $toolBar = new HtmlEditorField_Toolbar($controller, 'test'); + + $this->assertEquals(json_encode($expected), $toolBar->getanchors()); + } + public function testHtmlEditorFieldFileLocal() { $file = new HtmlEditorField_File('http://domain.com/folder/my_image.jpg?foo=bar'); $this->assertEquals('http://domain.com/folder/my_image.jpg?foo=bar', $file->URL);