/[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.3 by wakaba, Sun Apr 20 10:02:43 2008 UTC revision 1.6 by wakaba, Fri Apr 25 13:42:51 2008 UTC
# Line 13  Line 13 
13    output {    output {
14      display: block;      display: block;
15      font-family: monospace;      font-family: monospace;
16      white-space: pre;      white-space: -moz-pre-wrap;
17        white-space: pre-wrap;
18    }    }
19  </style>  </style>
20  <script>  <script>
21    function update () {    function update () {
22      document.logElement.textContent = '';      document.logElement.textContent = '';
23      var p = new Parser (new InputStream (document.sourceElement.value));      var p = new Parser (new InputStream (document.sourceElement.value));
24        var doc = p.doc;
25      p.parse ();      p.parse ();
26      log (dumpTree (p.doc, ''));      log (dumpTree (doc, ''));
27    } // update    } // update
28    
29      var logIndentLevel = 0;
30    function log (s) {    function log (s) {
31        for (var i = 0; i < logIndentLevel; i++) {
32          s = '  ' + s;
33        }
34      document.logElement.appendChild (document.createTextNode (s + "\n"));      document.logElement.appendChild (document.createTextNode (s + "\n"));
35    } // log    } // log
36    
# Line 32  Line 38 
38      this.s = s;      this.s = s;
39    } // InputStream    } // InputStream
40    
41    function Parser (i) {    function Parser (i, doc) {
42      this.parseMode = 'pcdata';      this.parseMode = 'pcdata';
43      this.doc = new JSDocument (this);      if (!doc) {
44      this.openElements = [this.doc];        doc = new JSDocument (this);
45          doc.manakaiIsHTML = true;
46        }
47        this.doc = doc;
48        this.openElements = [doc];
49      this.in = i;      this.in = i;
50        this.scriptsExecutedAfterParsing = [];
51    } // Parser    } // Parser
52    
53    Parser.prototype.getNextToken = function () {    Parser.prototype.getNextToken = function () {
# Line 47  Line 58 
58        if (p.insertionPoint <= 0) {        if (p.insertionPoint <= 0) {
59          return {type: 'abort'};          return {type: 'abort'};
60        }        }
61        i.s = i.s.replace (/^([\s\S]+?)<\/[Ss][Cc][Rr][Ii][Pp][Tt]>/,        i.s = i.s.replace (/^([^<]+)/,
62        function (s, t) {        function (s, t) {
63          if (0 < p.insertionPoint && p.insertionPoint < t.length) {          if (0 < p.insertionPoint && p.insertionPoint < t.length) {
64            token = {type: 'char', value: t.substring (0, p.insertionPoint)};            token = {type: 'char', value: t.substring (0, p.insertionPoint)};
65            var ip = p.insertionPoint;            var ip = p.insertionPoint;
66            p.insertionPoint = 0;            p.insertionPoint = 0;
67            return t.substring (ip, t.length) +            return t.substring (ip, t.length);
               s.substring (s.length - 9, s.length);  
68          }          }
69          token = {type: 'char', value: t};          token = {type: 'char', value: t};
70          p.insertionPoint -= s.length;          p.insertionPoint -= t.length;
71          return '<' + '/script>';          return '';
72        });        });
73        if (token) return token;        if (token) return token;
74        i.s = i.s.replace (/^<\/[Ss][Cc][Rr][Ii][Pp][Tt]>/, function (s) {        i.s = i.s.replace (/^<\/[Ss][Cc][Rr][Ii][Pp][Tt]>/, function (s) {
75          if (s.length < p.insertionPoint) {          if (p.insertionPoint < s.length) {
76            token = {type: 'abort'};            token = {type: 'abort'};
77            return s;            return s;
78          }          }
# Line 71  Line 81 
81          return '';          return '';
82        });        });
83        if (token) return token;        if (token) return token;
84          var m;
85          if ((p.insertionPoint < '</script'.length) &&
86              (m = i.s.match (/^<\/([SCRIPTscript]+)/))) {
87            var v = m[1].substring (0, p.insertionPoint).toLowerCase ();
88            if (v == 'script'.substring (0, p.insertionPoint - '</'.length)) {
89              return {type: 'abort'};
90            }
91          }
92          i.s = i.s.replace (/^</,
93          function (s) {
94            token = {type: 'char', value: s};
95            p.insertionPoint -= s.length;
96            return '';
97          });
98          if (token) return token;
99        return {type: 'eof'};        return {type: 'eof'};
100      }      }
101    
102      var token;      var token;
103      i.s = i.s.replace (/^<\/([^>]+)>/, function (s, e) {      i.s = i.s.replace (/^<\/([^>]+)(?:>|$)/, function (s, e) {
104        if (p.insertionPoint < s.length) {        if (p.insertionPoint < s.length ||
105              (p.insertionPoint <= s.length &&
106               s.substring (s.length - 1, 1) != '>')) {
107          token = {type: 'abort'};          token = {type: 'abort'};
108          return s;          return s;
109        }        }
# Line 85  Line 112 
112        return '';        return '';
113      });      });
114      if (token) return token;      if (token) return token;
115      i.s = i.s.replace (/^<([^>]+)>/, function (s, e) {      i.s = i.s.replace (/^<([^>]+)(?:>|$)/, function (s, e) {
116        if (p.insertionPoint < s.length) {        if (p.insertionPoint < s.length ||
117              (p.insertionPoint <= s.length &&
118               s.substring (s.length - 1, 1) != '>')) {
119          token = {type: 'abort'};          token = {type: 'abort'};
120          return s;          return s;
121        }        }
122        token = {type: 'start-tag', value: e.toLowerCase ()};        var tagName;
123          var attrs = {};
124          e = e.replace (/^[\S]+/, function (v) {
125            tagName = v.toLowerCase ();
126            return '';
127          });
128          e = e.replace (/^\s*([^\s=]+)\s*(?:=\s*(?:"([^"]*)"|'([^']*)'|([^"']+)))?/,
129          function (x, attrName, attrValue1, attrValue2, attrValue3) {
130            v = attrValue1 || attrValue2 || attrValue3;
131            v = v.replace (/&quot;/g, '"').replace (/&apos;/g, "'")
132                .replace (/&amp;/g, '&');
133            attrs[attrName.toLowerCase ()] = v;
134            return '';
135          });
136          if (e.length) {
137            log ('Broken start tag: "' + e + '"');
138          }
139          token = {type: 'start-tag', value: tagName, attrs: attrs};
140        p.insertionPoint -= s.length;        p.insertionPoint -= s.length;
141        return '';        return '';
142      });      });
# Line 120  Line 166 
166    } // getNextToken    } // getNextToken
167    
168    Parser.prototype.parse = function () {    Parser.prototype.parse = function () {
169      log ('start parsing');      logIndentLevel++;
170        log ('parse: start');
171    
172      while (true) {      while (true) {
173        var token = this.getNextToken ();        var token = this.getNextToken ();
# Line 130  Line 177 
177          if (token.value == 'script') {          if (token.value == 'script') {
178            // 1. Create an element for the token in the HTML namespace.            // 1. Create an element for the token in the HTML namespace.
179            var el = new JSElement (this.doc, token.value);            var el = new JSElement (this.doc, token.value);
180              if (token.attrs.async != null) el.async = true;
181              if (token.attrs.defer != null) el.defer = true;
182              if (token.attrs.src != null) el.src = token.attrs.src;
183    
184            // 2. Mark the element as being "parser-inserted".            // 2. Mark the element as being "parser-inserted".
185            el.manakaiParserInserted = true;            el.manakaiParserInserted = true;
# Line 185  Line 235 
235            this.openElements[this.openElements.length - 1].appendChild (el);            this.openElements[this.openElements.length - 1].appendChild (el);
236    
237            // 11. Let the insertion point have the value of the old ...            // 11. Let the insertion point have the value of the old ...
238              oldInsertionPoint += this.insertionPoint;
239            this.setInsertionPoint (oldInsertionPoint);            this.setInsertionPoint (oldInsertionPoint);
240    
241            // 12. If there is a script that will execute as soon as ...            // 12. If there is a script that will execute as soon as ...
242                        while (this.scriptExecutedWhenParserResumes) {
243                // 12.1. If the tree construction stage is being called reentrantly
244                if (this.reentrant) {
245                  log ('parse: abort (reentrance)');
246                  logIndentLevel--;
247                  return;
248    
249                // 12.2. Otherwise
250                } else {
251                  // 1.
252                  var script = this.scriptExecutedWhenParserResumes;
253                  this.scriptExecutedWhenParserResumes = null;
254    
255                  // 2. Pause until the script has completed loading.
256                  //
257    
258                  // 3. Let the insertion point to just before the next input char.
259                  this.setInsertionPoint (0);
260    
261                  // 4. Execute the script.
262                  executeScript (this.doc, script);
263    
264                  // 5. Let the insertion point be undefined again.
265                  this.setInsertionPoint (undefined);
266    
267                  // 6. If there is once again a script that will execute ...
268                  //
269                }
270              }
271          } else {          } else {
272            var el = new JSElement (this.doc, token.value);            var el = new JSElement (this.doc, token.value);
273            this.openElements[this.openElements.length - 1].appendChild (el);            this.openElements[this.openElements.length - 1].appendChild (el);
# Line 209  Line 287 
287          break;          break;
288        } else if (token.type == 'abort') {        } else if (token.type == 'abort') {
289          log ('parse: abort');          log ('parse: abort');
290            logIndentLevel--;
291          return;          return;
292        }        }
293      }      }
294    
295      log ('stop parsing');      log ('stop parsing');
296    
297        // readyState = 'interactive'
298    
299        // "When a script completes loading" rules start applying.
300    
301        // TODO: Handles "list of scripts that will execute as soon as possible"
302        // and "list of scripts that will execute asynchronously"
303    
304        // Handle "list of scripts that will execute when the document has finished
305        // parsing".
306        var list = this.scriptsExecutedAfterParsing;
307        while (list.length > 0) {
308          // TODO: break unless completed loading
309    
310          // Step 1.
311          //
312    
313          // Step 2. and Step 3.
314          log ('Executing a |defer|red script...');
315          executeScript (this.doc, list.shift ());
316    
317          // Step 4.
318        }
319    
320        log ('DOMContentLoaded event fired');
321    
322        // "delays tha load event" things has completed:
323        // readyState = 'complete'
324        log ('load event fired');
325    
326        logIndentLevel--;
327    } // parse    } // parse
328    
329    Parser.prototype.setInsertionPoint = function (ip) {    Parser.prototype.setInsertionPoint = function (ip) {
330      if (ip == undefined || ip == null || isNaN (ip)) {      if (ip == undefined || ip == null || isNaN (ip)) {
331        log ('insertion point: set to undefined');        log ('insertion point: set to undefined');
332        this.insertionPoint = undefined;        this.insertionPoint = undefined;
333        } else if (ip == this.in.s.length) {
334          log ('insertion point: end of file');
335          this.insertionPoint = ip;
336      } else {      } else {
337        log ('insertion point: set to ' + ip +        log ('insertion point: set to ' + ip +
338             ' (before "' + this.in.s.substring (0, 10) + '")');             ' (before "' + this.in.s.substring (0, 10) + '")');
# Line 244  Line 357 
357      e.parentNode = this;      e.parentNode = this;
358    
359      if (e.localName == 'script') {      if (e.localName == 'script') {
360        log ('start running a script');        logIndentLevel++;
361          log ('Running a script: start');
362    
363        var doc = this.ownerDocument || this;        var doc = this.ownerDocument || this;
364        var p = doc._parser;        var p = doc._parser;
# Line 261  Line 375 
375        // 2.4. If the script element has its "already executed" flag set        // 2.4. If the script element has its "already executed" flag set
376        if (e.manakaiAlreadyExecuted) {        if (e.manakaiAlreadyExecuted) {
377          // 2.5. Abort these steps at this point.          // 2.5. Abort these steps at this point.
378          log ('running a script: aborted');          log ('Running a script: aborted');
379            logIndentLevel--;
380          return e;          return e;
381        }        }
382    
# Line 276  Line 391 
391        // 5.1.        // 5.1.
392        if (/* TODO: If the document is still being parsed && */        if (/* TODO: If the document is still being parsed && */
393            e.defer && !e.async) {            e.defer && !e.async) {
394          // TODO          p.scriptsExecutedAfterParsing.push (e);
395            log ('Running a script: aborted (defer)');
396        } else if (e.async && e.src != null) {        } else if (e.async && e.src != null) {
397          // TODO          // TODO
398        } else if (e.async && e.src == null        } else if (e.async && e.src == null
399                   /* && list of scripts that will execute asynchronously is not empty */) {                   /* && list of scripts that will execute asynchronously is not empty */) {
400          // TODO          // TODO
401        } else if (e.src != null && e.manakaiParserInserted) {        } else if (e.src != null && e.manakaiParserInserted) {
402          // TODO          if (p.scriptExecutedWhenParserResumes) {
403              log ('Error: There is a script that will execute as soon as the parser resumes.');
404            }
405            p.scriptExecutedWhenParserResumes = e;
406            log ('Running a script: aborted (src)');
407        } else if (e.src != null) {        } else if (e.src != null) {
408          // TODO          // TODO
409        } else {        } else {
410          executeScript (doc, e); // even if other scripts are already executing.          executeScript (doc, e); // even if other scripts are already executing.
411        }        }
412    
413        log ('end running a script');        log ('Running a script: end');
414          logIndentLevel--;
415      }      }
416    
417      return e;      return e;
# Line 299  Line 420 
420    function executeScript (doc, e) {    function executeScript (doc, e) {
421      log ('executing a script block: start');      log ('executing a script block: start');
422    
423      // If the load resulted in an error, then ... firing an error event ...      var s;
424        if (e.src != null) {
425          s = getExternalScript (e.src);
426    
427          // If the load resulted in an error, then ... firing an error event ...
428          if (s == null) {
429            log ('error event fired at the script element');
430            return;
431          }
432    
433          log ('External script loaded: "' + s + '"');
434        } else {
435          s = e.text;
436        }
437    
438      // If the load was successful      // If the load was successful
439      log ('load event fired at the script element');      log ('load event fired at the script element');
# Line 308  Line 442 
442      // Scripting is enabled, Document.designMode is disabled,      // Scripting is enabled, Document.designMode is disabled,
443      // Document is the active document in its browsing context      // Document is the active document in its browsing context
444    
       var s;  
       if (e.src != null) {  
         // TODO: from external file  
       } else {  
         s = e.text;  
       }  
   
445        parseAndRunScript (doc, s);        parseAndRunScript (doc, s);
446      }      }
447    
448      log ('executing a script block: end');      log ('executing a script block: end');
449    } // executeScript    } // executeScript
450    
451      function getExternalScript (uri) {
452        if (uri.match (/^javascript:/i)) {
453          var m;
454          if (m = uri.match (/^javascript:\s*(?:'([^']*)'|"([^"]+)")\s*$/i)) {
455            if (m[1]) {
456              return m[1];
457            } else if (m[2]) {
458              return m[2];
459            } else {
460              return null;
461            }
462          } else {
463            log ('Complex javascript: URI is not supported: <' + uri + '>');
464            return null;
465          }
466        } else {
467          log ('URI scheme not supported: <' + uri + '>');
468          return null;
469        }
470      } // getExternalScript
471    
472    function parseAndRunScript (doc, s) {    function parseAndRunScript (doc, s) {
473      while (true) {      while (true) {
474        var matched = false;        var matched = false;
# Line 357  Line 505 
505      }      }
506    }; // manakaiAppendText    }; // manakaiAppendText
507    
508      JSDocument.prototype.open = function () {
509        // Two or fewer arguments
510    
511        // Step 1.
512        var type = arguments[0] || 'text/html';
513        
514        // Step 2.
515        var replace = arguments[1] == 'replace';
516    
517        // Step 3.
518        if (this._parser &&
519            !this._parser.scriptCreated &&
520            this._parser.in.insertionPoint != undefined) {
521          log ('document.open () in parsing mode is ignored');
522          return this;
523        }
524    
525        // Step 4.
526        log ('onbeforeunload event fired');
527        log ('onunload event fired');
528    
529        // Step 5.
530        if (this._parser) {
531          // Discard the parser.
532        }
533    
534        // Step 6.
535        log ('document cleared by document.open ()');
536        this.childNodes = [];
537    
538        // Step 7.
539        this._parser = new Parser (new InputStream (''), this);
540        this._parser.scriptCreated = true;
541    
542        // Step 8.
543        this.manakaiIsHTML = true;
544    
545        // Step 9.
546        // If not text/html, ...
547    
548        // Step 10.
549        if (!replace) {
550          // History      
551        }
552    
553        // Step 11.
554        this._parser.setInsertionPoint (this._parser.in.s.length);
555    
556        // Step 12.
557        return this;
558      }; // document.open
559    
560    JSDocument.prototype.write = function () {    JSDocument.prototype.write = function () {
561        logIndentLevel++;
562    
563      var p = this._parser;      var p = this._parser;
564    
565      // 1. If the insertion point is undefined, the open() method must be ...      // 1. If the insertion point is undefined, the open() method must be ...
566      if (p.insertionPoint == NaN || p.insertionPoint == undefined) {      if (isNaN (p.insertionPoint) || p.insertionPoint == undefined) {
567        // TODO: open ()        this.open ();
568          p = this._parser;
569      }      }
570    
571      // 2. ... inserted into the input stream just before the insertion point.      // 2. ... inserted into the input stream just before the insertion point.
# Line 374  Line 577 
577      p.insertionPoint += s.length;      p.insertionPoint += s.length;
578    
579      // 3. If there is a script that will execute as soon as the parser resumes      // 3. If there is a script that will execute as soon as the parser resumes
580      // TODO      if (p.scriptExecutedAfterParserResumes) {
581          log ('document.write: processed later (there is an unprocessed <script src>)');
582          logIndentLevel--;
583          return;
584        }
585    
586      // 4. Process the characters that were inserted, ...      // 4. Process the characters that were inserted, ...
587        var originalReentrant = p.reentrant;
588        p.reentrant = true;
589      p.parse ();      p.parse ();
590        p.reentrant = originalReentrant;
591        // TODO: "Abort the processing of any nested invokations of the tokeniser,
592        // yielding control back to the caller." (<script> parsing).  Do we need
593        // to do something here?
594    
595      // 5. Return      // 5. Return
596      log ('document.write: return');      log ('document.write: return');
597    
598        logIndentLevel--;
599      return;      return;
600    }; // document.write    }; // document.write
601    
# Line 400  Line 615 
615        var node = n.childNodes[i];        var node = n.childNodes[i];
616        if (node instanceof JSElement) {        if (node instanceof JSElement) {
617          r += '| ' + indent + node.localName + '\n';          r += '| ' + indent + node.localName + '\n';
618            if (node.async) r += '| ' + indent + '  async=""\n';
619            if (node.defer) r += '| ' + indent + '  defer=""\n';
620            if (node.src) r += '| ' + indent + '  src="' + node.src + '"\n';
621          r += dumpTree (node, indent + '  ');          r += dumpTree (node, indent + '  ');
622        } else if (node instanceof JSText) {        } else if (node instanceof JSText) {
623          r += '| ' + indent + '"' + node.data + '"\n';          r += '| ' + indent + '"' + node.data + '"\n';

Legend:
Removed from v.1.3  
changed lines
  Added in v.1.6

admin@suikawiki.org
ViewVC Help
Powered by ViewVC 1.1.24