-
Notifications
You must be signed in to change notification settings - Fork 28
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
RFC: Switch to disable String-to-HTML-parsing #268
Comments
Hi @SmithChart, currently there is no way to implement a settings switch that changes the behavior of the
That code like in your example crashes is a feature, not a bug. If your HTML snippet would be rendered into a website, the browser would try to display the email address as a HTML tag which would result in broken output. Lona always crashes if something is given that may not be displayable as intended by the developer. |
Ack.
I have seen that feature in the source. Should we advertise that in the documentation? I could fix all these problems by simply wrapping all user input in a
I can't reproduce this behaviour. On my side neither Firefox nor Chromium render text that contain HTML-tags. I'll add the following view to my example above:
|
https://lona-web.org/1.x/end-user-documentation/html.html#using-html-strings
Hm. The now widget, soon node is named |
Sorry, must have missed that :/
I am not against parsing a String as HTML. But maybe it should not be enabled by default. In my opinion parsing string as HTML is the special case, while forwarding user input to the client is the default case. In my case: But currently I would need |
I agree that parsing HTML is a special case. And I think it isn't used in 90% (or more?) apps now. But I'm not sure we should change something in lona 1. It may be redesigned in lona 2.
I recommend to wrap it with |
For me it's a pretty important feature because its great for building components for third-party libraries. https://github.com/lona-web-org/lona-bootstrap-5/blob/master/lona_bootstrap_5/modal.py#L12
I am open to proposals! But to be honest, i am not unhappy withe the current api |
I'm not insist, but it could be a dedicated |
If we decide Code like: return HTML(
H1('Hello World'),
P('Lorem Ipsum'),
) never produced an actual I thought it was pretty straight forward that |
Much time has passed, and i am still not sure what to do here. I am not completely happy with what Note: I am describing the behavior that is currently planned for Lona 2. 1. It gets used as a token, to communicate "this function/view returns HTML"Every HTML tree, used by a Lona view or component can have exactly one root node. So code like return (
H1('Hello'),
P('World'),
) would not work. When return HTML(
H1('Hello'),
P('World'),
) because 2. It "corrects" HTML node trees with more than one root nodeWhen given a HTML tree, as string or as Node objects, that has more than one root node, the whole tree gets wrapped into one 3. It parses HTML stringsSince html = HTML(
'<div id="foo">foo</div>',
Div('<div id="bar">bar</div>'),
) Possible SolutionI agree, it would be more intuitive to split these functionalities, and add return HTML(
H1('Hello'),
P('World'),
) We could either enforce that the application code has to provide exactly one root node, and wrap all HTML into one There is even one more problem: I lean towards a solution where we add |
Even some more time has passed here... (Sorry for disappearing.)
Making this impossible will very likely break 100% of my views :-) |
No worries!
Same goes for me and all views in the test_project folder :) Such a fundamental change could be made in a major release like Lona 2 though. I am still hesitant to remove this syntax because I (completely biased) think it is very intuitive and good at communicating the intend of that return statement. |
@SmithChart, @maratori: I think I know what I want to do here now and opened #436 The PR adds parse_html('<div>Hello World</div>') # returns one div
parse_html('<div>Hello</div><div>World</div>') # returns a list of two divs
class MyView(View):
def handle_request(self, request):
return HTML(
H1('Hello World'),
P('Lorem Ipsum'),
) |
I like the idea moving String-HTML-Parsing into a separate function. This still allows to easily integrate HTML-source for library binding and (at the same time) reduces the chance of accidentally parsing user-input as HTML. Deprecation sounds like a good plan for me. Do we have a roadmap with Lona 2 changes somewhere? |
Great! I will update the PR and the documentation and un-draft it afterwards.
No. I have a private todo-list, and you can find all spots when greping for |
Cheers,
the real world hit my Lona project again. Before starting with a PR I would like to hear your opinion on how to go forward here.
tl;dr: I would like to add a settings-switch that disables the auto HTML-parsing in
HTML()
-objects.IMHO auto-parsing is a footgun. With it enabled the developer has to escape each and every user input that could be passed (directly) into an HTML-object at some point. If the developers fails to do so a user can either break the application or inject custom HTML into the frontend.
And: With this feature enabled
HTML
-objects behave differently than other nodes (where there is no auto-parsing).Consider the following example:
In
/1
Lona will take the String containing the<p>
and will construct an actualNode
from that. The output will be:Now in
/2
we don't have a nice developer formatted string but some user input. (In our case this most often is an E-Mail message-ID we paste in a comment field for future reference).Lona will assume that something containing
<
or>
must be HTML and tries to parse it. This will fail:In
/3
the user input will now be passed to the frontend without being parsed as HTML by neither Lona nor the Browser.I am not sure if anybody is using Lona for the reason that one can build HTML-trees from strings. At least I / we are not using it that way.
I would suggest to add a switch to the settings that would disable the
isinstance(node, str)
check here:lona/lona/html/widgets.py
Line 13 in 87d0267
HTML
would be directly passed on to the client and user input could not break the application in this way.By leaving parsing enabled by default the behaviour of the application would not change. But disabling it would still allow an developer to pass user input to the frontend inside a
HTML
and not only inside aNode
.On the other hand one could also argue that having different behaviour in parsing between
HTML
andNode
is not desired. But I don't have made a decision if I would like to go that far.So: What's your opinion: Add that switch? Should the feature stay enabled by default?
The text was updated successfully, but these errors were encountered: