When text is selected by triple clicking or over-selecting, it will create extra lines when pasted

  • When using React/Typescript, I have a function that shows user input:
function displayMessage({ message }: UserMessage) {

    return (
       <div>
          {message.split('\n').map((line) => (
             <Fragment>{line}</Fragment>
          ))}
       </div>
    );
}

The issue arises when triple clicking to copy and paste the text. An extra line is added in the pasted content like so:

Input One-liner:

random text

Output Two-liner:

random text


Input: Multiple Lines

line1
line2
line3

Output: Multiple Lines.

This occurs only after selecting with three clicks, copying, and then pasting. The problem does not occur with normal two-click selection :/

line1
line2
line3

I attempted adjusting the CSS using the whiteSpace property, but it had no effect. :(

The issue persists across Firefox, Chrome, and Edge browsers.

Edit 1:

The operating system being used is Windows. Testing on other operating systems has not been done.

Edit 2:

I suspect that the HTML tag is included in the copied text along with the three-click selection or "Over-selecting," resulting in the extra line break upon pasting.

A similar problem can be observed widely on the internet. For instance, visit Worldcounter.net Here is an image displaying the outcome from the world counter!

Try copying the selected text as shown in the image, then paste it :)

Answer №1

Issue :

The issue arises when triple-clicking to copy and paste, resulting in an extra line in the pasted text.

Cause :

Upon triple-clicking on various web pages within a browser and copying that text into Notepad++, I discovered that enabling the Show Symbol option under the View Tab revealed the presence of CRLF control characters embedded within certain tags like p tag and li tag. The CRLF control character instructs the following character to move to the next line and then back to the beginning of the line.

Solution :

  • One possible solution is to replace the \n pattern with \r\n, specifically targeting the CRLF character.

  • Additionally, you can filter out empty characters by utilizing the trim() function on msg, followed by splitting and filtering elements to return true values (not null, undefined, or empty strings) using map.

    <div>
      {msg.trim().split('\r\n').filter(e => e).map((line) => (
          <Fragment>{line}</Fragment>
       ))}
    </div>
    

For More Information :

If you still have any questions or concerns, please leave a comment below

Answer №2

This method has proven successful for me:

<div style="user-select: none;">
    <span style="position: absolute; user-select: text;">
      Lorem ipsum dolor sit amet consectetur adipisicing elit. Laudantium molestiae dolores,
      suscipit officiis distinctio quos accusamus quas tenetur odio nostrum, soluta, fugit
      consequatur at quo temporibus. Non nisi expedita blanditiis?
    </span>
  </div>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Angular 4 with Typescript allows for the quick and easy deletion of multiple selected rows

I am currently working on an application where I need to create a function that will delete the selected checkboxes from an array. I have managed to log the number of checkboxes that are selected, but I am struggling to retrieve the index numbers of these ...

Challenges with loading content and async JavaScript within websites

I decided to replace the content on index.htm with the content from project.htm. By clicking on a#front, it redirects to project.htm and dynamically updates the content. However, I am facing an issue regarding how to run the javascript that accompanies thi ...

React's stackable snackbars/toasts offer a convenient and efficient way

I am currently developing my own simplistic snackbar/toast stacker, but I am encountering difficulties with properly queuing them. When attempting to remove a snack from the queue, it results in re-rendering and unexpected behavior. The sequence of events ...

Can you explain the significance of O("info") in the JavaScript code given below?

