Debian Codegraph @

R/html_parser.R

raw
1 # This function invokes a callback for each resource attribute discovered
2 # in its first argument. The parameters to the callback are:
3 #
4 # 1) The tag discovered (e.g. "img")
5 # 2) The resource attribute (e.g. "src")
6 # 3) The attribute's value (e.g. "thumbnail.png")
7 # 4) The position of the attribute value in the original string (in bytes)
8 #
9 call_resource_attrs <- function(html, callback = NULL) {
10
11 # for performance reasons, we do all regexp matching on literal bytes, and
12 # reapply encoding after extracting matches
13 html_encoding <- Encoding(html)
14 Encoding(html) <- "bytes"
15
16 # use the fastest defaults we can here--PCRE, bytes (RE and input both UTF-8)
17 # as the HTML can be quite large
18 tags <- gregexpr(
19 "<\\s*(img|link|object|script|audio|video|embed|iframe)\\s+([^>]+)>", html,
20 perl = TRUE, useBytes = TRUE, ignore.case = TRUE)[[1]]
21
22 for (pos in seq_along(tags)) {
23 # extract the HTML tag in question
24 tagstart <- attr(tags, "capture.start", exact = TRUE)[pos,1]
25 tag <- substr(html, tagstart, tagstart +
26 attr(tags, "capture.length", exact = TRUE)[pos,1] - 1)
27 Encoding(tag) <- html_encoding
28 tag <- tolower(tag)
29
30 # determine the attribute to look for based on the tag name
31 tagattr <- switch(tag,
32 img = "src",
33 link = "href",
34 object = "data",
35 script = "src",
36 audio = "src",
37 video = "src",
38 embed = "src",
39 iframe = "src")
40
41 # extract the tags
42 attrstart <- attr(tags, "capture.start", exact = TRUE)[pos,2]
43 attrs <- substr(html, attrstart, attrstart +
44 attr(tags, "capture.length", exact = TRUE)[pos,2] - 1)
45
46 # see if one of the attributes if the one we're looking for
47 attrmatch <- regexpr(paste0("\\s*", tagattr,
48 "\\s*=\\s*('[^']+'|\"[^\"]+\")"),
49 attrs, ignore.case = TRUE, perl = TRUE,
50 useBytes = TRUE)
51 if (attrmatch >= 0) {
52 matchstart <- attrstart +
53 attr(attrmatch, "capture.start", exact = TRUE) - 1
54 matchlength <- attr(attrmatch, "capture.length", exact = TRUE)
55
56 # before we attempt to extract the entire attribute value, check to see
57 # if it's a large data blob; if it is, skip it. note that substr() is
58 # very expensive on long strings so this is cheaper than just doing a
59 # prefix check immediately.
60 if (matchlength > 256) {
61 if (substr(html, matchstart + 1, matchstart + 5) == "data:") {
62 next
63 }
64 }
65
66 # extract the matching bytes, re-apply encoding, and invoke the callback
67 resource <- substr(html, matchstart + 1, matchstart + matchlength - 2)
68 Encoding(resource) <- html_encoding
69 callback(tag, tagattr, resource, matchstart + 1)
70 }
71 }
72 }
73
74 # as call_resource_attrs, but for CSS
75 call_css_resource_attrs <- function(css, callback = NULL) {
76 css_encoding <- Encoding(css)
77 Encoding(css) <- "bytes"
78
79 urls <- gregexpr("url\\s*\\(\\s*['\"]?([^'\")]+)['\"]?\\s*\\)", css,
80 perl = TRUE, useBytes = TRUE, ignore.case = TRUE)[[1]]
81 for (pos in seq_along(urls)) {
82 urlstart <- attr(urls, "capture.start", exact = TRUE)[pos,1]
83 url <- substr(css, urlstart, urlstart +
84 attr(urls, "capture.length", exact = TRUE)[pos,1] - 1)
85 Encoding(url) <- css_encoding
86 callback("css", "url", url, urlstart)
87 }
88 }