Example usage for org.jsoup.nodes Element attr

List of usage examples for org.jsoup.nodes Element attr

Introduction

In this page you can find the example usage for org.jsoup.nodes Element attr.

Prototype

public String attr(String attributeKey) 

Source Link

Document

Get an attribute's value by its key.

Usage

From source file:org.asqatasun.rules.rgaa30.Rgaa30Rule010306.java

/**
 * /*from  w  w  w . j a  va2 s .  c  o  m*/
 * @param element
 * @return whether the aria label exists and is empty
 */
private boolean isRoleImagePresent(Element element) {
    return element.hasAttr(ROLE_ATTR) && StringUtils.equalsIgnoreCase("img", element.attr(ROLE_ATTR));
}

From source file:org.asqatasun.rules.rgaa30.Rgaa30Rule010306.java

/**
 * //from ww  w  .jav  a  2 s. co m
 * @param element
 * @return the textual content of the aria-label attribute
 */
private String getAriaLabelAttrText(Element element) {
    return element.attr(ARIA_LABEL_ATTR);
}

From source file:org.asqatasun.rules.rgaa30.Rgaa30Rule010306.java

/**
 * //from  w  ww  .  ja v  a2  s.  c  om
 * @param element
 * @return the textual content of the title attribute
 */
private String getTitleAttrText(Element element) {
    return element.attr(TITLE_ATTR);
}

From source file:org.asqatasun.rules.rgaa30.Rgaa30Rule090104.java

@Override
protected void select(SSPHandler sspHandler) {
    super.select(sspHandler);
    Iterator<Element> elementsIterator = getElements().get().iterator();
    while (elementsIterator.hasNext()) {
        Element element = elementsIterator.next();
        if (element.hasAttr("aria-level")) {
            if (!PATTERN.matcher(element.attr("aria-level")).matches()) {
                elementsIterator.remove();
            }//from ww w.j  ava  2s  . c om
        }
    }
}

From source file:org.asqatasun.rules.rgaa30.Rgaa30Rule110102.java

