Search in sources :

Example 1 with DomText

use of com.gargoylesoftware.htmlunit.html.DomText in project htmlunit by HtmlUnit.

the class HTMLElement method setTextContent.

/**
 * Replaces all child elements of this element with the supplied text value.
 * @param value the new value for the contents of this element
 */
@Override
public void setTextContent(final Object value) {
    final DomNode domNode = getDomNodeOrDie();
    domNode.removeAllChildren();
    if (value != null) {
        final String textValue = Context.toString(value);
        if (StringUtils.isNotEmpty(textValue)) {
            domNode.appendChild(new DomText(domNode.getPage(), textValue));
        }
    }
}
Also used : DomNode(com.gargoylesoftware.htmlunit.html.DomNode) DomText(com.gargoylesoftware.htmlunit.html.DomText) HtmlBidirectionalOverride(com.gargoylesoftware.htmlunit.html.HtmlBidirectionalOverride)

Example 2 with DomText

use of com.gargoylesoftware.htmlunit.html.DomText in project htmlunit by HtmlUnit.

the class HTMLScriptElement method setText.

/**
 * Sets the {@code text} property.
 * @param text the {@code text} property
 */
@JsxSetter
public void setText(final String text) {
    final HtmlElement htmlElement = getDomNodeOrDie();
    htmlElement.removeAllChildren();
    final DomNode textChild = new DomText(htmlElement.getPage(), text);
    htmlElement.appendChild(textChild);
    ScriptElementSupport.executeScriptIfNeeded(htmlElement, false, false);
}
Also used : DomNode(com.gargoylesoftware.htmlunit.html.DomNode) HtmlElement(com.gargoylesoftware.htmlunit.html.HtmlElement) DomText(com.gargoylesoftware.htmlunit.html.DomText) JsxSetter(com.gargoylesoftware.htmlunit.javascript.configuration.JsxSetter)

Example 3 with DomText

use of com.gargoylesoftware.htmlunit.html.DomText in project htmlunit by HtmlUnit.

the class HTMLScriptElement method getText.

/**
 * Returns the {@code text} property.
 * @return the {@code text} property
 */
@JsxGetter
public String getText() {
    final StringBuilder scriptCode = new StringBuilder();
    for (final DomNode node : getDomNodeOrDie().getChildren()) {
        if (node instanceof DomText) {
            final DomText domText = (DomText) node;
            scriptCode.append(domText.getData());
        }
    }
    return scriptCode.toString();
}
Also used : DomNode(com.gargoylesoftware.htmlunit.html.DomNode) DomText(com.gargoylesoftware.htmlunit.html.DomText) JsxGetter(com.gargoylesoftware.htmlunit.javascript.configuration.JsxGetter)

Example 4 with DomText

use of com.gargoylesoftware.htmlunit.html.DomText in project htmlunit by HtmlUnit.

the class HTMLOptionElement method jsConstructor.

/**
 * JavaScript constructor.
 * @param newText the text
 * @param newValue the value
 * @param defaultSelected Whether the option is initially selected
 * @param selected the current selection state of the option
 */
@JsxConstructor({ CHROME, EDGE, FF, FF_ESR })
public void jsConstructor(final Object newText, final String newValue, final boolean defaultSelected, final boolean selected) {
    final SgmlPage page = (SgmlPage) getWindow().getWebWindow().getEnclosedPage();
    AttributesImpl attributes = null;
    if (defaultSelected) {
        attributes = new AttributesImpl();
        attributes.addAttribute(null, "selected", "selected", null, "selected");
    }
    final HtmlOption htmlOption = (HtmlOption) page.getWebClient().getPageCreator().getHtmlParser().getFactory(HtmlOption.TAG_NAME).createElement(page, HtmlOption.TAG_NAME, attributes);
    htmlOption.setSelected(selected);
    setDomNode(htmlOption);
    if (!Undefined.isUndefined(newText)) {
        final String newTextString = Context.toString(newText);
        htmlOption.appendChild(new DomText(page, newTextString));
        htmlOption.setLabelAttribute(newTextString);
    }
    if (!"undefined".equals(newValue)) {
        htmlOption.setValueAttribute(newValue);
    }
}
Also used : AttributesImpl(org.xml.sax.helpers.AttributesImpl) DomText(com.gargoylesoftware.htmlunit.html.DomText) SgmlPage(com.gargoylesoftware.htmlunit.SgmlPage) HtmlOption(com.gargoylesoftware.htmlunit.html.HtmlOption) JsxConstructor(com.gargoylesoftware.htmlunit.javascript.configuration.JsxConstructor)

