ghtml 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.eslintrc.json +5 -0
- package/.github/workflows/ci.yml +17 -0
- package/.github/workflows/npm-publish.yml +23 -0
- package/LICENSE +21 -0
- package/package.json +28 -0
- package/src/html.js +56 -0
- package/src/includeFile.js +22 -0
- package/src/index.js +1 -0
- package/test/index.js +115 -0
package/.eslintrc.json
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
name: npm-publish
|
|
2
|
+
|
|
3
|
+
on:
|
|
4
|
+
release:
|
|
5
|
+
types: [published]
|
|
6
|
+
|
|
7
|
+
jobs:
|
|
8
|
+
npm-publish:
|
|
9
|
+
runs-on: ubuntu-latest
|
|
10
|
+
|
|
11
|
+
permissions:
|
|
12
|
+
contents: read
|
|
13
|
+
id-token: write
|
|
14
|
+
|
|
15
|
+
steps:
|
|
16
|
+
- uses: actions/checkout@v4
|
|
17
|
+
- uses: actions/setup-node@v4
|
|
18
|
+
with:
|
|
19
|
+
node-version: ^20
|
|
20
|
+
registry-url: https://registry.npmjs.org
|
|
21
|
+
- run: npm publish --provenance --access public
|
|
22
|
+
env:
|
|
23
|
+
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
|
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 Gürgün Dayıoğlu
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/package.json
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "ghtml",
|
|
3
|
+
"description": "Replace your template engine with fast JavaScript by leveraging the power of tagged templates.",
|
|
4
|
+
"author": "Gürgün Dayıoğlu",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"version": "1.0.0",
|
|
7
|
+
"type": "module",
|
|
8
|
+
"main": "./src/index.js",
|
|
9
|
+
"exports": {
|
|
10
|
+
".": "./src/index.js",
|
|
11
|
+
"./*.js": "./src/*.js"
|
|
12
|
+
},
|
|
13
|
+
"engines": {
|
|
14
|
+
"node": ">=18"
|
|
15
|
+
},
|
|
16
|
+
"scripts": {
|
|
17
|
+
"test": "npm run lint && node --experimental-test-coverage test/index.js",
|
|
18
|
+
"lint": "eslint . && prettier --check .",
|
|
19
|
+
"lint:fix": "eslint --fix . && prettier --write ."
|
|
20
|
+
},
|
|
21
|
+
"devDependencies": {
|
|
22
|
+
"grules": "^0.5.0"
|
|
23
|
+
},
|
|
24
|
+
"repository": {
|
|
25
|
+
"type": "git",
|
|
26
|
+
"url": "git+https://github.com/gurgunday/ghtml.git"
|
|
27
|
+
}
|
|
28
|
+
}
|
package/src/html.js
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
const escapeDictionary = {
|
|
2
|
+
'"': """,
|
|
3
|
+
"'": "'",
|
|
4
|
+
"&": "&",
|
|
5
|
+
"<": "<",
|
|
6
|
+
">": ">",
|
|
7
|
+
};
|
|
8
|
+
|
|
9
|
+
const escapeRegExp = new RegExp(
|
|
10
|
+
`[${Object.keys(escapeDictionary).join("")}]`,
|
|
11
|
+
"gu",
|
|
12
|
+
);
|
|
13
|
+
|
|
14
|
+
const escapeFunction = (key) => {
|
|
15
|
+
return escapeDictionary[key];
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* @param {{ raw: string[] }} literals
|
|
20
|
+
* @param {...any} expressions
|
|
21
|
+
* @returns {string}
|
|
22
|
+
*/
|
|
23
|
+
const html = (literals, ...expressions) => {
|
|
24
|
+
const lastLiteralIndex = literals.raw.length - 1;
|
|
25
|
+
let accumulator = "";
|
|
26
|
+
|
|
27
|
+
if (lastLiteralIndex === -1) {
|
|
28
|
+
return accumulator;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
for (let index = 0; index < lastLiteralIndex; ++index) {
|
|
32
|
+
let literal = literals.raw[index];
|
|
33
|
+
let expression =
|
|
34
|
+
typeof expressions[index] === "string"
|
|
35
|
+
? expressions[index]
|
|
36
|
+
: expressions[index] == null
|
|
37
|
+
? ""
|
|
38
|
+
: Array.isArray(expressions[index])
|
|
39
|
+
? expressions[index].join("")
|
|
40
|
+
: `${expressions[index]}`;
|
|
41
|
+
|
|
42
|
+
if (literal.length && literal.charCodeAt(literal.length - 1) === 33) {
|
|
43
|
+
literal = literal.slice(0, -1);
|
|
44
|
+
} else if (expression.length) {
|
|
45
|
+
expression = expression.replace(escapeRegExp, escapeFunction);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
accumulator += literal + expression;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
accumulator += literals.raw[lastLiteralIndex];
|
|
52
|
+
|
|
53
|
+
return accumulator;
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
export { html };
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { readFileSync } from "node:fs";
|
|
2
|
+
|
|
3
|
+
const readFileSyncOptions = { encoding: "utf8" };
|
|
4
|
+
|
|
5
|
+
const fileCache = new Map();
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* @param {string} path
|
|
9
|
+
* @returns {string}
|
|
10
|
+
*/
|
|
11
|
+
const includeFile = (path) => {
|
|
12
|
+
let file = fileCache.get(path);
|
|
13
|
+
|
|
14
|
+
if (file === undefined) {
|
|
15
|
+
file = readFileSync(path, readFileSyncOptions);
|
|
16
|
+
fileCache.set(path, file);
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
return file;
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
export { includeFile };
|
package/src/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { html } from "./html.js";
|
package/test/index.js
ADDED
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
import test from "node:test";
|
|
2
|
+
import assert from "node:assert";
|
|
3
|
+
import { html } from "../src/index.js";
|
|
4
|
+
|
|
5
|
+
const username = "G";
|
|
6
|
+
const descriptionSafe = "This is a safe description.";
|
|
7
|
+
const descriptionUnsafe =
|
|
8
|
+
"<script>alert('This is an unsafe description.')</script>";
|
|
9
|
+
const array1 = [1, 2, 3, 4, 5];
|
|
10
|
+
const conditionTrue = true;
|
|
11
|
+
const conditionFalse = false;
|
|
12
|
+
const empty = "";
|
|
13
|
+
|
|
14
|
+
test("renders correctly", () => {
|
|
15
|
+
assert.strictEqual(html({ raw: [] }, []), "");
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
test("renders correctly", () => {
|
|
19
|
+
assert.strictEqual(html`${empty}`, "");
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
test("renders correctly", () => {
|
|
23
|
+
assert.strictEqual(html`Hey, ${username}!`, `Hey, ${username}!`);
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
test("renders safe content", () => {
|
|
27
|
+
assert.strictEqual(
|
|
28
|
+
html`<p>${descriptionSafe}</p>`,
|
|
29
|
+
"<p>This is a safe description.</p>",
|
|
30
|
+
);
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
test("escapes unsafe output", () => {
|
|
34
|
+
assert.strictEqual(
|
|
35
|
+
html`<p>${descriptionUnsafe}</p>`,
|
|
36
|
+
`<p><script>alert('This is an unsafe description.')</script></p>`,
|
|
37
|
+
);
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
test("escapes unsafe output", () => {
|
|
41
|
+
assert.strictEqual(
|
|
42
|
+
html`<p>${descriptionUnsafe}</p>`,
|
|
43
|
+
`<p><script>alert('This is an unsafe description.')</script></p>`,
|
|
44
|
+
);
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
test("renders arrays", () => {
|
|
48
|
+
assert.strictEqual(
|
|
49
|
+
html`<p>${[descriptionSafe, descriptionUnsafe]}</p>`,
|
|
50
|
+
"<p>This is a safe description.<script>alert('This is an unsafe description.')</script></p>",
|
|
51
|
+
);
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
test("bypass escaping", () => {
|
|
55
|
+
assert.strictEqual(
|
|
56
|
+
html`<p>!${[descriptionSafe, descriptionUnsafe]}</p>`,
|
|
57
|
+
"<p>This is a safe description.<script>alert('This is an unsafe description.')</script></p>",
|
|
58
|
+
);
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
test("renders wrapped html calls", () => {
|
|
62
|
+
assert.strictEqual(
|
|
63
|
+
// prettier-ignore
|
|
64
|
+
html`<p>!${conditionTrue ? html`<strong>${descriptionUnsafe}</strong>` : ""}</p>`,
|
|
65
|
+
"<p><strong><script>alert('This is an unsafe description.')</script></strong></p>",
|
|
66
|
+
);
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
test("renders multiple html calls", () => {
|
|
70
|
+
assert.strictEqual(
|
|
71
|
+
html`
|
|
72
|
+
<p>
|
|
73
|
+
!${conditionFalse ? "" : html`<strong> ${descriptionSafe} </strong>`}
|
|
74
|
+
<em> ${array1} </em>
|
|
75
|
+
!${conditionFalse ? html`<em> ${array1} </em>` : ""}
|
|
76
|
+
</p>
|
|
77
|
+
`,
|
|
78
|
+
// it should be formatted
|
|
79
|
+
`
|
|
80
|
+
<p>
|
|
81
|
+
<strong> This is a safe description. </strong>
|
|
82
|
+
<em> 12345 </em>
|
|
83
|
+
|
|
84
|
+
</p>
|
|
85
|
+
`,
|
|
86
|
+
);
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
test("renders multiple html calls with different expression types", () => {
|
|
90
|
+
const obj = {};
|
|
91
|
+
obj.toString = () => {
|
|
92
|
+
return "Description of the object.";
|
|
93
|
+
};
|
|
94
|
+
|
|
95
|
+
assert.strictEqual(
|
|
96
|
+
html`
|
|
97
|
+
<p>
|
|
98
|
+
!${conditionTrue ? html`<strong> ${descriptionSafe} </strong>` : ""}
|
|
99
|
+
!${conditionFalse
|
|
100
|
+
? ""
|
|
101
|
+
: // prettier-ignore
|
|
102
|
+
html`<em> ${array1.map((i) => {return i + 1;})} </em>`}<br />
|
|
103
|
+
And also, ${false} ${null}${undefined}${obj}
|
|
104
|
+
</p>
|
|
105
|
+
`,
|
|
106
|
+
// it should be formatted
|
|
107
|
+
`
|
|
108
|
+
<p>
|
|
109
|
+
<strong> This is a safe description. </strong>
|
|
110
|
+
<em> 23456 </em><br />
|
|
111
|
+
And also, false Description of the object.
|
|
112
|
+
</p>
|
|
113
|
+
`,
|
|
114
|
+
);
|
|
115
|
+
});
|