mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 12:05:37 +00:00
API Remove custom DBHTMLText::exists() custom behaviour
Fix merge regressions
This commit is contained in:
parent
6a7c1056fe
commit
26d46517ac
@ -8,7 +8,6 @@ use Object;
|
||||
use Director;
|
||||
use SilverStripe\ORM\FieldType\DBPrimaryKey;
|
||||
use SilverStripe\ORM\FieldType\DBField;
|
||||
use SilverStripe\ORM\FieldType\DBPrimaryKey;
|
||||
|
||||
|
||||
/**
|
||||
|
@ -13,10 +13,10 @@ use TextField;
|
||||
* This behaves similarly to {@link Text}, but the template processor won't escape any HTML content within it.
|
||||
*
|
||||
* Options can be specified in a $db config via one of the following:
|
||||
* - "HTMLFragment(['shortcodes=true', 'whitelist=meta,link'])"
|
||||
* - "HTMLFragment('whitelist=meta,link')"
|
||||
* - "HTMLFragment(['shortcodes=true'])". "HTMLText" is also a synonym for this.
|
||||
* - "HTMLFragment('shortcodes=true')"
|
||||
* - "HTMLFragment(['shortcodes' => true, 'whitelist' => 'meta,link'])"
|
||||
* - "HTMLFragment(['whitelist' => 'meta,link'])"
|
||||
* - "HTMLFragment(['shortcodes' => true])". "HTMLText" is also a synonym for this.
|
||||
* - "HTMLFragment(['shortcodes' => true])"
|
||||
*
|
||||
* @see HTMLVarchar
|
||||
* @see Text
|
||||
@ -189,35 +189,6 @@ class DBHTMLText extends DBText {
|
||||
return $value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true if the field has meaningful content.
|
||||
* Excludes null content like <h1></h1>, <p></p> ,etc
|
||||
*
|
||||
* @return boolean
|
||||
*/
|
||||
public function exists() {
|
||||
// If it's blank, it's blank
|
||||
if(!parent::exists()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$value = $this->RAW();
|
||||
|
||||
// If it's got a content tag
|
||||
if(preg_match('/<(img|embed|object|iframe|meta|source|link)[^>]*>/i', $value)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
// If it's just one or two tags on its own (and not the above) it's empty.
|
||||
// This might be <p></p> or <h1></h1> or whatever.
|
||||
if(preg_match('/^[\\s]*(<[^>]+>[\\s]*){1,2}$/', $value)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Otherwise its content is genuine content
|
||||
return true;
|
||||
}
|
||||
|
||||
public function scaffoldFormField($title = null) {
|
||||
return new HTMLEditorField($this->name, $title);
|
||||
}
|
||||
|
@ -115,33 +115,6 @@ class DBHTMLVarchar extends DBVarchar {
|
||||
return trim(\Convert::xml2raw($text));
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true if the field has meaningful content.
|
||||
* Excludes null content like <h1></h1>, <p></p> ,etc
|
||||
*
|
||||
* @return boolean
|
||||
*/
|
||||
public function exists() {
|
||||
// If it's blank, it's blank
|
||||
if(!parent::exists()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// If it's got a content tag
|
||||
if(preg_match('/<(img|embed|object|iframe|meta|source|link)[^>]*>/i', $this->RAW())) {
|
||||
return true;
|
||||
}
|
||||
|
||||
// If it's just one or two tags on its own (and not the above) it's empty.
|
||||
// This might be <p></p> or <h1></h1> or whatever.
|
||||
if(preg_match('/^[\\s]*(<[^>]+>[\\s]*){1,2}$/', $this->RAW())) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Otherwise its content is genuine content
|
||||
return true;
|
||||
}
|
||||
|
||||
public function scaffoldFormField($title = null) {
|
||||
return new HTMLEditorField($this->name, $title, 1);
|
||||
}
|
||||
@ -150,15 +123,4 @@ class DBHTMLVarchar extends DBVarchar {
|
||||
return new TextField($this->name, $title);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function NoHTML()
|
||||
{
|
||||
// Preserve line breaks
|
||||
$text = preg_replace('/\<br(\s*)?\/?\>/i', "\n", $this->RAW());
|
||||
// Convert back to plain text
|
||||
return \Convert::xml2raw(strip_tags($text));
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -35,48 +35,16 @@ abstract class DBString extends DBField {
|
||||
* {@link StringField::setOptions()} for information on the available options
|
||||
*/
|
||||
public function __construct($name = null, $options = array()) {
|
||||
$options = $this->parseConstructorOptions($options);
|
||||
if($options) {
|
||||
if(!is_array($options)) {
|
||||
throw new \InvalidArgumentException("Invalid options $options");
|
||||
}
|
||||
$this->setOptions($options);
|
||||
}
|
||||
|
||||
parent::__construct($name);
|
||||
}
|
||||
|
||||
/**
|
||||
* Parses the "options" parameter passed to the constructor. This could be a
|
||||
* string value, or an array of options. Config specification might also
|
||||
* encode "key=value" pairs in non-associative strings.
|
||||
*
|
||||
* @param mixed $options
|
||||
* @return array The list of parsed options, or empty if there are none
|
||||
*/
|
||||
protected function parseConstructorOptions($options) {
|
||||
if(is_string($options)) {
|
||||
$options = [$options];
|
||||
}
|
||||
if(!is_array($options)) {
|
||||
return [];
|
||||
}
|
||||
$parsed = [];
|
||||
foreach($options as $option => $value) {
|
||||
// Workaround for inability for config args to support associative arrays
|
||||
if(is_numeric($option) && strpos($value, '=') !== false) {
|
||||
list($option, $value) = explode('=', $value);
|
||||
$option = trim($option);
|
||||
$value = trim($value);
|
||||
}
|
||||
// Convert bool values
|
||||
if(strcasecmp($value, 'true') === 0) {
|
||||
$value = true;
|
||||
} elseif(strcasecmp($value, 'false') === 0) {
|
||||
$value = false;
|
||||
}
|
||||
$parsed[$option] = $value;
|
||||
}
|
||||
return $parsed;
|
||||
}
|
||||
|
||||
/**
|
||||
* Update the optional parameters for this field.
|
||||
*
|
||||
|
@ -6,8 +6,6 @@
|
||||
*/
|
||||
|
||||
use SilverStripe\Forms\Schema\FormSchema;
|
||||
|
||||
use SilverStripe\ORM\Hierarchy\Hierarchy;
|
||||
use SilverStripe\ORM\SS_List;
|
||||
use SilverStripe\ORM\Versioning\Versioned;
|
||||
use SilverStripe\ORM\DataModel;
|
||||
|
@ -1,8 +1,6 @@
|
||||
<?php
|
||||
use SilverStripe\Model\FieldType\DBHTMLText;
|
||||
|
||||
use SilverStripe\ORM\DataModel;
|
||||
use SilverStripe\ORM\FieldType\DBHTMLText;
|
||||
use SilverStripe\Security\BasicAuth;
|
||||
use SilverStripe\Security\Member;
|
||||
|
||||
|
@ -942,7 +942,7 @@ For example:
|
||||
|
||||
Will become:
|
||||
|
||||
use SilverStripe\Model\FieldType\DBVarchar;
|
||||
use SilverStripe\ORM\FieldType\DBVarchar;
|
||||
|
||||
class MyObject extends DataObject {
|
||||
private static $db = array(
|
||||
|
@ -1,4 +1,6 @@
|
||||
<?php
|
||||
use SilverStripe\ORM\FieldType\DBHTMLText;
|
||||
|
||||
/**
|
||||
* Abstract class for all fields without data.
|
||||
* Labels, headings and the like should extend from this.
|
||||
@ -32,7 +34,7 @@ class DatalessField extends FormField {
|
||||
* Returns the field's representation in the form.
|
||||
* For dataless fields, this defaults to $Field.
|
||||
*
|
||||
* @return string
|
||||
* @return DBHTMLText
|
||||
*/
|
||||
public function FieldHolder($properties = array()) {
|
||||
return $this->Field($properties);
|
||||
@ -41,6 +43,8 @@ class DatalessField extends FormField {
|
||||
/**
|
||||
* Returns the field's representation in a field group.
|
||||
* For dataless fields, this defaults to $Field.
|
||||
*
|
||||
* @return DBHTMLText
|
||||
*/
|
||||
public function SmallFieldHolder($properties = array()) {
|
||||
return $this->Field($properties);
|
||||
|
@ -3,9 +3,7 @@
|
||||
use SilverStripe\ORM\DataObject;
|
||||
use SilverStripe\ORM\FieldType\DBField;
|
||||
use SilverStripe\ORM\DataObjectInterface;
|
||||
use SilverStripe\ORM\FieldType\DBHTMLText;
|
||||
use SilverStripe\ORM\SS_List;
|
||||
use SilverStripe\ORM\FieldType\DBHTMLText;
|
||||
use SilverStripe\Security\SecurityToken;
|
||||
use SilverStripe\Security\NullSecurityToken;
|
||||
|
||||
@ -497,8 +495,8 @@ class Form extends RequestHandler {
|
||||
|| $this->actions->dataFieldByName('action_' . $action)
|
||||
// Always allow actions on fields
|
||||
|| (
|
||||
$field = $this->checkFieldsForAction($this->Fields(), $action)
|
||||
&& $field->checkAccessAction($action)
|
||||
($field = $this->checkFieldsForAction($this->Fields(), $action))
|
||||
&& $field->checkAccessAction($action)
|
||||
)
|
||||
);
|
||||
}
|
||||
|
@ -1,4 +1,6 @@
|
||||
<?php
|
||||
use SilverStripe\ORM\FieldType\DBHTMLText;
|
||||
|
||||
/**
|
||||
* The action buttons are <input type="submit"> as well as <button> tags.
|
||||
*
|
||||
@ -108,7 +110,7 @@ class FormAction extends FormField {
|
||||
|
||||
/**
|
||||
* @param array $properties
|
||||
* @return string
|
||||
* @return DBHTMLText
|
||||
*/
|
||||
public function FieldHolder($properties = array()) {
|
||||
return $this->Field($properties);
|
||||
|
@ -2,6 +2,7 @@
|
||||
|
||||
use SilverStripe\ORM\DataObjectInterface;
|
||||
use SilverStripe\ORM\FieldType\DBField;
|
||||
use SilverStripe\ORM\FieldType\DBHTMLText;
|
||||
|
||||
/**
|
||||
* Represents a field in a form.
|
||||
@ -940,7 +941,7 @@ class FormField extends RequestHandler {
|
||||
* such as an input tag.
|
||||
*
|
||||
* @param array $properties
|
||||
* @return string
|
||||
* @return DBHTMLText
|
||||
*/
|
||||
public function Field($properties = array()) {
|
||||
$context = $this;
|
||||
@ -974,7 +975,7 @@ class FormField extends RequestHandler {
|
||||
*
|
||||
* @param array $properties
|
||||
*
|
||||
* @return string
|
||||
* @return DBHTMLText
|
||||
*/
|
||||
public function FieldHolder($properties = array()) {
|
||||
$context = $this;
|
||||
|
@ -1,6 +1,6 @@
|
||||
<?php
|
||||
use SilverStripe\Model\FieldType\DBField;
|
||||
|
||||
use SilverStripe\ORM\FieldType\DBField;
|
||||
use SilverStripe\ORM\DataObjectInterface;
|
||||
|
||||
/**
|
||||
|
@ -1,5 +1,6 @@
|
||||
<?php
|
||||
use SilverStripe\Model\FieldType\DBField;
|
||||
|
||||
use SilverStripe\ORM\FieldType\DBField;
|
||||
|
||||
/**
|
||||
* Parses text in a variety of ways.
|
||||
|
@ -204,6 +204,29 @@ class DBHTMLTextTest extends SapphireTest {
|
||||
$this->assertEquals($expectedValue, $result);
|
||||
}
|
||||
|
||||
public function testCreate() {
|
||||
/** @var DBHTMLText $field */
|
||||
$field = Object::create_from_string("HTMLFragment(['whitelist' => 'link'])", 'MyField');
|
||||
$this->assertEquals(['link'], $field->getWhitelist());
|
||||
$field = Object::create_from_string("HTMLFragment(['whitelist' => 'link,a'])", 'MyField');
|
||||
$this->assertEquals(['link', 'a'], $field->getWhitelist());
|
||||
$field = Object::create_from_string("HTMLFragment(['whitelist' => ['link', 'a']])", 'MyField');
|
||||
$this->assertEquals(['link', 'a'], $field->getWhitelist());
|
||||
$field = Object::create_from_string("HTMLFragment", 'MyField');
|
||||
$this->assertEmpty($field->getWhitelist());
|
||||
|
||||
// Test shortcodes
|
||||
$field = Object::create_from_string("HTMLFragment(['shortcodes' => true])", 'MyField');
|
||||
$this->assertEquals(true, $field->getProcessShortcodes());
|
||||
$field = Object::create_from_string("HTMLFragment(['shortcodes' => false])", 'MyField');
|
||||
$this->assertEquals(false, $field->getProcessShortcodes());
|
||||
|
||||
// Mix options
|
||||
$field = Object::create_from_string("HTMLFragment(['shortcodes' => true, 'whitelist' => ['a'])", 'MyField');
|
||||
$this->assertEquals(true, $field->getProcessShortcodes());
|
||||
$this->assertEquals(['a'], $field->getWhitelist());
|
||||
}
|
||||
|
||||
public function providerToPlain()
|
||||
{
|
||||
return [
|
||||
@ -335,7 +358,7 @@ class DBHTMLTextTest extends SapphireTest {
|
||||
$this->assertEquals('"this is a test"', $data->ATT());
|
||||
|
||||
// HTML Text (passes shortcodes + tidy)
|
||||
$data = DBField::create_field('HTMLText', '"');
|
||||
$data = DBField::create_field('HTMLText', '"');
|
||||
$this->assertEquals('"', $data->ATT());
|
||||
}
|
||||
|
||||
@ -397,45 +420,19 @@ class DBHTMLTextTest extends SapphireTest {
|
||||
$h = new DBHTMLText();
|
||||
$h->setValue("");
|
||||
$this->assertFalse($h->exists());
|
||||
$h->setValue("<p></p>");
|
||||
$this->assertFalse($h->exists());
|
||||
$h->setValue("<p> </p>");
|
||||
$this->assertFalse($h->exists());
|
||||
$h->setValue("<h2/>");
|
||||
$this->assertFalse($h->exists());
|
||||
$h->setValue("<h2></h2>");
|
||||
$this->assertFalse($h->exists());
|
||||
|
||||
$h->setValue("something");
|
||||
$this->assertTrue($h->exists());
|
||||
$h->setValue("<img src=\"dummy.png\">");
|
||||
$this->assertTrue($h->exists());
|
||||
$h->setValue("<img src=\"dummy.png\"><img src=\"dummy.png\">");
|
||||
$this->assertTrue($h->exists());
|
||||
$h->setValue("<p><img src=\"dummy.png\"></p>");
|
||||
$this->assertTrue($h->exists());
|
||||
|
||||
$h->setValue("<iframe src=\"http://www.google.com\"></iframe>");
|
||||
$this->assertTrue($h->exists());
|
||||
$h->setValue("<embed src=\"test.swf\">");
|
||||
$this->assertTrue($h->exists());
|
||||
$h->setValue("<object width=\"400\" height=\"400\" data=\"test.swf\"></object>");
|
||||
$this->assertTrue($h->exists());
|
||||
|
||||
|
||||
$h->setValue("<p>test</p>");
|
||||
$h->setValue("<p>content</p>");
|
||||
$this->assertTrue($h->exists());
|
||||
}
|
||||
|
||||
function testWhitelist() {
|
||||
$textObj = new DBHTMLText('Test', 'whitelist=meta,link');
|
||||
$textObj = new DBHTMLText('Test', ['whitelist'=> 'meta,link']);
|
||||
$this->assertEquals(
|
||||
'<meta content="Keep"><link href="Also Keep">',
|
||||
$textObj->whitelistContent('<meta content="Keep"><p>Remove</p><link href="Also Keep" />Remove Text'),
|
||||
'Removes any elements not in whitelist excluding text elements'
|
||||
);
|
||||
|
||||
$textObj = new DBHTMLText('Test', 'whitelist=meta,link,text()');
|
||||
$textObj = new DBHTMLText('Test', ['whitelist'=> 'meta,link,text()']);
|
||||
$this->assertEquals(
|
||||
'<meta content="Keep"><link href="Also Keep">Keep Text',
|
||||
$textObj->whitelistContent('<meta content="Keep"><p>Remove</p><link href="Also Keep" />Keep Text'),
|
||||
@ -528,7 +525,7 @@ class DBHTMLTextTest extends SapphireTest {
|
||||
);
|
||||
$this->assertEquals(
|
||||
'Replaced short code with this. home',
|
||||
$field->NoHTML()
|
||||
$field->Plain()
|
||||
);
|
||||
Config::nest();
|
||||
Config::inst()->update('Director', 'alternate_base_url', 'http://example.com/');
|
||||
@ -550,11 +547,7 @@ class DBHTMLTextTest extends SapphireTest {
|
||||
$field->Summary(2)
|
||||
);
|
||||
$this->assertEquals(
|
||||
'Replaced short code with...',
|
||||
$field->BigSummary(4)
|
||||
);
|
||||
$this->assertEquals(
|
||||
'Replaced short code with this. home[home]',
|
||||
'Replaced short code with this. home',
|
||||
$field->FirstParagraph()
|
||||
);
|
||||
$this->assertEquals(
|
||||
|
@ -1534,13 +1534,6 @@ class DataObjectTest extends SapphireTest {
|
||||
$this->assertTrue($team->hasValue('Title', null, false));
|
||||
$this->assertFalse($team->hasValue('DatabaseField', null, false));
|
||||
|
||||
$team->DatabaseField = '<p></p>';
|
||||
$this->assertTrue($team->hasValue('Title', null, false));
|
||||
$this->assertFalse (
|
||||
$team->hasValue('DatabaseField', null, false),
|
||||
'Test that a blank paragraph on a HTML field is not a valid value.'
|
||||
);
|
||||
|
||||
$team->Title = '<p></p>';
|
||||
$this->assertTrue (
|
||||
$team->hasValue('Title', null, false),
|
||||
|
@ -5,7 +5,7 @@ use SilverStripe\ORM\DataObject;
|
||||
use SilverStripe\Security\Member;
|
||||
use SilverStripe\Security\SecurityToken;
|
||||
use SilverStripe\Security\Permission;
|
||||
use SilverStripe\Model\FieldType\DBField;
|
||||
use SilverStripe\ORM\FieldType\DBField;
|
||||
|
||||
class SSViewerTest extends SapphireTest {
|
||||
|
||||
@ -175,7 +175,7 @@ class SSViewerTest extends SapphireTest {
|
||||
// first make sure that our test js file causes an exception to be thrown
|
||||
try{
|
||||
require_once('thirdparty/jsmin/jsmin.php');
|
||||
$content = JSMin::minify($content);
|
||||
JSMin::minify($jsFileContents);
|
||||
$this->fail('JSMin did not throw exception on minify bad file: ');
|
||||
} catch(Exception $e) {
|
||||
// exception thrown... good
|
||||
|
@ -1,6 +1,6 @@
|
||||
<?php
|
||||
|
||||
use SilverStripe\Model\FieldType\DBField;
|
||||
use SilverStripe\ORM\FieldType\DBField;
|
||||
|
||||
/**
|
||||
* See {@link SSViewerTest->testCastingHelpers()} for more tests related to casting and ViewableData behaviour,
|
||||
|
@ -35,10 +35,10 @@ class SSViewer_Scope {
|
||||
const UP_INDEX = 4;
|
||||
const CURRENT_INDEX = 5;
|
||||
const ITEM_OVERLAY = 6;
|
||||
|
||||
|
||||
// The stack of previous "global" items
|
||||
// An indexed array of item, item iterator, item iterator total, pop index, up index, current index & parent overlay
|
||||
private $itemStack = array();
|
||||
private $itemStack = array();
|
||||
|
||||
// The current "global" item (the one any lookup starts from)
|
||||
protected $item;
|
||||
|
@ -1,8 +1,6 @@
|
||||
<?php
|
||||
|
||||
use SilverStripe\Model\FieldType\DBVarchar;
|
||||
use SilverStripe\Model\FieldType\DBField;
|
||||
use SilverStripe\Model\FieldType\DBHTMLText;
|
||||
use SilverStripe\ORM\FieldType\DBField;
|
||||
|
||||
/**
|
||||
* A ViewableData object is any object that can be rendered into a template/view.
|
||||
@ -399,9 +397,9 @@ class ViewableData extends Object implements IteratorAggregate {
|
||||
// Force cast
|
||||
$castingHelper = $this->castingHelper($fieldName);
|
||||
$valueObject = Object::create_from_string($castingHelper, $fieldName);
|
||||
$valueObject->setValue($value, $this);
|
||||
$value = $valueObject;
|
||||
}
|
||||
$valueObject->setValue($value, $this);
|
||||
$value = $valueObject;
|
||||
}
|
||||
|
||||
// Record in cache
|
||||
if($cache) {
|
||||
|
Loading…
x
Reference in New Issue
Block a user