Tuesday, August 4, 2015

Compiler throws StringIndexOutOfBoundsException when parsing

Hi,

I've a <ui:style> with a CSS class that has double hyphens in it (the CSS follows the BEM pattern) e.g.

<!DOCTYPE ui:UiBinder SYSTEM "http://dl.google.com/gwt/DTD/xhtml.ent">
<ui:UiBinder xmlns:ui="urn:ui:com.google.gwt.uibinder" xmlns:g="urn:import:com.google.gwt.user.client.ui">
   
<ui:style>

      .panel--default {
          background: grey;
      }

   
</ui:style>

   
<g:HTMLPanel addStyleNames="{style.panel--default}">Hello World!</g:HTMLPanel>
</ui:UiBinder>

When the compiler runs it throws a StringIndexOutOfBoundsException:

java.lang.StringIndexOutOfBoundsException: String index out of range: 1
    at java
.lang.String.substring(String.java:1907)
    at com
.google.gwt.uibinder.attributeparsers.CssNameConverter.convertName(CssNameConverter.java:46)
    at com
.google.gwt.uibinder.attributeparsers.FieldReferenceConverter.expandDots(FieldReferenceConverter.java:205)
    at com
.google.gwt.uibinder.attributeparsers.FieldReferenceConverter.convert(FieldReferenceConverter.java:191)
    at com
.google.gwt.uibinder.attributeparsers.StringAttributeParser.parse(StringAttributeParser.java:59)...

(Also the Eclipse plugin throws the same error when trying to save the UiBinder file - I'm not sure if it shares the same code as I couldn't find the source code for the plugin).

I think there's a couple of issues here:

1. The CssNameConverter.convertName() method should handle double hyphens - using a StringTokenizer (or similar) would fix this problem
2. A valid CSS identifier is not a valid Java identifier IMHO UiBinder and the Eclipse plugin should cope with this.

(Incidentally I don't get this error when using ClientBundle/CssResource but then I'll have to expose all the CSS through the CssResource.)

I'm not sure if the CssNameConverter is the correct place to be validating CSS class names - I don't know enough about GWTs internals or anything about the Eclipse plugin to suggest where it should go. Also I think issues/errors with CSS should be highlighted by the plugin (in the IDE) and not later at compile time.

I think it would also be useful if the documentation made it clear what is/isn't supported - it's important to remember that not everyone developing for a GWT project comes from a Java background.

I'm more than willing to get my hands dirty (submitting patches, updating documentation) but it's not something I've done for the main GWT project.

I've not raised a bug yet as docs said to post here first.

Cheers,
Dave

--
You received this message because you are subscribed to the Google Groups "Google Web Toolkit" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-web-toolkit+unsubscribe@googlegroups.com.
To post to this group, send email to google-web-toolkit@googlegroups.com.
Visit this group at http://groups.google.com/group/google-web-toolkit.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment