php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Return to Bug #48482
Patch bug48482-2 revision 2014-12-28 19:50 UTC by kalle@php.net
Patch bug48482-doc revision 2014-12-27 18:33 UTC by kalle@php.net
Patch bug48482 revision 2014-12-27 18:27 UTC by kalle@php.net

Patch bug48482-2 for *General Issues Bug #48482

Patch version 2014-12-28 19:50 UTC

Return to Bug #48482 | Download this patch
This patch renders other patches obsolete

Obsolete patches:

Patch Revisions:

Developer: kalle@php.net

 Zend/zend_highlight.c          | 46 ++++++++++++++++++++++++++++++++++--------
 Zend/zend_highlight.h          |  5 +++++
 ext/standard/basic_functions.c | 23 +++++++++++++++------
 main/main.c                    |  1 +
 php.ini-development            |  4 ++++
 php.ini-production             |  4 ++++
 6 files changed, 69 insertions(+), 14 deletions(-)

diff --git a/Zend/zend_highlight.c b/Zend/zend_highlight.c
index 27e4fee..2d391c1 100644
--- a/Zend/zend_highlight.c
+++ b/Zend/zend_highlight.c
@@ -26,11 +26,25 @@
 #include "zend_ptr_stack.h"
 #include "zend_globals.h"
 
+/* {{{ zend_html_putc
+ */
 ZEND_API void zend_html_putc(char c)
 {
+	zend_html_putc_ex(c, NULL);
+}
+/* }}} */
+
+/* {{{ zend_html_putc_ex
+ */
+ZEND_API void zend_html_putc_ex(char c, zend_syntax_highlighter_ini *syntax_highlighter_ini)
+{
 	switch (c) {
 		case '\n':
-			ZEND_PUTS("<br />");
+			if (syntax_highlighter_ini && !syntax_highlighter_ini->highlight_xhtml) {
+				ZEND_PUTS("<br>");
+			} else {
+				ZEND_PUTS("<br />");
+			}
 			break;
 		case '<':
 			ZEND_PUTS("&lt;");
@@ -52,10 +66,20 @@ ZEND_API void zend_html_putc(char c)
 			break;
 	}
 }
+/* }}} */
 
-
+/* {{{ zend_html_puts
+ */
 ZEND_API void zend_html_puts(const char *s, size_t len)
 {
+	zend_html_puts_ex(s, len, NULL);
+}
+/* }}} */
+
+/* {{{ zend_html_puts_ex
+ */
+ZEND_API void zend_html_puts_ex(const char *s, size_t len, zend_syntax_highlighter_ini *syntax_highlighter_ini)
+{
 	const unsigned char *ptr = (const unsigned char*)s, *end = ptr + len;
 	unsigned char *filtered = NULL;
 	size_t filtered_len;
@@ -69,10 +93,10 @@ ZEND_API void zend_html_puts(const char *s, size_t len)
 	while (ptr<end) {
 		if (*ptr==' ') {
 			do {
-				zend_html_putc(*ptr);
+				zend_html_putc_ex(*ptr, syntax_highlighter_ini);
 			} while ((++ptr < end) && (*ptr==' '));
 		} else {
-			zend_html_putc(*ptr++);
+			zend_html_putc_ex(*ptr++, syntax_highlighter_ini);
 		}
 	}
 
@@ -80,8 +104,10 @@ ZEND_API void zend_html_puts(const char *s, size_t len)
 		efree(filtered);
 	}
 }
+/* }}} */
 
