Skip to content
Open
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
5 changes: 2 additions & 3 deletions src/Helper/Scripts.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ public function addCond($cond, $src, $pos = 100, array $attr = array())
public function addInternal($script, $pos = 100, array $attr = array())
{
$attr = $this->attr(null, $attr);
$tag = "<script $attr>$script</script>";
$tag = $attr ? "<script $attr>$script</script>" : "<script>$script</script>";
Copy link
Member

Choose a reason for hiding this comment

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

Do we need this ? I think $attr will be empty string if $attr is empty. Any reason you bring this ?

Copy link
Member Author

Choose a reason for hiding this comment

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

Without the ternary, an empty $attr produces a trailing space:

  // $attr = '' (empty string)
  "<script $attr>$script</script>"
  // outputs: "<script >$script</script>"

The ternary ensures clean output:

  "<script>$script</script>"

$this->addElement($pos, $tag);
return $this;
}
Expand All @@ -109,7 +109,7 @@ public function addCondInternal($cond, $script, $pos = 100, array $attr = array(
{
$cond = $this->escaper->html($cond);
$attr = $this->attr(null, $attr);
$tag = "<!--[if $cond]><script $attr>$script</script><![endif]-->";
$tag = $attr ? "<!--[if $cond]><script $attr>$script</script><![endif]-->" : "<!--[if $cond]><script>$script</script><![endif]-->";
$this->addElement($pos, $tag);

return $this;
Expand Down Expand Up @@ -186,7 +186,6 @@ protected function attr($src = null, array $attr = array())
if (null !== $src) {
$attr['src'] = $src;
}
$attr['type'] = 'text/javascript';
return $this->escaper->attr($attr);
}
}
1 change: 0 additions & 1 deletion src/Helper/Styles.php
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,6 @@ protected function fixAttr($href, ?array $attr = null)
$base = array(
'rel' => 'stylesheet',
'href' => $href,
'type' => 'text/css',
'media' => 'screen',
);

Expand Down
20 changes: 10 additions & 10 deletions tests/Helper/ScriptsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public function test__invoke()
$this->assertInstanceOf('Aura\Html\Helper\Scripts', $actual);

$actual = $scripts('script.js')->__toString();
$expect = ' <script src="script.js" type="text/javascript"></script>' . PHP_EOL;
$expect = ' <script src="script.js"></script>' . PHP_EOL;
$this->assertSame($expect, $actual);
}

Expand All @@ -31,11 +31,11 @@ public function test()

$actual = $scripts->__toString();

$expect = ' <script src="/js/first.js" type="text/javascript"></script>' . PHP_EOL
. ' <script foo bar="baz" bing="true" src="/js/attr.js" type="text/javascript"></script>'. PHP_EOL
. ' <script src="/js/middle.js" type="text/javascript"></script>' . PHP_EOL
. ' <!--[if ie6]><script src="/js/ie6.js" type="text/javascript"></script><![endif]-->' . PHP_EOL
. ' <script src="/js/last.js" type="text/javascript"></script>' . PHP_EOL;
$expect = ' <script src="/js/first.js"></script>' . PHP_EOL
. ' <script foo bar="baz" bing="true" src="/js/attr.js"></script>'. PHP_EOL
. ' <script src="/js/middle.js"></script>' . PHP_EOL
. ' <!--[if ie6]><script src="/js/ie6.js"></script><![endif]-->' . PHP_EOL
. ' <script src="/js/last.js"></script>' . PHP_EOL;

$this->assertSame($expect, $actual);
}
Expand All @@ -57,10 +57,10 @@ public function testInternal()

$actual = $scripts->__toString();

$expect = ' <script type="text/javascript">alert("foo");</script>' . PHP_EOL
. ' <!--[if ie6]><script type="text/javascript">alert("ie6");</script><![endif]-->' . PHP_EOL
. ' <script type="text/javascript">alert("captured");</script>' . PHP_EOL
. ' <!--[if ie6]><script type="text/javascript">alert("captured ie6");</script><![endif]-->' . PHP_EOL;
$expect = ' <script>alert("foo");</script>' . PHP_EOL
. ' <!--[if ie6]><script>alert("ie6");</script><![endif]-->' . PHP_EOL
. ' <script>alert("captured");</script>' . PHP_EOL
. ' <!--[if ie6]><script>alert("captured ie6");</script><![endif]-->' . PHP_EOL;


