diff options
author | John Coggeshall <john@php.net> | 2004-01-06 18:24:17 +0000 |
---|---|---|
committer | John Coggeshall <john@php.net> | 2004-01-06 18:24:17 +0000 |
commit | d900d737638380e5d96c8bae9e297b5da868f346 (patch) | |
tree | 3484cde136a2e6d98119e3b01c710d6486d75ca9 | |
parent | 107a87ca8525faf65d73882ae923c669231fcdcc (diff) | |
download | php-git-d900d737638380e5d96c8bae9e297b5da868f346.tar.gz |
Added an optional array parameter to tidy_parse_file/string to
fix a bug. Apparently some libtidy config options must be set
prior to parsing in order to work properly.
-rw-r--r-- | ext/tidy/php_tidy.h | 2 | ||||
-rw-r--r-- | ext/tidy/tests/014.phpt | 17 | ||||
-rw-r--r-- | ext/tidy/tests/015.html | 1 | ||||
-rw-r--r-- | ext/tidy/tests/015.phpt | 16 | ||||
-rw-r--r-- | ext/tidy/tidy.c | 138 |
5 files changed, 124 insertions, 50 deletions
diff --git a/ext/tidy/php_tidy.h b/ext/tidy/php_tidy.h index f2750288b8..3dbb6a7d97 100644 --- a/ext/tidy/php_tidy.h +++ b/ext/tidy/php_tidy.h @@ -213,6 +213,8 @@ static void tidy_doc_update_properties(PHPTidyObj * TSRMLS_DC); static void tidy_add_default_properties(PHPTidyObj *, tidy_obj_type TSRMLS_DC); static void *php_tidy_get_opt_val(PHPTidyDoc *, TidyOption, TidyOptionType * TSRMLS_DC); static void php_tidy_create_node(INTERNAL_FUNCTION_PARAMETERS, tidy_base_nodetypes); +static int _php_tidy_set_tidy_opt(TidyDoc, char *, zval *); +static int _php_tidy_apply_config_array(TidyDoc doc, HashTable *ht_options); void _php_tidy_register_nodetypes(INIT_FUNC_ARGS); void _php_tidy_register_tags(INIT_FUNC_ARGS); diff --git a/ext/tidy/tests/014.phpt b/ext/tidy/tests/014.phpt new file mode 100644 index 0000000000..2b94f93b13 --- /dev/null +++ b/ext/tidy/tests/014.phpt @@ -0,0 +1,17 @@ +--TEST-- +Passing configuration options through tidy_parse_string(). +--SKIPIF-- +<?php if (!extension_loaded("tidy")) print "skip"; ?> +--POST-- +--GET-- +--INI-- +--FILE-- +<?php + $text = "<B>testing</I>"; + $tidy = tidy_parse_string($text, array('show-body-only'=>true)); + tidy_clean_repair($tidy); + echo tidy_get_output($tidy); + +?> +--EXPECT-- +<b>testing</b>
\ No newline at end of file diff --git a/ext/tidy/tests/015.html b/ext/tidy/tests/015.html new file mode 100644 index 0000000000..7dc0357779 --- /dev/null +++ b/ext/tidy/tests/015.html @@ -0,0 +1 @@ +<B>testing</I> diff --git a/ext/tidy/tests/015.phpt b/ext/tidy/tests/015.phpt new file mode 100644 index 0000000000..d553ca7aeb --- /dev/null +++ b/ext/tidy/tests/015.phpt @@ -0,0 +1,16 @@ +--TEST-- +Passing configuration options through tidy_parse_file(). +--SKIPIF-- +<?php if (!extension_loaded("tidy")) print "skip"; ?> +--POST-- +--GET-- +--INI-- +--FILE-- +<?php + $tidy = tidy_parse_file("ext/tidy/tests/015.html", array('show-body-only'=>true)); + tidy_clean_repair($tidy); + echo tidy_get_output($tidy); + +?> +--EXPECT-- +<b>testing</b>
\ No newline at end of file diff --git a/ext/tidy/tidy.c b/ext/tidy/tidy.c index b77d8dd8e8..7de548bd6f 100644 --- a/ext/tidy/tidy.c +++ b/ext/tidy/tidy.c @@ -32,8 +32,9 @@ #include "Zend/zend_API.h" #include "Zend/zend_hash.h" #include "safe_mode.h" -#include "zend_default_classes.h" -#include "zend_object_handlers.h" +#include "Zend/zend_default_classes.h" +#include "Zend/zend_object_handlers.h" +#include "Zend/zend_hash.h" ZEND_DECLARE_MODULE_GLOBALS(tidy) @@ -178,6 +179,52 @@ void php_tidy_panic(ctmbstr msg) zend_error(E_ERROR, "Could not allocate memory for tidy! (Reason: %s)", (char *)msg); } +static int _php_tidy_set_tidy_opt(TidyDoc doc, char *optname, zval *value) +{ + TidyOption opt; + + opt = tidyGetOptionByName(doc, optname); + + if (!opt) { + php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unknown Tidy Configuration Option '%s'", optname); + return FAILURE; + } + + if (tidyOptIsReadOnly(opt)) { + php_error_docref(NULL TSRMLS_CC, E_WARNING, "Attempted to set read-only option '%s'", optname); + return FAILURE; + } + + switch(tidyOptGetType(opt)) { + case TidyString: + convert_to_string_ex(&value); + if (tidyOptSetValue(doc, tidyOptGetId(opt), Z_STRVAL_P(value))) { + return SUCCESS; + } + break; + + case TidyInteger: + convert_to_long_ex(&value); + if (tidyOptSetInt(doc, tidyOptGetId(opt), Z_LVAL_P(value))) { + return SUCCESS; + } + break; + + case TidyBoolean: + convert_to_long_ex(&value); + if (tidyOptSetBool(doc, tidyOptGetId(opt), Z_LVAL_P(value))) { + return SUCCESS; + } + break; + + default: + php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to determine type of Tidy configuration constant to set"); + break; + } + + return FAILURE; +} + static void php_tidy_quick_repair(INTERNAL_FUNCTION_PARAMETERS, zend_bool is_file) { char *data=NULL, *cfg_file=NULL, *arg1; @@ -478,10 +525,6 @@ static void tidy_globals_ctor(zend_tidy_globals *g TSRMLS_DC) { } -static void tidy_globals_dtor(zend_tidy_globals *g TSRMLS_DC) -{ -} - static void tidy_add_default_properties(PHPTidyObj *obj, tidy_obj_type type TSRMLS_DC) { @@ -640,19 +683,41 @@ static void php_tidy_create_node(INTERNAL_FUNCTION_PARAMETERS, tidy_base_nodetyp tidy_add_default_properties(newobj, is_node TSRMLS_CC); } +static int _php_tidy_apply_config_array(TidyDoc doc, HashTable *ht_options) +{ + char *opt_name; + zval **opt_val; + ulong opt_indx; + + for (zend_hash_internal_pointer_reset(ht_options); + zend_hash_get_current_data(ht_options, (void **)&opt_val) == SUCCESS; + zend_hash_move_forward(ht_options)) { + + if(zend_hash_get_current_key(ht_options, &opt_name, &opt_indx, FALSE) == FAILURE) { + php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not retrieve key from array"); + } + + _php_tidy_set_tidy_opt(doc, opt_name, *opt_val); + + } + + return SUCCESS; +} + static void php_tidy_parse_file(INTERNAL_FUNCTION_PARAMETERS) { char *inputfile; int input_len; zend_bool use_include_path = 0; char *contents; - + zval *options = NULL; + zend_bool is_object = FALSE; zval *object = getThis(); PHPTidyObj *obj; - if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|b", &inputfile, &input_len, &use_include_path) == FAILURE) { + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|ab", &inputfile, &input_len, &options, &use_include_path) == FAILURE) { RETURN_FALSE; } @@ -673,6 +738,10 @@ static void php_tidy_parse_file(INTERNAL_FUNCTION_PARAMETERS) RETURN_FALSE; } + if(options) { + _php_tidy_apply_config_array(obj->ptdoc->doc, HASH_OF(options)); + } + if (tidyParseString(obj->ptdoc->doc, contents) < 0) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s", obj->ptdoc->errbuf->bp); RETVAL_FALSE; @@ -803,13 +872,14 @@ PHP_FUNCTION(tidy_parse_string) { char *input; int input_len; - + zval *options = NULL; + zend_bool is_object = FALSE; zval *object = getThis(); PHPTidyObj *obj; - if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &input, &input_len) == FAILURE) { + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|a", &input, &input_len, &options) == FAILURE) { RETURN_FALSE; } @@ -826,6 +896,10 @@ PHP_FUNCTION(tidy_parse_string) obj = (PHPTidyObj *) zend_object_store_get_object(return_value TSRMLS_CC); } + if(options) { + _php_tidy_apply_config_array(obj->ptdoc->doc, HASH_OF(options)); + } + if (tidyParseString(obj->ptdoc->doc, input) < 0) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s", obj->ptdoc->errbuf->bp); return; @@ -866,7 +940,7 @@ PHP_FUNCTION(tidy_get_output) } /* }}} */ -/* {{{ proto boolean tidy_parse_file(string file [, bool use_include_path]) +/* {{{ proto boolean tidy_parse_file(string file [, array config_options [, bool use_include_path]]) Parse markup in file or URI */ PHP_FUNCTION(tidy_parse_file) { @@ -1237,7 +1311,6 @@ PHP_FUNCTION(tidy_setopt) zval *value; char *optname; int optname_len; - TidyOption opt; if (object) { if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sz", &optname, &optname_len, &value) == FAILURE) { @@ -1251,45 +1324,10 @@ PHP_FUNCTION(tidy_setopt) obj = (PHPTidyObj *) zend_object_store_get_object(object TSRMLS_CC); - opt = tidyGetOptionByName(obj->ptdoc->doc, optname); - - if (!opt) { - php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unknown Tidy Configuration Option '%s'", optname); - RETURN_FALSE; - } - - if (tidyOptIsReadOnly(opt)) { - php_error_docref(NULL TSRMLS_CC, E_WARNING, "Attempted to set read-only option '%s'", optname); - RETURN_FALSE; - } - - switch(tidyOptGetType(opt)) { - case TidyString: - convert_to_string_ex(&value); - if (tidyOptSetValue(obj->ptdoc->doc, tidyOptGetId(opt), Z_STRVAL_P(value))) { - RETURN_TRUE; - } - break; - - case TidyInteger: - convert_to_long_ex(&value); - if (tidyOptSetInt(obj->ptdoc->doc, tidyOptGetId(opt), Z_LVAL_P(value))) { - RETURN_TRUE; - } - break; - - case TidyBoolean: - convert_to_long_ex(&value); - if (tidyOptSetBool(obj->ptdoc->doc, tidyOptGetId(opt), Z_LVAL_P(value))) { - RETURN_TRUE; - } - break; - - default: - php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to determine type of Tidy configuration constant to set"); - break; + if(_php_tidy_set_tidy_opt(obj->ptdoc->doc, optname, value) == SUCCESS) { + RETURN_TRUE; } - + RETURN_FALSE; } /* }}} */ |