diff --git a/framework/yii/widgets/Spaceless.php b/framework/yii/widgets/Spaceless.php new file mode 100644 index 0000000..8115f85 --- /dev/null +++ b/framework/yii/widgets/Spaceless.php @@ -0,0 +1,69 @@ + + * + * + *
+ * + *
+ * + * + * ``` + * + * This example will generate the following HTML: + * + * ```html + * + *
+ * ``` + * + * 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 + * @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())); + } +} diff --git a/tests/unit/framework/widgets/SpacelessTest.php b/tests/unit/framework/widgets/SpacelessTest.php new file mode 100644 index 0000000..9b67600 --- /dev/null +++ b/tests/unit/framework/widgets/SpacelessTest.php @@ -0,0 +1,38 @@ +\n"; + + Spaceless::begin(); + echo "\t
\n"; + + Spaceless::begin(); + echo "\t\t
\n"; + echo "\t\t\t

This is a left bar!

\n"; + echo "\t\t
\n\n"; + echo "\t\t
\n"; + echo "\t\t\t

This is a right bar!

\n"; + echo "\t\t
\n"; + Spaceless::end(); + + echo "\t
\n"; + Spaceless::end(); + + echo "\t

Bye!

\n"; + echo "\n"; + + $expected="\n

This is a left bar!

". + "

This is a right bar!

\t

Bye!

\n\n"; + $this->assertEquals($expected,ob_get_clean()); + } +}