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

feat(gnoweb): form - input md extension #4061

Open
wants to merge 19 commits into
base: master
Choose a base branch
from

Conversation

alexiscolin
Copy link
Member

@alexiscolin alexiscolin commented Apr 5, 2025

This PR introduces secure and interactive form handling capabilities to Gno realms, enabling user input through markdown-based forms with built-in security measures.

Technical Implementation

HTTP Handler Enhancements

  • Implemented POST request handling with form data processing in realms
  • Automatic transformation of form data into query parameters
  • GET request redirection for state management
  • Integration with the mux package for internal realm argument modification

Markdown Extensions

  • Introduced custom HTML tags:
    • <gno-form>: Container for form elements
    • <gno-input>: Text input fields (text only)
  • Attribute Support:
    • Required name attribute for input identification
    • Optional placeholder for user guidance
  • Automatic UI Components (for Security and UX):
    • Submit button generation
    • Input labels with placeholder fallback
    • Realm namespace display
    • Security tooltip

Security & UX Features

  • Realm-specific form processing (no cross-realm submissions)
  • Clear visual indication of form ownership
  • Security tooltip explaining the local processing
  • Automatic label generation with placeholder fallback
  • Input validation and error handling

Validation Rules

  • Required name attribute enforcement
  • Nested form prevention
  • Duplicate attribute detection
  • Proper HTML tag closure verification

Example Implementation

<gno-form>
  <gno-input name="username" placeholder="Enter your name" />
  <gno-input name="email" placeholder="Enter your email" />
</gno-form>

When submitted, the form data is automatically transformed into query parameters and redirected as a GET request. The realm must implement the necessary logic to handle these parameters and update its state accordingly.

Security Considerations

  • All form processing occurs within the realm's namespace
  • No cross-realm form submissions allowed
  • Input validation at both client and server levels
  • Clear visual indicators of form ownership and security
Form Implementation Example

@github-actions github-actions bot added the 📦 ⛰️ gno.land Issues or PRs gno.land package related label Apr 5, 2025
@Gno2D2 Gno2D2 requested a review from gfanton April 5, 2025 12:46
@Gno2D2 Gno2D2 added the review/triage-pending PRs opened by external contributors that are waiting for the 1st review label Apr 5, 2025
@Gno2D2
Copy link
Collaborator

Gno2D2 commented Apr 5, 2025

🛠 PR Checks Summary

All Automated Checks passed. ✅

Manual Checks (for Reviewers):
  • IGNORE the bot requirements for this PR (force green CI check)
Read More

🤖 This bot helps streamline PR reviews by verifying automated checks and providing guidance for contributors and reviewers.

✅ Automated Checks (for Contributors):

🟢 Maintainers must be able to edit this pull request (more info)
🟢 Changes related to gnoweb must be reviewed by its codeowners
🟢 Pending initial approval by a review team member, or review from tech-staff

☑️ Contributor Actions:
  1. Fix any issues flagged by automated checks.
  2. Follow the Contributor Checklist to ensure your PR is ready for review.
    • Add new tests, or document why they are unnecessary.
    • Provide clear examples/screenshots, if necessary.
    • Update documentation, if required.
    • Ensure no breaking changes, or include BREAKING CHANGE notes.
    • Link related issues/PRs, where applicable.
☑️ Reviewer Actions:
  1. Complete manual checks for the PR, including the guidelines and additional checks if applicable.
📚 Resources:
Debug
Automated Checks
Maintainers must be able to edit this pull request (more info)

If

🟢 Condition met
└── 🟢 And
    ├── 🟢 The base branch matches this pattern: ^master$
    └── 🟢 The pull request was created from a fork (head branch repo: alexiscolin/gno)

Then

🟢 Requirement satisfied
└── 🟢 Maintainer can modify this pull request

Changes related to gnoweb must be reviewed by its codeowners

If

🟢 Condition met
└── 🟢 And
    ├── 🟢 The base branch matches this pattern: ^master$
    └── 🟢 A changed file matches this pattern: ^gno.land/pkg/gnoweb/ (filename: gno.land/pkg/gnoweb/components/ui/icons.html)

Then

🟢 Requirement satisfied
└── 🟢 Or
    ├── 🔴 This user reviewed pull request: alexiscolin
    └── 🟢 This user reviewed pull request: gfanton

Pending initial approval by a review team member, or review from tech-staff

If

🟢 Condition met
└── 🟢 And
    ├── 🟢 The base branch matches this pattern: ^master$
    └── 🟢 Not (🔴 Pull request author is a member of the team: tech-staff)

Then

🟢 Requirement satisfied
└── 🟢 If
    ├── 🟢 Condition
    │   └── 🟢 Or
    │       ├── 🔴 At least 1 user(s) of the organization reviewed the pull request (with state "APPROVED")
    │       ├── 🟢 At least 1 user(s) of the team tech-staff reviewed pull request
    │       └── 🔴 This pull request is a draft
    └── 🟢 Then
        └── 🟢 Not (🔴 This label is applied to pull request: review/triage-pending)

Manual Checks
**IGNORE** the bot requirements for this PR (force green CI check)

If

🟢 Condition met
└── 🟢 On every pull request

Can be checked by

  • Any user with comment edit permission

Copy link

codecov bot commented Apr 5, 2025

Codecov Report

Attention: Patch coverage is 76.12613% with 53 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
gno.land/pkg/gnoweb/markdown/ext_forms.go 85.26% 25 Missing and 3 partials ⚠️
gno.land/pkg/gnoweb/handler.go 13.79% 25 Missing ⚠️

📢 Thoughts on this report? Let us know!

