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

Rich text: add HTML string methods to RichTextData #57322

Merged
merged 1 commit into from
Jan 4, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions packages/rich-text/src/create.js
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,19 @@ export class RichTextData {
}
}

for ( const name of Object.getOwnPropertyNames( String.prototype ) ) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Do you think converting all methods like that is good? I mean, there's a huge chance that most these methods will create sync issues between the string and the object no? Maybe we should convert case by case.

Copy link
Member Author

Choose a reason for hiding this comment

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

What do you mean with sync issues? Sure, I can hardcode the list here instead if you prefer.

if ( RichTextData.prototype.hasOwnProperty( name ) ) {
continue;
}

Object.defineProperty( RichTextData.prototype, name, {
value( ...args ) {
// Should we convert back to RichTextData?
return this.toHTMLString()[ name ]( ...args );
Copy link
Member Author

Choose a reason for hiding this comment

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

I think it would be great if we convert back to RichTextData here, but I'm not sure. One downside would be that you'd be parsing and serialising every time you use one of these methods.

Copy link
Member Author

Choose a reason for hiding this comment

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

In the future, should also warn when using these methods, but not until we have alternative methods available.

},
} );
}

/**
* Create a RichText value from an `Element` tree (DOM), an HTML string or a
* plain text string, with optionally a `Range` object to set the selection. If
Expand Down
Loading