Ability to add other attributes to the script tag#8540
Ability to add other attributes to the script tag#8540andrepereiradasilva wants to merge 23 commits intojoomla:stagingfrom
Conversation
|
👍
|
|
👍 on making the change to Also can we typehint the new parameter to ensure we have an array please? |
|
Ok. I will check your comments when i have time. Additional infoI'm also working in a more global solution to the scripts problems for the future. (check staging...andrepereiradasilva:scripts-dependencies). At least i have identified some things that the script manager needs:
@DGT41 what do you think? |
Done
And how we do this? Can you point to what needs to be changed/added?
Hum. This is outside se scope of this PR. See my previous comment.
Done |
There was a problem hiding this comment.
Couple notes:
First: this code will fail when $attrib_value is Object eg stdClass, so I would suggest test for is_scalar($attrib_value).
Second: need to be sure that both $attribute and $attribute_value is escaped.
There was a problem hiding this comment.
still have doubt here,
did you tried how it will work if $attrib_value contain '? 😉
more safe would be ="' . htmlspecialchars(json_encode($attrib_value)) . '"'
There was a problem hiding this comment.
addslashes() will do that or am I missing something here?
|
looks fine now, from my point of view ... just need time to test 😄 |
|
This PR has received new commits. CC: @anibalsanchez, @DGT41 This comment was created with the J!Tracker Application at issues.joomla.org/joomla-cms/8540. |
|
This PR has received new commits. CC: @anibalsanchez, @DGT41 This comment was created with the J!Tracker Application at issues.joomla.org/joomla-cms/8540. |
|
solved conflicts after JDocumentRenderer refactoring. |
|
I have tested this item ✅ successfully on 4697be7 This comment was created with the J!Tracker Application at issues.joomla.org/joomla-cms/8540. |
|
@andrepereiradasilva can't retest it right now, will do it in the week end. Just one thing that come across my mind: |
|
That's something JDocument should be handling (and IIRC isn't). Some of the JHtml helpers have checks to ensure they're only loaded once, but that's the extent of that type of handling. |
|
IIRC, and looking at the code it adds the same attribs to all the scripts. Update: i misunderstood what you were saying. |
|
@andrepereiradasilva let me explain what I am proposing here: |
|
That's a totally separate patch (and borderline B/C concern) unrelated to this. Right now JDocument's add methods for script/stylesheet will overwrite in full an item when you add something with the same URL. This patch will only expose that bug further. |
|
@mbabker if that's the case this functionality is too restricted and in some extend inefficient. I can imagine attaching different data-attr to bootstrap.js from different views (all of them rendered the same page). There must be a way around this... |
|
@andrepereiradasilva Any update on this PR? This comment was created with the J!Tracker Application at issues.joomla.org/joomla-cms/8540. |
|
@roland-d the problem @DGT41 is talking about is because of the not so good JDocument storing and rendering the scripts. See, for instance, my comment in #10223 (comment)
|
|
I have tested this item ✅ successfully on 4697be7 This comment was created with the J!Tracker Application at issues.joomla.org/joomla-cms/8540. |
|
@roland-d my comment is not directly related to this PR and the solution should be done in another PR. I suggest to merge this as it will help eliminate a lot of the inline scripts, it's a huge improvement! |
| $this->_scripts[$url]['mime'] = $type; | ||
| $this->_scripts[$url]['defer'] = $defer; | ||
| $this->_scripts[$url]['async'] = $async; | ||
| $this->_scripts[$url]['attribs'] = $attribs; |
There was a problem hiding this comment.
@andrepereiradasilva how about:
if (!empty($attribs))
{
if (!isset($this->_scripts[$url]) || (isset($this->_scripts[$url]) && empty($this->_scripts[$url]['attribs'])))
{
$this->_scripts[$url]['attribs'] = $attribs;
}
if (isset($this->_scripts[$url]) && !empty($this->_scripts[$url]['attribs']))
{
$this->_scripts[$url]['attribs'] = array_merge($this->_scripts[$url]['attribs'], $attribs);
}
}There was a problem hiding this comment.
@mbabker since $attribs is newly introduced there is no B/C for what I'm proposing above, right?
There was a problem hiding this comment.
actually that a good idea (i prefer with array_replace)
also i think i shoulnd' type hint this as an array.
i will try to check this better this weekend.
There was a problem hiding this comment.
For the most flexibility attribs should be an array. That way it's easy to add (and remove since the whole thing's stored in a public var and we have the onBeforeCompileHead event) attributes. Whereas when it's a string like some places support its harder to deal with that stuff.
|
i think i have a better alternative for this that i think it's more flexible. |
|
please remove the RTC and label from this. |
|
Removed the milestone |
Description
This PR adds the ability to add more attributes to the script tag using
JDocumentaddScriptoraddScriptVersionmethods.How to test
Add a external js script like, some examples:
Load the page and check the html code. The script tag should be rendered with the attribute(s) added.
B/C
None that i know of.