2
votes

I want to implement a simple (at least it looks this way) functionality: when user is pasting a text, copied from MS word or Open office writer, I want to wrap all the text that have been made bold in the copied document in <b></b> tags.

For example, if Word document looks like this:

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Cras velit mi, facilisis sodales facilisis sit amet, vehicula vitae urna. Sed arcu velit, eleifend et luctus a, gravida vel nulla. Etiam dapibus enim fermentum diam rutrum id hendrerit elit tincidunt. Nam enim ante, feugiat pretium iaculis id, luctus sit amet diam.

Then, if user copies and pastes it into text area, pasted text will look like this:

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Cras velit
mi, facilisis sodales <b>facilisis</b> sit amet, vehicula vitae urna. Sed
arcu velit, eleifend et luctus a, gravida vel nulla. Etiam dapibus
enim fermentum diam rutrum id hendrerit <b>elit</b> tincidunt. Nam enim ante,
feugiat pretium iaculis id, luctus sit amet diam.

I know livejournal.com used to have this functionality, so it's definetly possible. What are the principles of achieving this using javascript/jquery?

P.S. I am aware of WYSIWYG editors, but they are mostly an overkill for what I'm trying to do.

1

1 Answers

1
votes

Check out this article which details how to possibly do what you're looking for. It seems there are several caveats to doing this, however Snook managed to get a decent example working. http://snook.ca/archives/javascript/copying_from_mi

From the link:

Copying from Microsoft Word to a textarea using JavaScript

"I did some research and found that not many people had detailed a solution to do this so I've created my own solution. First, there's a couple things that you need to be aware of before you proceed. This is a client-based solution and therefore your client must have Microsoft Word installed on their machine. This solution accesses files on the client's machine and therefore your client must have their security settings to allow script not marked as safe to work..."