Search in sources :

Example 1 with TestRunner

use of nu.validator.client.TestRunner in project validator by validator.

the class Assertions method endElement.

/**
 * @see nu.validator.checker.Checker#endElement(java.lang.String,
 *      java.lang.String, java.lang.String)
 */
@Override
public void endElement(String uri, String localName, String name) throws SAXException {
    if ("http://www.w3.org/1999/xhtml" == uri && "template".equals(localName)) {
        numberOfTemplatesDeep--;
        if (numberOfTemplatesDeep != 0) {
            return;
        }
    } else if (numberOfTemplatesDeep > 0) {
        return;
    }
    StackNode node = pop();
    String systemId = node.locator().getSystemId();
    String publicId = node.locator().getPublicId();
    Locator locator = null;
    openSingleSelects.remove(node);
    openLabels.remove(node);
    openMediaElements.remove(node);
    if ("http://www.w3.org/1999/xhtml" == uri) {
        if ("figure" == localName) {
            if ((node.needsFigcaption() && !node.hasFigcaptionContent()) || node.hasTextNode() || node.hasEmbeddedContent()) {
                for (Locator imgLocator : node.getImagesLackingAlt()) {
                    err("An \u201Cimg\u201D element must have an" + " \u201Calt\u201D attribute, except under" + " certain conditions. For details, consult" + " guidance on providing text alternatives" + " for images.", imgLocator);
                }
            }
        } else if ("picture" == localName) {
            siblingSources.clear();
        } else if ("select" == localName && node.isOptionNeeded()) {
            if (!node.hasOption()) {
                err("A \u201Cselect\u201D element with a" + " \u201Crequired\u201D attribute, and without a" + " \u201Cmultiple\u201D attribute, and without a" + " \u201Csize\u201D attribute whose value is" + " greater than" + " \u201C1\u201D, must have a child" + " \u201Coption\u201D element.");
            }
            if (node.nonEmptyOptionLocator() != null) {
                err("The first child \u201Coption\u201D element of a" + " \u201Cselect\u201D element with a" + " \u201Crequired\u201D attribute, and without a" + " \u201Cmultiple\u201D attribute, and without a" + " \u201Csize\u201D attribute whose value is" + " greater than" + " \u201C1\u201D, must have either an empty" + " \u201Cvalue\u201D attribute, or must have no" + " text content." + " Consider either adding a placeholder option" + " label, or adding a" + " \u201Csize\u201D attribute with a value equal" + " to the number of" + " \u201Coption\u201D elements.", node.nonEmptyOptionLocator());
            }
        } else if ("section" == localName && !node.hasHeading()) {
            warn("Section lacks heading. Consider using" + " \u201ch2\u201d-\u201ch6\u201d elements to add" + " identifying headings to all sections.", node.locator());
        } else if ("article" == localName && !node.hasHeading()) {
            warn("Article lacks heading. Consider using" + " \u201ch2\u201d-\u201ch6\u201d elements to add" + " identifying headings to all articles.", node.locator());
        } else if (("h1" == localName || "h2" == localName || "h3" == localName || "h4" == localName || "h5" == localName || "h6" == localName) && !node.hasTextNode() && !node.hasImg()) {
            warn("Empty heading.", node.locator());
        } else if ("option" == localName && !stack[currentPtr].hasOption()) {
            stack[currentPtr].setOptionFound();
        } else if ("style" == localName) {
            String styleContents = node.getTextContent().toString();
            int lineOffset = 0;
            if (styleContents.startsWith("\n")) {
                lineOffset = 1;
            }
            ApplContext ac = new ApplContext("en");
            ac.setCssVersionAndProfile("css3svg");
            ac.setMedium("all");
            ac.setTreatVendorExtensionsAsWarnings(true);
            ac.setTreatCssHacksAsWarnings(true);
            ac.setWarningLevel(-1);
            ac.setFakeURL("file://localhost/StyleElement");
            StyleSheetParser styleSheetParser = new StyleSheetParser();
            styleSheetParser.parseStyleSheet(ac, new StringReader(styleContents.substring(lineOffset)), null);
            styleSheetParser.getStyleSheet().findConflicts(ac);
            Errors errors = styleSheetParser.getStyleSheet().getErrors();
            if (errors.getErrorCount() > 0) {
                incrementUseCounter("style-element-errors-found");
            }
            for (int i = 0; i < errors.getErrorCount(); i++) {
                String message = "";
                String cssProperty = "";
                String cssMessage = "";
                CssError error = errors.getErrorAt(i);
                int beginLine = error.getBeginLine() + lineOffset;
                int beginColumn = error.getBeginColumn();
                int endLine = error.getEndLine() + lineOffset;
                int endColumn = error.getEndColumn();
                if (beginLine == 0) {
                    continue;
                }
                Throwable ex = error.getException();
                if (ex instanceof CssParseException) {
                    CssParseException cpe = (CssParseException) ex;
                    if (// 
                    "generator.unrecognize".equals(cpe.getErrorType())) {
                        cssMessage = "Parse Error";
                    }
                    if (cpe.getProperty() != null) {
                        cssProperty = String.format("\u201c%s\u201D: ", cpe.getProperty());
                    }
                    if (cpe.getMessage() != null) {
                        cssMessage = cpe.getMessage();
                    }
                    if (!"".equals(cssMessage)) {
                        message = cssProperty + cssMessage + ".";
                    }
                } else {
                    message = ex.getMessage();
                }
                if (!"".equals(message)) {
                    int lastLine = // 
                    node.locator.getLineNumber() + endLine - 1;
                    int lastColumn = endColumn;
                    int columnOffset = node.locator.getColumnNumber();
                    if (error.getBeginLine() == 1) {
                        if (lineOffset != 0) {
                            columnOffset = 0;
                        }
                    } else {
                        columnOffset = 0;
                    }
                    String prefix = sourceIsCss ? "" : "CSS: ";
                    SAXParseException spe = new // 
                    SAXParseException(// 
                    prefix + message, // 
                    publicId, // 
                    systemId, lastLine, lastColumn);
                    int[] start = { node.locator.getLineNumber() + beginLine - 1, beginColumn, columnOffset };
                    if ((getErrorHandler() instanceof MessageEmitterAdapter) && !(getErrorHandler() instanceof TestRunner)) {
                        // 
                        ((MessageEmitterAdapter) getErrorHandler()).errorWithStart(spe, start);
                    } else {
                        getErrorHandler().error(spe);
                    }
                }
            }
        }
        if ("article" == localName || "aside" == localName || "nav" == localName || "section" == localName) {
            currentSectioningElementPtr = currentPtr - 1;
            currentSectioningDepth--;
        }
    }
    if ((locator = openActiveDescendants.remove(node)) != null) {
        warn("Attribute \u201Caria-activedescendant\u201D value should " + "either refer to a descendant element, or should " + "be accompanied by attribute \u201Caria-owns\u201D.", locator);
    }
}
Also used : MessageEmitterAdapter(nu.validator.messages.MessageEmitterAdapter) TestRunner(nu.validator.client.TestRunner) CssParseException(org.w3c.css.parser.CssParseException) StyleSheetParser(org.w3c.css.css.StyleSheetParser) Errors(org.w3c.css.parser.Errors) Locator(org.xml.sax.Locator) ApplContext(org.w3c.css.util.ApplContext) SAXParseException(org.xml.sax.SAXParseException) StringReader(java.io.StringReader) CssError(org.w3c.css.parser.CssError)

Aggregations

StringReader (java.io.StringReader)1 TestRunner (nu.validator.client.TestRunner)1 MessageEmitterAdapter (nu.validator.messages.MessageEmitterAdapter)1 StyleSheetParser (org.w3c.css.css.StyleSheetParser)1 CssError (org.w3c.css.parser.CssError)1 CssParseException (org.w3c.css.parser.CssParseException)1 Errors (org.w3c.css.parser.Errors)1 ApplContext (org.w3c.css.util.ApplContext)1 Locator (org.xml.sax.Locator)1 SAXParseException (org.xml.sax.SAXParseException)1