Using CSS and the InDesign DOM to format HTML

 

To enable CSS support, edit the field options and press the   button next to the HTML option:

This opens a dialog with a set of options, one of which is CSS and provides the ability to edit the stylesheet. A stylsheet contains a list of properties that map to InDesigns Scripting DOM properties.

Working with Text

The InsertionPoint DOM object is used for text based tags to process the CSS. Any writable property can be set. Collections are supported as shown in the following example:

If the HTML contained no formatting:

Use a stylesheet to apply properties via a basic CSS parser. For example. nth-of-type is used here to pick out each of the first 3 paragraphs:

Some properties, such as appliedFont can apply properties delimited by a tab. A meta character sequence should he used  for the tab:

Working with Images

The Rectangle DOM object is used for <img> tags:

In addition to the DOM properties, EasyCatalog also adds width, height and fit as a writable properties. fit accepts contentproportionally and frametocontent.

It’s also possible to manipulate the text inside an image tag, such as forcing the image into a new page or column.

However because this manipulation is inside the link, the field content is changed so flagged as an error as it modifies content. Modifying link content is generally not recommended for this reason. The following example turns the Rectangle object into a TextFrame and inserts text into it. Meta character sequences are used to insert a paragraph break:

Working with Tables

When working with tables, the <table> tag uses a Table object. The <tr> tag uses a Row object and the <td><th> tag uses a Cell object.

Row Properties

To color the header and an alternating rows, the following stylesheets can be defined. The properties here belong to a Row DOM object. Any property that takes a color can also take a css style color name in addition to a named swatch. These are automatically created by EasyCatalog:

Cell Properties

For <td> and <th> tags, a Cell object is used, so properties for the Cell DOM object can be assigned.  if the HTML contained a class attribute, eg:

Then a stylesheet can pick this out:

Setting a cell width:

Table Properties

The following HTML contains 2 tables with id attributes to identify them:

The following stylesheets targets specific tables using their ID:

This produces the following output:

Some HTML properties conflict with regular HTML properties, such as:

To stop the properties defined by the style node getting applied you can target the node and use the following:

Lua Based Property Content

When a property includes [[ and ]], the text enclosed is treated as Lua code, and the result of the code is substituted into the property. A field function can be used within this Lua code to access the contents of any field associated with the current table, cell, row, text range. If the field does not exist, the field function will return nil.

For example, the following code checks the content of the availability field and applies colors to body cells based on its value:

Lua Based DOM manipulation

A special lua property exists that enables execution of lua based manipulation of the DOM. The self object is the DOM object that matches the CSS target, so Cell, Table, Row, Column or Text.
For example, here we want a table to be 120 mm wide, with the last column always 35 mm. All other column widths are distributed evenly in the remaining space.

The following applies color to a text field using the field function, but also uses the lua property to apply an underline using self. All enhanced HTML formatted fields get a surrounding <body> tag by default.

Sharing CSS between fields

If the CSS just contains the name of a file in the data sources scripts folder, this is loaded and used.

Supported Selectors

The CSS parser is basic and evaluates whether a single HTML-like node matches a given CSS selector. The following selector types are supported:

1. Tag Selector

  • Matches elements by their tag name.
  • Examples:
    • div — matches all <div> elements
    • * — wildcard, matches any element

2. ID Selector

  • Matches elements by their id attribute.
  • Syntax: #idName
  • Example: #main — matches element with id="main"

3. Class Selector

  • Matches elements by their class attribute.
  • Syntax: .className
  • Multiple classes can be chained: .foo.bar
  • Matches elements containing all specified classes.

4. Attribute Selectors

  • Match elements based on attribute values.
  • Supported operators:
    • [attr=value] — exact match
    • [attr~=value] — whitespace-separated list contains value
    • [attr|=value] — equals value or starts with value-
    • [attr^=value] — starts with value
    • [attr$=value] — ends with value
    • [attr*=value] — contains value

5. Structural Pseudo-Classes

Target elements based on their position or type among siblings:

Pseudo-ClassDescription
:first-childMatches the first child of its parent
:last-childMatches the last child of its parent
:nth-child(n)Matches the nth child (1-based) of its parent
:nth-last-child(n)Matches the nth child from the end
:first-of-typeMatches the first element of its type among siblings
:last-of-typeMatches the last element of its type among siblings
:nth-of-type(n)Matches the nth element of its type among siblings
:nth-last-of-type(n)Matches the nth element of its type from the end
:only-childMatches the only child of its parent
:only-of-typeMatches the only element of its type among siblings
:emptyMatches elements with no children (no elements, text, or comments)

Note: nth-child and similar formulas support CSS-style numeric formulas (e.g., 2, 3n+1, odd, even)