]+)(?:>|$)/, function (s, e) { if (p.insertionPoint < s.length || (p.insertionPoint <= s.length && s.substring (s.length - 1, 1) != '>')) { token = {type: 'abort'}; return s; } token = {type: 'end-tag', value: e.toLowerCase ()}; p.insertionPoint -= s.length; return ''; }); if (token) return token; i.s = i.s.replace (/^<([^>]+)(?:>|$)/, function (s, e) { if (p.insertionPoint < s.length || (p.insertionPoint <= s.length && s.substring (s.length - 1, 1) != '>')) { token = {type: 'abort'}; return s; } var tagName; var attrs = {}; e = e.replace (/^[\S]+/, function (v) { tagName = v.toLowerCase (); return ''; }); while (true) { var m = false; e = e.replace (/^\s*([^\s=]+)\s*(?:=\s*(?:"([^"]*)"|'([^']*)'|([^"'\s]*)))?/, function (x, attrName, attrValue1, attrValue2, attrValue3) { v = attrValue1 || attrValue2 || attrValue3; v = v.replace (/"/g, '"').replace (/'/g, "'") .replace (/&/g, '&'); attrs[attrName.toLowerCase ()] = v; m = true; return ''; }); if (!m) break; } if (e.length) { log ('Broken start tag: "' + e + '"'); } token = {type: 'start-tag', value: tagName, attrs: attrs}; p.insertionPoint -= s.length; return ''; }); if (token) return token; if (p.insertionPoint <= 0) { return {type: 'abort'}; } i.s = i.s.replace (/^[^<]+/, function (s) { if (p.insertionPoint < s.length) { token = {type: 'char', value: s.substring (0, p.insertionPoint)}; var ip = p.insertionPoint; p.insertionPoint = 0; return s.substring (ip, s.length); } token = {type: 'char', value: s}; p.insertionPoint -= s.length; return ''; }); if (token) return token; i.s = i.s.replace (/^[\s\S]/, function (s) { token = {type: 'char', value: s}; p.insertionPoint -= s.length; return ''; }); if (token) return token; return {type: 'eof'}; } // getNextToken Parser.prototype.parse = function () { logIndentLevel++; log ('parse: start'); while (true) { var token = this.getNextToken (); log ('token: ' + token.type + ' "' + token.value + '"'); if (token.type == 'start-tag') { if (token.value == 'script') { // 1. Create an element for the token in the HTML namespace. var el = new JSElement (this.doc, token.value); if (token.attrs.async != null) el.async = true; if (token.attrs.defer != null) el.defer = true; if (token.attrs.src != null) el.src = token.attrs.src; // 2. Mark the element as being "parser-inserted". el.manakaiParserInserted = true; // 3. Switch the tokeniser's content model flag to the CDATA state. this.parseMode = 'script'; // 4.1. Collect all the character tokens. while (true) { var token = this.getNextToken (); log ('token: ' + token.type + ' "' + token.value + '"'); if (token.type == 'char') { // 5. Append a single Text node to the script element node. el.manakaiAppendText (token.value); // 4.2. Until it returns a token that is not a character token, or // until it stops tokenising. } else if (token.type == 'eof' || (token.type == 'end-tag' && token.value == 'script') || token.type == 'abort') { // 6. Switched back to the PCDATA state. this.parseMode = 'pcdata'; // 7.1. If the next token is not an end tag token with ... if (token.type != 'end-tag') { // 7.2. This is a parse error. log ('Parse error: no ' + 'script>'); // 7.3. Mark the script element as "already executed". el.manakaiAlreadyExecuted = true; } else { // 7.4. Ignore it. // } break; } } // 8.1. If the parser were originally created for the ... if (this.fragmentParsingMode) { // 8.2. Mark the script element as "already executed" and ... el.alreadyExecuted = true;