getCSSRules: Find the CSS element that applies to an XML/HTML node

Description Usage Arguments Value Author(s) References See Also Examples

Description

This is the function that allows us to lookup/resolve the CSS element that would apply to a particular XML/HTML element. We can take XML nodes created using the XML package.

Usage

1
getCSSRules(nodes, sheet, eng = NULL)

Arguments

nodes

an object of class XMLInternalNode for which we want to find the applicable CSS element(s).

sheet

the CSSStyleSheet-class in which to resolve the CSS element(s) for the given node.

eng

the CSS selection engine. This can be created once and used in multiple, separate calls to this function. This will improve performance marginally.

Value

A list of CSSStatement objects.

Author(s)

Duncan Temple Lang

References

libcroco at http://www.freespiders.org/projects/libcroco Cascading Style Sheets http://www.w3.org/Style/CSS/

See Also

readCSS xmlParse, getNodeSet, newXMLNode asCSSObject

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
css = readCSS( system.file("samples", "OmegaTech.css", package =  "RCSS"))

if(require(XML)) {
 b = newXMLNode("BODY")
 code = newXMLNode("code", attrs = c(class = "r"))
 rules = getCSSRules(b, css)

 x = asCSSObject(rules[[1]])
}

if(require(XML)) {
  css = readCSS( system.file("samples", "descendant.css", package =  "RCSS"))

  node = newXMLNode("a", newXMLNode("b", newXMLNode("c", "some text")))
     # nothing matches the top-level node "a"
  getCSSRules(node, css)
     # for b we get the match a > b
  r = getCSSRules(node[[1]], css)
  asCSSObject(r[[1]])

     # a c
  r = getCSSRules(node[[1]][[1]], css)
  asCSSObject(r[[1]])

   # All the nodes in a document.
  doc = newXMLDoc(node = node, addFinalizer = FALSE)
  allNodes = getNodeSet(doc, "//*")
  lapply(allNodes, getCSSRules, css)

  # We can create the selection engine just once

  eng = .Call("R_createSelectionEngine")
  lapply(allNodes, getCSSRules, css, eng)
}

omegahat/RCSS documentation built on May 24, 2019, 1:52 p.m.