@alexiscolin alexiscolin marked this pull request as draft April 5, 2025 12:50
@Gno2D2 Gno2D2 removed the review/triage-pending PRs opened by external contributors that are waiting for the 1st review label Apr 5, 2025
@alexiscolin alexiscolin added the 🌍 gnoweb Issues & PRs related to gnoweb and render label Apr 5, 2025
@alexiscolin alexiscolin changed the title WIP(gnoweb): form - input extension feat(gnoweb): form - input md extension Apr 7, 2025
@alexiscolin alexiscolin marked this pull request as ready for review April 7, 2025 08:25
@alexiscolin alexiscolin marked this pull request as draft April 7, 2025 08:25
@alexiscolin
Copy link
Member Author

alexiscolin commented Apr 7, 2025

Regarding a #3899 form implementation, it would need a modification of the realm to handle the query, like:

router.HandleFunc("/", func(res *mux.ResponseWriter, req *mux.Request) {
		hasDenom, hasAddr := req.Query.Has("denom"), req.Query.Has("addr")
		switch {
		case hasAddr && hasDenom:
			denom := req.Query.Get("denom")
			addr := req.Query.Get("addr")
			res.Write(renderAddressBalance(denom, std.Address(addr)))

		case hasDenom:
			denom := req.Query.Get("denom")
			totalSupply := TotalSupply(denom)
			res.Write(renderCoinInfo(denom, totalSupply))

		default: // default rendering
			res.Write(renderHomepage())
		}
	})

cc @notJoon

@alexiscolin alexiscolin marked this pull request as ready for review April 7, 2025 17:34
@Gno2D2 Gno2D2 added the review/triage-pending PRs opened by external contributors that are waiting for the 1st review label Apr 7, 2025
@notJoon
Copy link
Member

notJoon commented Apr 8, 2025

Regarding a #3899 form implementation, it would need a modification of the realm to handle the query, like:

@alexiscolin
Checked. Thank you for letting me know 👍

@leohhhn
Copy link
Contributor

leohhhn commented Apr 9, 2025

Can you please add an example of how to use this? preferably in r/docs/markdown?

@github-actions github-actions bot added the 🧾 package/realm Tag used for new Realms or Packages. label Apr 11, 2025
Copy link
Member

@gfanton gfanton left a comment

Choose a reason for hiding this comment

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

Besides my comments, I was thinking that since we ultimately fall back on ||| for columns, we could keep things markdown-friendly ?

syntax idea:

<gno-form>
@input[My Username](username)
@input_password[My Password](password)
@input_<type>[<placeholder>](<name>) # or maybe the other way around ?
@submit[Login]
</gno-form>

Im not against keeping html style but i think we could go for a more markdown syntax.

wdyt ?

-- output.html --
<form class="gno-form" method="post">
<div class="gno-form_header">
<span><span class="font-bold"> r/test </span> Form</span><span class="tooltip" data-tooltip="This form is secure and processed on the current r/test realm itself."><svg class="w-4 h-4"><use href="#ico-info"></use></svg></span></div>
Copy link
Member

Choose a reason for hiding this comment

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

error should be for something not recoverable, i suggest you simply override the previous value if duplicate and ignore attribute if it doest not exist.

Comment on lines +63 to +71
// formContext is used to keep track of form's state across parsing.
type formContext struct {
PrevContext *formContext
IsOpen bool // Indicates if a block has been correctly opened.
Index int // Index of the current form; 0 indicates no form.
OpenTag *FormNode // First opening tag for this context.
HasValidInputs bool // Indicates if the form has at least one valid input
RealmName string // Realm name from context
}
Copy link
Member

@gfanton gfanton Apr 11, 2025

Choose a reason for hiding this comment

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

Context makes sense for columns because you have to track tags between other markdown components. For your specific case, it's a single block, so you have to use Continue + Close to create your markdown component.

@Gno2D2 Gno2D2 removed the review/triage-pending PRs opened by external contributors that are waiting for the 1st review label Apr 11, 2025
Comment on lines +368 to +371
<gno-form>
<gno-input name="name" placeholder="Enter your name" />
<gno-input name="email" placeholder="Enter your email" />
</gno-form>
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
<gno-form>
<gno-input name="name" placeholder="Enter your name" />
<gno-input name="email" placeholder="Enter your email" />
</gno-form>
<form>
<input name="name" placeholder="Enter your name" />
<input name="email" placeholder="Enter your email" />
</form>

Wouldn't this make sense in GNO and standard HTML? What is the default behavior in HTML when we don't specify the method and action of a <form>?

If we pursue this, I suggest using the original W3C HTML but with filtered parameters. For instance, we could disable all the parameters of <form> for now, and include only the name, placeholder, type, and value for ainputandtextarea` elements.

Regarding the submit functionality, I believe we can add support for the submit input type. However, I suggest establishing a default behavior when there is no submit button, such as responding to the "Enter" key or applying changes when the user leaves the field.

As a reminder to readers, this original PR only supports GET forms. This allows for more flexible interaction with realms rendering. However, we currently do not permit forging transactions using HTML forms; this capability is only available through wallets at this time or by using txlink from the Render function. We will consider TX builders from forms in the future.

@leohhhn
Copy link
Contributor

leohhhn commented Apr 11, 2025

Would be great if we could also specify the render path for the form, apart from the query params. This would allow us to go to a specific page within the same realm, ie :user?name=alexis (although it's doable with queries as well, its ugly).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🌍 gnoweb Issues & PRs related to gnoweb and render 📦 ⛰️ gno.land Issues or PRs gno.land package related 🧾 package/realm Tag used for new Realms or Packages.
Projects
Status: In Progress
Status: Triage
Development

Successfully merging this pull request may close these issues.

6 participants