MINOR Made HtmlEditorField dependency to SiteTree optional (to be moved into separate aspect, or down to DBField/model layer)

This commit is contained in:
Ingo Schommer 2011-03-23 14:32:38 +13:00
parent a9b13509d2
commit 41e51abd9a
3 changed files with 74 additions and 59 deletions

View File

@ -72,6 +72,7 @@ class HtmlEditorField extends TextareaField {
$htmlValue = new SS_HTMLValue($this->value);
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'));
@ -100,6 +101,7 @@ class HtmlEditorField extends TextareaField {
}
}
}
}
// Resample images, add default attributes and add to assets tracking.
if($images = $htmlValue->getElementsByTagName('img')) foreach($images as $img) {
@ -138,6 +140,7 @@ class HtmlEditorField extends TextareaField {
}
// Save file & link tracking data.
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");
@ -158,6 +161,7 @@ class HtmlEditorField extends TextareaField {
$tracker->add($item, array('FieldName' => $this->name));
}
}
}
$record->{$this->name} = $htmlValue->getContent();
}

View File

@ -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'
);
}

View File

@ -1,3 +1,11 @@
File:
example_file:
Name: example.pdf
Image:
example_image:
Name: example.jpg
HtmlEditorFieldTest_Object:
home:
Title: Home Page