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

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

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.1 by wakaba, Sun Apr 20 06:07:24 2008 UTC revision 1.14 by wakaba, Tue Apr 29 02:50:00 2008 UTC
# Line 1  Line 1 
1  <!DOCTYPE HTML>  <!DOCTYPE HTML>
2  <html lang=en>  <html lang=en>
3  <head>  <head>
4  <title>Demo of HTML5 Parsing Algorithm with Scripting Enabled</title>  <title>Live Scripting HTML Parser</title>
5    <link rel=author href="http://suika.fam.cx/~wakaba/who?">
6    <link rel=license href="http://suika.fam.cx/c/gnu/gpl"
7        title="GNU GPL2 or later">
8  <style>  <style>
9      h1 {
10        margin: 0;
11        font-size: 150%;
12      }
13      h2 {
14        margin: 0;
15        font-size: 100%;
16      }
17      p {
18        margin: 0 1em;
19      }
20    textarea {    textarea {
21       display: block;      width: 100%;
22       width: 80%;      -width: 99%;
23       margin-left: auto;      height: 10em;
      margin-right: auto;  
      min-height: 20em;  
24    }    }
25    output {    output {
26      display: block;      display: block;
27      font-family: monospace;      font-family: monospace;
28      white-space: pre;      white-space: -moz-pre-wrap;
29        white-space: pre-wrap;
30    }    }
31  </style>  </style>
32  <script>  <script>
33      var delayedUpdater = 0;
34    
35    function update () {    function update () {
36      document.logElement.textContent = '';      if (delayedUpdater) {
37      var p = new Parser ();        clearTimeout (delayedUpdater);
38      p.parse (new InputStream (document.sourceElement.value));        delayedUpdater = 0;
39      log (dumpTree (p.doc, ''));      }
40        delayedUpdater = setTimeout (update2, 100);
41    } // update    } // update
42    
43      function update2 () {
44        var v = document.sourceElement.value;
45        if (v != document.previousSourceText) {
46          document.previousSourceText = v;
47          document.links['permalink'].href
48              = location.pathname + '?s=' + encodeURIComponent (v);
49          document.links['ldvlink'].href
50              = 'http://software.hixie.ch/utilities/js/live-dom-viewer/?'
51              + encodeURIComponent (v);
52    
53          document.logElement.textContent = '';
54          var p = new Parser (new InputStream (v));
55          var doc = p.doc;
56          p.parse ();
57          
58          log (dumpTree (doc, ''));
59          
60          if (p.hasAsyncScript) {
61            log ('Some script codes are executed asynchronously; it means that the document might be rendered in different ways depending on the network condition and other factors');
62          }
63        }
64      } // update2
65    
66      var logIndentLevel = 0;
67    function log (s) {    function log (s) {
68        for (var i = 0; i < logIndentLevel; i++) {
69          s = '  ' + s;
70        }
71      document.logElement.appendChild (document.createTextNode (s + "\n"));      document.logElement.appendChild (document.createTextNode (s + "\n"));
72    } // log    } // log
73    
# Line 32  Line 75 
75      this.s = s;      this.s = s;
76    } // InputStream    } // InputStream
77    
78    function Parser () {    function Parser (i, doc) {
79      this.parseMode = 'pcdata';      this.parseMode = 'pcdata';
80      this.doc = new JSDocument ();      if (!doc) {
81      this.openElements = [this.doc];        doc = new JSDocument (this);
82          doc.manakaiIsHTML = true;
83        }
84        this.doc = doc;
85        this.openElements = [doc];
86        this.input = i;
87        this.scriptsExecutedAfterParsing = [];
88        this.scriptsExecutedSoon = [];
89        this.scriptsExecutedAsynchronously = [];
90    } // Parser    } // Parser
91    
92    Parser.prototype.getNextToken = function (i) {    Parser.prototype.getNextToken = function () {
93      if (this.parseMode == 'script') {      var p = this;
94        var i = this.input;
95        if (this.parseMode == 'cdata') {
96          var tagName = this.endTagName;
97        var token;        var token;
98        i.s = i.s.replace (/^([\s\S]+?)<\/[Ss][Cc][Rr][Ii][Pp][Tt]>/,        if (p.insertionPoint <= 0) {
99            return {type: 'abort'};
100          }
101          i.s = i.s.replace (/^([^<]+)/,
102        function (s, t) {        function (s, t) {
103            if (0 < p.insertionPoint && p.insertionPoint < t.length) {
104              token = {type: 'char', value: t.substring (0, p.insertionPoint)};
105              var ip = p.insertionPoint;
106              p.insertionPoint = 0;
107              return t.substring (ip, t.length);
108            }
109          token = {type: 'char', value: t};          token = {type: 'char', value: t};
110          return '<' + '/script>';          p.insertionPoint -= t.length;
111            return '';
112        });        });
113        if (token) return token;        if (token) return token;
114        i.s = i.s.replace (/^<\/[Ss][Cc][Rr][Ii][Pp][Tt]>/, function () {        var pattern = new RegExp ('^</' + tagName + '>', 'i');
115          token = {type: 'end-tag', value: 'script'};        i.s = i.s.replace (pattern, function (s) {
116            if (p.insertionPoint < s.length) {
117              token = {type: 'abort'};
118              return s;
119            }
120            token = {type: 'end-tag', value: tagName};
121            p.insertionPoint -= s.length;
122            return '';
123          });
124          if (token) return token;
125          var m;
126          if ((p.insertionPoint < ('</' + tagName).length) &&
127              (m = i.s.match (/^<\/([A-Za-z]+)/))) {
128            var v = m[1].substring (0, p.insertionPoint).toLowerCase ();
129            if (v == tagName.substring (0, p.insertionPoint - '</'.length)) {
130              return {type: 'abort'};
131            }
132          }
133          i.s = i.s.replace (/^</,
134          function (s) {
135            token = {type: 'char', value: s};
136            p.insertionPoint -= s.length;
137          return '';          return '';
138        });        });
139        if (token) return token;        if (token) return token;
# Line 56  Line 141 
141      }      }
142    
143      var token;      var token;
144      i.s = i.s.replace (/^<\/([^>]+)>/, function (s, e) {      i.s = i.s.replace (/^<\/([^>]+)(?:>|$)/, function (s, e) {
145          if (p.insertionPoint < s.length ||
146              (p.insertionPoint <= s.length &&
147               s.substring (s.length - 1, 1) != '>')) {
148            token = {type: 'abort'};
149            return s;
150          }
151        token = {type: 'end-tag', value: e.toLowerCase ()};        token = {type: 'end-tag', value: e.toLowerCase ()};
152          p.insertionPoint -= s.length;
153        return '';        return '';
154      });      });
155      if (token) return token;      if (token) return token;
156      i.s = i.s.replace (/^<([^>]+)>/, function (s, e) {      i.s = i.s.replace (/^<([^>]+)(?:>|$)/, function (s, e) {
157        token = {type: 'start-tag', value: e.toLowerCase ()};        if (p.insertionPoint < s.length ||
158              (p.insertionPoint <= s.length &&
159               s.substring (s.length - 1, 1) != '>')) {
160            token = {type: 'abort'};
161            return s;
162          }
163          var tagName;
164          var attrs = {};
165          e = e.replace (/^[\S]+/, function (v) {
166            tagName = v.toLowerCase ();
167            return '';
168          });
169          while (true) {
170            var m = false;
171            e = e.replace (/^\s*([^\s=]+)\s*(?:=\s*(?:"([^"]*)"|'([^']*)'|([^"'\s]*)))?/,
172            function (x, attrName, attrValue1, attrValue2, attrValue3) {
173              v = attrValue1 || attrValue2 || attrValue3;
174              v = v.replace (/&quot;/g, '"').replace (/&apos;/g, "'")
175                  .replace (/&amp;/g, '&');
176              attrs[attrName.toLowerCase ()] = v;
177              m = true;
178              return '';
179            });
180            if (!m) break;
181          }
182          if (e.length) {
183            log ('Broken start tag: "' + e + '"');
184          }
185          token = {type: 'start-tag', value: tagName, attrs: attrs};
186          p.insertionPoint -= s.length;
187        return '';        return '';
188      });      });
189      if (token) return token;      if (token) return token;
190        if (p.insertionPoint <= 0) {
191          return {type: 'abort'};
192        }
193      i.s = i.s.replace (/^[^<]+/, function (s) {      i.s = i.s.replace (/^[^<]+/, function (s) {
194          if (p.insertionPoint < s.length) {
195            token = {type: 'char', value: s.substring (0, p.insertionPoint)};
196            var ip = p.insertionPoint;
197            p.insertionPoint = 0;
198            return s.substring (ip, s.length);
199          }
200        token = {type: 'char', value: s};        token = {type: 'char', value: s};
201          p.insertionPoint -= s.length;
202        return '';        return '';
203      });      });
204      if (token) return token;      if (token) return token;
205      i.s = i.s.replace (/^[\s\S]/, function (s) {      i.s = i.s.replace (/^[\s\S]/, function (s) {
206        token = {type: 'char', value: s};        token = {type: 'char', value: s};
207          p.insertionPoint -= s.length;
208        return '';        return '';
209      });      });
210      if (token) return token;      if (token) return token;
211      return {type: 'eof'};      return {type: 'eof'};
212    } // getNextToken    } // getNextToken
213    
214    Parser.prototype.parse = function (i) {    Parser.prototype.parse = function () {
215      log ('start parsing');      logIndentLevel++;
216        log ('parse: start');
217    
218      while (true) {      while (true) {
219        var token = this.getNextToken (i);        var token = this.getNextToken ();
220        log ('token: ' + token.type + ' "' + token.value + '"');        log ('token: ' + token.type + ' "' + token.value + '"');
221    
222        if (token.type == 'start-tag') {        if (token.type == 'start-tag') {
         var el = new JSElement (token.value);  
223          if (token.value == 'script') {          if (token.value == 'script') {
224            this.parseMode = 'script';            // 1. Create an element for the token in the HTML namespace.
225              var el = new JSElement (this.doc, token.value);
226              if (token.attrs.async != null) el.async = true;
227              if (token.attrs.defer != null) el.defer = true;
228              if (token.attrs.src != null) el.src = token.attrs.src;
229    
230              // 2. Mark the element as being "parser-inserted".
231              el.manakaiParserInserted = true;
232    
233              // 3. Switch the tokeniser's content model flag to the CDATA state.
234              this.parseMode = 'cdata';
235              this.endTagName = 'script';
236    
237              // 4.1. Collect all the character tokens.
238            while (true) {            while (true) {
239              var token = this.getNextToken (i);              var token = this.getNextToken ();
240              log ('token: ' + token.type + ' "' + token.value + '"');              log ('token: ' + token.type + ' "' + token.value + '"');
241    
242              if (token.type == 'char') {              if (token.type == 'char') {
243                  // 5. Append a single Text node to the script element node.
244                el.manakaiAppendText (token.value);                el.manakaiAppendText (token.value);
245    
246                // 4.2. Until it returns a token that is not a character token, or
247                // until it stops tokenising.
248              } else if (token.type == 'eof' ||              } else if (token.type == 'eof' ||
249                         (token.type == 'end-tag' && token.value == 'script')) {                         token.type == 'end-tag' ||
250                           token.type == 'abort') {
251                  // 6. Switched back to the PCDATA state.
252                this.parseMode = 'pcdata';                this.parseMode = 'pcdata';
253    
254                  // 7.1. If the next token is not an end tag token with ...
255                  if (!(token.type == 'end-tag' && token.value == 'script')) {
256                    // 7.2. This is a parse error.
257                    log ('Parse error: no </' + 'script>');
258    
259                    // 7.3. Mark the script element as "already executed".
260                    el.manakaiAlreadyExecuted = true;
261                  } else {
262                    // 7.4. Ignore it.
263                    //
264                  }
265                break;                break;
266              }              }
267            }            }
268    
269              // 8.1. If the parser were originally created for the ...
270              if (this.fragmentParsingMode) {
271                // 8.2. Mark the script element as "already executed" and ...
272                el.alreadyExecuted = true;
273                continue;
274              }
275    
276              // 9.1. Let the old insertion point have the same value as the ...
277              var oldInsertionPoint = this.insertionPoint;
278              // 9.2. Let the insertion point be just before the next input ...
279              this.setInsertionPoint (0);
280    
281              // 10. Append the new element to the current node.
282              this.openElements[this.openElements.length - 1].appendChild (el);
283    
284              // 11. Let the insertion point have the value of the old ...
285    
286              oldInsertionPoint += this.insertionPoint;
287              this.setInsertionPoint (oldInsertionPoint);
288    
289              // 12. If there is a script that will execute as soon as ...
290              while (this.scriptExecutedWhenParserResumes) {
291                // 12.1. If the tree construction stage is being called reentrantly
292                if (this.reentrant) {
293                  log ('parse: abort (reentrance)');
294                  logIndentLevel--;
295                  return;
296    
297                // 12.2. Otherwise
298                } else {
299                  // 1.
300                  var script = this.scriptExecutedWhenParserResumes;
301                  this.scriptExecutedWhenParserResumes = null;
302    
303                  // 2. Pause until the script has completed loading.
304                  //
305    
306                  // 3. Let the insertion point to just before the next input char.
307                  this.setInsertionPoint (0);
308    
309                  // 4. Execute the script.
310                  executeScript (this.doc, script);
311    
312                  // 5. Let the insertion point be undefined again.
313                  this.setInsertionPoint (undefined);
314    
315                  // 6. If there is once again a script that will execute ...
316                  //
317                }
318              }
319            } else if (token.value == 'style' ||
320                       token.value == 'noscript' ||
321                       token.value == 'xmp') {
322              // 1. Create an element for the token in the HTML namespace.
323              var el = new JSElement (this.doc, token.value);
324    
325              // 2. Append the new element to the current node.
326            this.openElements[this.openElements.length - 1].appendChild (el);            this.openElements[this.openElements.length - 1].appendChild (el);
327    
328              // 3. Switch the tokeniser's content model flag to the CDATA state.
329              this.parseMode = 'cdata';
330              this.endTagName = token.value;
331    
332              // 4.1. Collect all the character tokens.
333              while (true) {
334                var token = this.getNextToken ();
335                log ('token: ' + token.type + ' "' + token.value + '"');
336    
337                if (token.type == 'char') {
338                  // 5. Append a single Text node to the script element node.
339                  el.manakaiAppendText (token.value);
340