| 1 |
context("HTML parsing") |
| 2 |
|
| 3 |
accumulated <- data.frame(tag = c(), attribute = c(), value = c()) |
| 4 |
|
| 5 |
reset_accumulator <- function() { |
| 6 |
accumulated <<- data.frame(tag = c(), attribute = c(), value = c()) |
| 7 |
} |
| 8 |
|
| 9 |
html_accumulator <- function(tag, att, val, idx) { |
| 10 |
accumulated <<- rbind(accumulated, data.frame( |
| 11 |
tag = tag, |
| 12 |
attribute = att, |
| 13 |
value = val, |
| 14 |
stringsAsFactors = FALSE)) |
| 15 |
} |
| 16 |
|
| 17 |
test_that("different attribute quoting styles are supported", { |
| 18 |
call_resource_attrs(paste( |
| 19 |
"<img src='123'/>", |
| 20 |
"<img src=\"456\"/>"), html_accumulator) |
| 21 |
expect_equal(accumulated, data.frame( |
| 22 |
tag = c("img", "img"), |
| 23 |
attribute = c("src", "src"), |
| 24 |
value = c("123", "456"), |
| 25 |
stringsAsFactors = FALSE)) |
| 26 |
reset_accumulator() |
| 27 |
}) |
| 28 |
|
| 29 |
test_that("irrelevant white space is ignored", { |
| 30 |
call_resource_attrs(paste( |
| 31 |
"< img src = \n", |
| 32 |
" \t '123'\n", |
| 33 |
" \t value ='abc' />", |
| 34 |
"<link href= '456'>"), html_accumulator) |
| 35 |
expect_equal(accumulated, data.frame( |
| 36 |
tag = c("img", "link"), |
| 37 |
attribute = c("src", "href"), |
| 38 |
value = c("123", "456"), |
| 39 |
stringsAsFactors = FALSE)) |
| 40 |
reset_accumulator() |
| 41 |
}) |
| 42 |
|
| 43 |
test_that("common resource types are found in a simple document", { |
| 44 |
call_resource_attrs(paste( |
| 45 |
"<!DOCTYPE html>\n", |
| 46 |
"<HTML>\n", |
| 47 |
"<HEAD>\n", |
| 48 |
" <SCRIPT SRC=\"foo.js\"></SCRIPT>\n", |
| 49 |
" <LINK REL=\"stylesheet\" HREF=\"bar.css\"/>\n", |
| 50 |
"</HEAD>\n", |
| 51 |
"<BODY>\n", |
| 52 |
" <IMG SRC=\"baz.png\"/>\n", |
| 53 |
" <IFRAME SRC=\"quux.html\"/>\n", |
| 54 |
"</BODY>\n", |
| 55 |
"</HTML>\n"), html_accumulator) |
| 56 |
expect_equal(accumulated, data.frame( |
| 57 |
tag = c("script", "link", "img", "iframe"), |
| 58 |
attribute = c("src", "href", "src", "src"), |
| 59 |
value = c("foo.js", "bar.css", "baz.png", "quux.html"), |
| 60 |
stringsAsFactors = FALSE)) |
| 61 |
reset_accumulator() |
| 62 |
}) |
| 63 |
|
| 64 |
test_that("resources referenced in CSS files are discovered", { |
| 65 |
call_css_resource_attrs(paste( |
| 66 |
"body {\n", |
| 67 |
" background-image: url('foo.png');\n", |
| 68 |
"}\n", |
| 69 |
"p {\n", |
| 70 |
" background-image: url(bar.png), url(\"baz.png\");\n", |
| 71 |
"}\n)"), html_accumulator) |
| 72 |
expect_equal(accumulated, data.frame( |
| 73 |
tag = c("css", "css", "css"), |
| 74 |
attribute = c("url", "url", "url"), |
| 75 |
value = c("foo.png", "bar.png", "baz.png"), |
| 76 |
stringsAsFactors = FALSE)) |
| 77 |
reset_accumulator() |
| 78 |
}) |