Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

fix: throw runtime error when template returns different html#15524

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Draft
paoloricciuti wants to merge2 commits intomain
base:main
Choose a base branch
Loading
frominvalid-html-structure-error

Conversation

paoloricciuti
Copy link
Member

Closes#15502

I've added a runtime error that checks if the html generated by thetemplate is the same structure as the passed in string. This should prevent more obscure errors because if the structure is different our marching of the dom will still fail at runtime.

To properly check i had to transform both the innerHTML and the incoming structure to remove inner texts (that get's converted in html entities) and attributes (because boolean attributes get="" appended automatically.

Also i don't do this check in{@html} because you could technically pass invalid html there but that doesn't affect the marching algorithm.

Another idea I had (might still open a PR as an alternative) is to add avalidate_element call after everynext orchild...basically like this

import'svelte/internal/disclose-version';import'svelte/internal/flags/legacy';import*as$from'svelte/internal/client';varroot=$.template(`<p></p> <tr><td></td></tr>`,1);exportdefaultfunctionApp($$anchor){letname='world';varfragment=root();vartr=$.sibling($.first_child(fragment),2);$.validate_element(tr,'tr');vartd=$.child(tr);$.validate_element(td,'td');td.textContent=name;$.reset(tr);$.append($$anchor,fragment);}

but i think this would be more unreliable.

Before submitting the PR, please make sure you do the following

  • It's really useful if your PR references an issue where it is discussed ahead of time. In many cases, features are absent for a reason. For large changes, please create an RFC:https://github.com/sveltejs/rfcs
  • Prefix your PR title withfeat:,fix:,chore:, ordocs:.
  • This message body should clearly illustrate what problems it solves.
  • Ideally, include a test that fails without this PR but passes with it.
  • If this PR changes code withinpackages/svelte/src, add a changeset (npx changeset).

Tests and linting

  • Run the tests withpnpm test and lint the project withpnpm lint

@changeset-botchangeset-bot
Copy link

changeset-botbot commentedMar 16, 2025
edited
Loading

🦋 Changeset detected

Latest commit:62a6a15

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
NameType
sveltePatch

Not sure what this means?Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@svelte-docs-bot
Copy link

@github-actionsGitHub Actions
Copy link
Contributor

Playground

pnpm add https://pkg.pr.new/svelte@15524

Copy link
Member

@Rich-HarrisRich-Harris left a comment

Choose a reason for hiding this comment

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

Nice idea! This would be a breaking change though so it would need to be a warning rather than an error. I suspect the proposed alternative approach would work better — details inline. (It wouldn't work for{@html ...} though... so maybe we need both?)

Also, we should do this checking eagerly, otherwise no warning will be issued for malformed HTML in a block that happens not to get rendered

@@ -99,7 +99,7 @@ export function html(node, get_value, svg, mathml, skip_warning) {
// Don't use create_fragment_with_script_from_html here because that would mean script tags are executed.
// @html is basically `.innerHTML = ...` and that doesn't execute scripts either due to security reasons.
/** @type {DocumentFragment | Element} */
var node = create_fragment_from_html(html);
var node = create_fragment_from_html(html, false);
Copy link
Member

Choose a reason for hiding this comment

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

can you elaborate on why we're not doing this for{@html ...}? It may survive hydration but it's still delivering an unexpected outcome. Feels like we should just do it in all cases

Copy link
MemberAuthor

Choose a reason for hiding this comment

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

It mainly goes down to self-closing-but-not-really tags.

{@html"<div />"}

This will yield an error (I suppose if we switch to a warning it could be fine tho)

// we remove the text within the elements because the template change & to &amp; (and similar)
.replace(/>([^<>]*)/g, '>');
if (remove_attributes_and_text_input !== remove_attributes_and_text_output) {
e.invalid_html_structure(remove_attributes_and_text_input, remove_attributes_and_text_output);
Copy link
Member

Choose a reason for hiding this comment

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

The HTML snippets could become arbitrarily large (especially if we enabled this for{@html ...} as well), we may need to adding truncation here, or remove common prefixes/suffixes etc

The alternative approach of checking the structure manually could help here since it means we can point to the specific element that's being mishandled. (Perhapsadd_locations becomes a validation function as well — it already knows the intended structure.)

Copy link
MemberAuthor

Choose a reason for hiding this comment

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

Yeah I though about that but also thought a diffing algorithm would be too much and not showing complete information would be not really helpful.

I can try to put up a PR for the alternative method tomorrow to check if it could yield better results

@paoloricciuti
Copy link
MemberAuthor

This would be a breaking change though so it would need to be a warning rather than an error

Can you think of some cases where having the wrong output from the template would not lead to a runtime error anyway? I thought of making it a warning but I also thought that it would be a runtime error anyway.

@Rich-Harris
Copy link
Member

Many cases where you're not hydrating (the test case in this PR, for example). Hopefully there aren't too many of those cases in the wild but there's probably some where it's 'failing' in a way that's harmless

@paoloricciuti
Copy link
MemberAuthor

Many cases where you're not hydrating (the test case in this PR, for example). Hopefully there aren't too many of those cases in the wild but there's probably some where it's 'failing' in a way that's harmless

Mmm that's annoying...well gonna move it to warning tomorrow 😁

@7nik
Copy link
Contributor

Do I miss something orthe issue example still throws the same error? And I don't see how it will help when the problem caused by invalid composition of valid components, e.g.<Button><Button>btn</Button></Button>

@paoloricciuti
Copy link
MemberAuthor

Do I miss something orthe issue example still throws the same error? And I don't see how it will help when the problem caused by invalid composition of valid components, e.g.<Button><Button>btn</Button></Button>

Yes because now it's just a warning that you can see in the console.

Also no, it will not solve that issue, only local marching problems

7nik reacted with thumbs up emoji

@Rich-Harris
Copy link
Member

Come to think of it, couldn't we find these issues at compile time?

@Rich-Harris
Copy link
Member

(I mean not for{@html ...} but for anything that gets turned into a$.template call)

@paoloricciuti
Copy link
MemberAuthor

Come to think of it, couldn't we find these issues at compile time?

The main issue i see with this is that I'm pretty sure this is browser dependent...a browser might correct in a different way. Also keeping up with the actual transformation the browser does would be very error prone.

@Rich-Harris
Copy link
Member

I would bevery surprised if browsers differed — this stuff is all spec'd, it's not open to interpretation. I'm not sure exactly what rules we'd need to follow but I bet we could figure it out

@dummdidumm
Copy link
Member

Come to think of it, couldn't we find these issues at compile time?

We already find all statically analyzable issues at compile time. But once the invalid-ness of the template appears cross-component you can only check it at runtime.

@Rich-Harris
Copy link
Member

The test case in this PR contains a single component:

<p></p><tr></tr>

So we'renot finding all statically analyzable issues at compile time. And this PR doesn't touch cross-component stuff, it only warns on invalid templates. Aside from{@html ...}, there's nothing we can detect at runtime that we couldn't in theory detect at compile time where it's much more actionable. So I don't think we should merge this in its current state — converting to draft for now

@Rich-HarrisRich-Harris marked this pull request as draftMarch 31, 2025 19:35
@paoloricciuti
Copy link
MemberAuthor

I think we would have to do something similar to what Rich is suggesting to support the new quirky chrome parsing of select/option so we might just as well fix it in a more general way when we also fix that bug

Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Reviewers

@Rich-HarrisRich-HarrisRich-Harris left review comments

At least 1 approving review is required to merge this pull request.

Assignees
No one assigned
Labels
None yet
Projects
None yet
Milestone
No milestone
Development

Successfully merging this pull request may close these issues.

Unhelpful error message when dealing with invalid markup
4 participants
@paoloricciuti@Rich-Harris@7nik@dummdidumm

[8]ページ先頭

©2009-2025 Movatter.jp