$this->assertSame($expect, $actual);
Expand Down
26 changes: 13 additions & 13 deletions tests/Helper/StylesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public function test__invoke()
$this->assertInstanceOf('Aura\Html\Helper\Styles', $actual);

$actual = $styles('style.css')->__toString();
$expect = ' <link rel="stylesheet" href="style.css" type="text/css" media="screen" />' . PHP_EOL;
$expect = ' <link rel="stylesheet" href="style.css" media="screen" />' . PHP_EOL;
$this->assertSame($expect, $actual);
}

Expand All @@ -28,10 +28,10 @@ public function testAddAndGet()
$styles->addCond('ie6', '/css/ie6.css');

$actual = $styles->__toString();
$expect = ' <link rel="stylesheet" href="/css/first.css" type="text/css" media="screen" />' . PHP_EOL
. ' <link rel="stylesheet" href="/css/middle.css" type="text/css" media="screen" />' . PHP_EOL
. ' <!--[if ie6]><link rel="stylesheet" href="/css/ie6.css" type="text/css" media="screen" /><![endif]-->' . PHP_EOL
. ' <link rel="stylesheet" href="/css/last.css" type="text/css" media="screen" />' . PHP_EOL;
$expect = ' <link rel="stylesheet" href="/css/first.css" media="screen" />' . PHP_EOL
. ' <link rel="stylesheet" href="/css/middle.css" media="screen" />' . PHP_EOL
. ' <!--[if ie6]><link rel="stylesheet" href="/css/ie6.css" media="screen" /><![endif]-->' . PHP_EOL
. ' <link rel="stylesheet" href="/css/last.css" media="screen" />' . PHP_EOL;

$this->assertSame($expect, $actual);
}
Expand All @@ -47,10 +47,10 @@ public function testSetIndentAndAttribs()
$styles->addCond('ie6', '/css/ie6.css', array('media' => 'print'));
$actual = $styles->__toString();

$expect = ' <link rel="stylesheet" href="/css/first.css" type="text/css" media="screen" />' . PHP_EOL
. ' <link rel="stylesheet" href="/css/middle.css" type="text/css" media="print" />' . PHP_EOL
. ' <!--[if ie6]><link rel="stylesheet" href="/css/ie6.css" type="text/css" media="print" /><![endif]-->' . PHP_EOL
. ' <link rel="stylesheet" href="/css/last.css" type="text/css" media="screen" />' . PHP_EOL;
$expect = ' <link rel="stylesheet" href="/css/first.css" media="screen" />' . PHP_EOL
. ' <link rel="stylesheet" href="/css/middle.css" media="print" />' . PHP_EOL
. ' <!--[if ie6]><link rel="stylesheet" href="/css/ie6.css" media="print" /><![endif]-->' . PHP_EOL
. ' <link rel="stylesheet" href="/css/last.css" media="screen" />' . PHP_EOL;

$this->assertSame($expect, $actual);
}
Expand All @@ -72,10 +72,10 @@ public function testInternal()

$actual = $styles->__toString();

$expect = ' <style type="text/css" media="screen">.foo{color:red;}</style>' . PHP_EOL
. ' <!--[if ie6]><style type="text/css" media="screen">.foo{color:yellow;}</style><![endif]-->' . PHP_EOL
. ' <style type="text/css" media="screen">.bar{color:green;}</style>' . PHP_EOL
. ' <!--[if ie6]><style type="text/css" media="screen">.bar{color:pink;}</style><![endif]-->' . PHP_EOL;
$expect = ' <style media="screen">.foo{color:red;}</style>' . PHP_EOL
. ' <!--[if ie6]><style media="screen">.foo{color:yellow;}</style><![endif]-->' . PHP_EOL
. ' <style media="screen">.bar{color:green;}</style>' . PHP_EOL
. ' <!--[if ie6]><style media="screen">.bar{color:pink;}</style><![endif]-->' . PHP_EOL;

$this->assertSame($expect, $actual);
}
Expand Down