Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Get rid of html encode method #4861

Merged
merged 2 commits into from
Aug 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 9 additions & 6 deletions app/assets/javascripts/components/datalist_input.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ import { html, PropertyValues, TemplateResult } from "lit";
import { ShadowlessLitElement } from "components/meta/shadowless_lit_element";
import { ref, Ref, createRef } from "lit/directives/ref.js";
import { watchMixin } from "components/meta/watch_mixin";
import { unsafeHTML } from "lit/directives/unsafe-html.js";
import { htmlEncode } from "util.js";

export type Option = {label: string, value: string, extra?: string};

Expand Down Expand Up @@ -171,10 +169,15 @@ export class DatalistInput extends watchMixin(ShadowlessLitElement) {
this.getElementsByClassName("active")[0]?.scrollIntoView({ block: "nearest" });
}

mark(s: string): TemplateResult {
return this.filter ?
html`${unsafeHTML(htmlEncode(s).replace(new RegExp(this.filter, "gi"), m => `<b>${m}</b>`))}` :
html`${s}`;
mark(s: string): (TemplateResult | string)[] | string {
if (!this.filter) {
return s;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the original code, this was wrapped in a template string. This isn't needed anymore?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, it will be wrapped in a template when used, which made also wrapping it here an operation without effect

}

// using group syntax () to preserve matches in the split string
const regex = new RegExp(`(${this.filter})`, "gi");
// split the string on the regex, and make the matches bold
return s.split(regex).map(p => regex.test(p) ? html`<b>${p}</b>` : p);
}

render(): TemplateResult {
Expand Down
19 changes: 0 additions & 19 deletions app/assets/javascripts/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -190,24 +190,6 @@ const ready = new Promise(resolve => {
}
});

// source https://github.com/janl/mustache.js/blob/master/mustache.js#L73
const entityMap = {
"&": "&amp;",
"<": "&lt;",
">": "&gt;",
"\"": "&quot;",
"'": "&#39;",
"/": "&#x2F;",
"`": "&#x60;",
"=": "&#x3D;"
};

function htmlEncode(str) {
return String(str).replace(/[&<>"'`=/]/g, function (s) {
return entityMap[s];
});
}

/**
* Returns the first parent of an element that has at least all of the given classes.
* Returns null if no such parent exists.
Expand Down Expand Up @@ -244,6 +226,5 @@ export {
setDocumentTitle,
initDatePicker,
ready,
htmlEncode,
getParentByClassName,
};