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

Add support for attributes in backdrop_add_css(). #4981

Closed
herbdool opened this issue Mar 4, 2021 · 19 comments · Fixed by backdrop/backdrop#3555
Closed

Add support for attributes in backdrop_add_css(). #4981

herbdool opened this issue Mar 4, 2021 · 19 comments · Fixed by backdrop/backdrop#3555

Comments

@herbdool
Copy link

herbdool commented Mar 4, 2021

Description of the need

We should allow for attributes like integrity, and crossorigin to be passed to backdrop_add_js and backdrop_add_css. [Update: this was half done, it turns out.]

There's this long standing Drupal issue https://www.drupal.org/project/drupal/issues/1664602 where the latest patches seem to be working well.

I'm feeling like this issue already exists here but I just can't find it.

@indigoxela
Copy link
Member

Hm... Looking at the api page, I see:

attributes: An associative array of attributes for the <script> tag. This may be used to add 'async' or custom attributes. Note that setting any attributes will disable preprocessing as though the 'preprocess' option was set to FALSE.

Are you sure, that attributes aren't possible?

BTW: I'd reduce that to backdrop_add_js() - or is there any need for additional attributes on <style> tags?

@herbdool
Copy link
Author

herbdool commented Mar 5, 2021

I was looking first at backdrop_add_css https://docs.backdropcms.org/api/backdrop/core%21includes%21common.inc/function/backdrop_add_css/1 because I need to be able to add "integrity" and "crossorigin" attributes (maybe others).

So maybe we had changed backdrop_add_js already. That rings a bell. I suppose I can update this issue.

@herbdool herbdool changed the title Allow attributes to be passed to backdrop_add_[css|js] Allow attributes to be passed to backdrop_add_css Mar 5, 2021
@herbdool
Copy link
Author

herbdool commented Mar 5, 2021

Aha! Here it is for backdrop_add_css #2877. And I helped with the PR originally made by @quicksketch on the d.org issue. But later on in the issue Pol adds support for CSS as well. I think that threw me off.

@herbdool
Copy link
Author

herbdool commented Mar 5, 2021

@indigoxela I've got a PR here if you'd like to look. The test could be more robust. I mentioned it could be more like JavaScriptTestCase::testAttributes.

I've tested it manually with an external css file and it works well.

@quicksketch
Copy link
Member

I posted a question to the PR about what $group['attributes'] does: https://github.com/backdrop/backdrop/pull/3555/files#r588823603

@quicksketch quicksketch modified the milestone: 1.19.0 Mar 6, 2021
@indigoxela
Copy link
Member

@herbdool To be honest, I've never had a use case for additional attributes on css files. Can you give me some instructions, how to test that? Which attributes are valid (and useful) for <link> tags?

@herbdool
Copy link
Author

herbdool commented Mar 6, 2021

@indigoxela you could try to build a link like on this page https://forkaweso.me/Fork-Awesome/get-started

<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/fork-awesome@1.1.7/css/fork-awesome.min.css" integrity="sha256-gsmEoJAws/Kd3CjuOQzLie5Q3yshhvmo7YNtBG7aaEY=" crossorigin="anonymous">

That's how I got drawn into this issue.

@indigoxela
Copy link
Member

indigoxela commented Mar 7, 2021

@herbdool many thanks, now I got it. 👍 Subresource integrity.

This is how I tested:
In a theme I implemented hook_css_alter() and added the fork-awesome.min.css:

  $css_external = 'https://cdn.jsdelivr.net/npm/fork-awesome@1.1.7/css/fork-awesome.min.css';
  $css[$css_external] = array(
    'every_page' => TRUE,
    'type' => 'file',
    'media' => 'all',
    'preprocess' => FALSE,
    'data' => $css_external,
    'browsers' => array(),
    'attributes' => array(
      'integrity' => 'sha256-gsmEoJAws/Kd3CjuOQzLie5Q3yshhvmo7YNtBG7aaEY=',
      'crossorigin' => 'anonymous',
    ),
  );

The file gets added with the right attributes:

<link integrity="sha256-gsmEoJAws/Kd3CjuOQzLie5Q3yshhvmo7YNtBG7aaEY=" crossorigin="anonymous" rel="stylesheet" href="https://cdn.jsdelivr.net/npm/fork-awesome@1.1.7/css/fork-awesome.min.css?qpldsi" media="all" />

If the hash in the integrity attribute does not match, the browser nags into the console and refuses to load the file.
browser-hash-not-match

If the hash matches, the browser loads the file and the fa-xx classes work as expected.

PR works for me.

@herbdool
Copy link
Author

herbdool commented Mar 7, 2021

Thanks for testing @indigoxela. I'm going to try expand the tests to account for grouping. See if that works.

@quicksketch
Copy link
Member

@herbdool I don't see a big value in the support for adding attributes to an entire group. If the same attribute is needed on an entire group, an implementation of hook_css_alter() could loop over every file in a group and add the attribute to each file. Perhaps the group attributes can be removed and then the current test coverage would be acceptable?

@herbdool
Copy link
Author

herbdool commented Mar 8, 2021

@quicksketch I've updated the PR.

@herbdool
Copy link
Author

herbdool commented Mar 9, 2021

@quicksketch I've updated the test. It now matches the test for attributes on JS - tests a custom attribute on internal and external css.

@quicksketch
Copy link
Member

Looks great @herbdool. Marking this RTBC. I added a @since 1.19.0 Function added. line to the docblock for backdrop_css_defaults(). Letting tests run just to be safe and then this is mergeable.

@quicksketch
Copy link
Member

Merged backdrop/backdrop#3555 into 1.x for 1.19.0.

@quicksketch
Copy link
Member

Reopening until this gets a change record. Nice work @herbdool and thanks @indigoxela for testing!

@jenlampton
Copy link
Member

Change record created (but needs review, and likely needs work)
https://docs.backdropcms.org/change-records/backdrop_add_css-can-now-accept-attributes

@herbdool
Copy link
Author

@jenlampton thanks for the change record. It needs a tweak in the after example. There needs to be an attributes array for the backdrop_add_css() example whereas currently the attributes are mixed in with other elements.

$css_external = https://use.fontawesome.com/releases/v5.2.0/css/all.css';
backdrop_add_css($css_external, array(
      'attributes' => array(
        'integrity' => 'foo',
        'crossorigin' => 'anonymous',
      ),
      'type' => 'external',
    ));

@jenlampton
Copy link
Member

@jenlampton jenlampton changed the title Allow attributes to be passed to backdrop_add_css Add support for attributes in backdrop_add_css(). May 16, 2021
@drupol
Copy link

drupol commented May 16, 2021

Nice to see this in!

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

Successfully merging a pull request may close this issue.

5 participants