@Override
protected void check(SSPHandler sspHandler, TestSolutionHandler testSolutionHandler) {

    /* If the page has no input form element, the test is not applicable */
    if (inputFormMap.entrySet().isEmpty()) {
        testSolutionHandler.addTestSolution(TestSolution.NOT_APPLICABLE);
        return;/*  www. j a  v  a  2  s .com*/
    }

    for (Map.Entry<Element, ElementHandler<Element>> entry : inputFormMap.entrySet()) {
        /* The attribute Presence Checker */
        ElementChecker attributePresenceChecker = new AttributePresenceChecker(ID_ATTR,
                new ImmutablePair(TestSolution.PASSED, ""),
                new ImmutablePair(TestSolution.FAILED, ID_MISSING_MSG));

        attributePresenceChecker.check(sspHandler, entry.getValue(), testSolutionHandler);

        /* The attribute Emptiness Checker. Keep default value i.e failed 
         when attribute is empty
         */
        ElementChecker attributeEmptinessChecker = new TextEmptinessChecker(
                new TextAttributeOfElementBuilder(ID_ATTR), ID_MISSING_MSG, null);
        attributeEmptinessChecker.check(sspHandler, entry.getValue(), testSolutionHandler);

        /* The id unicityChecker */
        ElementChecker idUnicityChecker = new IdUnicityChecker(ID_NOT_UNIQUE_MSG);
        idUnicityChecker.check(sspHandler, entry.getValue(), testSolutionHandler);
    }

    for (Map.Entry<Element, ElementHandler<Element>> entry : labelFormMap.entrySet()) {
        /* The attribute Presence Checker */
        ElementChecker attributePresenceChecker = new AttributePresenceChecker(FOR_ATTR,
                new ImmutablePair(TestSolution.PASSED, ""),
                new ImmutablePair(TestSolution.FAILED, FOR_MISSING_MSG));

        attributePresenceChecker.check(sspHandler, entry.getValue(), testSolutionHandler);

        /* The attribute Emptiness Checker. Keep default value i.e failed 
         when attribute is empty
         */
        ElementChecker attributeEmptinessChecker = new TextEmptinessChecker(
                new TextAttributeOfElementBuilder(FOR_ATTR), FOR_MISSING_MSG, null);
        attributeEmptinessChecker.check(sspHandler, entry.getValue(), testSolutionHandler);
    }

    for (Map.Entry<Element, ElementHandler<Element>> entry : inputFormMap.entrySet()) {
        ElementHandler<Element> inputOnError = new ElementHandlerImpl();
        /* Check if each input id attribute is linked to a for attribute*/
        for (Element el : entry.getValue().get()) {
            String id = el.id();
            if (StringUtils.isNotBlank(id)) {
                ElementHandler<Element> linkedLabelToInputHandler = new ElementHandlerImpl();
                if (entry.getKey()
                        .select(LABEL_ELEMENT + " "
                                + CssLikeSelectorBuilder
                                        .buildSelectorFromElementsAndAttributeValue(INPUT_ELEMENT, ID_ATTR, id))
                        .isEmpty()) {
                    linkedLabelToInputHandler.addAll(entry.getKey().select(CssLikeSelectorBuilder
                            .buildSelectorFromElementsAndAttributeValue(LABEL_ELEMENT, FOR_ATTR, id)));
                    if (linkedLabelToInputHandler.isEmpty()) {
                        inputOnError.add(el);
                    }
                }
            }
        }
        ElementChecker elementPresenceChecker = new ElementPresenceChecker(
                new ImmutablePair(TestSolution.FAILED, INVALID_INPUT_MSG),
                new ImmutablePair(TestSolution.PASSED, ""));

        elementPresenceChecker.check(sspHandler, inputOnError, testSolutionHandler);
    }

    for (Map.Entry<Element, ElementHandler<Element>> entry : labelFormMap.entrySet()) {
        ElementHandler<Element> labelOnError = new ElementHandlerImpl();
        /* Check if each label for attribute is associated to an input id attribute*/
        for (Element el : entry.getValue().get()) {
            String id = el.attr(FOR_ATTR);
            if (StringUtils.isNotBlank(id)) {
                ElementHandler<Element> linkedLabelToInputHandler = new ElementHandlerImpl();
                linkedLabelToInputHandler
                        .addAll(entry.getKey().select(CssLikeSelectorBuilder.buildSelectorFromId(id)));
                if (linkedLabelToInputHandler.isEmpty()) {
                    labelOnError.add(el);
                }
            }
        }
        ElementChecker elementPresenceChecker = new ElementPresenceChecker(
                new ImmutablePair(TestSolution.FAILED, INVALID_LABEL_MSG),
                new ImmutablePair(TestSolution.PASSED, ""));

        elementPresenceChecker.check(sspHandler, labelOnError, testSolutionHandler);
    }
}

From source file:org.asqatasun.rules.rgaa30.Rgaa30Rule110103.java

