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

govukAttributes macro doesn't output values that use Nunjucks' safe filter #4937

Closed
querkmachine opened this issue Apr 18, 2024 · 0 comments · Fixed by #4938
Closed

govukAttributes macro doesn't output values that use Nunjucks' safe filter #4937

querkmachine opened this issue Apr 18, 2024 · 0 comments · Fixed by #4938
Assignees
Labels
🐛 bug Something isn't working the way it should (including incorrect wording in documentation) nunjucks
Milestone

Comments

@querkmachine
Copy link
Member

querkmachine commented Apr 18, 2024

Raised and investigated via support.

Description of the issue

The govukAttributes macro, when provided a map of key-value pairs, will fail to output values that have previously passed through the Nunjucks safe filter.

This appears to be because the output of the safe filter is (if not null) actually an object, which the govukAttributes macro is interpreting to be one of our first-party attribute configuration objects.

Steps to reproduce the issue

Use this code in a project using GOV.UK Frontend's Nunjucks macros. The use of Back Link component here is illustrative, this issue is present in all components that use govukAttributes.

{{ govukBackLink({
  attributes: {
    'data-text': 'Testing',
    'data-html': '<i>Testing</i>',
    'data-number': 123.45,
    'data-boolean': true,
    'data-safe-text': 'Testing' | safe,
    'data-safe-html': '<i>Testing</i>' | safe,
    'data-safe-number': 123.45 | safe,
    'data-safe-boolean': true | safe
  }
}) }}

Each of the safe filtered values as they're exposed by the dump filter. The safe filter appears to convert anything passed to it into a string.

// data-safe-text
{ "val": "Testing", "length": 7 }

// data-safe-html
{ "val": "<i>Testing</i>", "length": 14 }

// data-safe-number
{ "val": "123.45", "length": 6 }

// data-safe-boolean
{ "val": "true", "length": 4 }

Actual vs expected behaviour

(HTML output has been reformatted for easier reading.)

Actual output

<a 
  href="#"
  class="govuk-back-link"
  data-text="Testing"
  data-html="&lt;i&gt;Testing&lt;/i&gt;"
  data-number="123.45"
  data-boolean="true"
  data-safe-text=""
  data-safe-html=""
  data-safe-number=""
  data-safe-boolean="">
  Back
</a>

Expected output

<a 
  href="#"
  class="govuk-back-link"
  data-text="Testing"
  data-html="&lt;i&gt;Testing&lt;/i&gt;"
  data-number="123.45"
  data-boolean="true"
  data-safe-text="Testing"
  data-safe-html="<i>Testing</i>" <!-- maybe undesirable, but would be logically consistent -->
  data-safe-number="123.45"
  data-safe-boolean="true">
  Back
</a>

Environment (where applicable)

  • GOV.UK Frontend Version: 5.2.0, 5.3.0
@querkmachine querkmachine added 🐛 bug Something isn't working the way it should (including incorrect wording in documentation) nunjucks labels Apr 18, 2024
@36degrees 36degrees moved this to Backlog 🏃🏼‍♀️ in GOV.UK Design System cycle board Apr 18, 2024
@querkmachine querkmachine self-assigned this Apr 18, 2024
@querkmachine querkmachine moved this from Backlog 🏃🏼‍♀️ to In progress 📝 in GOV.UK Design System cycle board Apr 18, 2024
@querkmachine querkmachine moved this from In progress 📝 to Needs review 🔍 in GOV.UK Design System cycle board Apr 18, 2024
@owenatgov owenatgov added this to the 5.3.1 milestone Apr 19, 2024
@querkmachine querkmachine moved this from Needs review 🔍 to Done 🏁 in GOV.UK Design System cycle board Apr 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🐛 bug Something isn't working the way it should (including incorrect wording in documentation) nunjucks
Projects
Development

Successfully merging a pull request may close this issue.

2 participants