Question

I'm using PrinceXML to make some PDFs of some HTML which includes some MathJax markup. In order to render the math properly, the javascript handling the math rendering needs to be run on the page before conversion to PDF.

If I enable javascript in PrinceXML and try to load the MathJax library, I get a "TypeError: null value is not an object" in the initialisation code. Tracking this down, it turns out that the following causes the error:

var scripts = (document.documentElement || document).getElementsByTagName("script");
var namePattern = new RegExp("(^|/)"+BASENAME+"\\.js(\\?.*)?$");
for (var i = scripts.length-1; i >= 0; i--) {
if (scripts[i].src.match(namePattern)) { // <--- Error thrown here
  STARTUP.script = scripts[i].innerHTML;
  if (RegExp.$2) {
    <etc...>

The error is thrown on a script element without an src attribute (MathJax uses script elements to store LaTeX code in). PrinceXML appears to handle missing src attributes by returning null when accessing a missing src attribute - hence scripts[i].src.match(namePattern) throws an error as src is null. However, when done in Firefox (see fiddle: http://jsfiddle.net/9ZHMR/) element.src returns the empty string when the src attribute is missing, and so I didn't see this issue when working in the browser.

Which behaviour is 'correct' - should a script element's src attribute be null or the empty string if the attribute does not exist? Is this a case of real-life vs the standards?

EDIT I'm interested primarily in whether this behavior is specified by a standard somewhere, so I know whether to post a bug report to Firefox or PrinceXML, or if there is no standard, post a bug report to MathJax to handle this case.

Was it helpful?

Solution

The spec at http://www.whatwg.org/specs/web-apps/current-work/multipage/scripting-1.html#the-script-element says:

 attribute DOMString src;

That's not a nullable string return value, so returning null from .src is not allowed by the spec.

OTHER TIPS

This depends on the specific implementation for the HtmlScriptElement. If the specific implementation has the src attribute predefined as an empty string as it seems to be the case in FireFox then the src would be returned as an empty string. If it is initially defined to be null then src would be returned as null.

You could probably avoid the error with this minor amendment, which checks for a src before using it:

if (scripts[i].src && scripts[i].src.match(namePattern)) {
    ...

Or long hand...

if (scripts[i].src) {
    if (scripts[i].src.match(namePattern)) {
        ...

If scripts[i].src is null, it will evaluate as "falsey".

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top