What encoding function does useSearchParams() use? - reactjs

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!

Related

How to add paragraph in rest api including whitespaces?

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.

How to avoid leading whitespace being trimmed off of directive string bindings?

I'm creating a custom directive that I want to use to display the value of a field and an optional suffix (expected for units and such). Note that my example is shortened to stay concise.
My template looks something like
<div class="my-value">{{boundValue}}{{boundSuffix}}</div>
For the value, I'm using a two-way binding (=) and for the suffix, I'm using a string binding (&).
It worked great when I bound &deg into the suffix to display a temperature, but when I tried to bind in meters (note, there's a leading space - I don't want it pushed up against the number) the leading space seems to get trimmed and my result ends up looking like 123meters.
Using the chrome developer tools, I added a link function and inspected the directive's scope. By the time it reaches the link function, boundSuffix has already been trimmed. It seems like Angular is pulling some shenanigans on my behind the hood. Is there any way for me to avoid this trimming?
It's better to use angular filters to solve your problem. Filters allow to format your output as currency or as UPPERCASE (for example). Try to look here for more info. And here is working example

AugluarJS truncating trailing equal sign

AngularJS truncates the trailing equal signs when routing to them. I have a base64 string which need to be added as a query parameter to the url(route)
ex: http://example.com/#!/updatepassword?code=NnuW3q49QW38Mf-Cg==
this loads as:
http://example.com/#!/updatepassword?code=NnuW3q49QW38Mf-Cg
Is there a workaround for this?
If you try to set route as a string, then you need to escape = sign. That's because this character has a special meaning in the query string - it separates parameter name from its value.
So, one solution could be:
var query = "code=NnuW3q49QW38Mf-Cg==";
$location.url("/some/path?" + encodeURIComponent(query));
What encodeURIComponent() will do, is it will replace all special characters - = will be replaced with %3D for instance. This will prevent it from being interpreted as a key-value separator.
If you only want to change the query string parameters, not the whole URL, you can also use $location.search() method:
$location.search("code", "NnuW3q49QW38Mf-Cg==");
Just remember to pass two parameters to that method, not one. If you do:
$location.search("code=NnuW3q49QW38Mf-Cg==");
the = sign will not get escaped, only stripped.
Angular uses parseKeyValue() function internally to parse query string, it can be found here. You can see that the split is being done over the = sign. That's why they get stripped.
But if you take a look at .search() method implementation, you see that parseKeyValue() is being called only if you supply one argument to .search(). It's not invoked if you supply name of the parameter as a first, and value as a second argument.
Peeking at the source code also suggests yet another solution:
$location.search({"code": "NnuW3q49QW38Mf-Cg=="});

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