ES6 imports are hoisted to the top

Posted Over 2 years ago by Henning Koch.

From Exploring ES6: Module imports are hoisted (internally moved to the beginning of the current scope). Therefore, it doesn’t...

Ensure passing Jasmine specs from your Ruby E2E tests

Posted Over 2 years ago by Henning Koch.

Jasmine is a great way to unit test your JavaScript components without writing an expensive end-to-end test for...

Using feature flags to stabilize flaky E2E tests

Posted Over 2 years ago by Henning Koch.

A flaky test is a test that is often green, but sometimes red. It may only fail on some PCs...

Better numeric inputs in desktop browsers

Posted Over 2 years ago by Arne Hartherz.

You want to use fields in your applications. However, your desktop users may encounter some weird quirks: Aside from allowing...

Event delegation (without jQuery)

Posted Over 2 years ago by Henning Koch.

Event delegation is a pattern where a container element has a single event listener that handles events for all descendants...

Disable built-in dragging of text and images

Posted Over 2 years ago by Dominic Beger.

Most browsers have built-in drag and drop support for different page elements like text and images. While this may...

Unpoly 2: Don't try to download files through AJAX requests

Posted Over 2 years ago by Jakob Scholz.

Rails has the handy controller method send_file which lets us download files easily. We can decide whether the file...

You should probably load your JavaScript with <script defer>

Posted Over 2 years ago by Henning Koch.

It is generally discouraged to load your JavaScript by a tag in the : The reason is that a tag will pause the DOM parser until the script has loaded and executed. This will delay the browser's first contentful paint. A much better default is to load your scripts with a tag: A deferred script has many...

Unobtrusive JavaScript helper to progressively enhance HTML

Posted Over 2 years ago by Henning Koch.

The attached compiler() function below applies JavaScript behavior to matching HTML elements as they enter the DOM. This works like...

Using attribute event handlers with a strict Content Security Policy (CSP)

Posted Over 2 years ago by Henning Koch.

Given you have a strict CSP that only allows elements from your own domain: Content-Security-Policy: script-src 'self' This will block JavaScript handlers inlined as attribute into your HTML elements. Clicking on the following link will only log an error with a strict CSP: click me click me Solution 1: Move the handler into your JavaScript The recommended solution is to move the handler from the HTML to the allowed JavaScript file that we loaded via . In the example above we could invent a new [data-alert] attribute with the alert message: click me Then our JavaScript intercepts clicks on elements with that attribute: document.addEventListener('click', function(event) { let link = event.target.closest('[data-alert]') if (link) { let message = link.dataset.alert alert(message) event.preventDefault() } }) Solution 2: Allow that one handler in your CSP Some browsers allow the CSP directive script-src-attr. This lets you allow the hashes of actual JavaScript code. The SHA256 hash of alert('hello') is vIsp2avtxDy0157AryO+jEJVpLdmka7PI7o7C4q5ABE= (in Base64). We can allow this one event handlers like this: Content-Security-Policy: script-src 'self'; script-src-attr 'unsafe-hashes' 'sha256-vIsp2avtxDy0157AryO+jEJVpLdmka7PI7o7C4q5ABE=' Note the sha256- prefix. This event handler now works when clicked: click me But any other script will still be blocked: click me Dealing with legacy browsers Currently (November 2023) about 75% of browsers support script-src-attr. Here is a forward-looking compromise that many users use with new CSP features: Have a liberal CSP with old directives supported by all browsers Make your CSP stricter with new, more specific directives for browsers that support it The CSP spec supports that approach in that using newer, more specific directives disable older, more general features. In our case this means: For old browsers, allow all inline scripts For new browsers, disallow inline scripts but allow inline handlers with given hashes Here is a CSP directive that works like this: Content-Security-Policy: script-src 'self' 'unsafe-inline'; script-src-elem 'self'; script-src-attr 'unsafe-hashes' 'sha256-vIsp2avtxDy0157AryO+jEJVpLdmka7PI7o7C4q5ABE=' Old browsers will only use script-src. New browsers will use script-src-elem (for tags) and script-src-attr (for inline event handlers), which override the more liberal rules from script-src. Solution 3: Don't use a strict CSP You can have a secure web application without a strict CSP. A Content Security Policy should never be your first line of defense against Cross-Site-Scripting (XSS). Your primary tool should be to escape and sanitize user input. Many template engines make it easy to do the right thing by escaping dynamic content by default. A CSP is a second security net. It's there when you slip up and somehow, somewhere don't sanitize user input. Depending on your project, you may decide to prioritize developer convenience over a strict CSP.

Triggering JavaScript when an element is clicked

Posted Over 2 years ago.

Often people need links which are not linked directly, but should trigger execution of JavaScript. ❌ Bad workarounds

CSS: How to force background images to scale to the container, ignoring aspect ratio

Posted Almost 3 years ago by Arne Hartherz.

You can scale background images in CSS to the container size using background-size (Demo). Commonly, we use contain or...

How to: Ensure proper iconfont rendering with Webpack

Posted Almost 3 years ago by Michael Leimstädtner.

After switching a project from Sprockets to Webpack, I started observing a bug that was hard to debug: Our...

Accessing JavaScript objects from Capybara/Selenium

Posted Almost 3 years ago by Dominic Beger.

When testing JavaScript functionality in Selenium (E2E), you may need to access a class or function inside of a evaluate...

Pre-releasing a Ruby gem

Posted Almost 3 years ago by Henning Koch.

When a Ruby version gem has a letter in its version number, it is considered a pre-release:

RubyMine: How to exclude single files

Posted Almost 3 years ago by Daniel Straßner.
jetbrains.com

In RubyMine folders can be excluded from search, navigation etc. by marking it as excluded. You might sometimes wish to...

Debugging your Webpack build time with Speed Measure Plugin

Posted About 3 years ago by Arne Hartherz.

If your Webpack build is slow, you can use the Speed Measure Plugin for Webpack to figure out where time...

Webpacker: Loading code on demand

Posted About 3 years ago by Henning Koch.

Sometimes you want to load code on demand. For instance, when a a large library is only used on a...

When does Webpacker compile?

Posted About 3 years ago by Henning Koch.

Webpack builds can take a long time, so we only want to compile when needed. This card shows what will...

Converting ES6 to ES5 on the command line

Posted About 3 years ago by Henning Koch.

I use the TypeScript compiler for this, since its output is more minimal than Babel's. The following will transpile...

Capybara: Preventing server errors from failing your test

Posted About 3 years ago by Henning Koch.

When your Rails application server raises error, Capybara will fail your test when it clears the session after the last...

Capybara: Pretending to interact with the document

Posted About 3 years ago by Henning Koch.

Browsers blocks abusable JavaScript API calls until the user has interacted with the document. Examples would be opening new tab...

makandra/capybara-lockstep

Posted About 3 years ago by Henning Koch.
github.com

capybara-lockstep can help you with flaky end-to-end tests: This Ruby gem synchronizes Capybara commands with client-side...

Ruby: Generating and parsing JSON, or: understanding JSON::ParserError "unexpected token"

Posted About 3 years ago by Dominik Schöler.

json is part of the standard library of Ruby and deals with JSON, obviously. As you know, JSON is the...