-
+/* {{{ zend_highlight
+ */
 ZEND_API void zend_highlight(zend_syntax_highlighter_ini *syntax_highlighter_ini)
 {
 	zval token;
@@ -93,7 +119,7 @@ ZEND_API void zend_highlight(zend_syntax_highlighter_ini *syntax_highlighter_ini
 	zend_printf("<span style=\"color: %s\">\n", last_color);
 	/* highlight stuff coming back from zendlex() */
 	ZVAL_UNDEF(&token);
-	while ((token_type=lex_scan(&token))) {
+	while ((token_type = lex_scan(&token))) {
 		switch (token_type) {
 			case T_INLINE_HTML:
 				next_color = syntax_highlighter_ini->highlight_html;
@@ -121,7 +147,7 @@ ZEND_API void zend_highlight(zend_syntax_highlighter_ini *syntax_highlighter_ini
 				next_color = syntax_highlighter_ini->highlight_string;
 				break;
 			case T_WHITESPACE:
-				zend_html_puts((char*)LANG_SCNG(yy_text), LANG_SCNG(yy_leng));  /* no color needed */
+				zend_html_puts_ex((char*)LANG_SCNG(yy_text), LANG_SCNG(yy_leng), syntax_highlighter_ini);  /* no color needed */
 				ZVAL_UNDEF(&token);
 				continue;
 				break;
@@ -144,7 +170,7 @@ ZEND_API void zend_highlight(zend_syntax_highlighter_ini *syntax_highlighter_ini
 			}
 		}
 
-		zend_html_puts((char*)LANG_SCNG(yy_text), LANG_SCNG(yy_leng));
+		zend_html_puts_ex((char*)LANG_SCNG(yy_text), LANG_SCNG(yy_leng), syntax_highlighter_ini);
 
 		if (Z_TYPE(token) == IS_STRING) {
 			switch (token_type) {
@@ -169,7 +195,10 @@ ZEND_API void zend_highlight(zend_syntax_highlighter_ini *syntax_highlighter_ini
 	zend_printf("</span>\n");
 	zend_printf("</code>");
 }
+/* }}} */
 
+/* {{{ zend_strip
+ */
 ZEND_API void zend_strip(void)
 {
 	zval token;
@@ -225,6 +254,7 @@ ZEND_API void zend_strip(void)
 		ZVAL_UNDEF(&token);
 	}
 }
+/* }}} */
 
 /*
  * Local variables:
diff --git a/Zend/zend_highlight.h b/Zend/zend_highlight.h
index 37bf654..637c9ba 100644
--- a/Zend/zend_highlight.h
+++ b/Zend/zend_highlight.h
@@ -28,6 +28,8 @@
 #define HL_STRING_COLOR      "#DD0000"    /* red */
 #define HL_KEYWORD_COLOR     "#007700"    /* green */
 
+/* Note, no HL_XHTML_DEFAULT, it is simply just "1" */
+
 
 typedef struct _zend_syntax_highlighter_ini {
 	char *highlight_html;
@@ -35,6 +37,7 @@ typedef struct _zend_syntax_highlighter_ini {
 	char *highlight_default;
 	char *highlight_string;
 	char *highlight_keyword;
+	zend_bool highlight_xhtml: 1;
 } zend_syntax_highlighter_ini;
 
 
@@ -44,7 +47,9 @@ ZEND_API void zend_strip(void);
 ZEND_API int highlight_file(char *filename, zend_syntax_highlighter_ini *syntax_highlighter_ini);
 ZEND_API int highlight_string(zval *str, zend_syntax_highlighter_ini *syntax_highlighter_ini, char *str_name);
 ZEND_API void zend_html_putc(char c);
+ZEND_API void zend_html_putc_ex(char c, zend_syntax_highlighter_ini *syntax_highlighter_ini);
 ZEND_API void zend_html_puts(const char *s, size_t len);
+ZEND_API void zend_html_puts_ex(const char *s, size_t len, zend_syntax_highlighter_ini *syntax_highlighter_ini);
 END_EXTERN_C()
 
 extern zend_syntax_highlighter_ini syntax_highlighter_ini;
diff --git a/ext/standard/basic_functions.c b/ext/standard/basic_functions.c
index ed5206a..92f8ad0 100644
--- a/ext/standard/basic_functions.c
+++ b/ext/standard/basic_functions.c
@@ -717,6 +717,7 @@ ZEND_END_ARG_INFO()
 ZEND_BEGIN_ARG_INFO_EX(arginfo_highlight_file, 0, 0, 1)
 	ZEND_ARG_INFO(0, file_name)
 	ZEND_ARG_INFO(0, return)
+	ZEND_ARG_INFO(0, is_xhtml)
 ZEND_END_ARG_INFO()
 
 ZEND_BEGIN_ARG_INFO(arginfo_php_strip_whitespace, 0)
@@ -726,6 +727,7 @@ ZEND_END_ARG_INFO()
 ZEND_BEGIN_ARG_INFO_EX(arginfo_highlight_string, 0, 0, 1)
 	ZEND_ARG_INFO(0, string)
 	ZEND_ARG_INFO(0, return)
+	ZEND_ARG_INFO(0, is_xhtml)
 ZEND_END_ARG_INFO()
 
 ZEND_BEGIN_ARG_INFO(arginfo_ini_get, 0)
@@ -5067,19 +5069,20 @@ ZEND_API void php_get_highlight_struct(zend_syntax_highlighter_ini *syntax_highl
 	syntax_highlighter_ini->highlight_html    = INI_STR("highlight.html");
 	syntax_highlighter_ini->highlight_keyword = INI_STR("highlight.keyword");
 	syntax_highlighter_ini->highlight_string  = INI_STR("highlight.string");
+	syntax_highlighter_ini->highlight_xhtml	  = INI_BOOL("highlight.xhtml");
 }
 /* }}} */
 
-/* {{{ proto bool highlight_file(string file_name [, bool return] )
+/* {{{ proto bool highlight_file(string file_name [, bool return [, bool is_xhtml ]] )
    Syntax highlight a source file */
 PHP_FUNCTION(highlight_file)
 {
 	char *filename;
 	size_t filename_len, ret;
 	zend_syntax_highlighter_ini syntax_highlighter_ini;
-	zend_bool i = 0;
+	zend_bool i = 0, is_xhtml = 1;
 
-	if (zend_parse_parameters(ZEND_NUM_ARGS(), "p|b", &filename, &filename_len, &i) == FAILURE) {
+	if (zend_parse_parameters(ZEND_NUM_ARGS(), "p|bb", &filename, &filename_len, &i, &is_xhtml) == FAILURE) {
 		RETURN_FALSE;
 	}
 
@@ -5093,6 +5096,10 @@ PHP_FUNCTION(highlight_file)
 
 	php_get_highlight_struct(&syntax_highlighter_ini);
 
+	if (ZEND_NUM_ARGS() > 2) {
+		syntax_highlighter_ini.highlight_xhtml = is_xhtml;
+	}
+
 	ret = highlight_file(filename, &syntax_highlighter_ini);
 
 	if (ret == FAILURE) {
@@ -5147,17 +5154,17 @@ PHP_FUNCTION(php_strip_whitespace)
 }
 /* }}} */
 
-/* {{{ proto bool highlight_string(string string [, bool return] )
+/* {{{ proto bool highlight_string(string string [, bool return [, bool is_xhtml ]] )
    Syntax highlight a string or optionally return it */
 PHP_FUNCTION(highlight_string)
 {
 	zval *expr;
 	zend_syntax_highlighter_ini syntax_highlighter_ini;
 	char *hicompiled_string_description;
-	zend_bool i = 0;
+	zend_bool i = 0, is_xhtml = 1;
 	int old_error_reporting = EG(error_reporting);
 
-	if (zend_parse_parameters(ZEND_NUM_ARGS(), "z|b", &expr, &i) == FAILURE) {
+	if (zend_parse_parameters(ZEND_NUM_ARGS(), "z|bb", &expr, &i, &is_xhtml) == FAILURE) {
 		RETURN_FALSE;
 	}
 	convert_to_string_ex(expr);
@@ -5170,6 +5177,10 @@ PHP_FUNCTION(highlight_string)
 
 	php_get_highlight_struct(&syntax_highlighter_ini);
 
+	if (ZEND_NUM_ARGS() > 2) {
+		syntax_highlighter_ini.highlight_xhtml = is_xhtml;
+	}
+
 	hicompiled_string_description = zend_make_compiled_string_description("highlighted code");
 
 	if (highlight_string(expr, &syntax_highlighter_ini, hicompiled_string_description) == FAILURE) {
diff --git a/main/main.c b/main/main.c
index 276058c..1734cd9 100644
--- a/main/main.c
+++ b/main/main.c
@@ -527,6 +527,7 @@ PHP_INI_BEGIN()
 	PHP_INI_ENTRY_EX("highlight.html",			HL_HTML_COLOR,		PHP_INI_ALL,	NULL,			php_ini_color_displayer_cb)
 	PHP_INI_ENTRY_EX("highlight.keyword",		HL_KEYWORD_COLOR,	PHP_INI_ALL,	NULL,			php_ini_color_displayer_cb)
 	PHP_INI_ENTRY_EX("highlight.string",		HL_STRING_COLOR,	PHP_INI_ALL,	NULL,			php_ini_color_displayer_cb)
+	PHP_INI_ENTRY_EX("highlight.xhtml",			"1",				PHP_INI_ALL,	NULL,			php_ini_boolean_displayer_cb)
 
 	STD_PHP_INI_ENTRY_EX("display_errors",		"1",		PHP_INI_ALL,		OnUpdateDisplayErrors,	display_errors,			php_core_globals,	core_globals, display_errors_mode)
 	STD_PHP_INI_BOOLEAN("display_startup_errors",	"0",	PHP_INI_ALL,		OnUpdateBool,			display_startup_errors,	php_core_globals,	core_globals)
diff --git a/php.ini-development b/php.ini-development
index c619678..56e80f1 100644
--- a/php.ini-development
+++ b/php.ini-development
@@ -312,6 +312,10 @@ disable_classes =
 ;highlight.default = #0000BB
 ;highlight.html    = #000000
 
+; Whether or not to enable XHTML formatted highlighted strings, setting this to 
+; Off will make highlighted strings HTML4/5 compatible
+;highlight.xhtml = On
+
 ; If enabled, the request will be allowed to complete even if the user aborts
 ; the request. Consider enabling it if executing long requests, which may end up
 ; being interrupted by the user or a browser timing out. PHP's default behavior
diff --git a/php.ini-production b/php.ini-production
index 1310f6c..cc55eee 100644
--- a/php.ini-production
+++ b/php.ini-production
@@ -312,6 +312,10 @@ disable_classes =
 ;highlight.default = #0000BB
 ;highlight.html    = #000000
 
+; Whether or not to enable XHTML formatted highlighted strings, setting this to 
+; Off will make highlighted strings HTML4/5 compatible
+;highlight.xhtml = On
+
 ; If enabled, the request will be allowed to complete even if the user aborts
 ; the request. Consider enabling it if executing long requests, which may end up
 ; being interrupted by the user or a browser timing out. PHP's default behavior
 
PHP Copyright © 2001-2019 The PHP Group
All rights reserved.
Last updated: Sun Aug 25 20:01:27 2019 UTC