001 /*
002 * Copyright 2006 Google Inc.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
005 * use this file except in compliance with the License. You may obtain a copy of
006 * the License at
007 *
008 * http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
012 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
013 * License for the specific language governing permissions and limitations under
014 * the License.
015 */
016 package com.google.gwt.user.client.ui;
017
018 import com.google.gwt.user.client.DOM;
019 import com.google.gwt.user.client.Event;
020
021 /**
022 * A widget that can contain arbitrary HTML.
023 *
024 * <p>
025 * If you only need a simple label (text, but not HTML), then the
026 * {@link com.google.gwt.user.client.ui.Label} widget is more appropriate, as it
027 * disallows the use of HTML, which can lead to potential security issues if not
028 * used properly.
029 * </p>
030 *
031 * <h3>CSS Style Rules</h3>
032 * <ul class='css'>
033 * <li>.gwt-HTML { }</li>
034 * </ul>
035 *
036 * <h3>Example</h3>
037 * {@link com.google.gwt.examples.HTMLExample code}
038 */
039 public class HTML extends Label implements HasHTML {
040
041 /**
042 * Creates an empty HTML widget.
043 */
044 public HTML() {
045 setElement(DOM.createDiv());
046 sinkEvents(Event.ONCLICK | Event.MOUSEEVENTS);
047 setStyleName("gwt-HTML");
048 }
049
050 /**
051 * Creates an HTML widget with the specified HTML contents.
052 *
053 * @param html the new widget's HTML contents
054 */
055 public HTML(String html) {
056 this();
057 setHTML(html);
058 }
059
060 /**
061 * Creates an HTML widget with the specified contents, optionally treating it
062 * as HTML, and optionally disabling word wrapping.
063 *
064 * @param html the widget's contents
065 * @param wordWrap <code>false</code> to disable word wrapping
066 */
067 public HTML(String html, boolean wordWrap) {
068 this(html);
069 setWordWrap(wordWrap);
070 }
071
072 public String getHTML() {
073 return DOM.getInnerHTML(getElement());
074 }
075
076 public void setHTML(String html) {
077 DOM.setInnerHTML(getElement(), html);
078 }
079 }