mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 12:05:37 +00:00
MINOR Made HtmlEditorField dependency to SiteTree optional (to be moved into separate aspect, or down to DBField/model layer)
This commit is contained in:
parent
a9b13509d2
commit
41e51abd9a
@ -72,31 +72,33 @@ class HtmlEditorField extends TextareaField {
|
||||
|
||||
$htmlValue = new SS_HTMLValue($this->value);
|
||||
|
||||
// Populate link tracking for internal links & links to asset files.
|
||||
if($links = $htmlValue->getElementsByTagName('a')) foreach($links as $link) {
|
||||
$href = Director::makeRelative($link->getAttribute('href'));
|
||||
|
||||
if($href) {
|
||||
if(preg_match('/\[sitetree_link id=([0-9]+)\]/i', $href, $matches)) {
|
||||
$ID = $matches[1];
|
||||
|
||||
// clear out any broken link classes
|
||||
if($class = $link->getAttribute('class')) {
|
||||
$link->setAttribute('class', preg_replace('/(^ss-broken|ss-broken$| ss-broken )/', null, $class));
|
||||
}
|
||||
|
||||
$linkedPages[] = $ID;
|
||||
if(!DataObject::get_by_id('SiteTree', $ID)) $record->HasBrokenLink = true;
|
||||
if(class_exists('SiteTree')) {
|
||||
// Populate link tracking for internal links & links to asset files.
|
||||
if($links = $htmlValue->getElementsByTagName('a')) foreach($links as $link) {
|
||||
$href = Director::makeRelative($link->getAttribute('href'));
|
||||
|
||||
} else if(substr($href, 0, strlen(ASSETS_DIR) + 1) == ASSETS_DIR.'/') {
|
||||
$candidateFile = File::find(Convert::raw2sql(urldecode($href)));
|
||||
if($candidateFile) {
|
||||
$linkedFiles[] = $candidateFile->ID;
|
||||
} else {
|
||||
$record->HasBrokenFile = true;
|
||||
if($href) {
|
||||
if(preg_match('/\[sitetree_link id=([0-9]+)\]/i', $href, $matches)) {
|
||||
$ID = $matches[1];
|
||||
|
||||
// clear out any broken link classes
|
||||
if($class = $link->getAttribute('class')) {
|
||||
$link->setAttribute('class', preg_replace('/(^ss-broken|ss-broken$| ss-broken )/', null, $class));
|
||||
}
|
||||
|
||||
$linkedPages[] = $ID;
|
||||
if(!DataObject::get_by_id('SiteTree', $ID)) $record->HasBrokenLink = true;
|
||||
|
||||
} else if(substr($href, 0, strlen(ASSETS_DIR) + 1) == ASSETS_DIR.'/') {
|
||||
$candidateFile = File::find(Convert::raw2sql(urldecode($href)));
|
||||
if($candidateFile) {
|
||||
$linkedFiles[] = $candidateFile->ID;
|
||||
} else {
|
||||
$record->HasBrokenFile = true;
|
||||
}
|
||||
} else if($href == '' || $href[0] == '/') {
|
||||
$record->HasBrokenLink = true;
|
||||
}
|
||||
} else if($href == '' || $href[0] == '/') {
|
||||
$record->HasBrokenLink = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -138,24 +140,26 @@ class HtmlEditorField extends TextareaField {
|
||||
}
|
||||
|
||||
// Save file & link tracking data.
|
||||
if($record->ID && $record->many_many('LinkTracking') && $tracker = $record->LinkTracking()) {
|
||||
$filter = sprintf('"FieldName" = \'%s\' AND "SiteTreeID" = %d', $this->name, $record->ID);
|
||||
DB::query("DELETE FROM \"$tracker->tableName\" WHERE $filter");
|
||||
if(class_exists('SiteTree')) {
|
||||
if($record->ID && $record->many_many('LinkTracking') && $tracker = $record->LinkTracking()) {
|
||||
$filter = sprintf('"FieldName" = \'%s\' AND "SiteTreeID" = %d', $this->name, $record->ID);
|
||||
DB::query("DELETE FROM \"$tracker->tableName\" WHERE $filter");
|
||||
|
||||
if($linkedPages) foreach($linkedPages as $item) {
|
||||
$SQL_fieldName = Convert::raw2sql($this->name);
|
||||
DB::query("INSERT INTO \"SiteTree_LinkTracking\" (\"SiteTreeID\",\"ChildID\", \"FieldName\")
|
||||
VALUES ($record->ID, $item, '$SQL_fieldName')");
|
||||
if($linkedPages) foreach($linkedPages as $item) {
|
||||
$SQL_fieldName = Convert::raw2sql($this->name);
|
||||
DB::query("INSERT INTO \"SiteTree_LinkTracking\" (\"SiteTreeID\",\"ChildID\", \"FieldName\")
|
||||
VALUES ($record->ID, $item, '$SQL_fieldName')");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if($record->ID && $record->many_many('ImageTracking') && $tracker = $record->ImageTracking()) {
|
||||
$filter = sprintf('"FieldName" = \'%s\' AND "SiteTreeID" = %d', $this->name, $record->ID);
|
||||
DB::query("DELETE FROM \"$tracker->tableName\" WHERE $filter");
|
||||
|
||||
$fieldName = $this->name;
|
||||
if($linkedFiles) foreach($linkedFiles as $item) {
|
||||
$tracker->add($item, array('FieldName' => $this->name));
|
||||
if($record->ID && $record->many_many('ImageTracking') && $tracker = $record->ImageTracking()) {
|
||||
$filter = sprintf('"FieldName" = \'%s\' AND "SiteTreeID" = %d', $this->name, $record->ID);
|
||||
DB::query("DELETE FROM \"$tracker->tableName\" WHERE $filter");
|
||||
|
||||
$fieldName = $this->name;
|
||||
if($linkedFiles) foreach($linkedFiles as $item) {
|
||||
$tracker->add($item, array('FieldName' => $this->name));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -13,69 +13,71 @@ class HtmlEditorFieldTest extends FunctionalTest {
|
||||
'HtmlEditorField_Toolbar' => array('HtmlEditorFieldTest_DummyImageFormFieldExtension')
|
||||
);
|
||||
|
||||
protected $extraDataObjects = array('HtmlEditorFieldTest_Object');
|
||||
|
||||
public function testBasicSaving() {
|
||||
$sitetree = new SiteTree();
|
||||
$obj = new HtmlEditorFieldTest_Object();
|
||||
$editor = new HtmlEditorField('Content');
|
||||
|
||||
$editor->setValue('<p class="foo">Simple Content</p>');
|
||||
$editor->saveInto($sitetree);
|
||||
$this->assertEquals('<p class="foo">Simple Content</p>', $sitetree->Content, 'Attributes are preserved.');
|
||||
$editor->saveInto($obj);
|
||||
$this->assertEquals('<p class="foo">Simple Content</p>', $obj->Content, 'Attributes are preserved.');
|
||||
|
||||
$editor->setValue('<p>Unclosed Tag');
|
||||
$editor->saveInto($sitetree);
|
||||
$this->assertEquals('<p>Unclosed Tag</p>', $sitetree->Content, 'Unclosed tags are closed.');
|
||||
$editor->saveInto($obj);
|
||||
$this->assertEquals('<p>Unclosed Tag</p>', $obj->Content, 'Unclosed tags are closed.');
|
||||
}
|
||||
|
||||
public function testNullSaving() {
|
||||
$sitetree = new SiteTree();
|
||||
$obj = new HtmlEditorFieldTest_Object();
|
||||
$editor = new HtmlEditorField('Content');
|
||||
|
||||
$editor->setValue(null);
|
||||
$editor->saveInto($sitetree);
|
||||
$this->assertEquals('', $sitetree->Content, "Doesn't choke on empty/null values.");
|
||||
$editor->saveInto($obj);
|
||||
$this->assertEquals('', $obj->Content, "Doesn't choke on empty/null values.");
|
||||
}
|
||||
|
||||
public function testImageInsertion() {
|
||||
$sitetree = new SiteTree();
|
||||
$obj = new HtmlEditorFieldTest_Object();
|
||||
$editor = new HtmlEditorField('Content');
|
||||
|
||||
$editor->setValue('<img src="assets/example.jpg" />');
|
||||
$editor->saveInto($sitetree);
|
||||
$editor->saveInto($obj);
|
||||
|
||||
$xml = new SimpleXMLElement($sitetree->Content);
|
||||
$xml = new SimpleXMLElement($obj->Content);
|
||||
$this->assertNotNull($xml['alt'], 'Alt tags are added by default.');
|
||||
$this->assertNotNull($xml['title'], 'Title tags are added by default.');
|
||||
|
||||
$editor->setValue('<img src="assets/example.jpg" alt="foo" title="bar" />');
|
||||
$editor->saveInto($sitetree);
|
||||
$editor->saveInto($obj);
|
||||
|
||||
$xml = new SimpleXMLElement($sitetree->Content);
|
||||
$xml = new SimpleXMLElement($obj->Content);
|
||||
$this->assertNotNull('foo', $xml['alt'], 'Alt tags are preserved.');
|
||||
$this->assertNotNull('bar', $xml['title'], 'Title tags are preserved.');
|
||||
}
|
||||
|
||||
public function testMultiLineSaving() {
|
||||
$sitetree = $this->objFromFixture('SiteTree', 'home');
|
||||
$obj = $this->objFromFixture('HtmlEditorFieldTest_Object', 'home');
|
||||
$editor = new HtmlEditorField('Content');
|
||||
$editor->setValue("<p>First Paragraph</p><p>Second Paragraph</p>");
|
||||
$editor->saveInto($sitetree);
|
||||
$this->assertEquals("<p>First Paragraph</p><p>Second Paragraph</p>", $sitetree->Content);
|
||||
$editor->saveInto($obj);
|
||||
$this->assertEquals("<p>First Paragraph</p><p>Second Paragraph</p>", $obj->Content);
|
||||
}
|
||||
|
||||
public function testSavingLinksWithoutHref() {
|
||||
$sitetree = $this->objFromFixture('SiteTree', 'home');
|
||||
$obj = $this->objFromFixture('HtmlEditorFieldTest_Object', 'home');
|
||||
$editor = new HtmlEditorField('Content');
|
||||
|
||||
$editor->setValue('<p><a name="example-anchor"></a></p>');
|
||||
$editor->saveInto($sitetree);
|
||||
$editor->saveInto($obj);
|
||||
|
||||
$this->assertEquals (
|
||||
'<p><a name="example-anchor"/></p>', $sitetree->Content, 'Saving a link without a href attribute works'
|
||||
'<p><a name="example-anchor"/></p>', $obj->Content, 'Saving a link without a href attribute works'
|
||||
);
|
||||
}
|
||||
|
||||
public function testExtendImageFormFields() {
|
||||
$controller = new ContentController();
|
||||
$controller = new Controller();
|
||||
|
||||
$toolbar = new HtmlEditorField_Toolbar($controller, 'DummyToolbar');
|
||||
|
||||
@ -101,6 +103,7 @@ class HtmlEditorFieldTest_DummyImageFormFieldExtension extends Extension impleme
|
||||
|
||||
class HtmlEditorFieldTest_Object extends DataObject implements TestOnly {
|
||||
static $db = array(
|
||||
'Title' => 'Varchar'
|
||||
'Title' => 'Varchar',
|
||||
'Content' => 'HTMLText'
|
||||
);
|
||||
}
|
@ -1,3 +1,11 @@
|
||||
File:
|
||||
example_file:
|
||||
Name: example.pdf
|
||||
|
||||
Image:
|
||||
example_image:
|
||||
Name: example.jpg
|
||||
|
||||
HtmlEditorFieldTest_Object:
|
||||
home:
|
||||
Title: Home Page
|
||||
|
Loading…
x
Reference in New Issue
Block a user