Non-trivial screen scraping selections using pQuery - perl

I'm using pQuery (a Perl port of jQuery) to select elements and retrieve text from a HTML-document.
Consider the following markup:
<x>
<y>code1</y>
<z>stuff</z>
<y>code2</y>
<z>foobar</z>
</x>
And the following pQuery code:
my $target_value = pQuery($markup)->find($pquery_selector)->text;
I'm trying to formulate $pquery_selector so that it matches <z>foobar</z> in the markup above using the following rule: find the z-element that follows after a y-element which has a body containing "code2". While this is possible using jQuery I'm not sure that the pQuery syntax is powerful enough to handle such an expression.
Is this type of selection possible using the pQuery syntax?

In jQuery it might be possible to write a selector like 'y:contains(code2)+z'. However, pQuery is still unfinished (as of version 0.07), and a selector like x+z just gives an error demonstrating that the module developer hasn't gotten around to translating that part of the jQuery code.
Since pQuery hasn't been touched since 2008, I'd recommend either fixing it yourself (the code is on cpan and github), or using a more mature module like HTML::TreeBuilder::XPath (which does require learning XPath syntax, but actually works for non-trivial things).
The XPath equivalent of the above jQuery selector would be '//y[contains(text(), 'code2')]/following-sibling::z'

Related

DOM4J Xpath Truncating Results

We are maintaining an application that heavily relies on DOM4J and Xpath. Once in a while we see a strange behaviors with results of XPath execution via DOM4J: The text result would simply be truncated.
We tried applying the recommendation provided here: http://www.mail-archive.com/dom4j-user#lists.sourceforge.net/msg02688.html
It seems the problem occurs less frequently but it still manifests itsself. Last time we got it, it was after applying Xpath on a the clone of a document parsed as indicated by the previous link.
This also seems to be similar to the issue mentioned here: Use of text() function when using xPath in dom4j

removing a div element with coffeescript

I want to remove a div element with a specific class attribute using Coffeescript. I couldn't find any examples about DOM manipulation with Coffeescript on the Internet. How can I do this? Also any references to doing DOM would be great.
CoffeeScript is a JavaScript preprocessor, there is no additional standard library. What this means is that if you want to do DOM manipulation you would do it the same way you would in JavaScript.
You can use any JavaScript library like jQuery with CoffeeScript, alternatively you can use the document variable directly:
element.parentNode.removeChild(element) for element in document.getElementsByClassName('some-class')
Or (for browsers not supporting that method)
element.parentNode.removeChild(element) for element in document.getElementsByTagName('*') when element.className = 'some-class'
Or, since those identifiers are somewhat long, use block syntax:
for element in document.getElementsByTagName('*')
if element.className is 'some-class'
element.parentNode.removeChild(element)
Relevant quote from CoffeeScript.org:
The golden rule of CoffeeScript is: "It's just JavaScript". The code compiles one-to-one into the equivalent JS, and there is no interpretation at runtime. You can use any existing JavaScript library seamlessly from CoffeeScript (and vice-versa).
#lauren's answer works for me but when I'm using chrome, I'm getting the following error:
Uncaught TypeError: Cannot read property 'id' of undefined
Using the following works perfect, tested on Chrome.
$(document).on 'hidden.bs.modal', "#newProject", ->
document.getElementById("<ID>").outerHTML=''
delete element
From:
https://stackoverflow.com/a/19298575/5452072

The driver.findelement don't find the tab element:

i have this Problem with my test ..the
driver.findElement(By.xpath("//html/body/div[2]/div/div/div[2]/div[2]/div/div[2]/div/div/div/div/div/div/div/ul/li[2]/a[2]/em/span/span/span")).click();
don't find the element.
the eclipse show this message of error
Cannot locate a node using
//html/body/div[2]/div/div/div[2]/div[2]/div/div[2]/div/div/div/div/div/div/div/ul/li[2]/a[2]/em/span/span/span
EDIT : Post edited to reflect answer to actual problem. Original answer follows.
Long XPath expressions are fragile, and tests are prone to fail when relying on them : a completely unrelated change somewhere else in the document can mess everything up, and even if you're aware of the problem, the tests' code is just harder to maintain.
In this particular case, since the site is generated by GWT, it's even worse - there is little control over the actual HTML changes. A good solution when using GWT is to use the ensureDebugId method (see link in comments).
Are you sure that this XPath expression is correct ? Does other tests work with this driver ?
I'd recommend avoiding the use of long XPath expressions like that - wouldn't it be safer in the long term to start the expression at an id-specified div somewhere in the page rather than at the root of the DOM ?

How do I associate a CoffeeScript file with a view?

Just installed rails 3.1 rc1 and am trying to grok the best way to manage javascript with the new asset pipeline
By default all coffeescript is compiled into a single application.js file, this is a good thing.
Each seperate coffee script file is appended to the js file and wrapped in an anonymous function which is executed via the call method
A common scenario would be to use some jquery to turn various forms into ajax forms, update UI, etc...
Many of these scripts will be specific to a controller or action, I am trying to grok the 'conventional' way to handle this,
since everything is wrapped in an anonymous function how do I only execute just
the code for a particular controller / action, by default all of the anonymous functions are being executed
I did play around with some hacks where I load the controller and action name into js variables and then in
coffeescript check those to conditionally run code, I don't like that very much
my initial thought was that each coffee file would contain a js namespace/object and I would call the specific ones from the view,
going to spike this using the default_bare = true configuration
see How can I use option "--bare" in Rails 3.1 for CoffeeScript?
EDIT
Looking around some more: this looks like it might be the correct approach - "Can't find variable" error with Rails 3.1 and Coffeescript
There are two common approaches:
Make behavior conditional on the presence of a particular element. For instance, code to run a signup sheet should be prefaced with something like
if $('#signup').length > 0
Make behavior conditional on a class on the body element. You can set the body class using ERB. This is often desirable for stylesheets as well. The code would be something like
if $('body').hasClass 'user'
gistyle is a simple gem that helps you running action-specific javascript codes.
By following its setup, you set some data attributes in your body element, representing the current controller and action names. Then it will only call that action when the corresponding view is loaded.

Javadoc on CoffeeScript?

I'm new to CoffeeScript and seems that I can't find any document generator for CoffeeScript using Javadoc syntax. The only one I could find is available as a patch to the CoffeeScript compiler.
So, what do you use to generate documentation from Javadoc comment on CoffeeScript or how do you document your function arguments and return values?
So, JavaDoc syntax has never really caught on with JavaScript developers. There are those who use something like it—notably Google—but it's kind of at odds with JS, which doesn't have static typing and allows any number of arguments to any function.
If you want to create beautiful documentation with CoffeeScript, the standard is Docco (its home page is an excellent example). If you want to create JavaDoc-style comments for every function... well, you'll have to create them by hand, and escape them with backticks.
Alternatively, you could work in CoffeeScript until your code is release-ready, then document the resulting JavaScript.
Docco is great for prozedural coding style. If you want to document an API, coffeedoc is for you.
People looking forward to using javadoc style documentation in coffeescript can checkout codo ( http://netzpirat.github.com/codo/ ) which provides support for a subset of javadoc and automatically infers class names, function names and parameters from source code.
I'm using YUIDoc. I can add comments using a syntax similar to Javadoc to my classes, methods and events. The documentation gets output as html/css files and you can even customize the page layout.
Check this documentation example: http://yui.github.com/yuidoc/api/
PS: It relies on Node.JS and you need to install the package yuidocjs
npm install yuidocjs -g