trim() in Symfony with APY DataGridBundle - javascript

I'm having some difficulty stripping blank spaces off the end of an input in a form that uses the APY Data Grid bundle. My suspicions are that this might not be possible without cracking the source code, but I'd love a second opinion and possibly some ideas for a workaround. Quite simply the search provides no results if an extra blank character is added to the search, so I need to trim it before it hits the query.
Typically trim works fine in twig, like so:
{{ 'foo'|trim }}
However this, which uses the above mentioned datagrid, does not
{{ filterColumn(grid, 'foo'|trim) }
I've tried to handle it in the entity (the way APY works is that it takes the item directly from the entity to the twig), but that didn't work. Digging through the vendor files to find how the code takes a filter string and convert to a query, and there does not appear to be anywhere to edit the string anywhere along the way.
I'm looking for alternate solutions, such as using javascript to trim the variable before it gets posted (it's not a huge issue regarding the problems of javascript being required; it's for an internal app, and all users here have it installed). However, this too is proving difficult. I've been attempting something like this: [updating for better code]
var oldValue = document.forms[0].querySelector('input[id$="foo__query__from"]').value;
var newValue = oldValue.trim();
document.forms[0].querySelector('input[id$="foo__query__from"]').value = newValue;
However it still seems to be having the same problem. The issue is it seems to be only possible to deal with by modifying the vendor files, which a) I'm loathe to do, and b) won't work anyway, as they don't get deployed in the same way.
Note that it is difficult to call in the actual form prior to processing; there are no typical form files (e.g. formbuilder) being used by APY. All that is available to me is {{ filterColumn(grid, 'foo') }
Solutions/suggestions would be appreciated.

After some time revisited this, and came up with a javascript solution. Using this took care of the problem, and worked globally for all forms within the application.
$('form').submit(function(){
$(this).find('input').each(function(){
$(this).val($.trim($(this).val()));
});
});

Related

How to scrape a certain text regardless of which tags it is contained in using scrapy

I am trying to scrape a number of sites to find if a certain code snippet is present. Most of the time the scraper works perfectly as intended.
I am using the following method to find the bit of code I am looking for:
...
item["foo"] = response.xpath("//script[contains(text(), 'fooscript')]")
...
if len(item["foo"]) != 0:
doStuff()
However, my issue is the following: sometimes the thing I want to find is not in the script itself but as the source for the script (I know how to scrape this as well), and sometimes when JQuery is used, I can't get the correct scrape results.
So my question is, is there an easier way to look through the raw HTML/JS text to find a match for what I am looking for? Trying to look through all alternatives to scrapes will quickly bloat up the code, and I only need to see if this certain text is present. I have not found a suitable method from the official scrapy documentation (though I am still somewhat inexperienced with the tool, so I might have missed it), so if anyone has a solution for this it would be greatly appreciated.
Maybe simple regex search through the HTML source is what you are looking for? Something like
if re.search(r'fooscript', response.text):
doStuff()
Or, if you just know it's wrapped inside some element and just don't know which, you can do
item["foo"] = response.xpath("//*[contains(text(), 'fooscript')]")
Also, you don't need to use len to check the result, simply
if item["foo"]:
doStuff()
is enough.

is it possible to obfuscate while using soma.js dependency injection?

While looking at how to make JavaScript source code more secure I came upon a lot of 'solutions'. but most people said the same thing; "It's not possible to make your source code 100% secure", "try obfuscation", "run your code server side", etc, etc. After reading a lot posts here on stackoverflow, and other sites I came to the conclusion that a combination of minifying and obfuscating would do the job (for me).
But here is the problem: we are currently using soma.js with dependency injection, and the way we set it up it does not work well with obfuscation. It's basically this:
var session = function(id, sessionModel){
this._sessionmodel = sessionModel;
}
mapping:
injector.mapClass("sessionModel", project.SessionModel, true);
Obfuscation will then rename the sessionModel in the function to for example 'A', but the mapping that was done on SessionModel by the injector still remains 'sessionModel' and not 'A', this then basically breaks the code.
I've read this post which is about the same subject Dependency Injection and Code Obfuscation, but it does not provide a real answer to my problem so I decided to write my own question.
Any tips/hint/suggestions are welcome.
Thanks in advance.
EDIT
It seems you can tell Yuicompressor to exclude certain identifiers by putting in 'hints' into the files like this: "identifier:nomunge, identifier2:nomunge".
var session = function(id, sessionModel){
"sessionModel:nomunge";
this._sessionmodel = sessionModel;
}
I tested this and it works but that means you'll have to put it in yourself which is a lot of work if you have to do that for every script, especially if you have a very big project..
Gonna look into it further, and update this post if anything new pops up
EDIT 2
It's been a while, I only work 1 day a week on this =S.
As said before you can get it working by telling it which identifiers to exclude.
For that I looked into regular expression to get the "mapped classes" programmatically, since doing it by hand is just insane.
What I basically did was instead of putting every hint in by hand, I made a identifier, for example "#nomunge"; and used a simple replaceregexp task to find it and replace it with a string containing all the identifiers. This string is build by loading the script and going through it with a tokenfilter.
<target name="build hints">
<loadfile property="hints" srcFile="${temp.loc}/all.js">
<filterchain>
<tokenfilter delimoutput=":nomunge,">
<ignoreblank/>
<containsregex pattern="${regexp}"/>
</tokenfilter>
</filterchain>
</loadfile>
<echo message="${hints}"/>
</target>
<replaceregexp file="${temp.loc}/all.js"
match="#nomunge"
flags = "g"
replace = "target:nomunge, dispatcher:nomunge, injector:nomunge,${hints}"
/>
This seems to do the job, for now...
I'm behind the soma.js framework, feel free to ask me questions on the google group, happy to help.
This might help a bit more:
https://groups.google.com/forum/#!topic/somajs/noOX2R4K58g
Romu

Javascript mailto string loses 'encodeURI' encoding

Trying to create a simple 'mailto' function using javascript. I just need to be able to send some links (like: See this article bla bla).
Some of the links I need to send include spaces, danish chars. So I've been using the
encodeURI() function.
The problem arises when I try to mail the link (sample code below)
var _encodedPath = encodeURI(path);
var _tempString = "mailto:someemail#somewhere.dk?subject=Shared%20from%20some%20page&body=" + _encodedPath;
If I output the _tempString to the console I get the correct encoded string. However when using the same string in 'mailto' the string loses it's encoding and returns to the way it was before.
Any clue as to why this is?
Thanks in advance :)
The link is decoded when you click it - that's normal. Since you have an http link within a mailto link, it should be encoded twice.
Email clients do their best to make things that look like links clickable. They typically decide where the link ends in a somewhat arbitrary and unpredictable manner.
In email, the best way to keep a link contiguous is to enclose it in angle-brackets like this:
<http://www.example.com/url with spaces>
But this isn't foolproof. Email is fragile and you can't control the content well enough with a mailto link. It might be better to try to reduce the complexity of the url - perhaps by providing or utilizing a url-shortener service. Any url longer than 74 or so characters is likely to be mangled by some email clients.
You should use encodeURIComponent instead of encodeURI.
More information here.
this site helped me solving any troubles with mailto links:
http://www.1ngo.de/web/formular.html
may be it's not the nicest way, but it always works with every browser i know. And it also has very cool algorithm implemented to format the content so that everything should be alright. Just try it and play around a little with code by quoting out parts of the code and you will understand very fast what exactly happens there and how to modify it for your wishes. Althoug it's a little late I hope this one helps anybody checking this question.
althoug it's in german, you just need to copy the code shown there and run it and experiment with it.

