diff --git a/module.js b/module.js index f23198d..93d3192 100644 --- a/module.js +++ b/module.js @@ -1,7 +1,31 @@ + +import TemplateRenderer, { cache } from './modules/renderer-template.js'; + +export default function Literal(id) { + // TODO: I don't think this works, it replaces whatever template is in the + // DOM, it it probably isn't in the DOM if it's cached? + + const id = typeof id === 'object' ? + id.id || '' : + id ; + + if (cache[id]) { + return cache[id].create(); + } + + const template = typeof id === 'object' ? + id : + document.getElementById(id) ; + + return new TemplateRenderer(template) ; +} + +// TODO: Legacy, remove +export { TemplateRenderer as Renderer }; + export { compiled } from './modules/renderer/compile.js'; export { default as config } from './modules/config.js'; export { default as Data, observe } from './modules/data.js'; -export { default as Renderer } from './modules/renderer-template.js'; export { default as scope } from './modules/scope.js'; export { urls } from './modules/urls.js'; diff --git a/modules/config.js b/modules/config.js index 5e7f058..14768fc 100644 --- a/modules/config.js +++ b/modules/config.js @@ -2,9 +2,27 @@ /* config Configure Literal's behaviour. + +```js + // An event dispatched when Literal updates input values. Default is `false`. + config.updateEvent = CustomEvent('ping'); + + // Run templates in sloppy mode inside a `with(data)` statement, making + // `${ data.name }` available as simply `${ name }` in a template. + // Warning: MDN says `with` should be considered deprecated: + // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/with + // I really don't see how they can remove it, though. + useWith: false +``` */ export default { // An event dispatched when Literal updates input values. May be false. - updateEvent: false + updateEvent: false, + // Runs templates in sloppy mode inside a `with(data)` statment, making + // `${ data.name }` available as simply `${ name }` in a template. + // Warning: MDN says with should be considered deprecated: + // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/with + // I don't see how anyone can remove it, though. + useWith: false } diff --git a/modules/data.js b/modules/data.js index ef56665..24b3761 100644 --- a/modules/data.js +++ b/modules/data.js @@ -1,7 +1,9 @@ import { Observer as Data, getTarget } from '../../fn/observer/observer.js'; +import observe from '../../fn/observer/observe.js'; Data.getObject = getTarget; +Data.observe = observe; export default Data; -export { default as observe } from '../../fn/observer/observe.js'; +export { observe }; diff --git a/modules/renderer-template.js b/modules/renderer-template.js index 456f5c2..f104fd2 100644 --- a/modules/renderer-template.js +++ b/modules/renderer-template.js @@ -1,6 +1,6 @@ /** -TemplateRenderer(template, element, parameters) +TemplateRenderer(template, element, parameters, options) Import the `TemplateRenderer` constructor: @@ -40,7 +40,7 @@ import { groupCollapsed, groupEnd } from './log.js'; const assign = Object.assign; const keys = Object.keys; -const cache = {}; +export const cache = {}; const nodes = []; @@ -98,19 +98,19 @@ function prepareContent(content) { } } -function compileContent(content, message) { +function compileContent(content, message, options) { if (window.DEBUG) { groupCollapsed('compile', message, 'yellow'); } prepareContent(content); - const renderers = compileNode([], content, '', message); + const renderers = compileNode([], content, '', message, options); if (window.DEBUG) { groupEnd(); } return renderers; } -function compileTemplate(template, id) { +function compileTemplate(template, id, options) { const content = template.content || create('fragment', template.childNodes, template) ; - const renderers = compileContent(content, '#' + id); + const renderers = compileContent(content, '#' + id, options); return { id, content, renderers }; } @@ -136,7 +136,9 @@ export default function TemplateRenderer(template, element = template.parentElem const id = identify(template) ; const { content, renderers } = cache[id] - || (cache[id] = compileTemplate(template, id)); + || (cache[id] = compileTemplate(template, id, { + sloppy: template.hasAttribute('nostrict') + })); this.element = element; this.parameters = parameters; diff --git a/modules/renderer/compile-attribute.js b/modules/renderer/compile-attribute.js index 0066dc8..f8e1e32 100644 --- a/modules/renderer/compile-attribute.js +++ b/modules/renderer/compile-attribute.js @@ -17,6 +17,10 @@ compileAttributes(renderers, element, attribute, path, message) **/ const constructors = { + class: TokensRenderer, + value: ValueRenderer, + checked: CheckedRenderer, + async: BooleanRenderer, autofocus: BooleanRenderer, autoplay: BooleanRenderer, @@ -38,19 +42,16 @@ const constructors = { reversed: BooleanRenderer, selected: BooleanRenderer, default: BooleanRenderer, - checked: CheckedRenderer, - class: TokensRenderer, - value: ValueRenderer, // Workaround attribute used in cases where ${} cannot be added directly to // HTML, such as in
or