Repository navigation
Closed
Description
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" }
flow/flow-server/src/main/java/com/vaadin/flow/dom/impl/ClassAttributeHandler.java
Lines 49 to 59 in 028ba87
| 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
fix: trim class names value before splitting
fix: trim class names value before splitting (#11297)
fix: trim class names value before splitting (#11297)
fix: trim class names value before splitting (#11297)
fix: trim class names value before splitting (#11297)
fix: trim class names value before splitting (#11297) (#11316)
fix: trim class names value before splitting (#11297) (#11315)
fix: trim class names value before splitting (#11297) (#11317)
vaadin-bot commentedon Jun 28, 2021
This ticket/PR has been released with platform 21.0.0.alpha8. For prerelease versions, it will be included in its final version.
vaadin-bot commentedon Jul 6, 2021
This ticket/PR has been released with platform 20.0.4. For prerelease versions, it will be included in its final version.
vaadin-bot commentedon Aug 9, 2021
This ticket/PR has been released with platform 14.7.0.alpha3 and is also targeting the upcoming stable 14.7.0 version.