Skip to content

Commit

Permalink
modernize htmljs, html-tools, blaze-tools, spacebars-compiler & templ…
Browse files Browse the repository at this point in the history
…ating-tools
  • Loading branch information
nathan-muir committed Mar 22, 2021
1 parent 08a8e16 commit 68d463b
Show file tree
Hide file tree
Showing 39 changed files with 743 additions and 693 deletions.
13 changes: 3 additions & 10 deletions packages/blaze-tools/package.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,15 @@ Package.describe({
});

Package.onUse(function (api) {
api.use('underscore@1.0.9');

api.export('BlazeTools');

api.use('ecmascript');
api.use('htmljs@1.1.0-beta.2');

api.addFiles([
'preamble.js',
'tokens.js',
'tojs.js'
]);
api.mainModule('preamble.js');
});

Package.onTest(function (api) {
api.use('tinytest@1.0.11');
api.use('underscore@1.0.9');
api.use('ecmascript');

api.use('blaze-tools');
api.use('html-tools@1.1.0-beta.2');
Expand Down
28 changes: 27 additions & 1 deletion packages/blaze-tools/preamble.js
Original file line number Diff line number Diff line change
@@ -1 +1,27 @@
BlazeTools = {};

import {
EmitCode,
toJSLiteral,
toObjectLiteralKey,
ToJSVisitor,
toJS
} from './tojs';

import {
parseNumber,
parseIdentifierName,
parseExtendedIdentifierName,
parseStringLiteral
} from './tokens';

export const BlazeTools = {
EmitCode,
toJSLiteral,
toObjectLiteralKey,
ToJSVisitor,
toJS,
parseNumber,
parseIdentifierName,
parseExtendedIdentifierName,
parseStringLiteral
};
34 changes: 17 additions & 17 deletions packages/blaze-tools/tojs.js
Original file line number Diff line number Diff line change
@@ -1,50 +1,51 @@
import { HTML } from 'meteor/htmljs';

BlazeTools.EmitCode = function (value) {
if (! (this instanceof BlazeTools.EmitCode))

export function EmitCode (value) {
if (! (this instanceof EmitCode))
// called without `new`
return new BlazeTools.EmitCode(value);
return new EmitCode(value);

if (typeof value !== 'string')
throw new Error('BlazeTools.EmitCode must be constructed with a string');
throw new Error('EmitCode must be constructed with a string');

this.value = value;
};
BlazeTools.EmitCode.prototype.toJS = function (visitor) {
}

EmitCode.prototype.toJS = function (visitor) {
return this.value;
};

// Turns any JSONable value into a JavaScript literal.
toJSLiteral = function (obj) {
export function toJSLiteral (obj) {
// See <http://timelessrepo.com/json-isnt-a-javascript-subset> for `\u2028\u2029`.
// Also escape Unicode surrogates.
return (JSON.stringify(obj)
.replace(/[\u2028\u2029\ud800-\udfff]/g, function (c) {
return '\\u' + ('000' + c.charCodeAt(0).toString(16)).slice(-4);
}));
};
BlazeTools.toJSLiteral = toJSLiteral;
}



var jsReservedWordSet = (function (set) {
_.each("abstract else instanceof super boolean enum int switch break export interface synchronized byte extends let this case false long throw catch final native throws char finally new transient class float null true const for package try continue function private typeof debugger goto protected var default if public void delete implements return volatile do import short while double in static with".split(' '), function (w) {
"abstract else instanceof super boolean enum int switch break export interface synchronized byte extends let this case false long throw catch final native throws char finally new transient class float null true const for package try continue function private typeof debugger goto protected var default if public void delete implements return volatile do import short while double in static with".split(' ').forEach(function (w) {
set[w] = 1;
});
return set;
})({});

toObjectLiteralKey = function (k) {
export function toObjectLiteralKey (k) {
if (/^[a-zA-Z$_][a-zA-Z$0-9_]*$/.test(k) && jsReservedWordSet[k] !== 1)
return k;
return toJSLiteral(k);
};
BlazeTools.toObjectLiteralKey = toObjectLiteralKey;
}

var hasToJS = function (x) {
return x.toJS && (typeof (x.toJS) === 'function');
};

ToJSVisitor = HTML.Visitor.extend();
export const ToJSVisitor = HTML.Visitor.extend();
ToJSVisitor.def({
visitNull: function (nullOrUndefined) {
return 'null';
Expand Down Expand Up @@ -149,8 +150,7 @@ ToJSVisitor.def({
return null;
}
});
BlazeTools.ToJSVisitor = ToJSVisitor;

BlazeTools.toJS = function (content) {
export function toJS (content) {
return (new ToJSVisitor).visit(content);
};
}
5 changes: 4 additions & 1 deletion packages/blaze-tools/token_tests.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import { BlazeTools } from 'meteor/blaze-tools';
import { HTMLTools } from 'meteor/html-tools';

Tinytest.add("blaze-tools - token parsers", function (test) {

var run = function (func, input, expected) {
Expand Down Expand Up @@ -40,7 +43,7 @@ Tinytest.add("blaze-tools - token parsers", function (test) {
runValue(parseNumber, "-0xa", -10);
runValue(parseNumber, "1e+1", 10);

_.each([parseIdentifierName, parseExtendedIdentifierName], function (f) {
[parseIdentifierName, parseExtendedIdentifierName].forEach(function (f) {
run(f, "a", "a");
run(f, "true", "true");
run(f, "null", "null");
Expand Down
20 changes: 10 additions & 10 deletions packages/blaze-tools/tokens.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ var rLineContinuation =
/^\\(\r\n|[\u000A\u000D\u2028\u2029])/;


BlazeTools.parseNumber = function (scanner) {
export function parseNumber (scanner) {
var startPos = scanner.pos;

var isNegative = false;
Expand All @@ -75,9 +75,9 @@ BlazeTools.parseNumber = function (scanner) {
var value = Number(matchText);
value = (isNegative ? -value : value);
return { text: text, value: value };
};
}

BlazeTools.parseIdentifierName = function (scanner) {
export function parseIdentifierName (scanner) {
var startPos = scanner.pos;
var rest = scanner.rest();
var match = rIdentifierPrefix.exec(rest);
Expand Down Expand Up @@ -106,25 +106,25 @@ BlazeTools.parseIdentifierName = function (scanner) {
}

return scanner.input.substring(startPos, scanner.pos);
};
}

BlazeTools.parseExtendedIdentifierName = function (scanner) {
export function parseExtendedIdentifierName (scanner) {
// parse an identifier name optionally preceded by '@'
if (scanner.peek() === '@') {
scanner.pos++;
var afterAt = BlazeTools.parseIdentifierName(scanner);
var afterAt = parseIdentifierName(scanner);
if (afterAt) {
return '@' + afterAt;
} else {
scanner.pos--;
return null;
}
} else {
return BlazeTools.parseIdentifierName(scanner);
return parseIdentifierName(scanner);
}
};
}

BlazeTools.parseStringLiteral = function (scanner) {
export function parseStringLiteral (scanner) {
var startPos = scanner.pos;
var rest = scanner.rest();
var match = rStringQuote.exec(rest);
Expand Down Expand Up @@ -190,4 +190,4 @@ BlazeTools.parseStringLiteral = function (scanner) {
var text = scanner.input.substring(startPos, scanner.pos);
var value = JSON.parse(jsonLiteral);
return { text: text, value: value };
};
}
5 changes: 3 additions & 2 deletions packages/html-tools/charref.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { makeRegexMatcher } from './scanner';

// http://www.whatwg.org/specs/web-apps/current-work/multipage/entities.json

Expand Down Expand Up @@ -2360,7 +2361,7 @@ var isLegalCodepoint = function (cp) {
// either `"`, `'`, or `>` and is supplied when parsing attribute values. NOTE: In the current spec, the
// value of `allowedChar` doesn't actually seem to end up mattering, but there is still some debate about
// the right approach to ampersands.
getCharacterReference = HTMLTools.Parse.getCharacterReference = function (scanner, inAttribute, allowedChar) {
export function getCharacterReference (scanner, inAttribute, allowedChar) {
if (scanner.peek() !== '&')
// no ampersand
return null;
Expand Down Expand Up @@ -2411,4 +2412,4 @@ getCharacterReference = HTMLTools.Parse.getCharacterReference = function (scanne
return null;
}
}
};
}
4 changes: 3 additions & 1 deletion packages/html-tools/charref_tests.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { HTMLTools } from 'meteor/html-tools';

var Scanner = HTMLTools.Scanner;
var getCharacterReference = HTMLTools.Parse.getCharacterReference;

Expand All @@ -19,7 +21,7 @@ Tinytest.add("html-tools - entities", function (test) {
test.equal(result, {
t: 'CharRef',
v: match,
cp: _.map(codepoints,
cp: codepoints.map(
function (x) { return (typeof x === 'string' ?
x.charCodeAt(0) : x); })
});
Expand Down
27 changes: 27 additions & 0 deletions packages/html-tools/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@

import { getCharacterReference } from './charref';
import { asciiLowerCase, properCaseTagName, properCaseAttributeName} from "./utils";
import { TemplateTag } from './templatetag'
import { Scanner } from './scanner';
import { parseFragment, codePointToString, getContent, getRCData } from './parse';
import { getComment, getDoctype, getHTMLToken, getTagToken, TEMPLATE_TAG_POSITION } from './tokenize';

export const HTMLTools = {
asciiLowerCase,
properCaseTagName,
properCaseAttributeName,
TemplateTag,
Scanner,
parseFragment,
codePointToString,
TEMPLATE_TAG_POSITION,
Parse: {
getCharacterReference,
getContent,
getRCData,
getComment,
getDoctype,
getHTMLToken,
getTagToken,
}
};
14 changes: 3 additions & 11 deletions packages/html-tools/package.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,16 @@ Package.describe({
});

Package.onUse(function (api) {
api.export('HTMLTools');

api.use('ecmascript@0.14.4');
api.use('htmljs@1.1.0-beta.2');
api.imply('htmljs@1.1.0-beta.2');

api.addFiles([
'utils.js',
'scanner.js',
'charref.js',
'tokenize.js',
'templatetag.js',
'parse.js'
]);
api.mainModule('main.js');
});

Package.onTest(function (api) {
api.use('ecmascript');
api.use('tinytest@1.0.11');
api.use('underscore@1.0.9');

api.use('html-tools');
api.use('htmljs@1.1.0-beta.2');
Expand Down
22 changes: 13 additions & 9 deletions packages/html-tools/parse.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import { HTML } from 'meteor/htmljs';
import { Scanner } from './scanner';
import { properCaseAttributeName } from './utils';
import { getHTMLToken, isLookingAtEndTag } from './tokenize';

// Parse a "fragment" of HTML, up to the end of the input or a particular
// template tag (using the "shouldStop" option).
HTMLTools.parseFragment = function (input, options) {
export function parseFragment(input, options) {
var scanner;
if (typeof input === 'string')
scanner = new Scanner(input);
Expand Down Expand Up @@ -69,14 +73,14 @@ HTMLTools.parseFragment = function (input, options) {
}

return result;
};
}

// Take a numeric Unicode code point, which may be larger than 16 bits,
// and encode it as a JavaScript UTF-16 string.
//
// Adapted from
// http://stackoverflow.com/questions/7126384/expressing-utf-16-unicode-characters-in-javascript/7126661.
codePointToString = HTMLTools.codePointToString = function(cp) {
export function codePointToString(cp) {
if (cp >= 0 && cp <= 0xD7FF || cp >= 0xE000 && cp <= 0xFFFF) {
return String.fromCharCode(cp);
} else if (cp >= 0x10000 && cp <= 0x10FFFF) {
Expand All @@ -97,9 +101,9 @@ codePointToString = HTMLTools.codePointToString = function(cp) {
} else {
return '';
}
};
}

getContent = HTMLTools.Parse.getContent = function (scanner, shouldStopFunc) {
export function getContent (scanner, shouldStopFunc) {
var items = [];

while (! scanner.isEOF()) {
Expand Down Expand Up @@ -204,7 +208,7 @@ getContent = HTMLTools.Parse.getContent = function (scanner, shouldStopFunc) {
return items[0];
else
return items;
};
}

var pushOrAppendString = function (items, string) {
if (items.length &&
Expand All @@ -215,7 +219,7 @@ var pushOrAppendString = function (items, string) {
};

// get RCDATA to go in the lowercase (or camel case) tagName (e.g. "textarea")
getRCData = HTMLTools.Parse.getRCData = function (scanner, tagName, shouldStopFunc) {
export function getRCData(scanner, tagName, shouldStopFunc) {
var items = [];

while (! scanner.isEOF()) {
Expand Down Expand Up @@ -250,7 +254,7 @@ getRCData = HTMLTools.Parse.getRCData = function (scanner, tagName, shouldStopFu
return items[0];
else
return items;
};
}

var getRawText = function (scanner, tagName, shouldStopFunc) {
var items = [];
Expand Down Expand Up @@ -350,7 +354,7 @@ var parseAttrs = function (attrs) {

var outValue = (inValue.length === 0 ? '' :
(outParts.length === 1 ? outParts[0] : outParts));
var properKey = HTMLTools.properCaseAttributeName(k);
var properKey = properCaseAttributeName(k);
result[properKey] = outValue;
}

Expand Down
8 changes: 6 additions & 2 deletions packages/html-tools/parse_tests.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
import { HTML } from 'meteor/htmljs';
import { HTMLTools } from 'meteor/html-tools';
import { BlazeTools} from 'meteor/blaze-tools';

var Scanner = HTMLTools.Scanner;
var getContent = HTMLTools.Parse.getContent;

Expand Down Expand Up @@ -190,8 +194,8 @@ Tinytest.add("html-tools - parseFragment", function (test) {
test.equal(BlazeTools.toJS(HTMLTools.parseFragment("<div><p id=foo>Hello</p></div>")),
BlazeTools.toJS(DIV(P({id:'foo'}, 'Hello'))));

_.each(['asdf</br>', '{{!foo}}</br>', '{{!foo}} </br>',
'asdf</a>', '{{!foo}}</a>', '{{!foo}} </a>'], function (badFrag) {
['asdf</br>', '{{!foo}}</br>', '{{!foo}} </br>',
'asdf</a>', '{{!foo}}</a>', '{{!foo}} </a>'].forEach(function (badFrag) {
test.throws(function() {
HTMLTools.parseFragment(badFrag);
}, /Unexpected HTML close tag/);
Expand Down
Loading

0 comments on commit 68d463b

Please sign in to comment.