How to add paragraph in rest api including whitespaces? - reactjs

I am fetching string from api and it looks like thisstring original format in console.log()
but, when I use p tag to print it on website it become like this this is how it print on website
how do I maintain the original format of string and print on website

You can modify your string to use HTML tags. For instance you use \ for white space. However if you want to convert your regular String to preserve its formatting in HTML, I wrote a method public static java.lang.String formatStringToPreserveIndentationForHtml(java.lang.String rawText) that converts your regular String into HTML formatted string that preserve the original format. Here is a javadoc for that method. The class TextUtils that has this method is part of Open source MgntUtils java library written and maintained by me. You can get it as maven artifact and on Github (including javadoc and source code)

You can wrap a prefix and a postfix before and after your content before it is being posted to the server, so a string like
" sdfjsgfhks sdgsdjfsj
sdjksjh sdkhsfs "
would be converted to something like
"!^! sdfjsgfhks sdgsdjfsj
sdjksjh sdkhsfs !^!"
before it is posted. Then, the API side, fully aware of the wrapping would trim the prefix and postfix from the string it received.

Related

What encoding function does useSearchParams() use?

I am writing a wrapper around react-router's useSearchParams() hook that automatically handles the (de)serialization of individual search parameters. In my unit test I am attempting to verify that calling the update function returned from the useSearchParams() function will also update the location's search property properly (handling any encoding automatically).
My unit test is nearly working, but the final check is failing. I am expecting the updated value to be $%{ }#=! - and when I use encodeURIComponent on it it becomes: "%24%25%7B %20 %7D%23%3D !"
However, the value that I get back for the parameter from useLocation()'s search property is "%24%25%7B + %7D%23%3D %21"
(white space added so I can bold things for emphasis)
While very similar, and clearly the same value when decoded, react-router is encoding certain characters ever so slightly differently from the standard encodeURIComponent function. Here we see that the whitespace character is getting encoded as %20 by encodeURIComponent and as + by react-router. The exclamation mark is being left simply as ! by encodeURIComponent while react-router has encoded it as %21
Is there a standard function somewhere that will allow me to use and test against react-router's encoding? Or is there any documentation describing the differences between react-router's encoding vs the standard encodeURIComponent function so that I might write my own?
Thanks!

Getting a string result from XPath expression in Talend cSetHeader

I'm being stumped by something that should be trivial.
I've got an xml document, which is split using cSplitter using XPath which works fine, but then I want to set headers with values from the split document.
I've got a cSetHeader component with the Language set to XPath and the valid xpath. However, it returns the value as a NodeList object, when I need a string.
If I use an XPath expression that returns a string, it gives an exception as it can't convert to NodeList.
How, in Talend, do I configure the XPath expression to return a string. It seems ok if you're writing the camel directly, as there is a parameter, but I can't see how it is done in Talend.
Thanks!
I've figured it out...
As it's a code generator, talend puts in the .xpath( ... ) whatever you type in the field - so if you want it to generate a string you put
"/your/xpath/here", java.lang.String.class
in the cSetHeader xpath field and the code generator puts your xpath string with the requested class in the right place!
Easy! Now why didn't I think of that earlier...?

How to verify page title using getpagesource() as it is not returning the correct page source

I am writing simply get page source function to get page source and when I am printing it, it is not printing the same source which I am getting using View Page Source.
String Source = driver.getPageSource();
System.out.println(Source);
Change your code like this
driver.get("http://www.google.co.in");
String Source = driver.getPageSource();
System.out.println(Source);
You actually pass a String literal in the print statement. Just remove the double quotes in print statement

Unterminated string literal error while storing lesson_location

I have this error that doesnt give any indication as to what is the problem:
I'm trying to store this string in lesson_location field:
B^$eNrT0srLywNiLRANpiAsMAknoMKognlwdUi6ERrQuDARmCwmE2EMslOQ9aFaguw6dKyllQxhJcNp TFBTk1iSWhBvYBhvYGAKABXXVRI&#3d;
but it throws SyntaxError: unterminated string literal
when I've modified the way the reload stores the data in ReloadAPIAdaptor.js
from using eval on entire string:
eval("this.cmi.core.lesson_location.cmivalue =\"B^$eNrT0srLywNiLRANpiAsMAknoMKognlwdUi6ERrQuDARmCwmE2EMslOQ9aFaguw6dAzXnJwMozFB TU1ual4pAMimU3Q&#3d;\";");
to evaluate object first:
var o = eval("this." + element);
console.log("o",o);
if(o) o.cmivalue = value;
then it stores data without error,
now I can't modify the code in any lms so this was only to identify if the string can't be stored but it can. Just evil doesnt work so the question is what is in the given string that eval doesnt like and how to fix it.
There is nothing wrong with your string if you're doing the following:
API.SetValue("cmi.core.lesson_location", "B^$eNrT0srLywNiLRANpiAsMAknoMKognlwdUi6ERrQuDARmCwmE2EMslOQ9aFaguw6dKyllQxhJcNp TFBTk1iSWhBvYBhvYGAKABXXVRI&#3d;");
(where API is a reference to the window's API object)
The string is valid as far as SCORM is concerned, and the length falls within the acceptable character limit.
If you're encountering an issue, it might be a bug within the Reload wrapper. Frankly, the code in the Reload wrapper (as found on SourceForge) is TEN years old. It uses eval() and other JavaScript techniques that have been identified as problematic, and are highly discouraged by leading JavaScript developers. Your bug might very well be related to the wrapper's use of eval().
I'd try using a different wrapper and see if it makes a difference.

unterminated string literal error in salesforce

I am trying to get a value from salesforce class into a javascript variable. I am able to get the value if its a single line value, but if its multiline textarea it gives a unterminated string literal error
caseUpdate.Description = "{!ac__c.C_Info__c}";
After googling for sometime i came to know we can have a workaround for this by having a hidden field and using DOM storing it using the document.getElement.Id. But i am calling this code on a button click so i would not be able to create a input text or hidden field value.
Any body who can provide an way to do it?
Thanks
Prady
You should just be able to use the standard Salesforce JSENCODE() function if you are using OnClick Javascript in a button. This will escape any characters for you.
See the documentation.
It is because of line breaks. merge fields are rendered unescaped into the output stream meaning that CRLFs push into a new line and break javascript strings. Either use the div/input trick or use Apex to replace \r\n's in the field with <br/> or whatever best suits the purpose. Also keep in mind that " will also terminate your JS string.
The easiest way is to just include a function in your extension and then you can use it across board
public String FixNewLine(String s) {
if (s != null) return s.replaceAll('\r\n', '<br/>').replaceAll('"', '\\"');
return null;
}
I had the same issue but was able to fix it! The trick is the JSENCODE function. Its basically {!JSENCODE(Obj.Field)}"; So you are replacing your merge field with this function and nesting the merge field itself within the function. In my scenario I ended up with opptyObj.Description="{!JSENCODE(Case.Description)}"; as my total syntax. First calling upon my set object and field, and then the merge data to populate it.

Resources