Skip to content

Commit

Permalink
Add support for HTMLElements in raw.text TemplateStrings
Browse files Browse the repository at this point in the history
  • Loading branch information
paul-go committed Dec 10, 2023
1 parent 9060940 commit 6276d2a
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 7 deletions.
13 changes: 11 additions & 2 deletions Raw.cover.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -136,12 +136,21 @@ namespace Cover
function blue(text: string) { return "<b>" + text + "</b>"; }
function red(text: string) { return "<r>" + text + "</r>"; }

const x = raw.text`
raw.text`
We need a new ${blue("media channel")} where you can
have your ${blue("cake")} and ${red("eat")} it ${red("too")}.
`;

console.log(x);
function br()
{
return raw.br();
}

const text = raw.text`
Here is some text. And a line break ${br()}
`;

debugger;
}

//@ts-ignore
Expand Down
17 changes: 12 additions & 5 deletions Raw.ts
Original file line number Diff line number Diff line change
Expand Up @@ -312,12 +312,19 @@ class Raw extends (() => Object as any as RawElements)()
* A function that creates a new DOM Text node, but which may be overridden
* in the constructor to return a different but compatible value.
*/
text(template: TemplateStringsArray, ...placeholders: string[]): Text
text(string: string): Text
text(a: TemplateStringsArray | string, ...b: string[])
text(template: TemplateStringsArray, ...placeholders: (string | HTMLElement)[]): (Text | HTMLElement)[];
text(string: string): Text;
text(a: TemplateStringsArray | string, ...b: string[]): any
{
const text = typeof a === "string" ? a : a.map((value, i) => value + (i < a.length - 1 ? b[i] : "")).join("");
return this.doc.createTextNode(text);
if (typeof a === "string")
return this.doc.createTextNode(a);

const nodes: (string | HTMLElement)[] = [];
for (let i = -1; ++i < b.length;)
nodes.push(a[i], b[i])

nodes.push(a[a.length - 1]);
return nodes.map(n => typeof n === "string" ? this.doc.createTextNode(n) : n);
}

/**
Expand Down

0 comments on commit 6276d2a

Please sign in to comment.