Pages: 1
Print
Author Topic: CSS Selector last-child not working  (Read 326 times)
ndudek
Member

Posts: 14


« on: August 21, 2017, 01:22:36 AM »

Hello,

I'm trying to customize a certain element through css. I want to use the last-child selector in order to add a line break through css. Unfortunately it seems like XMetaL doesn't understand this selector. Any Idea on how to give css instruction to the last appearance of an element?
Logged
Derek Read
Program Manager (XMetaL)
Administrator
Member

Posts: 2513



WWW
« Reply #1 on: September 05, 2017, 12:44:05 PM »

For any selectors that are unsupported (now or into the future) or for other complex selectors that CSS just does not support in general you can use the Selection.ContainerStyle or the Range.ContainerStyle API. This will work for any element you can programmatically move a Range to in your document (ie: there is some logic you can use to uniquely identify an element). In your case running the following would likely give you what you want:

Code: (Example(JScript): setting CSS through script)
//XMetaL Script Language JScript:
/*Change following XPath to suit your needs.
Hopefully it is as simple as changing the element
name from Para to yours.
Setting color:red shows how to set multiple CSS
properties on the same node. Remove that if you
don't want them red.
*/
var xpath = "//Para[last()]";
var nodes = ActiveDocument.getNodesByXPath(xpath);
var rng = ActiveDocument.Range;
for(var i=0; i<nodes.length; i++) {
rng.SelectNodeContents(nodes.item(i));
rng.ContainerStyle = "margin-bottom:2em; color:red";
}

If that doesn't do it please provide more information on the XML structure (include a sample).

Note that in this example I use getNodesByXPath. You can use any logic to move the Range to the element you need to style. Other methods would include walking the document element by element to identify node name, content, attributes, etc. I merely use the getNodesByXPath API here because that was the simplest way to do it given the requirements.
« Last Edit: September 05, 2017, 12:46:41 PM by Derek Read » Logged
Pages: 1
Print
Jump to: