Skip to content

Ability to add other attributes to the script tag#8540

Closed
andrepereiradasilva wants to merge 23 commits intojoomla:stagingfrom
andrepereiradasilva:script-tag-attribs
Closed

Ability to add other attributes to the script tag#8540
andrepereiradasilva wants to merge 23 commits intojoomla:stagingfrom
andrepereiradasilva:script-tag-attribs

Conversation

@andrepereiradasilva
Copy link
Copy Markdown
Contributor

@andrepereiradasilva andrepereiradasilva commented Nov 25, 2015

Description

This PR adds the ability to add more attributes to the script tag using JDocument addScript or addScriptVersion methods.

How to test

Add a external js script like, some examples:

JFactory::getDocument()->addScript('/path/to/external/jsfile.js', '', false, false, array('id' => 'scriptid'));

JFactory::getDocument()->addScript('/path/to/external/jsfile.js', '', false, false, array('id' => 'scriptid', 'data-test' => 1));

JFactory::getDocument()->addScriptVersion('/path/to/external/jsfile.js', 3, '', false, false, array('id' => 'scriptid'));

JFactory::getDocument()->addScript('/path/to/external/jsfile.js', '', false, false, array('data-attrib' => 5));

JFactory::getDocument()->addScript('/path/to/external/jsfile.js', 'text/javascript', true, false, array('data-attrib' => array('item1' => 3, 'item2' => 'value2')));

JHtml::_('script', 'system/jsfile.js', false, true, false, true, true, array('data-attrib' => array('item1' => 3, 'item2' => 'value2')));

JHtml::_('script', 'system/jsfile.js', false, true, false, true, true, array('defer' => 'defer', 'data-attrib' => array('item1' => 3, 'item2' => 'value2')));

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.

@dgrammatiko
Copy link
Copy Markdown
Contributor

👍
Couple of things here:

  • we need to make the relevant changes into JHtml::script();
  • Unit testing is necessary for both functions
  • since you already did this can you also make a check for a key-value 'toBottom' => true so we can introduce a way to append scripts to the end of the page?

@wilsonge
Copy link
Copy Markdown
Contributor

👍 on making the change to JHtml::script as well.

Also can we typehint the new parameter to ensure we have an array please?

@andrepereiradasilva
Copy link
Copy Markdown
Contributor Author

Ok. I will check your comments when i have time.


Additional info

I'm also working in a more global solution to the scripts problems for the future. (check staging...andrepereiradasilva:scripts-dependencies).
Is a PoC and is not related to this PR, just to inform.

At least i have identified some things that the script manager needs:

  • Ability to choose the js dependencies of a external/inline script and load all those depencies if not loaded yet
  • Script should be executed in the order they are called (not external, then inline)
  • One method to rule them all and add scripts and is a simple way
  • Greater flexibility (add the attributes you want)
  • Tottaly B/C (maintaing the current methods working)

@DGT41 what do you think?

@andrepereiradasilva
Copy link
Copy Markdown
Contributor Author

@DGT41

we need to make the relevant changes into JHtml::script();

Done

Unit testing is necessary for both functions

And how we do this? Can you point to what needs to be changed/added?
Do you mean in this line?
https://github.com/joomla/joomla-cms/blob/staging/tests/unit/suites/libraries/joomla/document/html/JDocumentHTMLTest.php#L50

since you already did this can you also make a check for a key-value 'toBottom' => true so we can introduce a way to append scripts to the end of the page?

Hum. This is outside se scope of this PR. See my previous comment.

@wilsonge

Also can we typehint the new parameter to ensure we have an array please?

Done

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

still have doubt here,
did you tried how it will work if $attrib_value contain '? 😉
more safe would be ="' . htmlspecialchars(json_encode($attrib_value)) . '"'

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

addslashes() will do that or am I missing something here?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@DGT41 htmlspecialchars more safe here 😉

@andrepereiradasilva
Copy link
Copy Markdown
Contributor Author

@Fedik, @DGT41 thanks for the feedback.
Can you check if everything is ok now?

@Fedik
Copy link
Copy Markdown
Member

Fedik commented Nov 27, 2015

looks fine now, from my point of view ... just need time to test 😄

@joomla-cms-bot
Copy link
Copy Markdown

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.