@Override
protected void check(SSPHandler sspHandler, TestSolutionHandler testSolutionHandler) {

    /* If the page has no input form element, the test is not applicable */
    if (inputFormMap.entrySet().isEmpty()) {
        testSolutionHandler.addTestSolution(TestSolution.NOT_APPLICABLE);
        return;/*from   w  w w . ja v a  2 s  .c om*/
    }

    for (Map.Entry<Element, ElementHandler<Element>> entry : inputFormMap.entrySet()) {
        /* The attribute Emptiness Checker. Keep default value i.e failed 
         when attribute is empty
         */
        ElementChecker attributeEmptinessChecker = new TextEmptinessChecker(
                new TextAttributeOfElementBuilder(ARIA_LABELLEDBY_ATTR), ARIA_LABELLEDBY_EMPTY_MSG, null);
        attributeEmptinessChecker.check(sspHandler, entry.getValue(), testSolutionHandler);

        ElementHandler<Element> inputWithoutLabel = new ElementHandlerImpl();
        ElementHandler<Element> notUniqueLabel = new ElementHandlerImpl();
        for (Element el : entry.getValue().get()) {
            if (StringUtils.isNotEmpty(el.attr(ARIA_LABELLEDBY_ATTR))) {
                ElementHandler<Element> labelHandler = new ElementHandlerImpl();
                labelHandler.addAll(entry.getKey().select(CssLikeSelectorBuilder
                        .buildSelectorFromAttributeTypeAndValue(ID_ATTR, el.attr(ARIA_LABELLEDBY_ATTR))));
                if (labelHandler.get().isEmpty()) {
                    inputWithoutLabel.add(el);
                } else if (labelHandler.get().size() > 1) {
                    notUniqueLabel.add(el);
                    notUniqueLabel.addAll(labelHandler.get());
                }
            }
        }

        /* Check if the form element has a label associated */
        ElementChecker elementPresenceChecker = new ElementPresenceChecker(
                new ImmutablePair(TestSolution.FAILED, FORM_ELEMENT_WITHOUT_LABEL_MSG),
                new ImmutablePair(TestSolution.PASSED, ""));
        elementPresenceChecker.check(sspHandler, inputWithoutLabel, testSolutionHandler);

        /* Check if the id attr of the label associated to the form element is unique */
        elementPresenceChecker = new ElementPresenceChecker(
                new ImmutablePair(TestSolution.FAILED, FORM_ELEMENT_WITH_NOT_UNIQUE_LABEL_MSG),
                new ImmutablePair(TestSolution.PASSED, ""));
        elementPresenceChecker.check(sspHandler, notUniqueLabel, testSolutionHandler);
    }
}

From source file:org.asqatasun.rules.textbuilder.CompleteTextElementBuilder.java

/**
 * The textual content of an element can be composed with :
 * <ul>/* w  w w. j a  v  a2 s.com*/
 *     <li> The text of the element</li>
 *     <li> The text of the alt attribute of the element</li>
 *     <li> The text of the title attribute of the element</li>
 *     <li> The text of the summary attribute of the element</li>
 *     <li> The text of the value attribute of the element</li>
 *     <li> The text of the content attribute of the element when 
 *          the value of the name attribute is "description"
 *      </li>
 * </ul>
 * 
 * @param element
 * @return the textual content of an element
 */
@Override
public String buildTextFromElement(Element element) {
    StringBuilder strb = new StringBuilder();
    if (StringUtils.isNotBlank(element.ownText())) {
        strb.append(SPACER);
        strb.append(element.ownText().trim());
    }

    strb.append(getTextualContentOfAttribute(element, AttributeStore.ALT_ATTR));
    strb.append(getTextualContentOfAttribute(element, AttributeStore.TITLE_ATTR));
    strb.append(getTextualContentOfAttribute(element, AttributeStore.SUMMARY_ATTR));
    strb.append(getTextualContentOfAttribute(element, AttributeStore.VALUE_ATTR));

    if (element.hasAttr(AttributeStore.CONTENT_ATTR) && element.hasAttr(AttributeStore.NAME_ATTR)
            && StringUtils.equalsIgnoreCase(element.attr(AttributeStore.NAME_ATTR), "description")
            && StringUtils.isNotBlank(element.attr(AttributeStore.CONTENT_ATTR))) {
        strb.append(SPACER);
        strb.append(getTextualContentOfAttribute(element, AttributeStore.CONTENT_ATTR));
    }
    return StringUtils.trim(strb.toString());
}

From source file:org.asqatasun.rules.textbuilder.CompleteTextElementBuilder.java

/**
 * //  w  w  w .  j  a v a 2  s .c  o m
 * @param element
 * @param attributeName
 * @return the textual content of an attribute
 */
private String getTextualContentOfAttribute(Element element, String attributeName) {
    if (element.hasAttr(attributeName)) {
        return SPACER + StringUtils.trim(element.attr(attributeName));
    }
    return "";
}

From source file:org.asqatasun.rules.textbuilder.TextAttributeOfElementBuilder.java

/**
 * //  ww  w . j a  v a  2s .  c  om
 * @param element
 * @return the content of the attribute when it exits, null instead.
 */
