Skip to content

IllegalArgumentException when there are extra spaces in front of a class attribute #11153

Closed

Description

@javier-godoy

In Vaadin 14.6.1, a Polymer template will cause the application to fail with IllegalArgumentException when the class attribute of an @Id-mapped element begins with a space character (e.g. class=" foo bar"). The same case works in Vaadin 14.4.10.

import {html, PolymerElement} from '@polymer/polymer/polymer-element.js';
class FooBarPolymer extends PolymerElement {
    static get template() {
        return  html`
        <!-- Doesn't cause exception -->
        <div class=" foo bar"></div>
        <!-- Causes exception -->
        <div class=" foo bar" id="foobar"></div>
        `;
    }
    static get is() {
        return 'foo-bar';
    }
}
customElements.define(FooBarPolymer.is, FooBarPolymer);
@Tag("foo-bar-polymer")
@JsModule("./foo-bar-polymer.js")
public class FooBarPolymerTemplate extends PolymerTemplate<TemplateModel>{
    @Id("foobar")
    public Div foobar;
}

Expected behavior: the foobar element should be initialized with a class list containing "foo" and "bar".

Observed behavior: the attribute value is not trimmed before splitting, thus " foo bar".split("\\s+") returns a 3-element array { "", "foo", "bar" }

public void setAttribute(Element element, String value) {
Set<String> classList = element.getClassList();
classList.clear();
if (value.isEmpty()) {
return;
}
String[] parts = value.split("\\s+");
classList.addAll(Arrays.asList(parts));
}

Then, the application fails with the following exception:

Caused by: java.lang.IllegalArgumentException: Class name cannot be empty
	at com.vaadin.flow.internal.nodefeature.ElementClassList$ClassListView.validate(ElementClassList.java:45)
	at com.vaadin.flow.internal.nodefeature.ElementClassList$ClassListView.validate(ElementClassList.java:31)
	at com.vaadin.flow.internal.nodefeature.NodeList$SetView.add(NodeList.java:113)
	at com.vaadin.flow.internal.nodefeature.NodeList$SetView.add(NodeList.java:86)
	at java.base/java.util.AbstractCollection.addAll(AbstractCollection.java:352)
	at com.vaadin.flow.dom.impl.ClassAttributeHandler.setAttribute(ClassAttributeHandler.java:58)
	at com.vaadin.flow.dom.Element.setAttribute(Element.java:292)
	at com.vaadin.flow.component.template.internal.AttributeInitializationStrategy.initialize(AttributeInitializationStrategy.java:33)
	at com.vaadin.flow.component.template.internal.AbstractInjectableElementInitializer.initialize(AbstractInjectableElementInitializer.java:94)
	at java.base/java.util.HashMap.forEach(HashMap.java:1336)
	at com.vaadin.flow.component.template.internal.AbstractInjectableElementInitializer.accept(AbstractInjectableElementInitializer.java:67)
	at com.vaadin.flow.component.polymertemplate.TemplateInitializer.lambda$null$2(TemplateInitializer.java:165)
	at com.vaadin.flow.component.polymertemplate.IdMapper.injectTemplateElement(IdMapper.java:173)
	at com.vaadin.flow.component.polymertemplate.IdMapper.attachExistingElementById(IdMapper.java:165)
	at com.vaadin.flow.component.polymertemplate.IdMapper.injectClientSideElement(IdMapper.java:117)
	at com.vaadin.flow.component.polymertemplate.IdMapper.mapComponentOrElement(IdMapper.java:88)
	at com.vaadin.flow.component.polymertemplate.TemplateInitializer.lambda$mapComponents$3(TemplateInitializer.java:162)
	at com.vaadin.flow.component.polymertemplate.TemplateDataAnalyzer$ParserData.lambda$forEachInjectedField$0(TemplateDataAnalyzer.java:147)
	at java.base/java.util.HashMap.forEach(HashMap.java:1336)
	at java.base/java.util.Collections$UnmodifiableMap.forEach(Collections.java:1505)
	at com.vaadin.flow.component.polymertemplate.TemplateDataAnalyzer$ParserData.forEachInjectedField(TemplateDataAnalyzer.java:146)
	at com.vaadin.flow.component.polymertemplate.TemplateInitializer.mapComponents(TemplateInitializer.java:161)
	at com.vaadin.flow.component.polymertemplate.TemplateInitializer.initChildElements(TemplateInitializer.java:106)
	at com.vaadin.flow.component.polymertemplate.PolymerTemplate.<init>(PolymerTemplate.java:93)
	at com.vaadin.flow.component.polymertemplate.PolymerTemplate.<init>(PolymerTemplate.java:106)
	at test.FooBarPolymerTemplate.<init>(FooBarPolymerTemplate.java:12)
	at test.MainView.<init>(MainView.java:28)
	... 61 more
	```

Activity

  1. added a commit that references this issue on Jun 21, 2021
    eea83c9
  2. added a commit that references this issue on Jun 23, 2021
    5065426
  3. added 3 commits that reference this issue on Jun 23, 2021
    f249f7a
    7632620
    ba8d2cb
  4. added 3 commits that reference this issue on Jun 23, 2021
    e4ff878
    bbbf116
    37f895b
  5. vaadin-bot commented on Jun 28, 2021

    @vaadin-bot
    Collaborator

    This ticket/PR has been released with platform 21.0.0.alpha8. For prerelease versions, it will be included in its final version.

  6. deleted a comment from vaadin-bot on Jul 6, 2021
  7. vaadin-bot commented on Jul 6, 2021

    @vaadin-bot
    Collaborator

    This ticket/PR has been released with platform 20.0.4. For prerelease versions, it will be included in its final version.

  8. vaadin-bot commented on Aug 9, 2021

    @vaadin-bot
    Collaborator

    This ticket/PR has been released with platform 14.7.0.alpha3 and is also targeting the upcoming stable 14.7.0 version.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions