Compare commits

..
37 Commits
Author SHA1 Message Date
Matt Lantz 2f7f2e127c Merge pull request #258 from StefanPrintezis/htmlentities_encoding
Values are now encoded consistently by the formbuilder and the htmlbuilder
2016-12-13 09:23:36 -05:00
Stefan Printezis f153be5313 Added function parameters and corrected 2 function calls 2016-11-11 00:05:46 +01:00
Stefan Printezis 6d1d46a258 Created a function for correctly converting characters to entities. 2016-11-10 23:58:02 +01:00
Stefan Printezis df0e112720 Less ambiguous test samples for the select options and placeholder 2016-10-30 01:21:38 +02:00
Stefan Printezis c1a105975b Values are encoded consistently by the formbuilder and the htmlbuilder 2016-10-28 12:37:15 +02:00
Adam Engebretson 961ce141c1 v5.3.0 2016-08-27 18:52:43 -05:00
Adam Engebretson c4c87916c3 Updating for 5.3 2016-08-03 22:49:50 -05:00
Matt Lantz abec449ed8 Merge pull request #197 from rajatsharma94/5.2
added nbsp() to generate repeated &nbsp tag
2016-07-12 10:16:43 -04:00
rajatsharma94 eb132aed5c added nbsp() to generate repeated &nbsp tag 2016-07-11 11:47:54 +05:30
Matt Lantz 9f534cb45b Merge pull request #202 from aygee/get-form-value-related-model
Remove unreachable return statement
2016-07-01 11:45:31 -04:00
Adrian Gunawan 71b81b7f96 Remove unreachable return statement 2016-06-30 20:59:34 +10:00
Matt Lantz bcf0697d7d Merge pull request #191 from aygee/get-form-value-related-model
Getting form value from related model
2016-06-19 01:01:55 -04:00
Matt Lantz d79805ade4 Merge pull request #193 from JamesAlday/5.2-link-fix
Make escaping of labels for HTML and mailto links optional
2016-06-10 15:16:34 -04:00
James Alday 24ee0aadc0 StyleCI fixes - remove empty line after class declaration 2016-06-10 15:12:35 -04:00
James Alday 0fc25932db Update closure syntax for StyleCI check 2016-06-10 14:03:38 -04:00
James Alday 92a4833e11 Add test for htmlBuilder link and mailto escaping 2016-06-10 14:01:04 -04:00
Matt Lantz f6d9a66624 Merge pull request #174 from StefanPrintezis/form_label_html
Html escaping is now optional for labels
2016-06-10 11:26:30 -04:00
James Alday 0d1d1fcd5a Add 'escape' option to helper function 2016-05-21 15:50:41 -04:00
James Alday f9a4e185f4 Make escaping of labels for HTML and mailto links optional 2016-05-21 15:10:11 -04:00
Matt Lantz 4d5ed8a59a Merge pull request #155 from rafael-renan-pacheco/5.2
Avoid using session to build form
2016-05-19 11:46:23 -04:00
Adrian Gunawan 0625f71fd9 Fix the issue of getting form value from related model 2016-05-19 15:47:42 +10:00
Adrian Gunawan a3d42da280 Transform the field name when getting the form value from model 2016-05-14 20:50:52 +10:00
Stefan Printezis d65deb4e77 Html escaping is now optional for labels 2016-03-23 13:13:50 +01:00
Tom Shafer 02c75dff24 Merge pull request #128 from EspadaV8/feature/add-missing-form-select-test
Add test for select optgroups
2016-03-02 21:07:38 -07:00
Alexandre Butynski 9dc27a4188 Add PHP 7 to travis configuration 2016-03-02 21:07:31 -07:00
Rafael Renan Pacheco df8acc24bb Avoid using session to build form 2016-02-01 09:32:17 -02:00
Adam Engebretson 3a312d39ff Merge pull request #153 from mikemand/patch-1
Use array_get's third parameter for default value.
2016-01-27 16:29:54 -06:00
Micheal Mand fa2628d311 Use array_get's third parameter for default value.
This allows for using boolean values as default options.
2016-01-27 14:49:58 -06:00
Adam Engebretson e5011d337c Merge pull request #151 from antonioribeiro/patch-1
Remove uneeded use.
2016-01-26 18:26:51 -06:00
Antonio Carlos Ribeiro 0e8e2dbc67 Remove uneeded use. 2016-01-26 21:39:03 -02:00
Adam Engebretson 7ec0ca55ca Merge pull request #146 from paulvl/5.2
Function Tag
2016-01-20 12:28:21 -06:00
Paul Vidal 6f35784c8b added function tag 2016-01-20 00:56:22 -05:00
Adam Engebretson c88b2d59a5 Revert "[5.2] Use toArray() for Model Object Value"
This reverts commit e7db3dd11a.
2016-01-16 10:54:49 -06:00
Adam Engebretson add2c74b14 Merge pull request #135 from dennisoderwald/patch-1 2016-01-15 15:59:03 -06:00
Tom Shafer 10111f558c Merge pull request #136 from LaravelCollective/analysis-8mkeVq
Applied fixes from StyleCI
2015-12-28 20:09:30 -05:00
Tom Shafer b60bb4da7c Applied fixes from StyleCI 2015-12-28 20:09:17 -05:00
Dennis Oderwald e7db3dd11a [5.2] Use toArray() for Model Object Value 2015-12-23 22:28:01 +01:00
10 changed files with 210 additions and 37 deletions

