/[suikacvs]/markup/html/scripting-parser/parser.html
Suika

Contents of /markup/html/scripting-parser/parser.html

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.5 - (hide annotations) (download) (as text)
Fri Apr 25 11:40:56 2008 UTC (18 years, 6 months ago) by wakaba
Branch: MAIN
Changes since 1.4: +17 -4 lines
File MIME type: text/html
Bug fix

1 wakaba 1.1 <!DOCTYPE HTML>
2     <html lang=en>
3     <head>
4     <title>Demo of HTML5 Parsing Algorithm with Scripting Enabled</title>
5     <style>
6     textarea {
7     display: block;
8     width: 80%;
9     margin-left: auto;
10     margin-right: auto;
11     min-height: 20em;
12     }
13     output {
14     display: block;
15     font-family: monospace;
16 wakaba 1.4 white-space: -moz-pre-wrap;
17     white-space: pre-wrap;
18 wakaba 1.1 }
19     </style>
20     <script>
21     function update () {
22     document.logElement.textContent = '';
23 wakaba 1.2 var p = new Parser (new InputStream (document.sourceElement.value));
24 wakaba 1.4 var doc = p.doc;
25 wakaba 1.2 p.parse ();
26 wakaba 1.4 log (dumpTree (doc, ''));
27 wakaba 1.1 } // update
28    
29     function log (s) {
30     document.logElement.appendChild (document.createTextNode (s + "\n"));
31     } // log
32    
33     function InputStream (s) {
34     this.s = s;
35     } // InputStream
36    
37 wakaba 1.4 function Parser (i, doc) {
38 wakaba 1.1 this.parseMode = 'pcdata';
39 wakaba 1.4 if (!doc) {
40     doc = new JSDocument (this);
41     doc.manakaiIsHTML = true;
42     }
43     this.doc = doc;
44     this.openElements = [doc];
45 wakaba 1.2 this.in = i;
46 wakaba 1.4 this.scriptsExecutedAfterParsing = [];
47 wakaba 1.1 } // Parser
48    
49 wakaba 1.2 Parser.prototype.getNextToken = function () {
50 wakaba 1.3 var p = this;
51 wakaba 1.2 var i = this.in;
52 wakaba 1.1 if (this.parseMode == 'script') {
53     var token;
54 wakaba 1.3 if (p.insertionPoint <= 0) {
55     return {type: 'abort'};
56     }
57 wakaba 1.4 i.s = i.s.replace (/^([^<]+)/,
58 wakaba 1.1 function (s, t) {
59 wakaba 1.3 if (0 < p.insertionPoint && p.insertionPoint < t.length) {
60     token = {type: 'char', value: t.substring (0, p.insertionPoint)};
61     var ip = p.insertionPoint;
62     p.insertionPoint = 0;
63 wakaba 1.4 return t.substring (ip, t.length);
64 wakaba 1.3 }
65 wakaba 1.1 token = {type: 'char', value: t};
66 wakaba 1.4 p.insertionPoint -= t.length;
67     return '';
68 wakaba 1.1 });
69     if (token) return token;
70 wakaba 1.3 i.s = i.s.replace (/^<\/[Ss][Cc][Rr][Ii][Pp][Tt]>/, function (s) {
71 wakaba 1.4 if (p.insertionPoint < s.length) {
72 wakaba 1.3 token = {type: 'abort'};
73     return s;
74     }
75 wakaba 1.1 token = {type: 'end-tag', value: 'script'};
76 wakaba 1.3 p.insertionPoint -= s.length;
77 wakaba 1.1 return '';
78     });
79     if (token) return token;
80 wakaba 1.5 var m;
81     if ((p.insertionPoint < '</script'.length) &&
82