@Override
public String buildTextFromElement(Element element) {
    if (CollectionUtils.isEmpty(attributeNames)) {
        return null;
    }
    boolean elementHasAttr = false;
    StringBuilder strb = new StringBuilder();
    for (String attributeName : attributeNames) {
        if (element.hasAttr(attributeName)) {
            elementHasAttr = true;
            strb.append(element.attr(attributeName));
            strb.append(SPACER);
        }
    }
    if (!elementHasAttr) {
        return null;
    }
    return strb.toString().trim();
}

From source file:org.b3log.symphony.util.Markdowns.java

/**
 * Gets the safe HTML content of the specified content.
 *
 * @param content the specified content//from   w w w.  j a  v  a  2 s .  c o  m
 * @param baseURI the specified base URI, the relative path value of href will starts with this URL
 * @return safe HTML content
 */
public static String clean(final String content, final String baseURI) {
    final Document.OutputSettings outputSettings = new Document.OutputSettings();
    outputSettings.prettyPrint(false);

    final String tmp = Jsoup.clean(content, baseURI,
            Whitelist.relaxed().addAttributes(":all", "id", "target", "class")
                    .addTags("span", "hr", "kbd", "samp", "tt", "del", "s", "strike", "u")
                    .addAttributes("iframe", "src", "width", "height", "border", "marginwidth", "marginheight")
                    .addAttributes("audio", "controls", "src")
                    .addAttributes("video", "controls", "src", "width", "height")
                    .addAttributes("source", "src", "media", "type")
                    .addAttributes("object", "width", "height", "data", "type")
                    .addAttributes("param", "name", "value")
                    .addAttributes("input", "type", "disabled", "checked").addAttributes("embed", "src", "type",
                            "width", "height", "wmode", "allowNetworking"),
            outputSettings);
    final Document doc = Jsoup.parse(tmp, baseURI, Parser.htmlParser());

    final Elements ps = doc.getElementsByTag("p");
    for (final Element p : ps) {
        p.removeAttr("style");
    }

    final Elements iframes = doc.getElementsByTag("iframe");
    for (final Element iframe : iframes) {
        final String src = StringUtils.deleteWhitespace(iframe.attr("src"));
        if (StringUtils.startsWithIgnoreCase(src, "javascript")
                || StringUtils.startsWithIgnoreCase(src, "data:")) {
            iframe.remove();
        }
    }

    final Elements objs = doc.getElementsByTag("object");
    for (final Element obj : objs) {
        final String data = StringUtils.deleteWhitespace(obj.attr("data"));
        if (StringUtils.startsWithIgnoreCase(data, "data:")
                || StringUtils.startsWithIgnoreCase(data, "javascript")) {
            obj.remove();

            continue;
        }

        final String type = StringUtils.deleteWhitespace(obj.attr("type"));
        if (StringUtils.containsIgnoreCase(type, "script")) {
            obj.remove();
        }
    }

    final Elements embeds = doc.getElementsByTag("embed");
    for (final Element embed : embeds) {
        final String data = StringUtils.deleteWhitespace(embed.attr("src"));
        if (StringUtils.startsWithIgnoreCase(data, "data:")
                || StringUtils.startsWithIgnoreCase(data, "javascript")) {
            embed.remove();

            continue;
        }
    }

    final Elements as = doc.getElementsByTag("a");
    for (final Element a : as) {
        a.attr("rel", "nofollow");

        final String href = a.attr("href");
        if (href.startsWith(Latkes.getServePath())) {
            continue;
        }

        a.attr("target", "_blank");
    }

    final Elements audios = doc.getElementsByTag("audio");
    for (final Element audio : audios) {
        audio.attr("preload", "none");
    }

    final Elements videos = doc.getElementsByTag("video");
    for (final Element video : videos) {
        video.attr("preload", "none");
    }

    String ret = doc.body().html();
    ret = ret.replaceAll("(</?br\\s*/?>\\s*)+", "<br>"); // patch for Jsoup issue

    return ret;
}