Download & Extend

Provide option to load jsMath only on pages that contain math code

Project:JsMath for displaying mathematics with TeX
Version:6.x-1.1
Component:Miscellaneous
Category:feature request
Priority:normal
Assigned:AlexisWilke
Status:closed (fixed)
Issue tags:Security Advisory follow-up

Issue Summary

Hi,

I was wondering how to prevent jsMath from loading in certain pages, especially all admin pages admin/*, these pages contains some '$' and hence jsmath is processing them and output undesired text, especially in the view setting pages.

Thanks

Comments

#1

This is something I had planned to do but did not get around to yet. The following is in the future plans:

"Currently this module does not provide filters which can be added to input
formats but instead the jsMath processing applies to all Drupal pages once
the module is enabled. In a future release this will be changed, using the
mechanism for selective processing of parts of a page described by Davide in
the forum post at https://sourceforge.net/forum/message.php?msg_id=4413480

#2

Version:5.x-1.1» 6.x-1.1

I made jsMath a filter for D6. I do not have D5...

There are two issues, to my point of view, you probably want to create specific Input Format entries so as to turn on the feature just and only on these few specific nodes. And the filter is the piece of code that adds the necessary JavaScript to start jsMath. This means we cannot make use of the cache, unfortunate side effect.

None the less, this makes my admin pages DEAD FAST, in comparison. Although I have another JS that seems to do a similar thing because I still have some slowness, at least now I cannot blame jsMath for it!

This patch also includes the code that adds a SPAN tag around the [math] and [/math] (or whatever tag name that's used.) The span is given the special class name "jsmath". One thing in a specific theme, we could HIDE these span by default, and show them only after the math formula was transformed. Just an idea. That would require a change of the style once the jsMath function ran against a block.

Note that I did not use a DIV since that would break jsMath formulae that are written inline within a paragraph.

I'm offering a tarball for easy install, but the patch is otherwise going against 6.x-1.1. It is not intended as a fork, but... I strongly suggest to create a version 2.x since this is a completely different approach that will "break" existing sites (i.e. existing sites will see their formulae disappear... until they turn on the jsMath filter or create a new Input Format with the jsMath filter activated.)

Sorry if I do not support 5.x. It's old 9-)

Okay, now I can really love it...

Thank you.
Alexis Wilke

AttachmentSize
jsmath-6.x-1.1-filter.patch 5.77 KB
jsmath-6.x-1.1-as-filter.tar_.gz 12.76 KB

#3

Actually, there is an even better way. I will post a new entry later for it.

The idea is simple, inside the <span ...> we can have an event (probably won't work) or at least a unique ID. Then we can generate a list of <script> entries that reference/list these IDs. Actually, just an array would work. Then you do not need to parse anything in Javascript, all is done in PHP in Drupal. And that can be done one and cached. So it would be a lot better.

Thank you.
Alexis

#4

Alexis,

thanks for your work on this.

Have you read through the jsMath website to see if there is not some advice on how to best do what you are trying to do? See http://www.math.union.edu/~dpvc/jsMath/authors/welcome.html

#5

I have a better solution. Which i'm part way through implementing.

Firstly, move the js math loading functionallity into a separate function (rewritten to make use of drupals js adding facilities)

Next, we use hook_nodeapi $op=alter to search the finial node text and teaser for $,$$ and any custom math delimiters. Js math is the ONLY loaded if at least one of the delimiters is found in the finial output text.

If another module wishers to make use of jsmath, they can use something like if(function_exists('loadjsmaths')) { loadjsmaths() } to load it on pages that require it.

#6

carnage,

Won't work with the default Drupal cache. You need the JS to be added in the hook_init() function. There is documentation about that on the drupal site if you feel like verifying what I'm saying.

Thank you.
Alexis Wilke

#7

I have to disagree.

With caching enabled, this loading technique appears to work for both logged in and anon users.

also: http://api.drupal.org/api/function/drupal_add_js makes no reference to the javascript needing to be added in hook_init. In fact the opposite is implied by the list of functions that call drupal_add_js.

It is true, that the javascript cache thing dosn't work for JSmaths using this technique, however due to the way js maths loads itself, the cache is mostly rendered useless for jsmaths anyway

I've attached the modified module file if you want to test it for yourself. (I'd have made a patch but the jsmath file had already been modified. So I didn't have a common base.)

AttachmentSize
jsmath.module.txt 18.53 KB

#8

Okay, I agree that if you make the JS inline then the caching will work since the code is part of the HTML file! 8-)

Thank you.
Alexis Wilke

#9

Status:active» needs review

My first post wasn't too clear on my intentions as I was still experimenting with different solutions.

I think the ideal solution would be to give the admin the option of having jsmaths load on every page or just on the pages that maths is detected on.

#10

Because it is so slow to load, loading on all pages can be a big waste unless 99% of your pages include math expressions. The detection is so fast that the gain is visible immediately! That's a problem for slower computers that have to run all that Javascript code on pages that don't even include expressions...

Also I would suggest that you generate a patch starting from 6.x-1.1 so that way it shows clearly what has changed. You just have to extract the tarball in another directory and then apply something like:

diff -dur jsmath-6.x-1.1 jsmath >jsmath.patch

#11

Title:How to prevent jsMath from loading in certain pages» Provide option to load jsMath only on pages that contain math code
Category:support request» feature request

The changes carnage described in #5 are quite useful and his code in #7 looks good.

gustav, what is your opinion on including these improvements?

#12

Assigned to:Anonymous» AlexisWilke

Hi guys,

I checked in my changes so the module uses a filter. This prevents an XSS attack ([#854402]).

Let me know whether we can implement #7 in the new module.

Thank you.
Alexis

#13

The first worry was about admin pages which are not going to be processed unless you use jsMath on a block that appears in the admin pages (then... remove that offensive block from the admin/* area.)

If you think something else can be done to help this process, let me know.

At this point, I removed the <span> tag that was added in #2.

Thank you.
Alexis

#14

Status:needs review» fixed

Meant to mark this fixed. 8-)

#15

Status:fixed» closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.