Get the last leaf of a DOM tree (while considering text nodes)
I use this to simulate the (non-existing) :last-letter
CSS pseudoclass, e. g. to insert a tombstone at the end of an article:
findLastLeaf = ($container) ->
$children = $container.children()
if $children.length == 0
$container
else
$lastChild = $children.last()
$lastContent = $container.contents().filter(->
# Only return nodes that are either elements or non-empty text nodes
@nodeType == 1 || (@nodeType == 3 && _.strip(@nodeValue) != '')
).last()
...
AngularJS: Access the scope for a rendered DOM element
This trick might be useful to implement more complicated directives in AngularJS. I needed it to do drag'n'drop in a hierarchical tree.
Let's say you have this $scope
in your Angular controller:
$scope.tasks = [
{ 'text': 'Task 1' },
{ 'text': 'Task 2' }
]
And you have this template:
<ul ng-repeat="task in tasks">
<li>
{{task.text}}
</li>
</ul>
Which renders this HTML:
<ul>
<li>Task 1</li>
<li>Task 2</li>
</ul>
If you'd like to access the scope bound to the second <li>
you can say this in jQ...
Trigger an event with Javascript
This is non-trivial because you need to fake event objects and require different code for different browsers. Luckily, there is tool support for most types of events.
In jQuery you can say:
$('a#close_window').click();
In Prototype you can use event.simulate.js from the Protolicious library to say:
$$('a#close_window')[0].simulate('click');
To trigger custom events with Prototype, you can use the [built-in Element.fire()
](http://api.prototypejs.org/dom/...
Prevent double clicks on link_to_remote (simple case)
This works well in the simplified case, when your link disappears after it was clicked.
Let link_to_remote behave as „disabled“ after the first click. Use the :before
hook to replace the orignal link with a link that does nothing but looks like the original link:
:ruby
label = "do_something"
dummy_link = link_to(label)
other_attributes_hash = { :url => ..., :method => ..., ... }
disable_link_option = { :before => "$('your_link_selector').html('#{escape_javascript(dummy_link)}'" } # jquery
= link_to_remote(label, other_att...
Updated: Capybara: Check that a page element is hidden via CSS
- The step we used in the past (
Then "foo" should not be visibile
) doesn't reliably work in Selenium features. - I overhauled the entire step so it uses Javascript to detect visibility in Selenium.
- The step has support for jQuery and Prototype projects, so it should be a drop-in replacement for all your projects.
- For Rack::Test the step no longer uses XPath so you should be able to understand it when you are not a cyborg :)
- There were some other cards detailing alternative steps to detect visibility. I deleted all these other cards s...
CSS: Select elements that contain another selector
CSS4 comes with :has
. E.g. h1:has(b)
would select all <h1>
tags that contain a <b>
tag.
This is implemented in no browser but the jQuery query engine already supports it as a custom extension.
Onload callback for dynamically loaded images
Sometimes you need to dynamically load an image and do something as soon as its loaded (when for example its size is already available).
With jQuery, this seems to work across browsers:
$('<img>')
.attr('src', '')
.load(function() {
alert('fully loaded!');
})
.attr('src', '/the/real/image/url');
Keyboard Navigation Plugin for Safari, Chrome and Firefox with a rich feature set
gleeBox is an experimental project that takes a keyboard-centric approach to navigating the web. It provides alternatives to actions that are traditionally performed via the mouse. Some of these are radically more efficient than using a mouse, some not so much. In all cases, they are mostly meant for keyboard and command line lovers.
Gleebox is a terrific plugin that makes navigating through webpages a wink. It even allows for selecting by jQuery selector.
You cannot use :before or :after on img in CSS
Though the W3C even gives it as an example, no browser actually supports this CSS:
img:before {
content: "something";
}
Browsers will simply not render anything when doing that on images (Fun fact: It worked in an older version of Opera but got dropped).\
The same applies to the :after
pseudo-element.
This makes me sad.
You can try using jQuery instead.
CoffeeScript
Imagine all the syntactical delights of Ruby and Haml for your JavaScript. You write in a nice language, but get normal JavaScript at runtime. All whilst having full access to 3rd-party JavaScript libraries (jQuery, PrototypeJS), debugging support (it becomes pure, readable JavaScript), existing support from test suites (it’s normal JavaScript) and growing support from various text editors (TextMate, Vim, Emacs).
How to generate a Rails-compatible query string
From Rails 3.0.9, there is a method Hash#to_query that will turn a Hash into a query string:
>> {:a => "a", :b => ["c", "d", "e"]}.to_query
=> "a=a&b%5B%5D=c&b%5B%5D=d&b%5B%5D=e"
>> CGI.unescape _
=> "a=a&b[]=c&b[]=d&b[]=e"
If you're on the browser side, you can serialize nestd objects to query strings using jQuery's $.param
.
patbenatar/jquery-nested_attributes
jQuery plugin that makes it easy to dynamically add and remove records when using ActiveRecord's nested attributes.
kamens/jQuery-menu-aim
jQuery plugin to fire events when user's cursor aims at particular dropdown menu items. For making responsive mega dropdowns like Amazon's.
What's in a View? A look at the alternatives
Great look at the tradeoffs between progressive enhancement with jQuery or similiar, vs. client-side views.
Responsive & Touch-Friendly Audio Player | Codrops
A jQuery audio player plugin that is responsive and touch-friendly. The UI is css-only, no images used.
turn.js - The page flip effect for HTML5
turn.js is a plugin for jQuery that adds a beautiful transition similar to real pages in a book or magazine for HTML5.
I tested it successfully on Chrome, Firefox and IE9.
Rails 3.1.0 has been released!
jQuery as new default Javascript library, streaming response support, attr_accessible with roles, prepared statements, easier migrations.
Rails 3.1: Release candidate
Asset pipeline, HTTP streaming, jQuery as default framework, auto-reversable migrations, identity map for ActiveRecord.
Ruby 1.8.x support will be dropped with or after Rails 4.
Prototype 1.7 is out
jQuery's selector engine, live()-like event handlers, pixel-perfect layout measuring.
wkrte - Project Hosting on Google Code
WKRTE is a jQuery rich text editor based on lwrte by plandem.
Cucumber: Wait for any requests to finish before moving on to the next scenario
Background
Generally, Selenium tests use the browser to interact with the page. If it's unavailable, a timeout error is thrown.
Now, consider a scenario like this:
@javascript
Scenario: Receive an e-mail after clicking the fancy link
When I follow "fancy link"
Then I should have an e-mail with the subject "Hello"
When the last step in the scenario passes, you are done. Right? Wrong.
Why it's not enough
What if clicking our "fancy link" above sends the e-mail that we expect, but it also does stuff on the server...
Making IE 9 happy
If you're using jQuery, you need to update to 1.5.1 to get Internet Explorer 9 to work.
Apart from that there seem to be surprisingly few problems. Many CSS3 attributes work and no major layout problems at first glance.
I only recommend to take a look at your box shadows, since IE does render those a bit differently (generally lighter and offset by what looks to be half a pixel).