Skip to content

Commit

Permalink
fix: escape html for :each directive
Browse files Browse the repository at this point in the history
  • Loading branch information
jorenrui committed Feb 5, 2024
1 parent c97d304 commit 11e8f2b
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 4 deletions.
10 changes: 6 additions & 4 deletions lib/entity.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Interpreter, ClassInterpreter } from './generators/interpreter';
import { Lexer } from './generators/lexer';
import { escapeHTML } from './helpers/sanitize';

export default class Entity {
constructor(el) {
Expand Down Expand Up @@ -187,13 +188,13 @@ export default class Entity {
}

async evaluateClass() {
const expr = this.element.getAttribute(':class');
if (!expr) return;
const expr = this.element.getAttribute(':class')
if (!expr) return

this.element.className = await this._interpret(expr, {
base: this.baseClasses,
isClass: true,
});
})
}

async evaluateLoadEvents() {
Expand All @@ -214,9 +215,10 @@ export default class Entity {
let newHTML = ''

items.forEach((item, index) => {
// TODO: Use the lexer to replace the variables
newHTML += this.childClone
.replaceAll(indexName, index)
.replaceAll(variable, `'${item}'`);
.replaceAll(variable, `'${escapeHTML(item)}'`);
})

this.element.innerHTML = newHTML
Expand Down
18 changes: 18 additions & 0 deletions lib/helpers/sanitize.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/**
* Sanitize HTML string
* https://stackoverflow.com/questions/2794137/sanitizing-user-input-before-adding-it-to-the-dom-in-javascript
* @param {string} string
* @returns string
*/
export function escapeHTML(string) {
const map = {
'&': '&',
'<': '&lt;',
'>': '&gt;',
'"': '&quot;',
"'": '&#x27;',
"/": '&#x2F;',
}
const reg = /[&<>"'/]/ig
return string.replace(reg, (match) => map[match])
}

0 comments on commit 11e8f2b

Please sign in to comment.