0
0
Svelteframework~20 mins

HTML rendering with {@html} in Svelte - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
HTML Rendering Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What will this Svelte component render?
Consider this Svelte code snippet:
<script>
  let content = '<strong>Hello</strong> World!';
</script>

<p>{@html content}</p>

What will be the visible output in the browser?
Svelte
<script>
  let content = '<strong>Hello</strong> World!';
</script>

<p>{@html content}</p>
AThe literal text '&lt;strong&gt;Hello&lt;/strong&gt; World!' including tags
BHello World! with 'Hello' in bold
CAn empty paragraph with no visible text
DA runtime error because of unsafe HTML
Attempts:
2 left
💡 Hint
Remember that {@html} inserts raw HTML, not escaped text.
📝 Syntax
intermediate
1:30remaining
Which option correctly uses {@html} in Svelte?
You want to render a variable named 'htmlString' as raw HTML inside a div. Which code snippet is correct?
A<div>{@html htmlString}</div>
B<div>{htmlString}</div>
C<div>{@html(htmlString)}</div>
D<div>{@html: htmlString}</div>
Attempts:
2 left
💡 Hint
Check the exact syntax for {@html} in Svelte.
🔧 Debug
advanced
2:00remaining
Why does this Svelte code not render HTML tags correctly?
Given this code:
<script>
  let text = '<em>Italic</em> text';
</script>

<p>{text}</p>

Why does the browser show the tags as text instead of formatting?
Svelte
<script>
  let text = '<em>Italic</em> text';
</script>

<p>{text}</p>
ABecause {text} escapes HTML tags, so they show as text
BBecause the variable 'text' is not a string
CBecause Svelte does not support HTML tags inside variables
DBecause the <p> tag cannot contain HTML tags
Attempts:
2 left
💡 Hint
Think about how Svelte treats variables inside curly braces.
state_output
advanced
2:30remaining
What happens when the HTML string changes dynamically?
In this Svelte component:
<script>
  let htmlContent = '<span>First</span>';
  function update() {
    htmlContent = '<span style="color:red">Updated</span>';
  }
</script>

<div>{@html htmlContent}</div>
<button on:click={update}>Change</button>

What will the user see after clicking the button?
Svelte
<script>
  let htmlContent = '<span>First</span>';
  function update() {
    htmlContent = '<span style="color:red">Updated</span>';
  }
</script>

<div>{@html htmlContent}</div>
<button on:click={update}>Change</button>
AThe button click causes a runtime error
BThe text remains 'First' and does not update
CThe text changes from 'First' to 'Updated' in red color
DThe HTML tags become visible as text after update
Attempts:
2 left
💡 Hint
Svelte updates the DOM when reactive variables change.
🧠 Conceptual
expert
1:30remaining
What is a key risk of using {@html} in Svelte?
Why should you be careful when rendering user-generated content with {@html} in Svelte?
AIt slows down rendering because it converts HTML to plain text
BIt causes the app to crash if the HTML is too long
CIt automatically escapes all HTML tags, so no formatting is possible
DIt can introduce cross-site scripting (XSS) vulnerabilities if content is not sanitized
Attempts:
2 left
💡 Hint
Think about security risks when inserting raw HTML.