Skip to content

tidynode.props.attribute is missing "Boolean Attributes" and empty attributes. #12980

Closed
@whitehorsesupport

Description

@whitehorsesupport

Description

The following code:

<?php
$html = '<!DOCTYPE html><html lang="en"><head><title>bug</title></head><body><select><option selected value="en">English</option></select><img alt="" src="someurl"></body></html>';

$tidy = new tidy();
$tidy->ParseString($html);
//echo "\n".tidy_get_output($tidy)."\n\n"; Uncomment this line to verify that tidy is seeing the Boolean Attributes

function walk_nodes($node) {
	if(!empty($node->attribute)){
		echo '<'.$node->name.'>'."\n";
		foreach($node->attribute as $attributeKey=>$attributeValue) {
			echo '    ATTR '.var_export($attributeKey,true).'=>'.var_export($attributeValue,true)."\n";
		}
	}
	if($node->hasChildren()) {
		foreach($node->child as $child) {
			walk_nodes($child);
		}
	}
}
walk_nodes($tidy->root());

Resulted in this output:

<html>
    ATTR 'lang'=>'en'
<option>
    ATTR 'value'=>'en'
<img>
    ATTR 'src'=>'someurl'

But I expected this output instead:

<html>
    ATTR 'lang'=>'en'
<option>
    ATTR 'selected'=>true
    ATTR 'value'=>'en'
<img>
    ATTR 'alt'=>''
    ATTR 'src'=>'someurl'

Note: This ticket was originally submitted regarding boolean attributes but the example above has been updated to reflect empty attributes showing the same behavior.

Documentation for tidynode.props.attribute, There isn't any mention about this and I believe it is an unintended bug.
https://www.php.net/manual/en/class.tidynode.php#tidynode.props.attribute

The value of a "Boolean Attribute" is debatable. It could be NULL but I think it would be better if it was true.(If NULL was desired the fix below would need to be tweaked.)

After reviewing the PHP source code I believe the issue is probably located here
https://github.com/php/php-src/blob/master/ext/tidy/tidy.c
on line 665

I would suggest that the fix look something like this starting on line 665 /ext/tidy/tidy.c

=			if (name && val) {
=				add_assoc_string(&attribute, name, val);
-			}
+			} else if (name) {
+				add_assoc_string(&attribute, name, true);//I am not sure how a type "Boolean" true is set here so it may need to be tweaked.
+			}

Feel free to use the above code in making a patch, I the author disclaim and release all copyright to it.

PHP Version

PHP 8.2.10

Operating System

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions