-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Replace
dedent
with a template-only dedent
Summary: The `dedent` module has two problems that require its replacement. First, it dedents the substitutions in addition to the template, but the substitutions need to be treated as opaque. This is because one such substitution is the whole HTML contents, which could contain important leading whitespace in a `<pre>` tag. Second, it replaces the character sequence "backslash, newline, zero-or-more-whitespace" with the empty string, and there is no way to disable this behavior, so there is consequently no way to emit a non-final line ending in a backslash. This means that it will not be possible to use `dedent` to dedent code fragments. Both of these issues are solved by implementing a template tag function that serves the intended purpose. Test Plan: Add <test>${"\n indented!"}</test>\ to the trailing backslash in `server.js`, and check the rendered output to see that both issues mentioned above are fixed.
- Loading branch information
Showing
3 changed files
with
40 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
/* | ||
* A template tag function that performs dedenting on the template, but | ||
* not its arguments. | ||
* | ||
* For instance, given the template | ||
* | ||
* |dedent`\ | ||
* | one ${one} | ||
* | two ${two} | ||
* | done`, | ||
* | ||
* where `one === "1"` and `two === "\n 2"`, the template string | ||
* would expand to "one 1\n two\n 2\ndone". Note that four spaces | ||
* of indentation were stripped off of each of "one" and "two", but not | ||
* from "2". | ||
* | ||
* Lines that contain only whitespace are not used for measuring. | ||
*/ | ||
export default function dedent(strings, ...values) { | ||
const lineLengths = strings.join("") | ||
.split("\n") | ||
.filter(line => line.trim().length !== 0) | ||
.map(line => line.length - line.trimLeft().length); | ||
const trimAmount = Math.min.apply(null, lineLengths); | ||
|
||
const parts = []; | ||
for (let i = 0; i < strings.length; i++) { | ||
const trimmed = strings[i] | ||
.split("\n") | ||
.map((line, j) => i === 0 || j > 0 ? line.substr(trimAmount) : line) | ||
.join("\n"); | ||
parts.push(trimmed); | ||
if (i < values.length) { | ||
parts.push(values[i]); | ||
} | ||
} | ||
return parts.join(""); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
06475d4
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
<3