Java 类org.w3c.dom.html.HTMLBaseElement 实例源码

项目:ScriptBox    文件:Html5DocumentImpl.java   
/**
 * Returns base element if there is any inside Document.
 * 
 * @return Base element if there is any inside Document.
 */
public HTMLBaseElement getBaseElement() {
    // FIXME: We should not wait for completeness, but try to get base address from actual loaded elements - proper synchronized section is needed.
    if (_documentReadiness == DocumentReadyState.COMPLETE) {
        HTMLElement head = getHead();
        NodeList baseElements = head.getElementsByTagName("base");

        if (baseElements.getLength() > 0) {
            Node baseElement = baseElements.item(0);

            if (baseElement instanceof HTMLBaseElement) {
                return (HTMLBaseElement)baseElement;
            }
        }
    }

    return null;
}
项目:ScriptBox    文件:Html5DocumentImpl.java   
/**
 * Returns Document base address.
 * 
 * @return Document base address.
 * @see <a href="http://www.w3.org/html/wg/drafts/html/master/infrastructure.html#document-base-url">Document base URL</a>
 */
public URL getBaseAddress() {
    HTMLBaseElement baseElement = getBaseElement();
    URL baseURL = null;
    try {
        baseURL = (baseElement != null)? new URL(baseElement.getHref()) : null;
    } catch (MalformedURLException e) {
        e.printStackTrace();
    }

    /*
     * 1) If there is no base element that has an href attribute in the Document, 
     * then the document base URL is the Document's fallback base URL
     */
    if (baseURL == null) {
        return getFallbackBaseAddress();
    }

    /*
     * TODO?: Implement frozen URL?
     * http://www.w3.org/html/wg/drafts/html/master/document-metadata.html#frozen-base-url
     */
    /*
     * 2) Otherwise, the document base URL is the frozen base URL of the first 
     * base element in the Document that has an href attribute, in tree order.
     */
    return baseURL;

}
项目:ScriptBox    文件:NavigationController.java   
/**
 * Navigates to given hyperlink with specified source browsing context.
 * 
 * @param subject Element with the hyperlink.
 * @see <a href="http://www.w3.org/html/wg/drafts/html/master/links.html#following-hyperlinks">Following hyperlinks</a>
 */
public static void followHyperlink(Element subject, BrowsingContext specificSource) {
        boolean _replace = false;
        Document _subjectDocument = subject.getOwnerDocument();

        if (!(_subjectDocument instanceof Html5DocumentImpl)) {
            return;
        }

        Html5DocumentImpl subjectDocument = (Html5DocumentImpl)_subjectDocument;
        final BrowsingContext source = subjectDocument.getBrowsingContext();
        boolean _explicitSelfOverride = false;
        BrowsingContext _target = null;

        String targetAttr = null;
        HTMLBaseElement baseElement = subjectDocument.getBaseElement();

        if (specificSource != null) {
            _target = specificSource;
        } else if (isLinkableElement(subject) && (targetAttr = getTargetFromElement(subject)) != null) {
            _target = source.chooseBrowsingContextByName(targetAttr, false);
            _replace = source.isBlankBrowsingContext(targetAttr);
            _explicitSelfOverride = source.isExplicitSelfNavigationOverride(targetAttr);
        } else if (isLinkableElement(subject) && targetAttr == null && baseElement != null && (targetAttr = baseElement.getTarget()) != null) {
            _target = source.chooseBrowsingContextByName(targetAttr, false);
            _replace = source.isBlankBrowsingContext(targetAttr);
            _explicitSelfOverride = source.isExplicitSelfNavigationOverride(targetAttr);
        } else {
            _target = source;
        }
        final BrowsingContext target = _target;
        final boolean replace = _replace;
        final boolean explicitSelfOverride = _explicitSelfOverride;

        String hrefString = getHrefFromElement(subject);

        URL baseUrl = subjectDocument.getAddress();
        try {
        final URL resultUrl = new URL(baseUrl, hrefString);
        EventLoop eventLoop = subjectDocument.getEventLoop();
        eventLoop.queueTask(new Task(TaskSource.DOM_MANIPULATION, source) {

            @Override
            public void execute() throws TaskAbortedException, InterruptedException {
                NavigationController controller = target.getNavigationController();
                controller.navigate(source, resultUrl, false, explicitSelfOverride, replace);
            }
        });
    } catch (Exception e) {
        e.printStackTrace();
    }

}