r105535 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r105534‎ | r105535 | r105536 >
Date:10:37, 8 December 2011
Author:gwicke
Status:deferred
Tags:
Comment:
Rename html5TokenEmitter to HTML5TreeBuilder, and the contained Tokenizer to
TreeBuilder.
Modified paths:
  • /trunk/extensions/VisualEditor/modules/parser/html5/parser.js (modified) (history)
  • /trunk/extensions/VisualEditor/modules/parser/mediawiki.HTML5TreeBuilder.node.js (added) (history)
  • /trunk/extensions/VisualEditor/modules/parser/mediawiki.html5TokenEmitter.js (deleted) (history)
  • /trunk/extensions/VisualEditor/tests/parser/parserTests.js (modified) (history)

Diff [purge]

Index: trunk/extensions/VisualEditor/tests/parser/parserTests.js
@@ -146,7 +146,7 @@
147147 _import(pj('parser', 'mediawiki.parser.environment.js'), ['MWParserEnvironment']);
148148 _import(pj('parser', 'ext.cite.taghook.ref.js'), ['MWRefTagHook']);
149149
150 -_import(pj('parser', 'mediawiki.html5TokenEmitter.js'), ['FauxHTML5']);
 150+_import(pj('parser', 'mediawiki.HTML5TreeBuilder.node.js'), ['FauxHTML5']);
151151 _import(pj('parser', 'mediawiki.DOMPostProcessor.js'), ['DOMPostProcessor']);
152152
153153 // WikiDom and serializers
@@ -346,9 +346,9 @@
347347 failTreeTests = 0,
348348 failOutputTests = 0;
349349
 350+var postProcessor = new DOMPostProcessor();
