Support for even CSS url() rewriting!

This commit is contained in:
Dan 2017-12-16 22:08:33 -08:00
parent c033769279
commit c75b3dc50c
1 changed files with 11 additions and 7 deletions

View File

@ -8,18 +8,22 @@ function materialize_references_to_ipfs_content(node, element) {
// load that content and set it into the attribute as a blob.
//
// Return a promise for doing all of that.
// TODO: Support <div style="background-image: url(./styles/img/main/alpha-sunrise.png);"> rewriting.
for (let attribute_name of ['content', 'href', 'src']) {
for (let attribute_name of ['content', 'href', 'src', 'style']) {
let attribute_value = element.getAttribute(attribute_name)
if (attribute_value && attribute_value.startsWith('./')) {
if (attribute_value && attribute_value.includes('./')) {
let base_hash = window.location.pathname.match(/(^\/ipfs\/\w+)/)[1]
let uri = base_hash + attribute_value.slice(1)
let anchor = document.createElement('a')
anchor.href = uri
let relative_url = attribute_value.match(/(.\/[^)]+)/)[1]
anchor.href = base_hash + relative_url.slice(1)
console.log('loading IPFS sub-content for ' + anchor.pathname)
return node.files.cat(anchor.pathname).then(function (contents_buffer) {
element.setAttribute(attribute_name, URL.createObjectURL(new Blob([contents_buffer])))
let materialized_value = attribute_value.replace(
relative_url,
URL.createObjectURL(new Blob([contents_buffer]))
)
element.setAttribute(attribute_name, materialized_value)
})
}
}
@ -54,7 +58,7 @@ function App() {
// TODO: Should only do this if it's actually an HTML document. Need to gracefully skip this for other content types.
let contents_document = new DOMParser().parseFromString(contents_buffer.toString(), 'text/html')
let elements = contents_document.querySelectorAll('link,meta,script')
let elements = contents_document.querySelectorAll('link,meta,script,div')
let promises = []
for (let element of elements) {