/[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.4 by wakaba, Sun Apr 20 12:19:13 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    function log (s) {    function log (s) {
# Line 32  Line 34 
34      this.s = s;      this.s = s;
35    } // InputStream    } // InputStream
36    
37    function Parser (i) {    function Parser (i, doc) {
38      this.parseMode = 'pcdata';      this.parseMode = 'pcdata';
39      this.doc = new JSDocument (this);      if (!doc) {
40      this.openElements = [this.doc];        doc = new JSDocument (this);
41          doc.manakaiIsHTML = true;
42        }
43        this.doc = doc;
44        this.openElements = [doc];
45      this.in = i;      this.in = i;
46        this.scriptsExecutedAfterParsing = [];
47    } // Parser    } // Parser
48    
49    Parser.prototype.getNextToken = function () {    Parser.prototype.getNextToken = function () {
# Line 47  Line 54 
54        if (p.insertionPoint <= 0) {        if (p.insertionPoint <= 0) {
55          return {type: 'abort'};          return {type: 'abort'};
56        }        }
57        i.s = i.s.replace (/^([\s\S]+?)<\/[Ss][Cc][Rr][Ii][Pp][Tt]>/,        i.s = i.s.replace (/^([^<]+)/,
58        function (s, t) {        function (s, t) {
59          if (0 < p.insertionPoint && p.insertionPoint < t.length) {          if (0 < p.insertionPoint && p.insertionPoint < t.length) {
60            token = {type: 'char', value: t.substring (0, p.insertionPoint)};            token = {type: 'char', value: t.substring (0, p.insertionPoint)};
61            var ip = p.insertionPoint;            var ip = p.insertionPoint;
62            p.insertionPoint = 0;            p.insertionPoint = 0;
63            return t.substring (ip, t.length) +            return t.substring (ip, t.length);
               s.substring (s.length - 9, s.length);  
64          }          }
65          token = {type: 'char', value: t};          token = {type: 'char', value: t};
66          p.insertionPoint -= s.length;          p.insertionPoint -= t.length;
67          return '<' + '/script>';          return '';
68        });        });
69        if (token) return token;        if (token) return token;
70        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) {
71          if (s.length < p.insertionPoint) {          if (p.insertionPoint < s.length) {
72            token = {type: 'abort'};            token = {type: 'abort'};
73            return s;            return s;
74          }          }
# Line 71  Line 77 
77          return '';          return '';
78        });        });
79        if (token) return token;        if (token) return token;
80          i.s = i.s.replace (/^</,
81          function (s) {
82            token = {type: 'char', value: s};
83            p.insertionPoint -= s.length;
84            return '';
85          });
86          if (token) return token;
87        return {type: 'eof'};        return {type: 'eof'};
88      }      }
89    
# Line 90  Line 103 
103          token = {type: 'abort'};          token = {type: 'abort'};
104          return s;          return s;
105        }        }
106        token = {type: 'start-tag', value: e.toLowerCase ()};        var tagName;
107          var attrs = {};
108          e = e.replace (/^[\S]+/, function (v) {
109            tagName = v.toLowerCase ();
110            return '';
111          });
112          e = e.replace (/^\s*(\S+)\s*(?:=\s*"([^"]*)"|'([^']*)'|([^"']+))?/,
113          function (x, attrName, attrValue1, attrValue2, attrValue3) {
114            attrs[attrName] = attrValue1 || attrValue2 || attrValue3;
115            return '';
116          });
117          token = {type: 'start-tag', value: tagName, attrs: attrs};
118        p.insertionPoint -= s.length;        p.insertionPoint -= s.length;
119        return '';        return '';
120      });      });
# Line 130  Line 154 
154          if (token.value == 'script') {          if (token.value == 'script') {
155            // 1. Create an element for the token in the HTML namespace.            // 1. Create an element for the token in the HTML namespace.
156            var el = new JSElement (this.doc, token.value);            var el = new JSElement (this.doc, token.value);
157              if (token.attrs.async != null) el.async = true;
158              if (token.attrs.defer != null) el.defer = true;
159              if (token.attrs.src != null) el.src = token.attrs.src;
160    
161            // 2. Mark the element as being "parser-inserted".            // 2. Mark the element as being "parser-inserted".
162            el.manakaiParserInserted = true;            el.manakaiParserInserted = true;
# Line 214  Line 241 
241      }      }
242    
243      log ('stop parsing');      log ('stop parsing');
244    
245        // readyState = 'interactive'
246    
247        // "When a script completes loading" rules start applying.
248    
249        // TODO: Handles "list of scripts that will execute as soon as possible"
250        // and "list of scripts that will execute asynchronously"
251    
252        // Handle "list of scripts that will execute when the document has finished
253        // parsing".
254        var list = this.scriptsExecutedAfterParsing;
255        while (list.length > 0) {
256          // TODO: break unless completed loading
257    
258          // Step 1.
259          //
260    
261          // Step 2. and Step 3.
262          log ('Executing a |defer|red script...');
263          executeScript (this.doc, list.shift ());
264    
265          // Step 4.
266        }
267    
268        log ('DOMContentLoaded event fired');
269    
270        // "delays tha load event" things has completed:
271        // readyState = 'complete'
272        log ('load event fired');
273    } // parse    } // parse
274    
275    Parser.prototype.setInsertionPoint = function (ip) {    Parser.prototype.setInsertionPoint = function (ip) {
276      if (ip == undefined || ip == null || isNaN (ip)) {      if (ip == undefined || ip == null || isNaN (ip)) {
277        log ('insertion point: set to undefined');        log ('insertion point: set to undefined');
278        this.insertionPoint = undefined;        this.insertionPoint = undefined;
279        } else if (ip == this.in.s.length) {
280          log ('insertion point: end of file');
281          this.insertionPoint = ip;
282      } else {      } else {
283        log ('insertion point: set to ' + ip +        log ('insertion point: set to ' + ip +
284             ' (before "' + this.in.s.substring (0, 10) + '")');             ' (before "' + this.in.s.substring (0, 10) + '")');
# Line 244  Line 303 
303      e.parentNode = this;      e.parentNode = this;
304    
305      if (e.localName == 'script') {      if (e.localName == 'script') {
306        log ('start running a script');        log ('Running a script: start');
307    
308        var doc = this.ownerDocument || this;        var doc = this.ownerDocument || this;
309        var p = doc._parser;        var p = doc._parser;
# Line 261  Line 320 
320        // 2.4. If the script element has its "already executed" flag set        // 2.4. If the script element has its "already executed" flag set
321        if (e.manakaiAlreadyExecuted) {        if (e.manakaiAlreadyExecuted) {
322          // 2.5. Abort these steps at this point.          // 2.5. Abort these steps at this point.
323          log ('running a script: aborted');          log ('Running a script: aborted');
324          return e;          return e;
325        }        }
326    
# Line 276  Line 335 
335        // 5.1.        // 5.1.
336        if (/* TODO: If the document is still being parsed && */        if (/* TODO: If the document is still being parsed && */
337            e.defer && !e.async) {            e.defer && !e.async) {
338          // TODO          p.scriptsExecutedAfterParsing.push (e);
339            log ('Running a script: aborted (defer)');
340        } else if (e.async && e.src != null) {        } else if (e.async && e.src != null) {
341          // TODO          // TODO
342        } else if (e.async && e.src == null        } else if (e.async && e.src == null
# Line 290  Line 350 
350          executeScript (doc, e); // even if other scripts are already executing.          executeScript (doc, e); // even if other scripts are already executing.
351        }        }
352    
353        log ('end running a script');        log ('Running a script: end');
354      }      }
355    
356      return e;      return e;
# Line 357  Line 417 
417      }      }
418    }; // manakaiAppendText    }; // manakaiAppendText
419    
420      JSDocument.prototype.open = function () {
421        // Two or fewer arguments
422    
423        // Step 1.
424        var type = arguments[0] || 'text/html';
425        
426        // Step 2.
427        var replace = arguments[1] == 'replace';
428    
429        // Step 3.
430        if (this._parser &&
431            !this._parser.scriptCreated &&
432            this._parser.in.insertionPoint != undefined) {
433          log ('document.open () in parsing mode is ignored');
434          return this;
435        }
436    
437        // Step 4.
438        log ('onbeforeunload event fired');
439        log ('onunload event fired');
440    
441        // Step 5.
442        if (this._parser) {
443          // Discard the parser.
444        }
445    
446        // Step 6.
447        log ('document cleared by document.open ()');
448        this.childNodes = [];
449    
450        // Step 7.
451        this._parser = new Parser (new InputStream (''), this);
452        this._parser.scriptCreated = true;
453    
454        // Step 8.
455        this.manakaiIsHTML = true;
456    
457        // Step 9.
458        // If not text/html, ...
459    
460        // Step 10.
461        if (!replace) {
462          // History      
463        }
464    
465        // Step 11.
466        this._parser.setInsertionPoint (this._parser.in.s.length);
467    
468        // Step 12.
469        return this;
470      }; // document.open
471    
472    JSDocument.prototype.write = function () {    JSDocument.prototype.write = function () {
473      var p = this._parser;      var p = this._parser;
474    
475      // 1. If the insertion point is undefined, the open() method must be ...      // 1. If the insertion point is undefined, the open() method must be ...
476      if (p.insertionPoint == NaN || p.insertionPoint == undefined) {      if (isNaN (p.insertionPoint) || p.insertionPoint == undefined) {
477        // TODO: open ()        this.open ();
478          p = this._parser;
479      }      }
480    
481      // 2. ... inserted into the input stream just before the insertion point.      // 2. ... inserted into the input stream just before the insertion point.
# Line 400  Line 513 
513        var node = n.childNodes[i];        var node = n.childNodes[i];
514        if (node instanceof JSElement) {        if (node instanceof JSElement) {
515          r += '| ' + indent + node.localName + '\n';          r += '| ' + indent + node.localName + '\n';
516            if (node.async) r += '| ' + indent + '  async=""\n';
517            if (node.defer) r += '| ' + indent + '  defer=""\n';
518            if (node.src) r += '| ' + indent + '  src="' + node.src + '"\n';
519          r += dumpTree (node, indent + '  ');          r += dumpTree (node, indent + '  ');
520        } else if (node instanceof JSText) {        } else if (node instanceof JSText) {
521          r += '| ' + indent + '"' + node.data + '"\n';          r += '| ' + indent + '"' + node.data + '"\n';

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

admin@suikawiki.org
ViewVC Help
Powered by ViewVC 1.1.24