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

Filters::spacelessHtml() Add support for use <script> as link to JS #196

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
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
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,15 @@
"nette/utils": "to use filter |webalize"
},
"conflict": {
"nette/application": "<2.4.1"
"nette/application": "<3.0"
},
"autoload": {
"classmap": ["src/"]
},
"minimum-stability": "dev",
"extra": {
"branch-alias": {
"dev-master": "2.5-dev"
"dev-master": "3.0-dev"
}
}
}
2 changes: 1 addition & 1 deletion src/Latte/Engine.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class Engine
{
use Strict;

public const VERSION = '2.5.2';
public const VERSION = '3.0-dev';

/** Content types */
public const
Expand Down
3 changes: 3 additions & 0 deletions src/Latte/Macros/BlockMacros.php
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,9 @@ public function macroBlock(MacroNode $node, PhpWriter $writer)
return "\$this->checkBlockContentType($blockType, $fname);"
. "\$this->blockQueue[$fname][] = [\$this, '{$node->data->func}'];";
}

} elseif ($name[0] === '_') {
throw new CompileException("Block name '$name' must not start with an underscore.");
}

// static snippet/snippetArea
Expand Down
21 changes: 14 additions & 7 deletions src/Latte/Runtime/Filters.php
Original file line number Diff line number Diff line change
Expand Up @@ -316,26 +316,33 @@ public static function strip(FilterInfo $info, string $s): string
* @param bool $strip stripping mode
* @return string HTML
*/
public static function spacelessHtml(string $s, int $phase = null, bool &$strip = true): string
public static function spacelessHtml(string $s, ?int $phase = null, bool &$strip = true): string
{
if ($phase & PHP_OUTPUT_HANDLER_START) {
$s = ltrim($s);
}
if ($phase & PHP_OUTPUT_HANDLER_FINAL) {
$s = rtrim($s);
}
return preg_replace_callback(
$return = (string) preg_replace_callback( // Other cases
Copy link
Member

Choose a reason for hiding this comment

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

Why (string)?

Copy link
Author

Choose a reason for hiding this comment

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

Because PhpStorm hint to me new type can be returned in future. For example null in case of error.

'#[ \t\r\n]+|<(/)?(textarea|pre|script)(?=\W)#si',
function ($m) use (&$strip) {
static function (array $m) use (&$strip): string {
Copy link
Member

Choose a reason for hiding this comment

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

Why static?

Copy link
Author

Choose a reason for hiding this comment

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

PHP does not have to create a new instance every time.

if (empty($m[2])) {
return $strip ? ' ' : $m[0];
} else {
$strip = !empty($m[1]);
return $m[0];
return (string) $strip ? ' ' : $m[0];
}
$strip = !empty($m[1]);
return (string) $m[0];
},
$s
);
$return = (string) preg_replace_callback( // <script> for include JS file
'/<script\s*([^>]+?)>(?:\s*)<\/script>/',
Copy link
Member

Choose a reason for hiding this comment

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

Will this regexp work with input like <script title="> <\/script>"> ?

Copy link
Author

Choose a reason for hiding this comment

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

This input is not supported now. I'll try to come up with a better solution.

static function (array $m) use (&$strip): string {
return $strip ? '<script ' . trim(preg_replace('/\s+/', ' ', $m[1])) . '></script>' : (string) $m[0];
},
$return
);
return $return;
}


Expand Down
14 changes: 7 additions & 7 deletions src/Latte/Runtime/ISnippetBridge.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,17 @@
*/
interface ISnippetBridge
{
function isSnippetMode();
function isSnippetMode(): bool;

function setSnippetMode($snippetMode);
function setSnippetMode(bool $snippetMode);

function needsRedraw($name);
function needsRedraw(string $name): bool;

function markRedrawn($name);
function markRedrawn(string $name): void;

function getHtmlId($name);
function getHtmlId(string $name): string;

function addSnippet($name, $content);
function addSnippet(string $name, string $content): void;

function renderChildren();
function renderChildren(): void;
}
4 changes: 4 additions & 0 deletions tests/Latte/BlockMacros.block5.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,7 @@ Assert::match(
'<br class="123">',
$latte->renderToString('{block test}<br n:class="$var">{/block}', ['var' => 123])
);

Assert::exception(function () use ($latte) {
$latte->renderToString('{define _foobar}Hello{/define}');
}, Latte\CompileException::class, "Block name '_foobar' must not start with an underscore.");
10 changes: 5 additions & 5 deletions tests/Latte/mocks/SnippetBridge.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,33 +18,33 @@ public function isSnippetMode(): bool
}


public function setSnippetMode($snippetMode)
public function setSnippetMode(bool $snippetMode)
{
$this->snippetMode = $snippetMode;
}


public function needsRedraw($name): bool
public function needsRedraw(string $name): bool
{
return $this->invalid === true || isset($this->invalid[$name]);
}


public function markRedrawn($name): void
public function markRedrawn(string $name): void
{
if ($this->invalid !== true) {
unset($this->invalid[$name]);
}
}


public function getHtmlId($name): string
public function getHtmlId(string $name): string
{
return $name;
}


public function addSnippet($name, $content): void
public function addSnippet(string $name, string $content): void
{
$this->payload[$name] = $content;
}
Expand Down