Skip to content

Commit

Permalink
Merge pull request #56 from syumai/remove-last-semi
Browse files Browse the repository at this point in the history
Changed to remove last semi in raw and escaped
  • Loading branch information
syumai authored May 19, 2020
2 parents b19e748 + 25dc3e2 commit dedf4a1
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 7 deletions.
16 changes: 11 additions & 5 deletions mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ async function bufToStr(buf: Deno.Buffer): Promise<string> {
return decoder.decode(await Deno.readAll(buf));
}

function removeLastSemi(s: string): string {
return s.trimRight().replace(/;$/, "");
}

async function bufToStrWithSanitize(buf: Deno.Buffer): Promise<string> {
return sanitize(await bufToStr(buf));
}
Expand Down Expand Up @@ -134,18 +138,20 @@ export async function compile(reader: Reader): Promise<Template> {
switch (readMode) {
case ReadMode.Raw:
statements.push(
`;$$OUTPUT.push(${await bufToStr(statementBuf)});`,
`;$$OUTPUT.push(${
removeLastSemi(await bufToStr(statementBuf))
});`,
);
break;
case ReadMode.Escaped:
statements.push(
`;$$OUTPUT.push($$ESCAPE(${await bufToStrWithSanitize(
statementBuf,
)}));`,
`;$$OUTPUT.push($$ESCAPE(${
removeLastSemi(await bufToStr(statementBuf))
}));`,
);
break;
case ReadMode.Evaluate:
statements.push(await bufToStrWithSanitize(statementBuf));
statements.push(await bufToStr(statementBuf));
break;
}
}
Expand Down
22 changes: 20 additions & 2 deletions mod_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,18 @@ const decoder = new TextDecoder("utf-8");
param: "<div>test</div>",
expected: "",
},
{
name: "Escaped with semi",
body: "<%= param; %>",
param: "<div>test</div>",
expected: escape("<div>test</div>"),
},
{
name: "Raw with semi",
body: "<%- param; %>",
param: "<div>test</div>",
expected: "<div>test</div>",
},
{
name: "Security: Includes JavaScript",
body: "<%= param %>console.log(`${param}`)\\\\",
Expand Down Expand Up @@ -128,11 +140,17 @@ const decoder = new TextDecoder("utf-8");
expected: "<div>test</div>",
},
{
name: "Include",
fileName: "include",
name: "Raw Include",
fileName: "raw-include",
param: "<div>test</div>",
expected: "<div>test</div>",
},
{
name: "Escaped Include",
fileName: "escaped-include",
param: "<div>test</div>",
expected: escape("<div>test</div>"),
},
];

for (const tc of testCases) {
Expand Down
1 change: 1 addition & 0 deletions testdata/escaped-include.ejs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<%= await include(`testdata/included.ejs`, { param }) %>
File renamed without changes.

0 comments on commit dedf4a1

Please sign in to comment.