crysknife-io / crysknife

Dependency Injection and html templates framework for J2CL. Relaxed implementation of Jakarta CDI
Apache License 2.0
27 stars 8 forks source link

Templating element field name verifier doing case-insensitive match #65

Closed rob5n closed 3 years ago

rob5n commented 3 years ago

TemplateProcessor.verifySelector(...) uses jsoup library incorrectly when matching the "data-field" attribute value:

    private void verifySelector(String selector, Element element, TemplateSelector templateSelector,
            org.jsoup.nodes.Element root) {
        // make sure to use the same logic for finding matching elements as in TemplateUtils!
        Elements elements = root.getElementsByAttributeValue("data-field", selector);

Which calls the jsoup library:

    /**
     * Find elements that have an attribute with the specific value. Case insensitive.
...
     */
    public Elements getElementsByAttributeValue(String key, String value) {

This is case insensitive for attribute name (good) and value (BAD!)

Changing ``verifySelector(...) as follows fixes the issue:

        //Elements elements = root.getElementsByAttributeValue("data-element", selector);
        Elements elements = root.getElementsByAttribute("data-element");
        long matchCount = elements.stream().filter(elem -> elem.attributes().getIgnoreCase("data-element").equals(selector)).count();
        if (matchCount == 0) {
            abortWithError(element,
                    "Cannot find a matching element in %s using \"[data-element=%s]\" as selector",
                    templateSelector, selector);
        } else if (matchCount > 1) {
            warning(element,
                    "Found %d matching elements in %s using \"[data-element=%s]\" as selector. Only the first will be used.",
                    elements.size(), templateSelector, selector);
        }
treblereel commented 3 years ago

@rob5n Cool, good catch, i ll update it this weekend

Thanks!

treblereel commented 3 years ago

resolved https://github.com/crysknife-io/crysknife/commit/7c37f04cd433c94457a416454cee1b03db8cc0be

Thanks once again @rob5n