Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Definition of ExecCommand function for bold?

ExecCommand offers a way to bold text inside iFrame, make it italic, underline it etc.

But it's missing an option to create <cite> or <strong> or <em> (there is formatBlock but only for block elements and not inline ones).

I'd like to use ExecCommand function for creating <cite> - is there any way to achieve this? And obviously I want to maintain flawless parsing like in case of bold and not something like surroundContents which will fail when you use it twice on the same selection.

I'm looking for a definition of ExecCommand bold command or a way to use existing commants to flawlessly add <cite>. Any suggestions? I couldn't extract it from browser's functions. It says "native code" when I try to do that.

like image 588
Atadj Avatar asked Aug 28 '12 11:08

Atadj


People also ask

What does document execCommand do?

Creates a bulleted unordered list for the selection or at the insertion point. Inserts a paragraph around the selection or the current line. (Internet Explorer inserts a paragraph at the insertion point and deletes the selection.) Inserts the given plain text at the insertion point (deletes selection).

What can I use instead of execCommand?

The alternative to document. execCommand() is Clipboard API, via navigator. clipboard .


1 Answers

I see that CKEditor was mentioned in one of previous comments :) So this is how we handle executing commands:

  • We do everything manually. As you can see DOM API isn't sufficient and implementations differ between browsers, so I guess (but I haven't checked this) that HTML containing some text with applied styles in one browser won't work in other (because one browser applied strong and second b or span with inline style).
  • First of all we need custom Range and Selection implementations. You can't use them, but there's a cool Rangy lib.
  • Next component that you need is set of methods for applying, removing and checking styles. Here's our impl.
  • And the last component is trivial - custom commands, because it's convenient to mimic W3C's APIs and ideas.
like image 59
Reinmar Avatar answered Oct 13 '22 04:10

Reinmar