Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
Y
yii2
Project
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
PSDI Army
yii2
Commits
9a0717dc
Commit
9a0717dc
authored
Jun 20, 2013
by
Qiang Xue
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #560 from resurtm/spaceless-widget
Add spaceless widget.
parents
6fd1c166
aa8fbd96
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
107 additions
and
0 deletions
+107
-0
Spaceless.php
framework/yii/widgets/Spaceless.php
+69
-0
SpacelessTest.php
tests/unit/framework/widgets/SpacelessTest.php
+38
-0
No files found.
framework/yii/widgets/Spaceless.php
0 → 100644
View file @
9a0717dc
<?php
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace
yii\widgets
;
use
yii\base\Widget
;
/**
* Spaceless widget removes whitespace characters between HTML tags. Whitespaces within HTML tags
* or in a plain text are always left untouched.
*
* Usage example:
*
* ```php
* <body>
* <?php Spaceless::begin(); ?>
* <div class="nav-bar">
* <!-- tags -->
* </div>
* <div class="content">
* <!-- tags -->
* </div>
* <?php Spaceless::end(); ?>
* </body>
* ```
*
* This example will generate the following HTML:
*
* ```html
* <body>
* <div class="navbar"><!-- other tags --></div><div class="content"><!-- other tags --></div></body>
* ```
*
* This method is not designed for content compression (you should use `gzip` output compression to
* achieve it). Main intention is to strip out extra whitespace characters between HTML tags in order
* to avoid browser rendering quirks in some circumstances (e.g. newlines between inline-block elements).
*
* Note, never use this method with `pre` or `textarea` tags. It's not that trivial to deal with such tags
* as it may seem at first sight. For this case you should consider using
* [HTML Tidy Project](http://tidy.sourceforge.net/) instead.
*
* @see http://tidy.sourceforge.net/
* @author resurtm <resurtm@gmail.com>
* @since 2.0
*/
class
Spaceless
extends
Widget
{
/**
* Starts capturing an output to be cleaned from whitespace characters between HTML tags.
*/
public
function
init
()
{
ob_start
();
ob_implicit_flush
(
false
);
}
/**
* Marks the end of content to be cleaned from whitespace characters between HTML tags.
* Stops capturing an output and echoes cleaned result.
*/
public
function
run
()
{
echo
trim
(
preg_replace
(
'/>\s+</'
,
'><'
,
ob_get_clean
()));
}
}
tests/unit/framework/widgets/SpacelessTest.php
0 → 100644
View file @
9a0717dc
<?php
namespace
yiiunit\framework\widgets
;
use
yii\widgets\Spaceless
;
class
SpacelessTest
extends
\yiiunit\TestCase
{
public
function
testWidget
()
{
ob_start
();
ob_implicit_flush
(
false
);
echo
"<body>
\n
"
;
Spaceless
::
begin
();
echo
"
\t
<div class='wrapper'>
\n
"
;
Spaceless
::
begin
();
echo
"
\t\t
<div class='left-column'>
\n
"
;
echo
"
\t\t\t
<p>This is a left bar!</p>
\n
"
;
echo
"
\t\t
</div>
\n\n
"
;
echo
"
\t\t
<div class='right-column'>
\n
"
;
echo
"
\t\t\t
<p>This is a right bar!</p>
\n
"
;
echo
"
\t\t
</div>
\n
"
;
Spaceless
::
end
();
echo
"
\t
</div>
\n
"
;
Spaceless
::
end
();
echo
"
\t
<p>Bye!</p>
\n
"
;
echo
"</body>
\n
"
;
$expected
=
"<body>
\n
<div class='wrapper'><div class='left-column'><p>This is a left bar!</p>"
.
"</div><div class='right-column'><p>This is a right bar!</p></div></div>
\t
<p>Bye!</p>
\n
</body>
\n
"
;
$this
->
assertEquals
(
$expected
,
ob_get_clean
());
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment