/
apply-style.R
367 lines (316 loc) · 11.8 KB
/
apply-style.R
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# https://developer.mozilla.org/en-US/docs/Web/SVG/Attribute/Presentation
#
# SVG can define properties for a node in the presentation attributes on that
# node - separate from both the inline style and the inherited style from the
# parent.
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
svg_presentation_attrs <- c(
"alignment-baseline",
"baseline-shift",
"clip",
"clip-path",
"clip-rule",
"color",
"color-interpolation",
"color-interpolation-filters",
"color-profile",
"color-rendering",
"cursor",
"direction",
"display",
"dominant-baseline",
"enable-background",
"fill",
"fill-opacity",
"fill-rule",
"filter",
"flood-color",
"flood-opacity",
"font-family",
"font-size",
"font-size-adjust",
"font-stretch",
"font-style",
"font-variant",
"font-weight",
"glyph-orientation-horizontal",
"glyph-orientation-vertical",
"image-rendering",
"kerning",
"letter-spacing",
"lighting-color",
"marker-end",
"marker-mid",
"marker-start",
"mask",
"opacity",
"overflow",
"pointer-events",
"shape-rendering",
"solid-color",
"solid-opacity",
"stop-color",
"stop-opacity",
"stroke",
"stroke-dasharray",
"stroke-dashoffset",
"stroke-linecap",
"stroke-linejoin",
"stroke-miterlimit",
"stroke-opacity",
"stroke-width",
"text-anchor",
"text-decoration",
"text-rendering",
"transform",
"unicode-bidi",
"vector-effect",
"visibility",
"word-spacing",
"writing-mode"
)
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# https://www.w3.org/TR/2016/CR-SVG2-20160915/styling.html#PresentationAttributes
#
# SVG has some presentation attributes that apply to the tag they are currently
# on, but otherwise do not take part in the cascade.
#
# For example: a circle can have a 'cx' presentation attribute, but that
# does not flow to child elements (not that child elements of a <circle>
# even make sense).
#
# From the other direction, if a <g> element contains a 'cx' attribute, and
# since 'cx' does not apply to <g>, then this attribute will
# (a) NOT be part of the <g> element's style
# (b) NOT cascade to any child elements.
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
svg_tag_specific_presentation_attrs <- list(
circle = c('cx', 'cy', 'r'),
ellipse = c('cx', 'cy', 'rx', 'ry'),
foreignObject = c('height', 'width', 'x', 'y'),
image = c('height', 'width', 'x', 'y'),
rect = c('height', 'width', 'x', 'y', 'rx', 'ry'),
svg = c('height', 'width', 'x', 'y'),
symbol = c('height', 'width', 'x', 'y'),
use = c('height', 'width', 'x', 'y'),
path = c('d')
)
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#' Depth-first recursion into the HTML document node tree to accumulate/cascade styles
#'
#' @param node current XML node
#' @param xpath_styles list containing the accumulated styles for each xpath
#' @param parent_style the style from the direct parent
#' @param svg include SVG presentation tags in the cascade? default: FALSE
#'
#' @return updated xpath style lookup list
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
recurse_node_tree <- function(node, xpath_styles, parent_style = list(), svg) {
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Get the current style for this node from the xpath style lookup
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
tag <- xml2::xml_name(node)
xpath <- xml2::xml_path(node)
xpath_style <- xpath_styles[[xpath]]
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# If this is SVG, then look at the attributes, and extract any
# attributes to take part in the cascade.
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
presentation_attrs <- list()
if (svg) {
attrs <- xml2::xml_attrs(node)
attr_names <- c(svg_presentation_attrs, svg_tag_specific_presentation_attrs[[tag]])
attr_names <- intersect(names(attrs), attr_names)
presentation_attrs <- as.list(attrs[attr_names])
}
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Combine all the style elements we have access to.
# The style of the parent is given lowest priority. The inline style
# on this element itself has the highest priority/specificity
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
this_style <- css_merge(
parent_style,
presentation_attrs,
xpath_style,
read_inline_style(
xml2::xml_attr(node, 'style')
)
)
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Set the style for this node
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
xpath_styles[[xpath]] <- this_style
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Expclicity drop any properties that are not inherited before going on
# to parse the child nodes of the node.
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
this_style[non_inherited_properties] <- NULL
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# tag specific presentation attributes don't get inherited.
# No reference for this, but I created a few <svg> docs and tested this
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
if (svg) {
tag_pres_attrs <- svg_tag_specific_presentation_attrs[[tag]]
this_style[tag_pres_attrs] <- NULL
}
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Recurse into child nodes
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
for (child in xml2::xml_children(node)) {
xpath_styles <- recurse_node_tree(child, xpath_styles, this_style, svg = svg)
}
xpath_styles
}
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#' Create a named list (indexed by \code{xpath}) of the final computed style for each element
#'
#' @param xml xml document as returned by \code{xml2::read_html()} or
#' \code{xml2::read_xml()}
#' @param css list of rules parsed from CSS stylesheet. E.g as returned
#' by \code{cssparser::read_css()}
#' @param svg include SVG presentation tags in the cascade? default: FALSE
#'
#' @return named list of styles where the name is the \code{xpath} to a
#' node in the document, and the value is a named list of
#' property/values for this element.
#'
#' @import xml2
#' @import selectr
#' @export
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
css_apply <- function(xml, css = list(), svg = FALSE) {
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# The 'xpath' searching seems to work better if namespaces (ns) are
# stripped from the xml
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
xml <- xml2::xml_ns_strip(xml)
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# remove '@' rules e.g. "@media" as these do not take part in
# the style cascade.
# Pseudo-elements and classes not handled either, so remove
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
selectors <- names(css) %||% character(0)
exclude <- startsWith(selectors, '@') | grepl(':', selectors)
css <- css[!exclude]
selectors <- names(css) %||% character(0)
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Calculate CSS selector specificity
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
specificity <- vapply(selectors, function(sel) {
selector_specificity(sel)
}, integer(1))
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Rearrange the rules in increasing order, so we will end up addressing
# least specific rules first, and then override with rules of increasing
# specificity
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
css <- css[order(specificity)]
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Create an empty styles list
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
xpath_styles <- list()
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# For each rule
# - get the CSS selector
# - convert selector to xpath
# - fina all elements matching that xpath
# - merge the current style in `xpath_styles` with this new rule
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
for (i in seq_along(css)) {
selector <- names(css)[i]
rule <- css[[i]]
xpath <- selectr::css_to_xpath(selector)
nodes <- xml2::xml_find_all(xml, xpath)
for (node in nodes) {
sig <- xml2::xml_path(node)
# cat(selector, sig, "\n")
xpath_styles[[sig]] <- style_merge(
xpath_styles[[sig]],
rule
)
}
}
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# - In the above, all the direct rules from CSS stylesheet were applied
# - Now performe the CSS cascase from parent-to-child
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
recurse_node_tree(xml, xpath_styles, svg = svg)
}
if (FALSE) {
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Style sheet
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
stylesheet <- '
body {
color: blue;
}
.light {
font-weight: lighter;
}
.light.speed {
font-weight: normal;
}
div > p {
color: hotpink;
}
'
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# HTML document
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
html <- xml2::read_html('
<body>
<html>
<h1> hello </h1>
<p> Nothing happens in this p </p>
<div id="main" style="color:white; width:50%">
<div id="sidebar">Sidebar</div>
<div id="content">Main content</div>
<p> This paragraph </p>
<ul></ul>
</div>
<div id="footer" class="light" style="color:green;"> goodbye </div>
</html>
</body>
')
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Parse the CSS rules and get the root node of the HTML
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
rules <- read_css(stylesheet)
user_agent_css <- list(svg = list(
`color` = 'black',
`clip-path` = 'none',
`clip-rule` = 'nonzero',
`clipPathUnits` = 'userSpaceOnUse',
`fill` = 'black',
`fill-opacity` = 1,
`fill-rule` = 'nonzero',
`font-family` = 'serif',
`font-size` = 12,
`font-style` = 'normal',
`font-weight` = 'normal',
`mask` = 'none',
`stroke` = 'none',
`stroke-dasharray` = 'none',
`stroke-dashoffset` = 0,
`stroke-linecap` = 'butt',
`stroke-linejoin` = 'miter',
`stroke-miterlimit` = 4,
`stroke-opacity` = 1,
`stroke-width` = 1,
`visibility` = 'visible'
))
svg <- TRUE
rules <- list()
user_agent_css <- list()
parent_style <- list()
svg_file = 'working/tiger.svg'
svg_file = 'working/Freesample.svg'
svg_file = "working/test-transform-translate.svg"
svg_file = "working/polygon2.svg"
html <- xml2::read_xml(svg_file) %>% xml2::xml_ns_strip()
cat(readLines(svg_file), sep = "\n")
xpstyle <- css_apply(html, user_agent_css = user_agent_css, svg = TRUE)
xpstyle[['/svg/rect']]
xpstyle[['/svg/rect']]$width
}