Skip to content

Commit

Permalink
v0.22.2 - Fix issue with escaped backslashes (2 consecutive backslashes)
Browse files Browse the repository at this point in the history
were not appearing correctly in HTML output
  • Loading branch information
robole committed Oct 14, 2023
1 parent 1cdd935 commit b629eea
Show file tree
Hide file tree
Showing 8 changed files with 54 additions and 12 deletions.
12 changes: 12 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,18 @@ All notable changes to this project are documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [0.22.2] - 2023-10-14

### Fixed

- Escaped backslashes (2 consecutive backslashes) were not appearing correctly in HTML output. e.g a snippet with a `body` of "\\${num};" appears as "\${num}".

### Changed

- Modify *webpack.config.js* to work with Node v18.x. It was throwing a ['ERR_OSSL_EVP_UNSUPPORTED' error](https://stackoverflow.com/questions/69394632/webpack-build-failing-with-err-ossl-evp-unsupported) whose cause is: "it’s likely that your application or a module you’re using is attempting to use an algorithm or key size which is no longer allowed by default with OpenSSL 3.0".
- Updated tests in *formatter.test.js*.
- Updated secret for GitHub Action to publish automatically.

## [0.22.1] - 2022-06-29

### Fixed
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ If you have a suggestion or find a bug, please file an issue.

## Show gratitude

If you are happy with the extension: please star the repo, and leave a review to help others find it. 🌟
If you are happy with the extension: please star the repo, and leave [a review ](https://marketplace.visualstudio.com/items?itemName=robole.snippets-ranger&ssr=false#review-details) in the marketplace to help others find it. 🌟

You can [buy me a coffee or sponsor me](https://ko-fi.com/roboleary) if you would like to support me in maintaining this project and creating more open-source software. 🙏

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
},
"description": "View and edit all your snippets in one purty place. Yee-haw!",
"icon": "img/logo.png",
"version": "0.22.1",
"version": "0.22.2",
"engines": {
"vscode": "^1.4.0",
"node": ">=12.0.0"
Expand Down
7 changes: 5 additions & 2 deletions src/formatter.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,20 +38,23 @@ class Formatter {
.replace(/'/g, "'")
.replace(/</g, "&lt;")
.replace(/>/g, "&gt;")
.replace(/\\/g, "&#92;&#92;")
.replace(/\r?\n/g, "<br/>")
.replace(/\t/g, "&emsp;");

return str;
}

/**
* Escapes all HTML content in the elements of an array and concatenates it into a string that can be consumed by a web page. Each value is appended with a HTML line break (BR tag).
* Escapes all HTML content in the elements of an array and concatenates it into a string
* that can be consumed by a web page. Each value is appended with a HTML line break (BR tag).
* @param {Array} array Array of values
*/
static escapeBody(array) {
let str = "";
array.forEach((element) => {
str += `${Formatter.escapeHtml(element)}<br>`;
str += Formatter.escapeHtml(element);
str += "<br>";
});
return str;
}
Expand Down
8 changes: 5 additions & 3 deletions src/view.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable class-methods-use-this */
// @ts-nocheck
// eslint-disable-next-line import/no-unresolved
const vscode = require("vscode");
Expand Down Expand Up @@ -220,9 +221,10 @@ class View {
<td>${snippet.prefix}</td>
<td>${snippet.name}</td>
<td>${snippet.description}</td>`;
table += `<td><code>${Formatter.escapeBody(snippet.body)}</code></td>
<td>${editButton}${deleteButton}</td>
</tr>`;
table += "<td><code>";
table += Formatter.escapeBody(snippet.body);
table += "</code></td>";
table += `<td>${editButton}${deleteButton}</td></tr>`;
});

table += `${tableEnd}`;
Expand Down
29 changes: 24 additions & 5 deletions test/suite/formatter.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,16 +42,35 @@ describe("Formatter", () => {
});

describe("escapeHtml()", () => {
it("should replace anything that could be interpeted as HTML with a text equivalent", () => {
it("should replace html element with a HTML-safe equivalent", () => {
let text1 = formatter.escapeHtml(`This is the <html> tag.`);

assert.strictEqual(text1, "This is the &lt;html&gt; tag.");
});

it("should replace single quotes, double quotes, and ampersands with a HTML-safe equivalent", () => {
let text2 = formatter.escapeHtml(`No '"& allowed.`);

assert.strictEqual(text2, "No &#39;&quot;&amp; allowed.");
});

it("should replace a tab with a HTML-safe equivalent", () => {
let text3 = formatter.escapeHtml(`\t let x = 1;`);
let text4 = formatter.escapeHtml(`Line 1.\r\n`);

assert.strictEqual(text1, "This is the &lt;html&gt; tag.");
assert.strictEqual(text2, "No &#39;&quot;&amp; allowed.");
assert.strictEqual(text3, "&emsp; let x = 1;");
assert.strictEqual(text4, "Line 1.<br/>");
});

it("should replace line breaks with a HTML-safe equivalent", () => {
let text1 = formatter.escapeHtml(`Line 1.\r\nLine 2.`);
let text2 = formatter.escapeHtml(`Line 1.\nLine 2.`);

assert.strictEqual(text1, "Line 1.<br/>Line 2.");
assert.strictEqual(text2, "Line 1.<br/>Line 2.");
});

it("should replace an escaped backslash with a HTML-safe equivalent", () => {
let text = formatter.escapeHtml("`\\$num is a variable`;");
assert.strictEqual(text, "`&#92;&#92;$num is a variable`;");
});
});
});
2 changes: 2 additions & 0 deletions todo.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# To Do

1. Add workspace snippets.
1. Emphasize in readme that it uses the same source files as VS Code.
1. Filter?
1. Search?
1. Show the shortcuts assigned to any snippets (via command 'insert snippet')?
4 changes: 4 additions & 0 deletions webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ const path = require("path");
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
const OptimizeCSSAssetsPlugin = require("optimize-css-assets-webpack-plugin");
const TerserPlugin = require("terser-webpack-plugin");
const crypto = require("crypto");

const crypto_orig_createHash = crypto.createHash;
crypto.createHash = algorithm => crypto_orig_createHash(algorithm === "md4" ? "sha256" : algorithm);

const config = {
target: "node",
Expand Down

0 comments on commit b629eea

Please sign in to comment.