Skip to content

Commit

Permalink
Rudimentary script support
Browse files Browse the repository at this point in the history
End scripts on '</'
  • Loading branch information
isaacs committed Aug 17, 2011
1 parent 46e3577 commit c8ffb08
Showing 1 changed file with 37 additions and 5 deletions.
42 changes: 37 additions & 5 deletions lib/sax.js
Expand Up @@ -20,7 +20,7 @@ sax.MAX_BUFFER_LENGTH = 64 * 1024
var buffers = [ var buffers = [
"comment", "sgmlDecl", "textNode", "tagName", "doctype", "comment", "sgmlDecl", "textNode", "tagName", "doctype",
"procInstName", "procInstBody", "entity", "attribName", "procInstName", "procInstBody", "entity", "attribName",
"attribValue", "cdata" "attribValue", "cdata", "script"
] ]


function SAXParser (strict, opt) { function SAXParser (strict, opt) {
Expand Down Expand Up @@ -61,6 +61,11 @@ function checkBufferLength (parser) {
parser.cdata = "" parser.cdata = ""
break break


case "script":
emitNode(parser, "onscript", parser.script)
parser.script = ""
break

default: default:
error(parser, "Max buffer length exceeded: "+buffers[i]) error(parser, "Max buffer length exceeded: "+buffers[i])
} }
Expand Down Expand Up @@ -215,8 +220,8 @@ sax.STATE =
, CDATA_ENDING_2 : S++ // ]] , CDATA_ENDING_2 : S++ // ]]
, PROC_INST : S++ // <?hi , PROC_INST : S++ // <?hi
, PROC_INST_BODY : S++ // <?hi there , PROC_INST_BODY : S++ // <?hi there
, PROC_INST_QUOTED : S++ // <?hi there , PROC_INST_QUOTED : S++ // <?hi "there
, PROC_INST_ENDING : S++ // <?hi there ? , PROC_INST_ENDING : S++ // <?hi "there" ?
, OPEN_TAG : S++ // <strong , OPEN_TAG : S++ // <strong
, OPEN_TAG_SLASH : S++ // <strong / , OPEN_TAG_SLASH : S++ // <strong /
, ATTRIB : S++ // <a , ATTRIB : S++ // <a
Expand All @@ -229,6 +234,8 @@ sax.STATE =
, ATTRIB_VALUE_ENTITY_U : S++ // <foo bar=&quot; , ATTRIB_VALUE_ENTITY_U : S++ // <foo bar=&quot;
, CLOSE_TAG : S++ // </a , CLOSE_TAG : S++ // </a
, CLOSE_TAG_SAW_WHITE : S++ // </a > , CLOSE_TAG_SAW_WHITE : S++ // </a >
, SCRIPT : S++ // <script> ...
, SCRIPT_ENDING : S++ // <script> ... <
} }


sax.ENTITIES = sax.ENTITIES =
Expand All @@ -246,7 +253,8 @@ S = sax.STATE
sax.EVENTS = [ // for discoverability. sax.EVENTS = [ // for discoverability.
"text", "processinginstruction", "sgmldeclaration", "text", "processinginstruction", "sgmldeclaration",
"doctype", "comment", "attribute", "opentag", "closetag", "doctype", "comment", "attribute", "opentag", "closetag",
"opencdata", "cdata", "closecdata", "error", "end", "ready" ] "opencdata", "cdata", "closecdata", "error", "end", "ready",
"script" ]


function emit (parser, event, data) { function emit (parser, event, data) {
parser[event] && parser[event](data) parser[event] && parser[event](data)
Expand Down Expand Up @@ -304,9 +312,14 @@ function openTag (parser, selfClosing) {
parser.tags.push(parser.tag) parser.tags.push(parser.tag)
emitNode(parser, "onopentag", parser.tag) emitNode(parser, "onopentag", parser.tag)
if (!selfClosing) { 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.tag = null
parser.tagName = "" parser.tagName = ""
parser.state = S.TEXT
} }
parser.attribName = parser.attribValue = "" parser.attribName = parser.attribValue = ""
} }
Expand Down Expand Up @@ -421,6 +434,25 @@ function write (chunk) {
} }
continue 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: case S.OPEN_WAKA:
// either a /, ?, !, or text is coming next. // either a /, ?, !, or text is coming next.
if (c === "!") { if (c === "!") {
Expand Down

0 comments on commit c8ffb08

Please sign in to comment.