Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
139 changes: 139 additions & 0 deletions ext/tidy/tests/gh20374.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
--TEST--
GH-20374 (PHP with tidy and custom-tags)
--EXTENSIONS--
tidy
--CREDITS--
franck-paul
--FILE--
<?php

class MyStringable {
public function __construct(private $ret) {}

public function __toString(): string {
return $this->ret;
}
}

class MyThrowingStringable {
public function __toString(): string {
throw new Error('no');
}
}

$values = [
'string blocklevel' => 'blocklevel',
'int' => 1,
'numeric string int 1' => '1',
'numeric string double 1.0' => '1.0',
Copy link
Member

Choose a reason for hiding this comment

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

Can I be annoying and ask you to have test cases for INF and NAN? :)

Actually curious what the behaviour would be.

Copy link
Member Author

Choose a reason for hiding this comment

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

Pushed. Apparently: nothing meaningful as they get turned into (int)0.

'false' => false,
'true' => true,
'object with numeric string int 0' => new MyStringable('0'),
'object with string blocklevel' => new MyStringable('blocklevel'),
'object with string empty' => new MyStringable('empty'),
'object with exception' => new MyThrowingStringable,
];

foreach ($values as $key => $value) {
echo "--- $key ---\n";
$str = '<custom-html-element>test</custom-html-element>';

$config = [
'custom-tags' => $value,
];

$tidy = new tidy();
try {
$tidy->parseString($str, $config, 'utf8');
echo $tidy->value, "\n";
} catch (Throwable $e) {
echo $e::class, ": ", $e->getMessage(), "\n";
}
}

?>
--EXPECT--
--- string blocklevel ---
<html>
<head>
<title></title>
</head>
<body>
<custom-html-element>test</custom-html-element>
</body>
</html>
--- int ---
<html>
<head>
<title></title>
</head>
<body>
<custom-html-element>test</custom-html-element>
</body>
</html>
--- numeric string int 1 ---
<html>
<head>
<title></title>
</head>
<body>
<custom-html-element>test</custom-html-element>
</body>
</html>
--- numeric string double 1.0 ---
<html>
<head>
<title></title>
</head>
<body>
<custom-html-element>test</custom-html-element>
</body>
</html>
--- false ---
<html>
<head>
<title></title>
</head>
<body>
test
</body>
</html>
--- true ---
<html>
<head>
<title></title>
</head>
<body>
<custom-html-element>test</custom-html-element>
</body>
</html>
--- object with numeric string int 0 ---
<html>
<head>
<title></title>
</head>
<body>
test
</body>
</html>
--- object with string blocklevel ---
<html>
<head>
<title></title>
</head>
<body>
<custom-html-element>test</custom-html-element>
</body>
</html>
--- object with string empty ---
<custom-html-element>
<html>
<head>
<title></title>
</head>
<body>
test
</body>
</html>
--- object with exception ---
Error: no
35 changes: 31 additions & 4 deletions ext/tidy/tidy.c
Original file line number Diff line number Diff line change
Expand Up @@ -251,10 +251,37 @@ static int _php_tidy_set_tidy_opt(TidyDoc doc, char *optname, zval *value)
zend_tmp_string_release(tmp_str);
break;

case TidyInteger:
lval = zval_get_long(value);
if (tidyOptSetInt(doc, tidyOptGetId(opt), lval)) {
return SUCCESS;
case TidyInteger: /* integer or enum */
ZVAL_DEREF(value);
/* Enum will correspond to a non-numeric string or object */
if (Z_TYPE_P(value) == IS_STRING || Z_TYPE_P(value) == IS_OBJECT) {
double dval;
str = zval_try_get_tmp_string(value, &tmp_str);
if (UNEXPECTED(!str)) {
return FAILURE;
}
uint8_t type = is_numeric_string(ZSTR_VAL(str), ZSTR_LEN(str), &lval, &dval, true);
if (type == IS_DOUBLE) {
lval = zend_dval_to_lval_cap(dval);
type = IS_LONG;
}
if (type == IS_LONG) {
if (tidyOptSetInt(doc, tidyOptGetId(opt), lval)) {
zend_tmp_string_release(tmp_str);
return SUCCESS;
}
} else {
if (tidyOptSetValue(doc, tidyOptGetId(opt), ZSTR_VAL(str))) {
zend_tmp_string_release(tmp_str);
return SUCCESS;
}
}
zend_tmp_string_release(tmp_str);
} else {
lval = zval_get_long(value);
if (tidyOptSetInt(doc, tidyOptGetId(opt), lval)) {
return SUCCESS;
}
}
break;

Expand Down
Loading