Eclipse: Type mismatch: cannot convert from String[] to any

Eclipse is complaining about my JavaScript code:
As fas as I can tell, the code is working fine. What do I have to change to get rid of the warning?
The semantic validator (despite it's failings, like this one) actually has a lot of uses. For instance, it can tell you when you've got a variable that's not being used anywhere (eg. because you have a typo in the variable's name).
If you don't want to see that error message, but you still want to keep semantic validation on, you can use this hack:
var textArray = 0 || [ ...
it's a little ugly, and your non-Eclipse-using co-workers may not like it, but at least it gives you a way to ditch the warning and still have the benefits of the validator.
A similar hack (if you don't like the first one, or if you're a big Douglas Crockford fan) that will also work is:
var textArray;
textArray = [ ...
Seems to be a bug of WTP , more specifically of the JS Validator component which is by default configured to "Enable JavaScript semantic validation".
Try to turn off the semantic validation from preferences page.

How would I solve a coding puzzle with Javascript?

There is a website called Gild.com that has different coding puzzles/challenges for users to do. They can be completed in wide array of languages including Javascript. I am interested in solving these puzzles in Javascript, but I am unsure of the following:
How am I supposed to access the input file which is supposed to be passed as an argument?
How am I supposed to output the result?
My understanding of Javascript is that it is run from within an HTML page and that output really is only in the form of placing values in the HTML, modifying the DOM, etc. For that reason it is not clear to me how Javascript can be used for solving these types of problems. Can someone who has used Gild before or has some insights into my question suggest how to proceed?
An example of a problem would be: the given input file contains a positive integer, find the sum of all prime numbers smaller than that integer and output it.
EDIT: Some of the solutions below involve using external resources, but on Gild, I am supposed to put my solution in their editor and then submit it that way, like the following picture shows:
In other words, I don't think my solution can have access to Node.js or other external resources.
Edit: Here are some interesting articles that I have found that I think are the answer to my question:
http://www.phpied.com/installing-rhino-on-mac/
http://www.phpied.com/javascript-shell-scripting/
I haven't spent much time on Gild, but I do a lot of similar types of problems on Project Euler. I think the best way to go is probably Node.js.
If you're not familiar, Node is basically server-side JavaScript that runs in Google's V8 engine. Installing it on your own Mac/Windows machine takes about 2 minutes. It's also really fast (considering it's JavaScript).
And you'd use it like this:
var fs = require('fs'); // the filesystem module
var contents = fs.readFileSync('theFile.txt', 'utf-8');
// Do stuff with the file contents...
Everything after those first two lines can be done with the same JS you'd write in the browser, right down to calling console.log() to spit out the answer.
So, if you wrote your script in a file on your desktop called getprimes.js, you'd open up your terminal and enter node ~/Desktop/getprimes.js (assuming you're on a Mac)
If you're:
on a Mac,
planning to do a lot of these puzzles, and
willing to pay $10, then
I highly recommend CodeRunner. It encapsulates runtimes for a variety of languages — from C to JavaScript — and lets you quickly build and run any sort of one-off code. Just hack together your code, ⌘R, and the results are printed right there in the same window.
I haven't used any file-based JavaScript in CodeRunner, but I imagine kennis's suggestions would apply. To output your results:
console.log(...)
Easy as pie!

Categories