Search in sources :

Example 1 with HtmlLink

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

the class HTMLLinkElement method getHref.

/**
 * Returns the value of the href property.
 * @return the href property
 */
@JsxGetter
public String getHref() {
    final HtmlLink link = (HtmlLink) getDomNodeOrDie();
    final String href = link.getHrefAttribute();
    if (href.isEmpty()) {
        return href;
    }
    try {
        return ((HtmlPage) link.getPage()).getFullyQualifiedUrl(href).toString();
    } catch (final MalformedURLException e) {
        return href;
    }
}
Also used : MalformedURLException(java.net.MalformedURLException) HtmlLink(com.gargoylesoftware.htmlunit.html.HtmlLink) JsxGetter(com.gargoylesoftware.htmlunit.javascript.configuration.JsxGetter)

Example 2 with HtmlLink

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

the class CSSStyleSheet method getHref.

/**
 * Returns the URL of the stylesheet.
 * @return the URL of the stylesheet
 */
@JsxGetter
public String getHref() {
    if (ownerNode_ != null) {
        final DomNode node = ownerNode_.getDomNodeOrDie();
        if (node instanceof HtmlStyle) {
            return null;
        }
        if (node instanceof HtmlLink) {
            // <link rel="stylesheet" type="text/css" href="..." />
            final HtmlLink link = (HtmlLink) node;
            final String href = link.getHrefAttribute();
            if ("".equals(href) && getBrowserVersion().hasFeature(STYLESHEET_HREF_EMPTY_IS_NULL)) {
                return null;
            }
            // Expand relative URLs.
            try {
                final HtmlPage page = (HtmlPage) link.getPage();
                final URL url = page.getFullyQualifiedUrl(href);
                return url.toExternalForm();
            } catch (final MalformedURLException e) {
                // Log the error and fall through to the return values below.
                LOG.warn(e.getMessage(), e);
            }
        }
    }
    return getUri();
}
Also used : DomNode(com.gargoylesoftware.htmlunit.html.DomNode) MalformedURLException(java.net.MalformedURLException) HtmlStyle(com.gargoylesoftware.htmlunit.html.HtmlStyle) HtmlPage(com.gargoylesoftware.htmlunit.html.HtmlPage) HtmlLink(com.gargoylesoftware.htmlunit.html.HtmlLink) URL(java.net.URL) JsxGetter(com.gargoylesoftware.htmlunit.javascript.configuration.JsxGetter)

Example 3 with HtmlLink

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

the class CSSStyleSheet method isActive.

/**
 * Returns {@code true} if this stylesheet is active, based on the media types it is associated with (if any).
 * @return {@code true} if this stylesheet is active, based on the media types it is associated with (if any)
 */
public boolean isActive() {
    final String media;
    final HtmlElement e = ownerNode_.getDomNodeOrNull();
    if (e instanceof HtmlStyle) {
        final HtmlStyle style = (HtmlStyle) e;
        media = style.getMediaAttribute();
    } else if (e instanceof HtmlLink) {
        final HtmlLink link = (HtmlLink) e;
        media = link.getMediaAttribute();
    } else {
        return true;
    }
    if (StringUtils.isBlank(media)) {
        return true;
    }
    final WebClient webClient = getWindow().getWebWindow().getWebClient();
    final MediaListImpl mediaList = parseMedia(webClient.getCssErrorHandler(), media);
    return isActive(this, mediaList);
}
Also used : HtmlStyle(com.gargoylesoftware.htmlunit.html.HtmlStyle) HtmlElement(com.gargoylesoftware.htmlunit.html.HtmlElement) HtmlLink(com.gargoylesoftware.htmlunit.html.HtmlLink) WebClient(com.gargoylesoftware.htmlunit.WebClient) MediaListImpl(com.gargoylesoftware.css.dom.MediaListImpl)

Example 4 with HtmlLink

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

the class StyleSheetList method isActiveStyleSheetLink.

/**
 * Verifies if the provided node is a link node pointing to an active stylesheet.
 *
 * @param domNode the mode to check
 * @return true if the provided node is a stylesheet link
 */
boolean isActiveStyleSheetLink(final DomNode domNode) {
    if (domNode instanceof HtmlLink) {
        final HtmlLink link = (HtmlLink) domNode;
        if (link.isStyleSheetLink()) {
            final String media = link.getMediaAttribute();
            if (StringUtils.isBlank(media)) {
                return true;
            }
            final WebClient webClient = getWindow().getWebWindow().getWebClient();
            final MediaListImpl mediaList = CSSStyleSheet.parseMedia(webClient.getCssErrorHandler(), media);
            return CSSStyleSheet.isActive(this, mediaList);
        }
    }
    return false;
}
Also used : HtmlLink(com.gargoylesoftware.htmlunit.html.HtmlLink) WebClient(com.gargoylesoftware.htmlunit.WebClient) MediaListImpl(com.gargoylesoftware.css.dom.MediaListImpl)

Example 5 with HtmlLink

use of com.gargoylesoftware.htmlunit.html.HtmlLink in project jenkins by jenkinsci.

the class LayoutTest method rejectedLinks.

@Issue("JENKINS-21254")
@PresetData(PresetData.DataSet.NO_ANONYMOUS_READACCESS)
@Test
public void rejectedLinks() throws Exception {
    JenkinsRule.WebClient wc = r.createWebClient();
    String prefix = r.contextPath + '/';
    for (DomElement e : wc.goTo("login").getElementsByTagName("link")) {
        String href = ((HtmlLink) e).getHrefAttribute();
        if (!href.startsWith(prefix)) {
            System.err.println("ignoring " + href);
            continue;
        }
        System.err.println("checking " + href);
        wc.goTo(href.substring(prefix.length()), null);
    }
}
Also used : DomElement(com.gargoylesoftware.htmlunit.html.DomElement) HtmlLink(com.gargoylesoftware.htmlunit.html.HtmlLink) JenkinsRule(org.jvnet.hudson.test.JenkinsRule) Issue(org.jvnet.hudson.test.Issue) PresetData(org.jvnet.hudson.test.recipes.PresetData) Test(org.junit.Test)

Aggregations

HtmlLink (com.gargoylesoftware.htmlunit.html.HtmlLink)6 MediaListImpl (com.gargoylesoftware.css.dom.MediaListImpl)2 WebClient (com.gargoylesoftware.htmlunit.WebClient)2 DomElement (com.gargoylesoftware.htmlunit.html.DomElement)2 HtmlStyle (com.gargoylesoftware.htmlunit.html.HtmlStyle)2 JsxGetter (com.gargoylesoftware.htmlunit.javascript.configuration.JsxGetter)2 MalformedURLException (java.net.MalformedURLException)2 Test (org.junit.Test)2 Issue (org.jvnet.hudson.test.Issue)2 JenkinsRule (org.jvnet.hudson.test.JenkinsRule)2 PresetData (org.jvnet.hudson.test.recipes.PresetData)2 DomNode (com.gargoylesoftware.htmlunit.html.DomNode)1 HtmlElement (com.gargoylesoftware.htmlunit.html.HtmlElement)1 HtmlPage (com.gargoylesoftware.htmlunit.html.HtmlPage)1 URL (java.net.URL)1