I came across a passage in a book discussing the process of passing an ajax html form to validate a username using code similar to this: function checkUser(user) { if (user.value == "") { O('info').innerHTML = '' re ...

Incorporate CSS styling into ScalaHelpers

Is it possible to customize the CSS for Scala Helpers and remove certain text from the textfield? https://i.sstatic.net/eG6HY.png @inputText(advForm("weeknr")) @inputText(advForm("jaar")) @inputText(advForm("datum")) --------------------EDIT 1---------- ...

What is the best way to assign a variable with the type (x:number)=>{y:number,z:number}?

I am trying to initialize a variable called foo, but my current code is not compiling successfully. let foo: (x: number) => {y:number,z: number} = (x) => {x+1, x+2}; This results in the following error: Left side of comma operator is unused and ha ...

NextJS 14 bug causing issues with getStaticPaths() and getStaticProps() for nested routes

Currently, I am in the process of setting up a blog structure that includes two levels of nested routes. You can access the posts at example.com/blogs/[chapterID]/[postID], and these static posts are saved as markdown files outside the app folder. https:/ ...

Customizing font weights in Bootstrap 5.3 through CSS stylesheet: A step-by-step guide

My goal is to have my h1 heading styled with Montserrat font in black and font weight 900. However, when I checked my website using Google Developer Tools, I noticed that the CSS code I applied is being overridden by the Bootstrap link. Unfortunately, dele ...

Develop a step-by-step chart for implementing in React

I am currently creating a web application to assist parents in monitoring their children's vaccination schedule. I am looking for a way to implement a table that allows operations to be performed at the cell level, such as clicking on a cell to mark a ...

Alphabetic divider for organizing lists in Ionic

I am currently working with a list in ionic that is fetched from a controller and stored in localStorage. My goal is to add alphabetic dividers to the list, but I am facing some confusion on how to achieve this. Here is a snippet of the code: app.js $ion ...

The test execution in Azure Dev Ops using react-scripts seems to be stuck indefinitely

Having trouble getting my React app's tests to stop running in Azure Dev Ops after they finish. The pipeline just hangs indefinitely while the tests have already completed. This is a simple create-react-app with a few tests included. Here is the YAML ...

What causes the cleanup function in React hooks to be triggered upon reopening a previously closed tab?

There seems to be an issue with closing a tab and then undoing that action. This causes all the cleanup functions in the component to execute, resulting in the abortion of new fetches needed to load the component. This behavior is observed only on Safari ...

Tips on selecting specific text from a drop-down menu

After struggling to find a solution for retrieving the text of a selected item from a dropdown, I decided to create a common directive. This directive would allow me to easily access the text for all dropdown items when used in my code. Below is the snippe ...

What is the best way to showcase information in Angular?

I am facing an issue where my cards are not displaying data from the database, they appear empty. Can anyone provide a solution to help me fix this problem? Below is the output I am getting, with the empty cards that I want to populate with data. https:// ...

Leveraging useContext with ImmutableJS

My data Object is stored as the context, and I populate it with API responses. However, I suspect there may be performance issues with my current setup. I have looked into ImmutableJS but can't find any information on integrating it with ContextAPI. W ...

Discover how to access the translations of a specific key in a chosen language by utilizing the angular $translate functionality

How can I retrieve a specific language translation using angular's $translate function within a controller? The $translate.instant(KEY) method returns the translation of the specified key based on the currently selected language. What I am looking for ...

A guide to accessing parent attributes in Vue3 using typescript

Within my child component, I am facing an issue where I need to access the parent object but the commented lines are not functioning as expected. The structure of AccordionState is defined below: export type AccordionKeys = | "open" | "disa ...

Troubleshooting issue with absolute paths in Vite project using React and TypeScript

I'm having trouble implementing absolute paths in a Vite react-ts project. This is how I set up the project: npm init @vitejs/app npx: installed 6 in 1.883s √ Project name: ... test-vite √ Select a framework: » react √ Select a variant: » rea ...

Using React - accessing a variable that has been declared within the componentDidMount method and utilizing it within the render

Below is the code I have implemented to retrieve an object from Window and utilize it during rendering. Upon checking the console, I noticed that the value of componentData appears as undefined. How can I resolve this issue and access the value properly? ...

Reset hover effect upon exiting the current website

Hey there! I am currently dealing with an interesting situation regarding a link styled as a button on my website. <a href="http://externalsite" class="button" target="_blank">go to external site</a> Additionally, I have applied a hover effec ...