350351 function processTest(item) {
351 - var tokenizer = new FauxHTML5.Tokenizer(),
352 - postProcessor = new DOMPostProcessor();
 352+ var treeBuilder = new FauxHTML5.TreeBuilder();
353353 if (!('title' in item)) {
354354 console.log(item);
355355 throw new Error('Missing title from test case.');
@@ -397,14 +397,13 @@
398398
399399 // Build a DOM tree from tokens using the HTML tree
400400 // builder/parser.
401 - processTokens(tokens, tokenizer);
 401+ processTokens(tokens, treeBuilder);
402402
403403 // Perform post-processing on DOM.
404 - postProcessor.doPostProcess(tokenizer.parser.document);
 404+ postProcessor.doPostProcess(treeBuilder.parser.document);
405405
406406 // And serialize the result.
407 - var out = tokenizer.parser.document
408 - .getElementsByTagName('body')[0]
 407+ var out = treeBuilder.body()
409408 .innerHTML;
410409
411410 if ( err ) {
@@ -485,15 +484,15 @@
486485 });
487486 }
488487
489 -function processTokens ( tokens, tokenizer ) {
 488+function processTokens ( tokens, treeBuilder ) {
490489 // push a body element, just to be sure to have one
491 - tokenizer.processToken({type: 'TAG', name: 'body'});
 490+ treeBuilder.processToken({type: 'TAG', name: 'body'});
492491 // Process all tokens
493492 for (var i = 0, length = tokens.length; i < length; i++) {
494 - tokenizer.processToken(tokens[i]);
 493+ treeBuilder.processToken(tokens[i]);
495494 }
496495 // And signal the end
497 - tokenizer.processToken({type: 'END'});
 496+ treeBuilder.processToken({type: 'END'});
498497 }
499498
500499 var comments = [];
Index: trunk/extensions/VisualEditor/modules/parser/mediawiki.html5TokenEmitter.js
@@ -1,68 +0,0 @@
2 -var events = require('events');
3 -
4 -
5 -var HTML5 = require('./html5/index');
6 -
7 -FauxHTML5 = {};
8 -
9 -
10 -FauxHTML5.Tokenizer = function ( ) {
11 - this.parser = new HTML5.Parser();
12 - this.parser.parse(this);
13 - return this;
14 -};
15 -
16 -FauxHTML5.Tokenizer.prototype = new events.EventEmitter();
17 -
18 -FauxHTML5.Tokenizer.prototype.processToken = function (token) {
19 - var att = function (maybeAttribs) {
20 - if ( $.isArray(maybeAttribs) ) {
21 - var atts = [];
22 - for(var i = 0, length = maybeAttribs.length; i < length; i++) {
23 - var att = maybeAttribs[i];
24 - atts.push({nodeName: att[0], nodeValue: att[1]});
25 - }
26 - return atts;
27 - } else {
28 - return [];
29 - }
30 - };
31 -
32 - switch (token.type) {
33 - case "TEXT":
34 - this.emit('token', {type: 'Characters', data: token.value});
35 - break;
36 - case "TAG":
37 - this.emit('token', {type: 'StartTag',
38 - name: token.name,
39 - data: att(token.attribs)});
40 - break;
41 - case "ENDTAG":
42 - this.emit('token', {type: 'EndTag',
43 - name: token.name,
44 - data: att(token.attribs)});
45 - break;
46 - case "SELFCLOSINGTAG":
47 - this.emit('token', {type: 'StartTag',
48 - name: token.name,
49 - data: att(token.attribs)});
50 - break;
51 - case "COMMENT":
52 - this.emit('token', {type: 'Comment',
53 - data: token.value});
54 - break;
55 - case "END":
56 - this.emit('end');
57 - break;
58 - case "NEWLINE":
59 - //this.emit('end');
60 - break;
61 - default:
62 - console.log("Unhandled token: " + JSON.stringify(token));
63 - break;
64 - }
65 -};
66 -
67 -if (typeof module == "object") {
68 - module.exports.FauxHTML5 = FauxHTML5;
69 -}
Index: trunk/extensions/VisualEditor/modules/parser/mediawiki.HTML5TreeBuilder.node.js
@@ -0,0 +1,79 @@
 2+var events = require('events');
 3+
 4+
 5+var HTML5 = require('./html5/index');
 6+
 7+FauxHTML5 = {};
 8+
 9+
 10+FauxHTML5.TreeBuilder = function ( ) {
 11+ // The parser we are going to emit our tokens to
 12+ this.parser = new HTML5.Parser();
 13+
 14+ // Sets up the parser
 15+ this.parser.parse(this);
 16+ this.document = this.parser.document;
 17+ return this;
 18+};
 19+
 20+FauxHTML5.TreeBuilder.prototype = new events.EventEmitter();
 21+
 22+// Adapt the token format to internal HTML tree builder format, call the actual
 23+// html tree builder by emitting the token.
 24+FauxHTML5.TreeBuilder.prototype.processToken = function (token) {
 25+ var att = function (maybeAttribs) {
 26+ if ( $.isArray(maybeAttribs) ) {
 27+ var atts = [];
 28+ for(var i = 0, length = maybeAttribs.length; i < length; i++) {
 29+ var att = maybeAttribs[i];
 30+ atts.push({nodeName: att[0], nodeValue: att[1]});
 31+ }
 32+ return atts;
 33+ } else {
 34+ return [];
 35+ }
 36+ };
 37+
 38+ switch (token.type) {
 39+ case "TEXT":
 40+ this.emit('token', {type: 'Characters', data: token.value});
 41+ break;
 42+ case "TAG":
 43+ this.emit('token', {type: 'StartTag',
 44+ name: token.name,
 45+ data: att(token.attribs)});
 46+ break;
 47+ case "ENDTAG":
 48+ this.emit('token', {type: 'EndTag',
 49+ name: token.name,
 50+ data: att(token.attribs)});
 51+ break;
 52+ case "SELFCLOSINGTAG":
 53+ this.emit('token', {type: 'StartTag',
 54+ name: token.name,
 55+ data: att(token.attribs)});
 56+ break;
 57+ case "COMMENT":
 58+ this.emit('token', {type: 'Comment',
 59+ data: token.value});
 60+ break;
 61+ case "END":
 62+ this.emit('end');
 63+ break;
 64+ case "NEWLINE":
 65+ //this.emit('end');
 66+ break;
 67+ default:
 68+ console.log("Unhandled token: " + JSON.stringify(token));
 69+ break;
 70+ }
 71+};
 72+
 73+FauxHTML5.TreeBuilder.prototype.body = function () {
 74+ return this.parser.document.getElementsByTagName('body')[0];
 75+}
 76+
 77+
 78+if (typeof module == "object") {
 79+ module.exports.FauxHTML5 = FauxHTML5;
 80+}
Property changes on: trunk/extensions/VisualEditor/modules/parser/mediawiki.HTML5TreeBuilder.node.js
___________________________________________________________________
Added: svn:eol-style
181 + native
Index: trunk/extensions/VisualEditor/modules/parser/html5/parser.js
@@ -5,7 +5,7 @@
66 var events = require('events');
77
88 require('./treebuilder');
9 -require('../mediawiki.html5TokenEmitter');
 9+require('../mediawiki.HTML5TreeBuilder.node');
1010
1111 var Phase = require('./parser/phase').Phase;
1212

Status & tagging log