highlight icon indicating copy to clipboard operation
highlight copied to clipboard

HTML attributes not highlighted if unquoted

Open edent opened this issue 8 months ago • 4 comments

Tempest version

2.11.4

PHP version

8.4

Operating system

Linux

Description

Consider this HTML:

<img class="a" alt src=example.png>

This library only highlights the attribute "class".

&lt;<span style="color: #0000ff;">img</span> <span style="color: #795E26;">class</span>=&quot;a&quot; alt src=example.png&gt;

HTML only requires attribute values to be quoted if they contain a space, equals sign, or a few other characters. See https://html.spec.whatwg.org/multipage/syntax.html#attributes-2

So, in this case the empty alt and the unquoted src should both be highlighted.

Steps to reproduce

echo $highlighter->parse( '<img class="a" alt src=example.png>', "html" );

edent avatar Apr 24 '25 08:04 edent

Thanks! Are you up for submitting a PR fix? Otherwise I'll try to look into it as soon as possible :)

brendt avatar Apr 28 '25 08:04 brendt

Changing this:

https://github.com/tempestphp/highlight/blob/5a239a92ad6bd3e506ca86a0de3e99ac9dbcb0dd/src/Languages/Xml/Patterns/XmlAttributePattern.php#L21

To make the quote optional:

return '(?<match>[\w\-]+)="?';

Does work for unquoted but not empty values, but may be invalid for XML.

However, as this isn't a validator, it's probably fine.

If you're happy with that, I'll send a PR and will think on how to tackle the empty value case.

edent avatar Apr 29 '25 13:04 edent

Yes, that should work!

brendt avatar Apr 30 '25 06:04 brendt

Did some testing, unfortunately it's not as simple as we thought: simply making " optional makes it so that our attribute-name match part can match too many things (anything with \w- character. We'd need to fine-tune to regex to make sure it's only matching words that actually are attributes (so within <> tags)

brendt avatar May 08 '25 06:05 brendt