Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Omitting Script attribute if none or default #53

Merged
merged 1 commit into from
Oct 25, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions src/Helper/HeadScript.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ class HeadScript extends Placeholder\Container\AbstractStandalone
const FILE = 'FILE';
const SCRIPT = 'SCRIPT';

/**
* @internal
*/
const DEFAULT_SCRIPT_TYPE = 'text/javascript';
DennisDobslaf marked this conversation as resolved.
Show resolved Hide resolved
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This constant should also be used in Laminas\View\Helper\InlineScript:

public function __invoke(
$mode = self::FILE,
$spec = null,
$placement = 'APPEND',
array $attrs = [],
$type = 'text/javascript'
) {

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Like $type = HeadScript::DEFAULT_SCRIPT_TYPE
or by making a separated "Enum" Class?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The InlineScript helper is an extension of the HeadScript helper:

class InlineScript extends HeadScript

Therefore: $type = self::DEFAULT_SCRIPT_TYPE

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, I've overlooked this fact.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably, as we cannot add visibility on the constant, we should add php annotation @internal for now. Then in next minor, when we bump php version we can use private/protected visibility.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added your suggestion @michalbundyra


/**
* Are arbitrary attributes allowed?
*
Expand Down Expand Up @@ -130,7 +135,7 @@ public function __invoke(
$spec = null,
$placement = 'APPEND',
array $attrs = [],
$type = 'text/javascript'
$type = self::DEFAULT_SCRIPT_TYPE
) {
if ((null !== $spec) && is_string($spec)) {
$action = ucfirst(strtolower($mode));
Expand Down Expand Up @@ -171,7 +176,7 @@ public function __call($method, $args)

$action = $matches['action'];
$mode = strtolower($matches['mode']);
$type = 'text/javascript';
$type = self::DEFAULT_SCRIPT_TYPE;
$attrs = [];

if ('offsetSet' == $action) {
Expand Down Expand Up @@ -267,7 +272,7 @@ public function toString($indent = null)
*/
public function captureStart(
$captureType = Placeholder\Container\AbstractContainer::APPEND,
$type = 'text/javascript',
$type = self::DEFAULT_SCRIPT_TYPE,
$attrs = []
) {
if ($this->captureLock) {
Expand Down Expand Up @@ -401,7 +406,10 @@ public function itemToString($item, $indent, $escapeStart, $escapeEnd)
$addScriptEscape = ! (isset($item->attributes['noescape'])
&& filter_var($item->attributes['noescape'], FILTER_VALIDATE_BOOLEAN));

if (empty($item->type) && $this->view && $this->view->plugin('doctype')->isHtml5()) {
if ((empty($item->type) || strtolower($item->type) === self::DEFAULT_SCRIPT_TYPE)
&& $this->view
&& $this->view->plugin('doctype')->isHtml5()
) {
$html = '<script ' . $attrString . '>';
} else {
$type = ($this->autoEscape) ? $this->escapeAttribute($item->type) : $item->type;
Expand Down
2 changes: 1 addition & 1 deletion src/Helper/InlineScript.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public function __invoke(
$spec = null,
$placement = 'APPEND',
array $attrs = [],
$type = 'text/javascript'
$type = self::DEFAULT_SCRIPT_TYPE
) {
return parent::__invoke($mode, $spec, $placement, $attrs, $type);
}
Expand Down
14 changes: 14 additions & 0 deletions test/Helper/HeadScriptTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -534,4 +534,18 @@ public function testSupportsAsyncAttribute()
$test = $this->helper->__invoke()->toString();
$this->assertContains('async="', $test);
}

/**
* @group 23
*/
public function testOmitsTypeAttributeIfNoneGivenAndHtml5Doctype()
{
$view = new \Laminas\View\Renderer\PhpRenderer();
$view->plugin('doctype')->setDoctype(\Laminas\View\Helper\Doctype::HTML5);
$this->helper->setView($view);

$this->helper->__invoke()->appendScript('// some script' . PHP_EOL);
$test = $this->helper->__invoke()->toString();
$this->assertNotRegExp('#type="text/javascript"#i', $test);
}
}