Reviewers: jgw, Description: This patch adds the ability to insert arbitrary HTML at the current caret position in a RichTextArea. The InsertHTML command is actually widely supported on all browsers except IE. I used IEs doc.selection to achieve the same effect.
If text is selected, it will be overwritten by the new HTML. If text is not selected, the html will be inserted at the caret position. This patch is really cool because it allows users to insert richer html into the text area, such as tables or custom widgets. Testing: ======= I manually verified that this works on all browsers. Please review this at http://gwt-code-reviews.appspot.com/48804 Affected files: user/src/com/google/gwt/user/client/ui/RichTextArea.java user/src/com/google/gwt/user/client/ui/impl/RichTextAreaImplIE6.java user/src/com/google/gwt/user/client/ui/impl/RichTextAreaImplStandard.java Index: user/src/com/google/gwt/user/client/ui/impl/RichTextAreaImplIE6.java =================================================================== --- user/src/com/google/gwt/user/client/ui/impl/RichTextAreaImplIE6.java (revision 5628) +++ user/src/com/google/gwt/user/client/ui/impl/RichTextAreaImplIE6.java (working copy) @@ -61,6 +61,25 @@ }-*/; @Override + public native void insertHTML(String html) /*-{ + try { + var elem = [email protected]::elem; + var doc = elem.contentWindow.document; + var tr = doc.selection.createRange(); + if (tr == null) { + return; + } + if ([email protected]::isOrHasChild(Lcom/google/gwt/user/client/Element;Lcom/google/gwt/user/client/Element;)(doc.body, tr.parentElement())) { + return; + } + tr.pasteHTML(html); + } + catch (e) { + return; + } + }-*/; + + @Override protected native String getTextImpl() /*-{ var elem = [email protected]::elem; return elem.contentWindow.document.body.innerText; Index: user/src/com/google/gwt/user/client/ui/impl/RichTextAreaImplStandard.java =================================================================== --- user/src/com/google/gwt/user/client/ui/impl/RichTextAreaImplStandard.java (revision 5630) +++ user/src/com/google/gwt/user/client/ui/impl/RichTextAreaImplStandard.java (working copy) @@ -88,6 +88,10 @@ execCommand("InsertHorizontalRule", null); } + public void insertHTML(String html) { + execCommand("InsertHTML", html); + } + public void insertImage(String url) { execCommand("InsertImage", url); } Index: user/src/com/google/gwt/user/client/ui/RichTextArea.java =================================================================== --- user/src/com/google/gwt/user/client/ui/RichTextArea.java (revision 5630) +++ user/src/com/google/gwt/user/client/ui/RichTextArea.java (working copy) @@ -186,6 +186,13 @@ void insertHorizontalRule(); /** + * Inserts generic html. + * + * @param html the HTML to insert + */ + void insertHTML(String html); + + /** * Inserts an image element. * * @param url the url of the image to be inserted --~--~---------~--~----~------------~-------~--~----~ http://groups.google.com/group/Google-Web-Toolkit-Contributors -~----------~----~----~----~------~----~------~--~---
