Forum How do I...?

Dealing with html abbr abbreviations

kiriel
Ok, came up with another braintwister.

I've got an html document full of abbr tags with title attributes for abbreviations, eg.

<abbr title="Residential Gateway">RG</abbr>

How do I deal with this cleanly in Prince? For example how would I say that the first instance of an abbr with a certain title is expanded by saying
abbr:after {
content: "(" attr(title) ") ";
}
, but not the subsequent ones?

Would there be a way to go even further with this by saying that you only want the first abbreviation with a certain title 'in each page' to be expanded, so that you can remind users once per page what an abbreviation means?

Probably not feasible but if somebody has some genius insight that would be great :)
mikeday
Neither of these are possible with CSS, as there is no selector for matching the first element in a document (or on a page) that has a particular attribute value. It's an interesting idea though! :)
mophor
the first-of-type pseudoclass would do I guess

http://www.w3.org/TR/css3-selectors/#first-of-type-pseudo

edit @ below: you are very right, it's early in my timezone :)

Edited by mophor

mikeday
The :first-of-type pseudo-class will be able to detect the first <abbr> element, but only within that parent element, for example the first <abbr> in a single paragraph, but not the first one in the document. Also, it will not be able to check that it is the first <abbr> element with a particular title attribute. That would require something like this in XPath:
abbr[not(@title = preceding::abbr/@title)]

That should select all abbreviations that do not have a title attribute equal to the title attribute of another abbreviation from earlier in the document. Rather tricky, and currently impossible to express with CSS selectors, which are designed to be simple and efficient.

Trying to do this on a page by page basis (ie. expanding the first instance of abbreviations on the page) is even harder, as CSS selectors (and XPath) apply to the whole document before pagination has taken place. Otherwise you could do sneaky things like this:
abbr:first-on-page { display: none }

Please note that the :first-on-page pseudo-class does not exist! And you can see why: this imaginary example would select the first abbreviation on the page, then remove it from the document. But then it would not be on the page, so the second abbreviation would now be the first abbreviation on the page, so the selector would grab that one and remove it, and so on. It's quite hard to extend selectors with pagination-dependent features without running in to issues like this.