No files matched your search

+2 -1
View File
@@ -1,8 +1,9 @@
language: php
php:
- 5.5
- 5.6
- 7.0
- 7.1
- hhvm
sudo: false
+9 -9
View File
@@ -18,17 +18,17 @@
}
],
"require": {
"php": ">=5.5.9",
"illuminate/http": "5.2.*",
"illuminate/routing": "5.2.*",
"illuminate/session": "5.2.*",
"illuminate/support": "5.2.*",
"illuminate/view": "5.2.*"
"php": ">=5.6.4",
"illuminate/http": "5.3.*",
"illuminate/routing": "5.3.*",
"illuminate/session": "5.3.*",
"illuminate/support": "5.3.*",
"illuminate/view": "5.3.*"
},
"require-dev": {
"illuminate/database": "5.2.*",
"mockery/mockery": "~0.9",
"phpunit/phpunit": "~4.0"
"illuminate/database": "5.3.*",
"mockery/mockery": "~0.9.4",
"phpunit/phpunit": "~5.4"
},
"autoload": {
"psr-4": {
+1 -2
View File
@@ -81,7 +81,7 @@ trait Componentable
$default = null;
}
$data[$variable] = array_get($arguments, $i) ?: $default;
$data[$variable] = array_get($arguments, $i, $default);
$i++;
}
@@ -107,5 +107,4 @@ trait Componentable
throw new BadMethodCallException("Method {$method} does not exist.");
}
}
+3 -2
View File
@@ -41,7 +41,8 @@ trait FormAccessible
return $this->mutateFormAttribute($key, $value);
}
return $value;
// No form mutator, let the model resolve this
return data_get($this, $key);
}
/**
@@ -54,7 +55,7 @@ trait FormAccessible
$methods = $this->getReflection()->getMethods(ReflectionMethod::IS_PUBLIC);
$mutator = collect($methods)
->first(function ($index, ReflectionMethod $method) use ($key) {
->first(function (ReflectionMethod $method) use ($key) {
return $method->getName() == 'form' . Str::studly($key) . 'Attribute';
});
Executable → Regular
+13 -10
View File
@@ -7,14 +7,12 @@ use BadMethodCallException;
use Illuminate\Support\Collection;
use Illuminate\Support\HtmlString;
use Illuminate\Contracts\View\Factory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Session\SessionInterface;
use Illuminate\Support\Traits\Macroable;
use Illuminate\Contracts\Routing\UrlGenerator;
class FormBuilder
{
use Macroable, Componentable {
Macroable::__call as macroCall;
Componentable::__call as componentCall;
@@ -211,16 +209,21 @@ class FormBuilder
* @param string $name
* @param string $value
* @param array $options
* @param bool $escape_html
*
* @return \Illuminate\Support\HtmlString
*/
public function label($name, $value = null, $options = [])
public function label($name, $value = null, $options = [], $escape_html = true)
{
$this->labels[] = $name;
$options = $this->html->attributes($options);
$value = e($this->formatLabel($name, $value));
$value = $this->formatLabel($name, $value);
if ($escape_html) {
$value = $this->html->entities($value);
}
return $this->toHtmlString('<label for="' . $name . '"' . $options . '>' . $value . '</label>');
}
@@ -482,7 +485,7 @@ class FormBuilder
// the element. Then we'll create the final textarea elements HTML for us.
$options = $this->html->attributes($options);
return $this->toHtmlString('<textarea' . $options . '>' . e($value) . '</textarea>');
return $this->toHtmlString('<textarea' . $options . '>' . $this->html->escapeAll($value). '</textarea>');
}
/**
@@ -659,7 +662,7 @@ class FormBuilder
$html[] = $this->option($display, $value, $selected);
}
return $this->toHtmlString('<optgroup label="' . e($label) . '">' . implode('', $html) . '</optgroup>');
return $this->toHtmlString('<optgroup label="' . $this->html->escapeAll($label) . '">' . implode('', $html) . '</optgroup>');
}
/**
@@ -677,7 +680,7 @@ class FormBuilder
$options = ['value' => $value, 'selected' => $selected];
return $this->toHtmlString('<option' . $this->html->attributes($options) . '>' . e($display) . '</option>');
return $this->toHtmlString('<option' . $this->html->attributes($options) . '>' . $this->html->escapeAll($display) . '</option>');
}
/**
@@ -695,7 +698,7 @@ class FormBuilder
$options = compact('selected');
$options['value'] = '';
return $this->toHtmlString('<option' . $this->html->attributes($options) . '>' . e($display) . '</option>');
return $this->toHtmlString('<option' . $this->html->attributes($options) . '>' . $this->html->escapeAll($display) . '</option>');
}
/**
@@ -1081,7 +1084,7 @@ class FormBuilder
return $value;
}
if (! is_null($this->old($name))) {
if (! is_null($this->old($name)) && $name != '_method') {
return $this->old($name);
}
@@ -1104,7 +1107,7 @@ class FormBuilder
protected function getModelValueAttribute($name)
{
if (method_exists($this->model, 'getFormValue')) {
return $this->model->getFormValue($name);
return $this->model->getFormValue($this->transformKey($name));
}
return data_get($this->model, $this->transformKey($name));
+55 -7
View File
@@ -10,7 +10,6 @@ use Illuminate\Contracts\Routing\UrlGenerator;
class HtmlBuilder
{
use Macroable, Componentable {
Macroable::__call as macroCall;
Componentable::__call as componentCall;
@@ -54,6 +53,18 @@ class HtmlBuilder
return htmlentities($value, ENT_QUOTES, 'UTF-8', false);
}
/**
* Convert all applicable characters to HTML entities.
*
* @param string $value
*
* @return string
*/
public function escapeAll($value)
{
return htmlentities($value, ENT_QUOTES, 'UTF-8');
}
/**
* Convert entities to HTML characters.
*
@@ -147,10 +158,11 @@ class HtmlBuilder
* @param string $title
* @param array $attributes
* @param bool $secure
* @param bool $escape
*
* @return \Illuminate\Support\HtmlString
*/
public function link($url, $title = null, $attributes = [], $secure = null)
public function link($url, $title = null, $attributes = [], $secure = null, $escape = true)
{
$url = $this->url->to($url, [], $secure);
@@ -158,7 +170,11 @@ class HtmlBuilder
$title = $url;
}
return $this->toHtmlString('<a href="' . $url . '"' . $this->attributes($attributes) . '>' . $this->entities($title) . '</a>');
if ($escape) {
$title = $this->entities($title);
}
return $this->toHtmlString('<a href="' . $url . '"' . $this->attributes($attributes) . '>' . $title . '</a>');
}
/**
@@ -242,18 +258,23 @@ class HtmlBuilder
* @param string $email
* @param string $title
* @param array $attributes
* @param bool $escape
*
* @return \Illuminate\Support\HtmlString
*/
public function mailto($email, $title = null, $attributes = [])
public function mailto($email, $title = null, $attributes = [], $escape = true)
{
$email = $this->email($email);
$title = $title ?: $email;
if ($escape) {
$title = $this->entities($title);
}
$email = $this->obfuscate('mailto:') . $email;
return $this->toHtmlString('<a href="' . $email . '"' . $this->attributes($attributes) . '>' . $this->entities($title) . '</a>');
return $this->toHtmlString('<a href="' . $email . '"' . $this->attributes($attributes) . '>' . $title . '</a>');
}
/**
@@ -268,6 +289,18 @@ class HtmlBuilder
return str_replace('@', '&#64;', $this->obfuscate($email));
}
/**
* Generates non-breaking space entities based on number supplied.
*
* @param int $num
*
* @return string
*/
public function nbsp($num = 1)
{
return str_repeat('&nbsp;', $num);
}
/**
* Generate an ordered list of items.
*
@@ -366,7 +399,7 @@ class HtmlBuilder
if (is_array($value)) {
return $this->nestedListing($key, $type, $value);
} else {
return '<li>' . e($value) . '</li>';
return '<li>' . $this->escapeAll($value) . '</li>';
}
}
@@ -428,7 +461,7 @@ class HtmlBuilder
}
if (! is_null($value)) {
return $key . '="' . e($value) . '"';
return $key . '="' . $this->escapeAll($value) . '"';
}
}
@@ -486,6 +519,21 @@ class HtmlBuilder
return $this->toHtmlString('<meta' . $this->attributes($attributes) . '>' . PHP_EOL);
}
/**
* Generate an html tag.
*
* @param string $tag
* @param mixed $content
* @param array $attributes
*
* @return \Illuminate\Support\HtmlString
*/
public function tag($tag, $content, array $attributes = [])
{
$content = is_array($content) ? implode(PHP_EOL, $content) : $content;
return $this->toHtmlString('<' . $tag . $this->attributes($attributes) . '>' . PHP_EOL . $this->toHtmlString($content) . PHP_EOL . '</' . $tag . '>' . PHP_EOL);
}
/**
* Transform the string to an Html serializable object
*
+3 -2
View File
@@ -8,12 +8,13 @@ if (! function_exists('link_to')) {
* @param string $title
* @param array $attributes
* @param bool $secure
* @param bool $escape
*
* @return string
*/
function link_to($url, $title = null, $attributes = [], $secure = null)
function link_to($url, $title = null, $attributes = [], $secure = null, $escape = true)
{
return app('html')->link($url, $title, $attributes, $secure);
return app('html')->link($url, $title, $attributes, $secure, $escape);
}
}
+9 -3
View File
@@ -14,7 +14,6 @@ use Mockery as m;
class FormAccessibleTest extends PHPUnit_Framework_TestCase
{
public function setUp()
{
Capsule::table('models')->truncate();
@@ -25,6 +24,9 @@ class FormAccessibleTest extends PHPUnit_Framework_TestCase
$this->modelData = [
'string' => 'abcdefghijklmnop',
'email' => 'tj@tjshafer.com',
'address' => [
'street' => 'abcde st'
],
'created_at' => $this->now,
'updated_at' => $this->now,
];
@@ -44,6 +46,12 @@ class FormAccessibleTest extends PHPUnit_Framework_TestCase
$this->assertEquals($model->getFormValue('created_at'), $this->now->timestamp);
}
public function testItCanGetRelatedValueForForms()
{
$model = new ModelThatUsesForms($this->modelData);
$this->assertEquals($model->getFormValue('address.street'), 'abcde st');
}
public function testItCanStillMutateValuesForViews()
{
$model = new ModelThatUsesForms($this->modelData);
@@ -65,7 +73,6 @@ class FormAccessibleTest extends PHPUnit_Framework_TestCase
class ModelThatUsesForms extends Model
{
use FormAccessible;
protected $table = 'models';
@@ -93,7 +100,6 @@ class ModelThatUsesForms extends Model
class ModelThatDoesntUseForms extends Model
{
protected $table = 'models';
public function getStringAttribute($value)
+46 -1
View File
@@ -58,9 +58,11 @@ class FormBuilderTest extends PHPUnit_Framework_TestCase
{
$form1 = $this->formBuilder->label('foo', 'Foobar');
$form2 = $this->formBuilder->label('foo', 'Foobar', ['class' => 'control-label']);
$form3 = $this->formBuilder->label('foo', 'Foobar <i>bar</i>', null, false);
$this->assertEquals('<label for="foo">Foobar</label>', $form1);
$this->assertEquals('<label for="foo" class="control-label">Foobar</label>', $form2);
$this->assertEquals('<label for="foo">Foobar <i>bar</i></label>', $form3);
}
public function testFormInput()
@@ -237,11 +239,13 @@ class FormBuilderTest extends PHPUnit_Framework_TestCase
$form2 = $this->formBuilder->textarea('foo', 'foobar');
$form3 = $this->formBuilder->textarea('foo', null, ['class' => 'span2']);
$form4 = $this->formBuilder->textarea('foo', null, ['size' => '60x15']);
$form5 = $this->formBuilder->textarea('encoded_html', '&amp;');
$this->assertEquals('<textarea name="foo" cols="50" rows="10"></textarea>', $form1);
$this->assertEquals('<textarea name="foo" cols="50" rows="10">foobar</textarea>', $form2);
$this->assertEquals('<textarea class="span2" name="foo" cols="50" rows="10"></textarea>', $form3);
$this->assertEquals('<textarea name="foo" cols="60" rows="15"></textarea>', $form4);
$this->assertEquals('<textarea name="encoded_html" cols="50" rows="10">&amp;amp;</textarea>', $form5);
}
public function testSelect()
@@ -278,6 +282,38 @@ class FormBuilderTest extends PHPUnit_Framework_TestCase
['name' => 'select-name']
);
$this->assertEquals($select, '<select name="select-name" id="select-name-id"></select>');
$select = $this->formBuilder->select(
'size',
[
'Large sizes' => [
'L' => 'Large',
'XL' => 'Extra Large',
],
'S' => 'Small',
],
null,
[
'class' => 'class-name',
'id' => 'select-id',
]
);
$this->assertEquals(
$select,
'<select class="class-name" id="select-id" name="size"><optgroup label="Large sizes"><option value="L">Large</option><option value="XL">Extra Large</option></optgroup><option value="S">Small</option></select>'
);
$select = $this->formBuilder->select(
'encoded_html',
['no_break_space' => '&nbsp;', 'ampersand' => '&amp;', 'lower_than' => '&lt;'],
null
);
$this->assertEquals(
$select,
'<select name="encoded_html"><option value="no_break_space">&amp;nbsp;</option><option value="ampersand">&amp;amp;</option><option value="lower_than">&amp;lt;</option></select>'
);
}
public function testFormSelectRepopulation()
@@ -321,6 +357,16 @@ class FormBuilderTest extends PHPUnit_Framework_TestCase
);
$this->assertEquals($select,
'<select name="size"><option value="">Select One...</option><option value="L" selected="selected">Large</option><option value="S">Small</option></select>');
$select = $this->formBuilder->select(
'encoded_html',
['no_break_space' => '&nbsp;', 'ampersand' => '&amp;', 'lower_than' => '&lt;'],
null,
['placeholder' => 'Select the &nbsp;']
);
$this->assertEquals($select,
'<select name="encoded_html"><option selected="selected" value="">Select the &amp;nbsp;</option><option value="no_break_space">&amp;nbsp;</option><option value="ampersand">&amp;amp;</option><option value="lower_than">&amp;lt;</option></select>'
);
}
public function testFormSelectYear()
@@ -511,7 +557,6 @@ class FormBuilderTest extends PHPUnit_Framework_TestCase
class FormBuilderModelStub
{
protected $data;
public function __construct(array $data = [])
+69
View File
@@ -39,6 +39,28 @@ class HtmlBuilderTest extends PHPUnit_Framework_TestCase
$this->assertEquals('<dl class="example"><dt>foo</dt><dd>bar</dd><dt>bing</dt><dd>baz</dd></dl>', $result);
}
public function testOl()
{
$list = ['foo', 'bar', '&amp;'];
$attributes = ['class' => 'example'];
$ol = $this->htmlBuilder->ol($list, $attributes);
$this->assertEquals('<ol class="example"><li>foo</li><li>bar</li><li>&amp;amp;</li></ol>', $ol);
}
public function testUl()
{
$list = ['foo', 'bar', '&amp;'];
$attributes = ['class' => 'example'];
$ul = $this->htmlBuilder->ul($list, $attributes);
$this->assertEquals('<ul class="example"><li>foo</li><li>bar</li><li>&amp;amp;</li></ul>', $ul);
}
public function testMeta()
{
$result = $this->htmlBuilder->meta('description', 'Lorem ipsum dolor sit amet.');
@@ -46,6 +68,27 @@ class HtmlBuilderTest extends PHPUnit_Framework_TestCase
$this->assertEquals('<meta name="description" content="Lorem ipsum dolor sit amet.">' . PHP_EOL, $result);
}
public function testTag()
{
$result1 = $this->htmlBuilder->tag('p', 'Lorem ipsum dolor sit amet.');
$result2 = $this->htmlBuilder->tag('p', 'Lorem ipsum dolor sit amet.', ['class' => 'text-center']);
$result3 = $this->htmlBuilder->tag('div', '<p>Lorem ipsum dolor sit amet.</p>', ['class' => 'row']);
$content = [
$this->htmlBuilder->image('http://example.com/image1'),
$this->htmlBuilder->image('http://example.com/image2'),
];
$result4 = $this->htmlBuilder->tag('div', $content, ['class' => 'row']);
$this->assertEquals('<p>' . PHP_EOL . 'Lorem ipsum dolor sit amet.' . PHP_EOL . '</p>' . PHP_EOL, $result1);
$this->assertEquals('<p class="text-center">' . PHP_EOL . 'Lorem ipsum dolor sit amet.' . PHP_EOL . '</p>' . PHP_EOL, $result2);
$this->assertEquals('<div class="row">' . PHP_EOL . '<p>Lorem ipsum dolor sit amet.</p>' . PHP_EOL . '</div>' . PHP_EOL, $result3);
$this->assertEquals('<div class="row">' . PHP_EOL . '<img src="http://example.com/image1">' . PHP_EOL . '<img src="http://example.com/image2">' . PHP_EOL . '</div>' . PHP_EOL, $result4);
}
public function testMetaOpenGraph()
{
$result = $this->htmlBuilder->meta(null, 'website', ['property' => 'og:type']);
@@ -68,4 +111,30 @@ class HtmlBuilderTest extends PHPUnit_Framework_TestCase
$this->assertTrue($this->htmlBuilder->hasComponent('tweet'));
}
public function testLink()
{
$result1 = $this->htmlBuilder->link("http://www.example.com", "<span>Example.com</span>", ["class" => "example-link"], null, true);
$result2 = $this->htmlBuilder->link("http://www.example.com", "<span>Example.com</span>", ["class" => "example-link"], null, false);
$this->assertEquals('<a href="http://www.example.com" class="example-link">&lt;span&gt;Example.com&lt;/span&gt;</a>', $result1);
$this->assertEquals('<a href="http://www.example.com" class="example-link"><span>Example.com</span></a>', $result2);
}
public function testMailto()
{
$htmlBuilder = m::mock('Collective\Html\HtmlBuilder[obfuscate,email]', [$this->urlGenerator, $this->viewFactory]);
$htmlBuilder->shouldReceive('obfuscate', 'email')->andReturnUsing(function () {
$args = func_get_args();
return $args[0];
});
$result1 = $htmlBuilder->mailto("person@example.com", "<span>First Name Last</span>", ["class" => "example-link"], true);
$result2 = $htmlBuilder->mailto("person@example.com", "<span>First Name Last</span>", ["class" => "example-link"], false);
$this->assertEquals('<a href="mailto:person@example.com" class="example-link">&lt;span&gt;First Name Last&lt;/span&gt;</a>', $result1);
$this->assertEquals('<a href="mailto:person@example.com" class="example-link"><span>First Name Last</span></a>', $result2);
}
}