Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Global variable declaration issue in IE7 #68

Open
seanhodges opened this issue Apr 17, 2012 · 4 comments
Open

Global variable declaration issue in IE7 #68

seanhodges opened this issue Apr 17, 2012 · 4 comments

Comments

@seanhodges
Copy link

This does not occur in IE9, I have not tested others...

Instantiating the parser in IE7 causes the following Javascript error to occur:

"TypeError: 'S' is undefined"

Moving the declaration of 'S' further up the script (above the SAXParser constructor) fixes the problem, but then chokes during a parse on the 'whitespace' global variable:

"TypeError: 'whitespace' is undefined"

Again, the workaround is to move 'whitespace' above the constructor. Finally, IE7 does not support the Object.create function:

"TypeError: Object doesn't support this property or method"

I added Crockford's Object.create implementation in my own code to support this, however I believe you have a declaration in sax.js that might work by moving up the script as well.

@seanhodges
Copy link
Author

I have attached below a dirty patch for my fix - sorry I don't have a lot of time to fork and patch upstream right now:

Index: lib/sax.js
===================================================================
--- lib/sax.js  (revision 32424)
+++ lib/sax.js  (revision 32425)
@@ -43,6 +43,92 @@
   , "closenamespace"
   ]
 
+/////////////////////////////////////////
+// SEAN: Moved this declaration block here to support IE7...
+var S = 0
+sax.STATE =
+{ BEGIN                     : S++
+    , TEXT                      : S++ // general stuff
+    , TEXT_ENTITY               : S++ // & and such.
+    , OPEN_WAKA                 : S++ // <
+    , SGML_DECL                 : S++ // 
+    , SCRIPT                    : S++ // <script> ...
+    , SCRIPT_ENDING             : S++ // <script> ... <
+}
+
+sax.ENTITIES =
+{ "apos" : "'"
+    , "quot" : "\""
+    , "amp"  : "&"
+    , "gt"   : ">"
+    , "lt"   : "<"
+}
+
+for (var S in sax.STATE) sax.STATE[sax.STATE[S]] = S
+
+// shorthand
+S = sax.STATE
+
+
+// character classes and tokens
+var whitespace = "\r\n\t "
+    // this really needs to be replaced with character classes.
+    // XML allows all manner of ridiculous numbers and digits.
+    , number = "0124356789"
+    , letter = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
+    // (Letter | "_" | ":")
+    , nameStart = letter+"_:"
+        , nameBody = nameStart+number+"-."
+        , quote = "'\""
+        , entity = number+letter+"#"
+        , attribEnd = whitespace + ">"
+        , CDATA = "[CDATA["
+        , DOCTYPE = "DOCTYPE"
+        , XML_NAMESPACE = "http://www.w3.org/XML/1998/namespace"
+        , XMLNS_NAMESPACE = "http://www.w3.org/2000/xmlns/"
+        , rootNS = { xml: XML_NAMESPACE, xmlns: XMLNS_NAMESPACE }
+
+// turn all the string character sets into character class objects.
+whitespace = charClass(whitespace)
+number = charClass(number)
+letter = charClass(letter)
+nameStart = charClass(nameStart)
+nameBody = charClass(nameBody)
+quote = charClass(quote)
+entity = charClass(entity)
+attribEnd = charClass(attribEnd)
+
+// SEAN: Declaration block end
+/////////////////////////////////////////
+
 function SAXParser (strict, opt) {
   if (!(this instanceof SAXParser)) return new SAXParser(strict, opt)
 
@@ -223,34 +309,8 @@
 
 
 
-// character classes and tokens
-var whitespace = "\r\n\t "
-  // this really needs to be replaced with character classes.
-  // XML allows all manner of ridiculous numbers and digits.
-  , number = "0124356789"
-  , letter = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
-  // (Letter | "_" | ":")
-  , nameStart = letter+"_:"
-  , nameBody = nameStart+number+"-."
-  , quote = "'\""
-  , entity = number+letter+"#"
-  , attribEnd = whitespace + ">"
-  , CDATA = "[CDATA["
-  , DOCTYPE = "DOCTYPE"
-  , XML_NAMESPACE = "http://www.w3.org/XML/1998/namespace"
-  , XMLNS_NAMESPACE = "http://www.w3.org/2000/xmlns/"
-  , rootNS = { xml: XML_NAMESPACE, xmlns: XMLNS_NAMESPACE }
 
-// turn all the string character sets into character class objects.
-whitespace = charClass(whitespace)
-number = charClass(number)
-letter = charClass(letter)
-nameStart = charClass(nameStart)
-nameBody = charClass(nameBody)
-quote = charClass(quote)
-entity = charClass(entity)
-attribEnd = charClass(attribEnd)
-
 function charClass (str) {
   return str.split("").reduce(function (s, c) {
     s[c] = true
@@ -266,58 +326,10 @@
   return !charclass[c]
 }
 
-var S = 0
-sax.STATE =
-{ BEGIN                     : S++
-, TEXT                      : S++ // general stuff
-, TEXT_ENTITY               : S++ // & and such.
-, OPEN_WAKA                 : S++ // <
-, SGML_DECL                 : S++ // 
-, SCRIPT                    : S++ // <script> ...
-, SCRIPT_ENDING             : S++ // <script> ... <
-}
 
-sax.ENTITIES =
-{ "apos" : "'"
-, "quot" : "\""
-, "amp"  : "&"
-, "gt"   : ">"
-, "lt"   : "<"
-}
-
-for (var S in sax.STATE) sax.STATE[sax.STATE[S]] = S
-
-// shorthand
-S = sax.STATE
-
 function emit (parser, event, data) {
   parser[event] && parser[event](data)
 }

@dscape
Copy link

dscape commented Apr 17, 2012

You can fork this project, applies the fixes, send a pull request. In the commit reference "Fixes #68" to close this issue

@seanhodges
Copy link
Author

The above fix was a quick-and-dirty hack and has not been checked against the test suite. Once I get time I'll fork and test it properly; but in the meantime the patch above gives an opportunity for someone else to apply the same workaround or suggest a more elegant fix.

@isaacs
Copy link
Owner

isaacs commented May 8, 2012

@seanhodges On the blob page on github, you can actually edit the file right there, and then it'll automatically make a pull req for you.

I am willing to accept contributions on this, but please send a patch via a pull request, or you can use the more oldschool method:

git clone git://github.com/isaacs/sax-js.git
cd sax-js
# make your changes
git commit -m "Fix #68 Some reasonable short message goes here"
git format-patch HEAD^
# that creates a file like 0001-Fix-68-Some-reasonable-short-message-goes-here.patch
# get that file to me.  https://gist.github.com/ is a common way to do that.  email works, too.

The patch should not have a block comment with your name. That's what commit history is for. We can always git blame to see where code came from much more effectively :)

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

No branches or pull requests

3 participants