@joomla-cms-bot
Copy link
Copy Markdown

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.

@andrepereiradasilva
Copy link
Copy Markdown
Contributor Author

solved conflicts after JDocumentRenderer refactoring.

@anibalsanchez
Copy link
Copy Markdown
Contributor

I have tested this item ✅ successfully on 4697be7

It works OK


This comment was created with the J!Tracker Application at issues.joomla.org/joomla-cms/8540.

@dgrammatiko
Copy link
Copy Markdown
Contributor

@andrepereiradasilva can't retest it right now, will do it in the week end. Just one thing that come across my mind:
If multiple times JHtml::_('script', 'some/script.js', .. ); is called then the data attributes should be merged. Is this the current functionality? If not then I guess we should make this change.
If this is already implemented, just discard this comment 😄

@mbabker
Copy link
Copy Markdown
Contributor

mbabker commented Jan 27, 2016

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.

@andrepereiradasilva
Copy link
Copy Markdown
Contributor Author

IIRC, and looking at the code it adds the same attribs to all the scripts.
But a test is needed.

Update: i misunderstood what you were saying.
I don't know if you call several times what happens. Will check when i have time.

@dgrammatiko
Copy link
Copy Markdown
Contributor

@andrepereiradasilva let me explain what I am proposing here:
you have template that registers bootstrap.js and appends some data for tooltip
then a module is also trying to set some other attribs for tooltips
The last call should not overwrite the existing data attributes but rather merge it to the existing.
Ok the example might be stupid, but I hope it gives you some insights...

@mbabker
Copy link
Copy Markdown
Contributor

mbabker commented Jan 28, 2016

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.

@dgrammatiko
Copy link
Copy Markdown
Contributor

@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...

@roland-d
Copy link
Copy Markdown
Contributor

@andrepereiradasilva Any update on this PR?


This comment was created with the J!Tracker Application at issues.joomla.org/joomla-cms/8540.

@andrepereiradasilva
Copy link
Copy Markdown
Contributor Author

@roland-d the problem @DGT41 is talking about is because of the not so good JDocument storing and rendering the scripts.
This problem as already been discussed several times.

See, for instance, my comment in #10223 (comment)

This needs a full rework IMO and there are several PR proposing most of this changes needed.
But i guess it will be very difficult before 4.0 because of B/C

@dgrammatiko
Copy link
Copy Markdown
Contributor

I have tested this item ✅ successfully on 4697be7


This comment was created with the J!Tracker Application at issues.joomla.org/joomla-cms/8540.

@dgrammatiko
Copy link
Copy Markdown
Contributor

@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!

@joomla-cms-bot joomla-cms-bot added the RTC This Pull Request is Ready To Commit label Jul 18, 2016
$this->_scripts[$url]['mime'] = $type;
$this->_scripts[$url]['defer'] = $defer;
$this->_scripts[$url]['async'] = $async;
$this->_scripts[$url]['attribs'] = $attribs;
Copy link
Copy Markdown
Contributor

@dgrammatiko dgrammatiko Jul 18, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@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);
            }
        }

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mbabker since $attribs is newly introduced there is no B/C for what I'm proposing above, right?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@andrepereiradasilva
Copy link
Copy Markdown
Contributor Author

i think i have a better alternative for this that i think it's more flexible.
Made a new PR for this alternative method.
Please check #11289 . If ok will close this one.

@roland-d roland-d added this to the Joomla 3.7.0 milestone Jul 26, 2016
@andrepereiradasilva
Copy link
Copy Markdown
Contributor Author

please remove the RTC and label from this.
i think #11289 is a better way to do this so i'm going to close it

@joomla-cms-bot joomla-cms-bot removed the RTC This Pull Request is Ready To Commit label Jul 28, 2016
@andrepereiradasilva andrepereiradasilva deleted the script-tag-attribs branch July 28, 2016 14:40
@brianteeman
Copy link
Copy Markdown
Contributor

Removed the milestone

@brianteeman brianteeman removed this from the Joomla 3.7.0 milestone Jul 28, 2016
@brianteeman brianteeman added the RTC This Pull Request is Ready To Commit label Jul 28, 2016
@joomla-cms-bot joomla-cms-bot removed the RTC This Pull Request is Ready To Commit label Jul 28, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

9 participants