HTMLHint icon indicating copy to clipboard operation
HTMLHint copied to clipboard

feat: added new rule “attr-space-between”

Open actengage opened this issue 1 year ago • 4 comments

Consider the following the HTML:

<div id="bar"class="foo"></div>

The following error is what I get in the current version of HTMLHint.

Special characters must be escaped : [ &lt; ].
Special characters must be escaped : [ &gt; ].
Tag must be paired, no start tag: [ &lt;/div&gt; ]

The attr-space-between rule ensures HTML tags with attributes must have spaces between them. Without this change, tags with attributes without the required space results in the parser interpreting the tag as text. The new error would be:

Attribute "class" must be separated with a space

Proposed changes:

Change this Regex in htmlparser.ts:

const regTag =
      // eslint-disable-next-line no-control-regex
      /<(?:\/([^\s>]+)\s*|!--([\s\S]*?)--|!([^>]*?)|([\w\-:]+)((?:\s+[^\s"'>\/=\x00-\x0F\x7F\x80-\x9F]+(?:\s*=\s*(?:"[^"]*"|'[^']*'|[^\s"'>]*))?)*?)\s*(\/?))>/g

To the following:

const regTag =
      // eslint-disable-next-line no-control-regex
      /<(?:\/([^\s>]+)\s*|!--([\s\S]*?)--|!([^>]*?)|([\w\-:]+)((?:\s*[^\s"'>\/=\x00-\x0F\x7F\x80-\x9F]+(?:\s*=\s*(?:"[^"]*"|'[^']*'|[^\s"'>]*))?)*?)\s*(\/?))>/g

I also added the attr-space-between rule. The name of this rule could be subject to change, but this was the best I came up with. All tests pass with these changes, and docs were updated accordingly. Let me know what you think, and if I should make any additional changes.

actengage avatar Aug 12 '24 18:08 actengage

Thanks for the PR! It looks good to me at a glance and could be an helpful addition to HTMLHint.

We need @thedaviddias to review it.

coliff avatar Aug 13 '24 07:08 coliff

I didn't see this reply before. Thanks for looking into this. I'd love to get this approved in a new release so I don't have to use my fork in production.

actengage avatar Sep 05 '24 17:09 actengage

I didn't see this reply before. Thanks for looking into this. I'd love to get this approved in a new release so I don't have to use my fork in production.

No problem. I'd like to see this added too but we need @thedaviddias to review it as he is the project owner but he hasn't been active here for a couple of years.

coliff avatar Sep 06 '24 10:09 coliff

There are conflicts in the branch now so moving this to Draft status.

coliff avatar May 08 '25 02:05 coliff