Skip to content

Commit

Permalink
[fix] treat array in tag template as a sub template (#1755)
Browse files Browse the repository at this point in the history
  • Loading branch information
jchip committed Nov 17, 2020
1 parent 4a68683 commit 5f885e3
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 17 deletions.
8 changes: 4 additions & 4 deletions packages/xarc-tag-renderer/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,10 @@
"test"
],
"check-coverage": true,
"statements": 97.67,
"branches": 98.55,
"functions": 92.86,
"lines": 97.55,
"statements": 100,
"branches": 100,
"functions": 100,
"lines": 100,
"cache": false
},
"fyn": {
Expand Down
2 changes: 2 additions & 0 deletions packages/xarc-tag-renderer/src/render-execute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,13 +90,15 @@ export function renderNext(err: Error, xt: any) {
);
case STEP_FUNC_HANDLER: {
const result = tk(context);
// in case the function handler returned a sub template
return handleSubTemplate("", step, result, xt, (e: Error) => renderNext(e, xt));
}
case STEP_HANDLER: {
if (withId) {
insertTokenId(tk);
}
const result = tk[TOKEN_HANDLER](context, tk);
// in case the handler returned a sub template
return handleSubTemplate(tk.id, step, result, xt, (e: Error) => {
if (withId) {
insertTokenIdEnd(tk);
Expand Down
11 changes: 7 additions & 4 deletions packages/xarc-tag-renderer/src/render-processor.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
/* eslint-disable max-statements */
/* eslint-disable max-statements, complexity */

import { executeTagTemplate, executeSteps } from "./render-execute";
import { TAG_TYPE } from "./symbols";
import { TagTemplate } from "./tag-template";
import { createTemplateTagsFromArray, TagTemplate } from "./tag-template";
import { RenderContext } from "@xarc/render-context";
import { TagRenderer } from "./tag-renderer";

Expand Down Expand Up @@ -111,8 +111,9 @@ export class RenderProcessor {
} else if (tk[TAG_TYPE] === "register-token-ids") {
tk({ asyncTemplate: options.asyncTemplate });
opCode = null;
} else if (tk[TAG_TYPE] === "template") {
const template = new TagTemplate({ templateTags: tk, processor: this });
} else if (tk[TAG_TYPE] === "template" || Array.isArray(tk)) {
const tt = tk[TAG_TYPE] === "template" ? tk : createTemplateTagsFromArray(tk);
const template = new TagTemplate({ templateTags: tt, processor: this });
this.applyTokenModuleLoad({ insertTokenIds: this._options.insertTokenIds }, template);
opCode = {
tk,
Expand All @@ -122,6 +123,8 @@ export class RenderProcessor {
} else if (tk.hasOwnProperty("str")) {
// token is a literal string, just add it to output
opCode = { tk, code: STEP_STR_TOKEN };
} else if (typeof tk === "string") {
opCode = { data: tk, code: STEP_LITERAL_HANDLER };
} else if (!tk.isModule) {
// token is not pointing to a module, so lookup from token handlers
opCode = this.makeHandlerStep(tk);
Expand Down
11 changes: 3 additions & 8 deletions packages/xarc-tag-renderer/src/tag-renderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,9 @@ export class TagRenderer {
* @returns render context
*/
async render(options) {
if (!this._processor) {
this.initializeRenderer();
}
let context;
try {
context = new RenderContext(options, this);
Expand Down Expand Up @@ -134,14 +137,6 @@ export class TagRenderer {
this._initializeTokenHandlers(this._tokenHandlers);
}

_applyTokenLoad(options) {
this._tokens.forEach(tokenModule => {
if (tokenModule.load) {
tokenModule.load(options);
}
});
}

_initializeTokenHandlers(handlers) {
const loaded = handlers.map((h, ix) => {
if (h.loaded) {
Expand Down
34 changes: 34 additions & 0 deletions packages/xarc-tag-renderer/test/data/template4.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/* eslint-disable new-cap */

import { createTemplateTags, Token, TokenInvoke, RegisterTokenIds } from "../../src";

function foo() {
return {
process() {
return `foo `;
}
};
}

function test() {
return {
process() {
return `test `;
}
};
}

const data = [foo, "Bar", test];
export const templateTags = createTemplateTags`
${RegisterTokenIds(() => {
return {
Bar() {
return `from Bar `;
}
};
})}
${data.map((x: string | Function) => {
return typeof x === "string" ? Token(x) : TokenInvoke(x, {});
})}
${["hello", " world"].map((x: string) => x)}
`;
10 changes: 10 additions & 0 deletions packages/xarc-tag-renderer/test/spec/tag-renderer.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { TagRenderer, createTemplateTags } from "../../src";
import { templateTags as templateTags1 } from "../data/template1";
import { templateTags as templateTags2 } from "../data/template2";
import { templateTags as templateTags3 } from "../data/template3";
import { templateTags as templateTags4 } from "../data/template4";

import { describe, it } from "mocha";

Expand All @@ -24,6 +25,15 @@ describe("tag template", function () {
expect(context.result).contains("custom-1</div>hello world from function: user,options");
});

it("should render a TagTemplate with array of tokens", async () => {
const renderer = new TagRenderer({
templateTags: templateTags4
});

const context = await renderer.render({});
expect(context.result.trim()).to.equal("foo from Bar test hello world");
});

it("should render a TagTemplate with handler that returns a sub template", async () => {
const renderer = new TagRenderer({
templateTags: templateTags3
Expand Down
8 changes: 7 additions & 1 deletion packages/xarc-tag-renderer/test/spec/tag-template.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ import { expect } from "chai";
import { templateTags as templateTags1 } from "../data/template1";
import { templateTags as templateTags2 } from "../data/template2";
import { describe, it } from "mocha";
import { TagTemplate } from "../../src";
import { TagTemplate, createTemplateTagsFromArray } from "../../src";
import { TAG_TYPE } from "../../src/symbols";

describe("tag template", function () {
const template2 = new TagTemplate({ templateTags: templateTags2, processor: null });
Expand All @@ -14,6 +15,11 @@ describe("tag template", function () {
expect(ssrToken).to.exist;
});

it("should create TagTemplate from array", () => {
const template = createTemplateTagsFromArray([1, 2, 3]);
expect(template[TAG_TYPE]).equal("template");
});

describe("_findTokenIndex", function () {
it("should validate and return index", () => {
expect(template2._findTokenIndex(null, null, 0)).to.equal(0);
Expand Down

0 comments on commit 5f885e3

Please sign in to comment.