Skip to content

Commit

Permalink
fix(sbb-logo,sbb-signet): solve SSR issue with text assignment (#2208)
Browse files Browse the repository at this point in the history
Previous attempts to fix SSR failed due to various reasons. Now we just use property assignment with `.textContent`, which forces any text given to be interpreted as text. This avoid exploitation and simplifies the code.
  • Loading branch information
kyubisation authored Nov 21, 2023
1 parent 06a5365 commit 548d45d
Show file tree
Hide file tree
Showing 4 changed files with 2 additions and 22 deletions.
1 change: 0 additions & 1 deletion src/components/core/dom/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,3 @@ export * from './input-element';
export * from './is-valid-attribute';
export * from './platform';
export * from './scroll';
export * from './strip-html';
7 changes: 0 additions & 7 deletions src/components/core/dom/strip-html.ts

This file was deleted.

8 changes: 1 addition & 7 deletions src/components/logo/logo.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import { CSSResultGroup, html, LitElement, nothing, TemplateResult } from 'lit';
import { customElement, property } from 'lit/decorators.js';
import { unsafeSVG } from 'lit/directives/unsafe-svg.js';

import { stripHTML } from '../core/dom';
import { SbbProtectiveRoom } from '../core/interfaces';

import style from './logo.scss?lit&inline';
Expand All @@ -25,15 +23,11 @@ export class SbbLogo extends LitElement {
@property({ attribute: 'accessibility-label' }) public accessibilityLabel = 'Logo';

protected override render(): TemplateResult {
// unsafeSVG is required due to bug in Lit SSR: https://github.com/lit/lit/issues/4405
// As unsafeSVG would allow any kind of content (including HTML) to be used, we
// remove any HTML content from the given accessibility label.
const accessibilityLabel = this.accessibilityLabel ? stripHTML(this.accessibilityLabel) : '';
return html`
<span class="sbb-logo">
<span class="sbb-logo__svg-container">
<svg focusable="false" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 454 48">
${accessibilityLabel ? unsafeSVG(`<title>${accessibilityLabel}</title>`) : nothing}
<title .textContent=${this.accessibilityLabel}></title>
<path
id="sbb-logo__word-mark"
d="M238.2 34.6c4.1 0 6.2-1.4 6.2-4.5 0-3.2-2-4.6-6.6-4.6h-7v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4.1 3.7l6 1c6.4 1.1 9.8 4.6 9.8 9.7 0 6.6-5.3 10.8-13.7 10.8-8.9 0-14-4.3-14.1-11.9h7.1c.4 4.4 2.6 6.2 7.3 6.2 3.7 0 6.2-1.9 6.2-4.6 0-2.1-1.4-3.4-4.1-3.9l-5.5-1c-7-1.3-10.2-4.3-10.2-9.7 0-6.2 4.9-10 12.9-10 7.8 0 12.7 4 13 10.6l-6.7.2zm-40.4-3.9v7.8h14.9v5.8h-14.9v13.7h-7V6.8h23.4v6h-16.4zm-29.7 0v7.8h15v5.8h-15v13.7h-7V6.8h23.5v6h-16.5z"
Expand Down
8 changes: 1 addition & 7 deletions src/components/signet/signet.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import { CSSResultGroup, html, LitElement, nothing, TemplateResult } from 'lit';
import { customElement, property } from 'lit/decorators.js';
import { unsafeSVG } from 'lit/directives/unsafe-svg.js';

import { stripHTML } from '../core/dom';
import { SbbProtectiveRoom } from '../core/interfaces';

import style from './signet.scss?lit&inline';
Expand All @@ -22,15 +20,11 @@ export class SbbSignet extends LitElement {
@property({ attribute: 'accessibility-label' }) public accessibilityLabel = 'Logo';

protected override render(): TemplateResult {
// unsafeSVG is required due to bug in Lit SSR: https://github.com/lit/lit/issues/4405
// As unsafeSVG would allow any kind of content (including HTML) to be used, we
// remove any HTML content from the given accessibility label.
const accessibilityLabel = this.accessibilityLabel ? stripHTML(this.accessibilityLabel) : '';
return html`
<span class="sbb-signet">
<span class="sbb-signet__svg-container">
<svg focusable="false" viewBox="0 0 80 40" xmlns="http://www.w3.org/2000/svg">
${accessibilityLabel ? unsafeSVG(`<title>${accessibilityLabel}</title>`) : nothing}
<title .textContent=${this.accessibilityLabel}></title>
<path
id="sbb-signet__icon"
d="M20.0265 40H31.6821L16 24.6154H35.3907V40H44.6093V24.6154H64.106L48.4238 40H60.0795L80 20.0531L60.0795 0H48.4238L64.106 15.3846H44.6093V0H35.3907V15.3846H16L31.6821 0H20.0265L0 20.0531L20.0265 40Z"
Expand Down

0 comments on commit 548d45d

Please sign in to comment.