Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[5.x]: HTML IDs should be allowed to begin with a number #15066

Closed
MoritzLost opened this issue May 24, 2024 · 4 comments
Closed

[5.x]: HTML IDs should be allowed to begin with a number #15066

MoritzLost opened this issue May 24, 2024 · 4 comments
Assignees
Labels

Comments

@MoritzLost
Copy link
Contributor

MoritzLost commented May 24, 2024

What happened?

Description

The craft\helpers\Html::id() function was recently updated to strip out numbers at the beginning as a follow-up to #13943

The justification was that HTML IDs aren't allowed to begin with numbers. However, this was only the case in HTML 4. In HTML 5, there is no such restriction.

We're using this function to generate unique IDs for sections in our page builder matrix field. Those look like 123-section-name, where 123 is the entries' (previously matrix block's) ID and section-name is the headline. This string is truncated to a specific character length, so to ensure the entry ID is always part of the section ID, it needs to be at the start.

Steps to reproduce

  1. Call craft\helpers\Html::id('123-custom-string')

Expected behavior

  1. The function should not strip out numbers at the beginning, as there is no reason for doing so to be standard-compliant.

Actual behavior

The function unexpectedly strips out numbers at the beginning.

Craft CMS version

5.1.6

PHP version

No response

Operating system and version

No response

Database type and version

No response

Image driver and version

No response

Installed plugins and versions

@MoritzLost MoritzLost added the bug label May 24, 2024
@brandonkelly brandonkelly self-assigned this May 28, 2024
@brandonkelly
Copy link
Member

Doh, you’re right. Not sure how I missed that I was looking at the HTML4 spec.

Fixed for the next release.

@MoritzLost
Copy link
Contributor Author

@brandonkelly Thanks Brandon!

@brandonkelly
Copy link
Member

Craft 5.1.8 is out with that fix.

@AugustMiller
Copy link
Contributor

Just a heads up that while HTML5 allows numerals at the beginning of id attributes, CSS identifiers do not:

{% set id = '123-custom-string' | id %}

<div id="{{ id }}">Hello, world!</div>

{% css %}
  /* This block won't be applied to our div: */
  #{{ id }} {
    color: red;
  }
{% endcss %}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants