Hybrid View

  1. #1
    Ext User
    Join Date
    Jun 2007
    Posts
    67
    Vote Rating
    0
    jschick is on a distinguished road

      0  

    Question TextArea <br> and newline

    TextArea <br> and newline


    Hello,

    I have read numerous threads related to TextArea and newlines but I haven't found one that describes a solution that has worked for me.

    What I want to be able to do is put an instance of TextArea in a FormPanel, have the user enter multiple lines of text in it, save the text in a db on the server (including the newlines), and send the text back to the client at some point and display it in the TextArea instance newlines and all just as it looked when the user entered it.

    Presently I escape the newlines in the text on the server before it is sent to the client using JSON (so the text as a javascript string might look like "this\nis\nsome\ntext") but I found out that that doesn't work because the text is displayed as HTML. So next I tried sending the text back with the newlines replaced with "<br />" but just as with the newlines "<br />" was simply displayed.

    I'm probably missing something obvious and may have very well failed to enter the correct search terms into the forum search to find what I'm looking for. Others must have tackled this issue 100X over....what am I missing?

  2. #2
    Sencha - Ext JS Dev Team Animal's Avatar
    Join Date
    Mar 2007
    Location
    Notts/Redwood City
    Posts
    30,507
    Vote Rating
    56
    Animal has a spectacular aura about Animal has a spectacular aura about Animal has a spectacular aura about

      0  

    Default


    You escape on the server before sending?

    Show us the code you use to do that.

  3. #3
    Ext User
    Join Date
    Jul 2007
    Posts
    3,128
    Vote Rating
    1
    devnull is an unknown quantity at this point

      0  

    Default


    If it is still showing up with <BR> tags then you arent in fact doing a replace on the server. Firebug can inspect the ajax communication to see for sure.
    I typically run the replace regex on the client side when i need to do this.

  4. #4
    Sencha - Ext JS Dev Team Animal's Avatar
    Join Date
    Mar 2007
    Location
    Notts/Redwood City
    Posts
    30,507
    Vote Rating
    56
    Animal has a spectacular aura about Animal has a spectacular aura about Animal has a spectacular aura about

      0  

    Default


    You don't use <br> in a textarea, you use newline characters. The OP probably is not escaping them correctly.

  5. #5
    Ext User
    Join Date
    Jun 2007
    Posts
    67
    Vote Rating
    0
    jschick is on a distinguished road

      0  

    Default


    The server is a device so it's written in C. Here's the code that does the escaping.

    Code:
    #define TRYPUTC(ch) \
    	{ \
    		if (putc((ch), cgiOut) == EOF) { \
    			return -1; \
    		} \
    	} 
    
    
    int jsonEscapeData(char *data, int len) {
    	while (len--) {
    		if(*data == '\"' || *data == '\\') {
    			TRYPUTC('\\');
    			TRYPUTC(*data);
    		}
    		else if(*data == '\r') {
    			TRYPUTC('\\');
    			TRYPUTC('\\');
    			TRYPUTC('r');
    		}
    		else if(*data == '\n') {
    			TRYPUTC('\\');
    			TRYPUTC('\\');
    			TRYPUTC('n');
    		}
    		else {
    		    TRYPUTC(*data);
    		}
    		data++;
    	}
    	return 0;
    }
    
    int jsonEscape(char *s) {
    	return jsonEscapeData(s, (int) strlen(s));
    }

  6. #6
    Ext User
    Join Date
    Jun 2007
    Posts
    67
    Vote Rating
    0
    jschick is on a distinguished road

      0  

    Default


    Animal's right....

    It just took that short break of not looking at the code to realize that I had one too many '\' when I escaped the text.

    Sorry for the trouble. The least I can do is post the working code.

    Code:
    #define TRYPUTC(ch) \
    	{ \
    		if (putc((ch), cgiOut) == EOF) { \
    			return -1; \
    		} \
    	} 
    
    
    int jsonEscapeData(char *data, int len) {
    	while (len--) {
    		if(*data == '\"' || *data == '\\') {
    			TRYPUTC('\\');
    			TRYPUTC(*data);
    		}
    		else if(*data == '\r') {
    			TRYPUTC('\\');
    			TRYPUTC('r');
    		}
    		else if(*data == '\n') {
    			TRYPUTC('\\');
    			TRYPUTC('n');
    		}
    		else {
    		    TRYPUTC(*data);
    		}
    		data++;
    	}
    	return 0;
    }
    
    int jsonEscape(char *s) {
    	return jsonEscapeData(s, (int) strlen(s));
    }
    To clarify where I went wrong...\r and \n are not actually being escaped but instead being replaced by the the distinct characters '\\', 'r', and 'n'.