Hybrid View

  1. #1
    Sencha User
    Join Date
    Nov 2007
    Posts
    35
    Vote Rating
    0
    albertogiantin is on a distinguished road

      0  

    Default Problem with XTemplate and html entities

    Problem with XTemplate and html entities


    Hi all, I have a problem that is driving me crazy.

    I have the method redditivita() in a i18 Constants class that returns the String "Redditività":
    Code:
    ...
    
      /**
       * Translated "Redditività".
       * 
       * @return translated "Redditività"
       */
      @DefaultStringValue("Redditività")
      @Key("redditivita")
      String redditivita();
    
    ...
    As you can see inside this string there is the html entity "à"
    When i try to use it in a XTemplate the result is as follow:

    shot.png

    while I expect the text of the label is Redditività
    This is my code:

    Code:
    ...
    
    	interface DetailRenderer extends XTemplates {
    		@XTemplate(source = "SectionDetail.html")
    		public SafeHtml renderItem(SectionItem section);
    	}
    
    ...
    
    		final DetailRenderer r = GWT.create(DetailRenderer.class);
    
    		IsideCommonCssBundle.INSTANCE.listViewStyle().ensureInjected();
    		ListViewStyle style = IsideCommonCssBundle.INSTANCE.listViewStyle();
    		ListViewCustomAppearance<SectionItem> appearance = new ListViewCustomAppearance<SectionItem>("." + style.thumbWrap()) {
    
    			@Override
    			public void render(SafeHtmlBuilder builder) {
    				builder.appendHtmlConstant("<div class='" + ListView.getStandardResources().css().view() + "' id='list-view'></div>");
    			}
    
    			@Override
    			public void renderEnd(SafeHtmlBuilder builder) {
    				String markup = new StringBuilder("<div class=\"").append(CommonStyles.get().clear()).append("\"></div>").toString();
    				builder.appendHtmlConstant(markup);
    			}
    
    			@Override
    			public void renderItem(SafeHtmlBuilder builder, SafeHtml content) {
    				builder.append(content);
    			}
    		};
    		listView = new ListView<SectionItem, SectionItem>(listStore, new IdentityValueProvider<SectionItem>() {
    
    			@Override
    			public void setValue(SectionItem object, SectionItem value) {
    
    			}
    		}, appearance);
    		listView.setCell(new SimpleSafeHtmlCell<SectionItem>(new AbstractSafeHtmlRenderer<SectionItem>() {
    
    			public SafeHtml render(SectionItem object) {
    				SafeHtml h = r.renderItem(object);
    				return h;
    			}
    		}));
    and this is my template file:
    HTML Code:
    <div class="thumb-wrap">
    <div class="thumb"><img src="{section.iconUrl}" title="{section.name}"></div>
    <span class="x-editable">{section.name}</span></div>
    section.name is the field containing the result of redditivita() method from i18n Constants class.

    Where is the problem?

  2. #2
    Ext GWT Premium Member icfantv's Avatar
    Join Date
    Sep 2011
    Location
    Superior, CO
    Posts
    411
    Vote Rating
    21
    icfantv will become famous soon enough icfantv will become famous soon enough

      0  

    Default


    If you know that your browser can handle that entity character, then it appears when XTemplates encounters a method that returns a String it appears to escape the entities so that they are not rendered correctly. Since the SectionItem.getName() needs to be treated without being escaped, you are probably going to want to pass SectionItem.getName in as a SafeHtml.

    I believe this would prevent XTemplates from doing anything to the String. I think you can use SafeHtmlUtils.fromTrustedString(String), or SafeHtmlUtils.htmlEscapeAllowEntities(String) to get the right value.

    This would change your XTemplate method to:

    Code:
    renderItem(SectionItem section, SafeHtml sectionName)
    where

    Code:
    SafeHtml sectionName = SafeHtmlUtils.fromTrustedString(String); // or .htmlEscapeAllowEntities(...)
    Here's the link to the javadocs for SafeHtmlUtils.

    Give that a go and see if that fixes your issue.

  3. #3
    Sencha - GXT Dev Team
    Join Date
    Feb 2009
    Location
    Minnesota
    Posts
    2,734
    Vote Rating
    90
    Colin Alworth is a glorious beacon of light Colin Alworth is a glorious beacon of light Colin Alworth is a glorious beacon of light Colin Alworth is a glorious beacon of light Colin Alworth is a glorious beacon of light

      0  

    Default


    I could be mistaken, but I think that the Constants subclass is allowed to return SafeHtml instead of String. Doing so would let you skip the fromTrustedString call.

  4. #4
    Sencha User
    Join Date
    Nov 2007
    Posts
    35
    Vote Rating
    0
    albertogiantin is on a distinguished road

      0  

    Default


    I have modified my code like this:
    Code:
    interface DetailRenderer extends XTemplates {
    		@XTemplate(source = "SectionDetail.html")
    		public SafeHtml renderItem(SectionItem section, SafeHtml sectionName);
    	}
    
    ....
    		listView.setCell(new SimpleSafeHtmlCell<SectionItem>(new AbstractSafeHtmlRenderer<SectionItem>() {
    
    			public SafeHtml render(SectionItem object) {
    				SafeHtml sectionName = SafeHtmlUtils.fromTrustedString(object.getName());
    				SafeHtml h = r.renderItem(object,sectionName);
    				return h;
    			}
    		}));
    it seems that I'm not allowed to use SafeHtml in the render method, this is the error:

    SafeHtml used in a non-text context. Did you mean to use java.lang.String or SafeStyles instead?

    I've tried also other solution with SafeHtmlUtils.htmlEscapeAllowEntities(object.getName()); but this not change anything...
    I think the problem is with the class AbstractSafeHtmlRenderer that i use in
    listView.setCell(new SimpleSafeHtmlCell<SectionItem>(new AbstractSafeHtmlRenderer<SectionItem>() {....

    looking the source of this class I've found this method:
    Code:
     protected SafeHtml toSafeHtml(Object obj) {
        return obj == null ? EMPTY_STRING
            : SafeHtmlUtils.fromString(String.valueOf(obj));
      }
    Is it possible that method toSafeHtml is called and the red part is the trouble?

  5. #5
    Sencha User
    Join Date
    Nov 2007
    Posts
    35
    Vote Rating
    0
    albertogiantin is on a distinguished road

      0  

    Default


    No the problem is certainly not this.

  6. #6
    Sencha User
    Join Date
    Nov 2007
    Posts
    35
    Vote Rating
    0
    albertogiantin is on a distinguished road

      0  

    Default


    Now, after further analysis, I'm pretty sure that the problem si with XTemplates core implementation class, as you ca see in the image below,debugging my application the result of the renderItem method contain the mistake...

    PrintScreen.jpg

    There is no development group member who can help me?

Thread Participants: 2