Edgewall Software
Modify

Opened 15 years ago

Closed 13 years ago

#7775 closed enhancement (wontfix)

Method to add script tag with in-file javascript code

Reported by: martin@… Owned by:
Priority: normal Milestone:
Component: web frontend Version: none
Severity: normal Keywords: needpatch
Cc: Branch:
Release Notes:
API Changes:
Internal Changes:

Description

While it is possible to add script tags for external javascript file using the add_script function in trac.web.chrome, it isn't possible to add script tags with in-file javascript code content.

While using only external javascript files is a good web design approach, some plug-ins might need to add small dynamic generated javascript code into the generated XHTML, e.g. to set variables which value depends on trac.ini settings or external sources. I had this problem recently with the GoogleMapMacro.

Because script tags shoudn't refer to an external file and have code content, I would like to suggest a new python method and template tag:

  • trac/templates/layout.html

    diff -Naur Trac-0.11.1-py2.5.egg/trac/templates/layout.html Trac-0.11.1-py2.5.egg.new/trac/templates/layout.html
    old new  
    2222    </py:if>
    2323    <script py:for="script in chrome.scripts"
    2424            type="${script.type}" src="${script.href}"></script>
     25    <script py:for="script in chrome.scriptscontent" type="${script.type}">
     26        /*<![CDATA[*/
     27        ${script.content}
     28        /*]]>*/
     29    </script>
    2530    ${Markup('&lt;!--[if lt IE 7]&gt;')}
    2631    <script type="text/javascript" src="${chrome.htdocs_location}js/ie_pre7_hacks.js"></script>
    2732    ${Markup('&lt;![endif]--&gt;')}
  • Trac-0.11.1-py2.5.egg

    diff -Naur Trac-0.11.1-py2.5.egg/trac/web/chrome.py Trac-0.11.1-py2.5.egg.new/trac/web/chrome.py
    old new  
    106106    req.chrome.setdefault('scripts', []).append(script)
    107107    scriptset.add(filename)
    108108
     109def add_script_content(req, content, mimetype='text/javascript'):
     110    """Add in-file javascript code to the template.
     111
     112    """
     113    import hashlib
     114    m = hashlib.md5()
     115    m.update(content)
     116    digest = m.hexdigest()
     117
     118    scriptcontentset = req.chrome.setdefault('scriptcontentset', set())
     119    if digest in scriptcontentset:
     120        return False # Already added that code
     121
     122    scriptcontent = {'content': content, 'type': mimetype}
     123
     124    req.chrome.setdefault('scriptscontent', []).append(scriptcontent)
     125    scriptcontentset.add(digest)
     126
    109127def add_javascript(req, filename):
    110128    """Deprecated: use `add_script()` instead."""
    111129    add_script(req, filename, mimetype='text/javascript')

Attachments (1)

7775-add-inline-script-r10417.patch (1.2 KB ) - added by Remy Blank 13 years ago.
Updated patch for trunk.

Download all attachments as: .zip

Change History (12)

comment:1 by Christian Boos, 15 years ago

e.g. to set variables which value depends on trac.ini settings or external sources

Couldn't that be done by sending a request?

comment:2 by Christian Boos, 15 years ago

Keywords: needinfo added

Heading to a wontfix…

in reply to:  1 comment:3 by martin@…, 15 years ago

Replying to cboos:

e.g. to set variables which value depends on trac.ini settings or external sources

Couldn't that be done by sending a request?

Yes it could be handled using a request, but this would add an extra unneeded step. It would be a little silly if a script tag can be added but not inline code which is also valid XHTML.

comment:4 by Christian Boos, 15 years ago

Milestone: 0.13

Ok, admitting we allow adding inline js code like you suggested, isn't md5 computation a bit overkill? Simply adding the code itself in a set would be enough.

in reply to:  4 comment:5 by Christian Boos, 14 years ago

Simply adding the code itself in a set would be enough.

And we might want to do the same for warnings, as I've noticed we can get multiple times the same warning.

comment:6 by Christian Boos, 14 years ago

Keywords: needpatch added; needinfo removed

comment:7 by Christian Boos, 14 years ago

Milestone: next-major-0.1X0.13

Patch is more or less OK, will tweak according to comment:4 and apply.

by Remy Blank, 13 years ago

Updated patch for trunk.

comment:8 by Remy Blank, 13 years ago

7775-add-inline-script-r10417.patch is an update for current trunk, and the duplicate elimination is much simpler (even using a set() is overkill, as there will only ever be a handful of items at most).

But isn't this superseded by add_script_data()? It allows adding JavaScript data from Python, which was the original request. I vote "wontfix" (or "worksforme").

comment:9 by Christian Boos, 13 years ago

Milestone: 0.13next-major-0.1X

This add_inline_script() could still be theoretically useful for triggering some immediate action on page load ($(document).ready(...)). Maybe we could just defer the ticket until someone has a real use for this?

comment:10 by Remy Blank, 13 years ago

I don't mind. My thought was that any code triggered on page load would almost always be the same, and only the data would change. In that case, the code should be in the template or a separate .js, and the data can be passed with add_script_data().

in reply to:  10 comment:11 by Christian Boos, 13 years ago

Milestone: next-major-0.1X
Resolution: wontfix
Status: newclosed

Replying to rblank:

… In that case, the code should be in the template

Not easily possible for plugins, unless they write a template stream filter, which seems overkill for that.

or a separate .js,

and I was not sure what the timing would be for a $(document).ready(...) snippet present in a secondary .js file… well, I suppose it's going to be the same (the document as a whole must be ready…).

So OK for wontfix.

Modify Ticket

Change Properties
Set your email in Preferences
Action
as closed The ticket will remain with no owner.
The resolution will be deleted. Next status will be 'reopened'.
to The owner will be changed from (none) to the specified user.

Add Comment


E-mail address and name can be saved in the Preferences .
 
Note: See TracTickets for help on using tickets.