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

Format API - Provide a way for some attributes to be removed (or not included) in the saved format. #11484

Closed
talldan opened this issue Nov 5, 2018 · 7 comments
Labels
[Feature] Rich Text Related to the Rich Text component that allows developers to render a contenteditable [Type] Enhancement A suggestion for improvement.
Milestone

Comments

@talldan
Copy link
Contributor

talldan commented Nov 5, 2018

Is your feature request related to a problem? Please describe.
This request originates from working on #1838

Currently, in the classic editor, when a user creates a link with an invalid URL, an extra attribute data-wp-url-error="true" is added to the outputted <a> tag. This is used as a selector to add a border around the formatting, indicating there's an error:
Classic editor
screen shot 2018-11-05 at 4 53 14 pm

HTML
screen shot 2018-11-05 at 4 57 22 pm

CSS
screen shot 2018-11-05 at 4 49 21 pm

Most importantly, the data-wp-url-error attribute is not included in the post content:
screen shot 2018-11-05 at 4 59 55 pm

In Gutenberg, the Format API supports adding an additional attribute like data-wp-url-error, but the attribute will always be saved to the post content (even in the classic block). Having a way to mark this attribute as something that shouldn't be included in the post content would enable the same sort of behaviour as the classic editor's link validation.

Describe the solution you'd like
There are lots of options. When declaring a format, an additional property could be used to details attributes that aren't saved:

{
    name,
    title: __( 'Link' ),
    match: {
        tagName: 'a',
    },
    attributes: {
        url: 'href',
        target: 'target',
    },
    strippedAttributes: [
        'data-wp-url-error'
    ],
    // ...

Alternatively, any attribute beginning with 'data-wp' could be removed, or this could be something configurable at a higher level.

Describe alternatives you've considered
The format api could have an entirely different way to specify things like errors that are stored separately from attributes. To allow styling, class names could be added to the formatting tags, but only on the editor side.

@talldan talldan added [Type] Enhancement A suggestion for improvement. [Feature] Rich Text Related to the Rich Text component that allows developers to render a contenteditable labels Nov 5, 2018
@talldan
Copy link
Contributor Author

talldan commented Nov 5, 2018

In Gutenberg, the Format API supports adding an additional attribute like data-wp-url-error, but the attribute will always be saved to the post content (even in the classic block).

Thought I'd mention that this part is really a bug (and the red border is also missing in the classic block), is it worth creating a separate bug ticket to track this?

@ellatrix
Copy link
Member

ellatrix commented Nov 6, 2018

Could we not prevent invalid link from being added? Once an invalid link is added, how is the border applied on a new parse, if the attribute is gone?

@talldan
Copy link
Contributor Author

talldan commented Nov 7, 2018

Could we not prevent invalid link from being added?

I don't think it's feasible that we could write validation to adequately check all types of href values, there are a huge number of variations—different protocols, schemes, forms that could be considered valid use cases. The only option would be to massively restrict the types of hrefs that can be used.

Once an invalid link is added, how is the border applied on a new parse, if the attribute is gone?

I did wonder about that, for example when loading a post into the editor that has an invalid link. The value would have to be revalidated. I think I see where the issues with this lie with this, it looks like the formatting would have to be re-applied. Using an attribute possibly isn't the best idea then, instead would we need validation to occur during parsing?

Over here, #11286 (comment), @chrisvanpatten mentioned that the error is really metadata, and so potentially a similar use-case to the annotations api. I haven't looked too much into that, so I'm not sure if there are similarities.

@ellatrix
Copy link
Member

ellatrix commented Nov 7, 2018

Over here, #11286 (comment), @chrisvanpatten mentioned that the error is really metadata, and so potentially a similar use-case to the annotations api. I haven't looked too much into that, so I'm not sure if there are similarities.

Maybe. For annotations it's the entire format. For links it would be just an attribute.

I think I see where the issues with this lie with this, it looks like the formatting would have to be re-applied. Using an attribute possibly isn't the best idea then, instead would we need validation to occur during parsing?

I'm just trying to figure out if it can't be done in a way that is currently possible, before introducing new APIs that might not even solve the issue entirely. So apologies to be a bit pushing back here. Why can't it just be saved with all the rest? I guess you don't want the attribute on the front-end?

Maybe we need to make it so you can have an additional attribute set that sources from href, checks if it's valid and returns boolean:

url: 'href',
isValid: ( { href } ) => isValidURL( href ),

@talldan
Copy link
Contributor Author

talldan commented Nov 7, 2018

Maybe we need to make it so you can have an additional attribute set that sources from href, checks if it's valid and returns boolean

That seems like something that could work - how would the isValid attribute then affect the resulting format?

@talldan
Copy link
Contributor Author

talldan commented Jan 9, 2019

I've assigned this to the 'WordPress 5.0.x Follow Ups' milestone as the issue it blocks (#11793) is also in that milestone.

@talldan
Copy link
Contributor Author

talldan commented Mar 9, 2023

Closing as a stale issue.

@talldan talldan closed this as not planned Won't fix, can't repro, duplicate, stale Mar 9, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Feature] Rich Text Related to the Rich Text component that allows developers to render a contenteditable [Type] Enhancement A suggestion for improvement.
Projects
None yet
Development

No branches or pull requests

3 participants