Permalink
Browse files

Rudimentary script support

End scripts on '</'
  • Loading branch information...
1 parent 46e3577 commit c8ffb082ccde4ebd0f893a0be78d50630d33c4a5 @isaacs isaacs committed Aug 17, 2011
Showing with 37 additions and 5 deletions.
  1. +37 −5 lib/sax.js
View
@@ -20,7 +20,7 @@ sax.MAX_BUFFER_LENGTH = 64 * 1024
var buffers = [
"comment", "sgmlDecl", "textNode", "tagName", "doctype",
"procInstName", "procInstBody", "entity", "attribName",
- "attribValue", "cdata"
+ "attribValue", "cdata", "script"
]
function SAXParser (strict, opt) {
@@ -61,6 +61,11 @@ function checkBufferLength (parser) {
parser.cdata = ""
break
+ case "script":
+ emitNode(parser, "onscript", parser.script)
+ parser.script = ""
+ break
+
default:
error(parser, "Max buffer length exceeded: "+buffers[i])
}
@@ -215,8 +220,8 @@ sax.STATE =
, CDATA_ENDING_2 : S++ // ]]
, PROC_INST : S++ // <?hi
, PROC_INST_BODY : S++ // <?hi there
-, PROC_INST_QUOTED : S++ // <?hi there
-, PROC_INST_ENDING : S++ // <?hi there ?
+, PROC_INST_QUOTED : S++ // <?hi "there
+, PROC_INST_ENDING : S++ // <?hi "there" ?
, OPEN_TAG : S++ // <strong
, OPEN_TAG_SLASH : S++ // <strong /
, ATTRIB : S++ // <a
@@ -229,6 +234,8 @@ sax.STATE =
, ATTRIB_VALUE_ENTITY_U : S++ // <foo bar=&quot;
, CLOSE_TAG : S++ // </a
, CLOSE_TAG_SAW_WHITE : S++ // </a >
+, SCRIPT : S++ // <script> ...
+, SCRIPT_ENDING : S++ // <script> ... <
}
sax.ENTITIES =
@@ -246,7 +253,8 @@ S = sax.STATE
sax.EVENTS = [ // for discoverability.
"text", "processinginstruction", "sgmldeclaration",
"doctype", "comment", "attribute", "opentag", "closetag",
- "opencdata", "cdata", "closecdata", "error", "end", "ready" ]
+ "opencdata", "cdata", "closecdata", "error", "end", "ready",
+ "script" ]
function emit (parser, event, data) {
parser[event] && parser[event](data)
@@ -304,9 +312,14 @@ function openTag (parser, selfClosing) {
parser.tags.push(parser.tag)
emitNode(parser, "onopentag", parser.tag)
if (!selfClosing) {
+ // special case for <script> in non-strict mode.
+ if (!parser.strict && parser.tagName.toLowerCase() === "script") {
+ parser.state = S.SCRIPT
+ } else {
+ parser.state = S.TEXT
+ }
parser.tag = null
parser.tagName = ""
- parser.state = S.TEXT
}
parser.attribName = parser.attribValue = ""
}
@@ -421,6 +434,25 @@ function write (chunk) {
}
continue
+ case S.SCRIPT:
+ // only non-strict
+ if (c === "<") {
+ parser.state = S.SCRIPT_ENDING
+ } else parser.script += c
+ continue
+
+ case S.SCRIPT_ENDING:
+ if (c === "/") {
+ emitNode(parser, "onscript", parser.script)
+ parser.state = S.CLOSE_TAG
+ parser.script = ""
+ parser.tagName = ""
+ } else {
+ parser.script += "<" + c
+ parser.state = S.SCRIPT
+ }
+ continue
+
case S.OPEN_WAKA:
// either a /, ?, !, or text is coming next.
if (c === "!") {

0 comments on commit c8ffb08

Please sign in to comment.