The HTML5 DOM Element API: Dataset

Datasets are one of the new Element features added with the HTML5 spec. They allow you to add arbitrary attributes to DOM Elements that can be easily accessed and modified via JavaScript. These attributes are great for adding additional information for projects such as photo galleries that can use the attributes to enhance the display.

Using data attributes on elements

Datasets are added to an element via element attributes. A dataset attribute is prefixed by data- and followed by the name. For example, data-author.

For this post, we are going to use a photo gallery as an example of when you might want to use datasets to add additional markup to your HTML. To start, let us create the first div with attributes that define the author, location, and date of the photo. Here is the markup:

As you can see, the three items we want in the dataset are prefixed by data- and are assigned a value the same way as any other HTML attribute.

Accessing an element’s dataset

To access the dataset of an element, simply use the attribute dataset. Here is a quick example.

The dataset attribute returns a DOMStringMap, a new datatype added for datasets. Note: hyphenated attribute names are turned into camel case for JavaScript access.

Modifying and removing an element’s dataset

Modifying an item in the dataset is very easy, just assign the property in the dataset a new value.

Deleting is equally easy, just use the keyword delete and specify the property of the dataset to delete.

Using data attributes with CSS

Like other HTML Element attributes, dataset attributes can be used as part of CSS selectors. Adding a blue background to all divs with Dave as the author looks like the following:

Datasets provide an easy way to associate little bits of meta data with your HTML tags and allows easy access from your JavaScript. For more intensive JavaScript applications you will still be better off using JavaScript objects to hold your state. However, for quick projects like a photo gallery, dataset attributes provide easy access to additional data for each element. As far as browser support, here is the caniuse page.

Introduction to Google Guava's Joiner class

The Joiner class performs the complementary function of the Splitter class. The Joiner class will join an Iterable, an array, or a variable argument list using a given separator in between them.

Starting with the basics

The basic usage of the Joiner uses the on function and then by calling join with an array or Iterator object. To demonstrate this, let us use a Set of names to create a comma separated list.

Dealing with nulls

Like the Splitter, the Joiner comes with a couple of utilities for dealing with null in the input. The first is the skipNulls function that will return a Joiner which will automatically skip over any nulls it encounters when iterating over the elements. The second is the useForNull function which will use the parameter it was given in place of nulls. Here are examples of using each one:

If you use both the skipNulls and useForNull on the same joiner, it will throw an UnsupportedOperationException.

Using the Joiner class to build up a StringBuilder

In addition to joining a String all at the same time, you can build it up instead using a StringBuilder. The appendTo function of the Joiner takes the same arguments as the join function: an Iterable, array, or argument list an will work for both StringBuilders and any Appendable object. One caveat of this function is that it will only add the separator between elements within each function call. Below is an example of using it with a StringBuilder.

As you can see, the commas were only added in between elements in the same appendTo call which is why johndanmatt was not separated.

Using the MapJoiner (new in Guava 10.0)

A good example of using a MapJoiner is to create a query string out of a Map of query parameters. Let us use the following map:

{"param": "v", "p2": "v2", "q": "java"}

And the code:

The MapJoiner does not have utilities for trimming and cleaning up the strings, so that will have to be done while building the Map. You can read the full documentation on the Joiner class here.

View the previous post on Google Guava’s Splitter class

Introduction to Google Guava's Splitter class

The Splitter class takes a common, and what should not have to be verbose, exercise and makes it easy and readable. Let us start with a scenario, you have a list of key value pairs that you need to parse into a map. We are also going to assume the input could be malformed with empty entries and extra spaces. For the sake of this example, let us use names and user ids.

"dave:123, john:314,, matt:989"

Now, how this is typically handled:

This ends up being rather verbose and also error prone. We will see how the Splitter class makes this much easier and readable.

Starting with the basics

The simplest example of the Splitter is parsing a standard comma separated list.

Pretty straightforward. In addition to a string separator, the Splitter.on function can also use a char, CharSequence, or a Pattern. The API provides a convenience method Splitter.onPattern that will turn a String into a java Pattern.

Utilities to sanitize the input

We saw in the initial scenario a poorly formatted string that required us to check for null and trim at various points to clean up the input. The Splitter class comes with the utility functions omitEmptyStrings and trimResults to take away some of that boilerplate code.

Another benefit of using the utility functions to clean up the split string is readability. When someone is reading your code, it is much easier to understand what is happening when seeing omitEmptyStrings and trimResults versus checks for null, checks for empty string, and various trim calls.

Using the MapSplitter (new in Guava 10.0)

The MapSplitter allows you to take a list of key value pairs and easily turn it into a Java Map. Well, that sounds great for solving our initial scenario, let us see it in action.

That looks much better. We removed the loop, the input was sanitized, we threw out any empty strings, and we broke up the separated values into key/value pairs. It also reads a lot better:

Split a string on comma, omit empty strings, trim those results, then use colon as a key value separator. With these rules, split stringToSplit

This is much easier to read than our initial version. You can read the full documentation on the Splitter class here.