Example 5 with DomText

use of com.gargoylesoftware.htmlunit.html.DomText in project htmlunit by HtmlUnit.

the class GWT250Test method assertElementValue.

/**
 * Test value inside {@link HtmlDivision}, {@link HtmlInput} or {@link DomText}
 *
 * @param element the element to search in
 * @param expectedValue expected value of the value inside the cell
 */
private static void assertElementValue(final HtmlElement element, final String expectedValue) {
    DomNode child = element.getFirstChild();
    while (child != null && !(child instanceof DomElement) && (!(child instanceof DomText) || !((DomText) child).getData().equals(expectedValue))) {
        child = child.getNextSibling();
    }
    if (child instanceof HtmlDivision) {
        final HtmlDivision div = (HtmlDivision) child;
        DomNode firstChild = div.getFirstChild();
        while (firstChild != null && !(firstChild instanceof DomElement)) {
            firstChild = firstChild.getNextSibling();
        }
        if (firstChild instanceof HtmlBold || firstChild instanceof HtmlItalic) {
            firstChild = firstChild.getFirstChild();
        }
        if (firstChild instanceof DomText) {
            final DomText text = (DomText) firstChild;
            assertEquals(expectedValue, text.getData());
        } else {
            fail("Could not find '" + expectedValue + "'");
        }
    } else if (child instanceof HtmlInput) {
        final HtmlInput input = (HtmlInput) child;
        assertEquals(expectedValue, input.getValueAttribute());
    } else if (child instanceof DomText) {
        final DomText text = (DomText) child;
        assertEquals(expectedValue, text.getData());
    } else {
        fail("Could not find '" + expectedValue + "'");
    }
}
Also used : DomNode(com.gargoylesoftware.htmlunit.html.DomNode) DomElement(com.gargoylesoftware.htmlunit.html.DomElement) DomText(com.gargoylesoftware.htmlunit.html.DomText) HtmlItalic(com.gargoylesoftware.htmlunit.html.HtmlItalic) HtmlBold(com.gargoylesoftware.htmlunit.html.HtmlBold) HtmlDivision(com.gargoylesoftware.htmlunit.html.HtmlDivision) HtmlInput(com.gargoylesoftware.htmlunit.html.HtmlInput)

Aggregations

DomText (com.gargoylesoftware.htmlunit.html.DomText)24 DomNode (com.gargoylesoftware.htmlunit.html.DomNode)15 SgmlPage (com.gargoylesoftware.htmlunit.SgmlPage)5 JsxFunction (com.gargoylesoftware.htmlunit.javascript.configuration.JsxFunction)5 ArrayList (java.util.ArrayList)4 JsxSetter (com.gargoylesoftware.htmlunit.javascript.configuration.JsxSetter)3 NodeList (org.w3c.dom.NodeList)3 ElementNotFoundException (com.gargoylesoftware.htmlunit.ElementNotFoundException)2 DomAttr (com.gargoylesoftware.htmlunit.html.DomAttr)2 DomCDataSection (com.gargoylesoftware.htmlunit.html.DomCDataSection)2 DomComment (com.gargoylesoftware.htmlunit.html.DomComment)2 DomElement (com.gargoylesoftware.htmlunit.html.DomElement)2 DomProcessingInstruction (com.gargoylesoftware.htmlunit.html.DomProcessingInstruction)2 HtmlPage (com.gargoylesoftware.htmlunit.html.HtmlPage)2 JsxGetter (com.gargoylesoftware.htmlunit.javascript.configuration.JsxGetter)2 Node (com.gargoylesoftware.htmlunit.javascript.host.dom.Node)2 FunctionObject (net.sourceforge.htmlunit.corejs.javascript.FunctionObject)2 Test (org.junit.Test)2 DomDocumentFragment (com.gargoylesoftware.htmlunit.html.DomDocumentFragment)1 DomDocumentType (com.gargoylesoftware.htmlunit.html.DomDocumentType)1