tr:html-embed-resources html-embed-resources

xproc-util/html-embed-resources/xpl/html-embed-resources.xpl

Import URI: http://transpect.io/xproc-util/html-embed-resources/xpl/html-embed-resources.xpl

tr:html-embed-resources

This step tries to embed external resources such as images, CSS and JavaScript via data URI into the HTML document.

Output Ports

NameDocumentationConnections

result

provides the XHTML document with embedded resources

Options

NameDocumentationDefault

exclude

Space-separated list of tokens. Available tokens are: image video script style audio object #all. (Question: support font as a category on its own?)

''

debug

'no'

fail-on-error

'true'

Subpipeline

StepInputsOutputsOptions

p:variable top-level-base-uri

( /*/@xml:base, base-uri(/*) )[1]

p:variable suppress-video

tokenize($exclude, '\s+')[. = ('#all', 'video')]

p:variable suppress-audio

tokenize($exclude, '\s+')[. = ('#all', 'audio')]

p:variable suppress-image

tokenize($exclude, '\s+')[. = ('#all', 'image')]

p:variable suppress-script

tokenize($exclude, '\s+')[. = ('#all', 'script')]

p:variable suppress-style

tokenize($exclude, '\s+')[. = ('#all', 'style')]

p:variable suppress-object

tokenize($exclude, '\s+')[. = ('#all', 'object')]

p:viewport viewport

p:variable local-base-uri

(base-uri(.), $top-level-base-uri)[1]

p:variable href-attribute

(*[local-name() = ('img', 'audio', 'video', 'script')]/@src, html:object/@data, html:link/@href, svg:image/@xlink:href)[1]

p:variable href

if(starts-with($href-attribute, 'data:')) (: leave data URIs as-is :) then $href-attribute else resolve-uri(if(matches($href-attribute, '^(http[s]?|file)://?')) (: resolve regular URIs :) then $href-attribute else concat(replace($local-base-uri, '^(.+/).+$', '$1'), $href-attribute), $local-base-uri)

p:choose d172e51

exists( /*[local-name() = ('img'[$suppress-image], 'audio'[$suppress-audio], 'video'[$suppress-video], 'script'[$suppress-script])][@src] | /html:object[@data][$suppress-object] | /html:link[@rel eq 'stylesheet'][@href][$suppress-style] | /svg:image[@xlink:href][$suppress-image] )

Suppress embedding for elements meeting these conditions. Unfortunately, the conditions could not be specified in the p:viewport match attribute because options and variables seem to be inaccessible there.

p:identity d172e58

source

result

not(starts-with($href-attribute, 'data:'))

p:try d172e63

p:group d172e65

p:choose d172e67

$debug eq 'yes'

cx:message d172e71

source

result

message = 'embed: ', $href

p:otherwise

p:identity d172e79

source

result

p:add-attribute construct-http-request

source

 <c:request method="GET" detailed="false"/>

result

attribute-name = 'href'

match = '/c:request'

attribute-value = $href

p:http-request http-request

source

result on construct-http-request

result

p:add-attribute add-xmlbase

source

result on http-request

result

attribute-name = 'xml:base'

match = '//c:body'

attribute-value = $href

p:choose d172e108

html:img|html:audio|html:video|html:script|html:object|svg:image

p:string-replace d172e121

source

current on viewport

result

match = '*[local-name() = ('img', 'audio', 'video', 'script')]/@src|html:object/@data|svg:image/@xlink:href|html:video/html:source|html:audio/@src/html:source/@src'

replace = concat('''', 'data:', $content-type, ';', $encoding, ',', //c:body, '''')

p:otherwise

p:insert insert-style

source

 <style/>

insertion

result on add-xmlbase

result

match = 'html:style'

position = 'first-child'

p:try try-extract-references-from-css

p:group d172e157

p:xslt extract-references-from-css

source

result on insert-style

stylesheet

p:document../xsl/css-embed-resources.xsl

result

p:viewport viewport-data-uri

p:variable data-uri

tr:data-uri/@href

p:variable mime-type

tr:data-uri/@mime-type

p:choose d172e177

$debug eq 'yes'

cx:message d172e181

source

result

message = 'embed: ', $data-uri

p:otherwise

p:identity d172e189

source

result

p:unwrap d172e223

source

result

match = 'html:style//tr:data-uri'

p:catch d172e226

error

p:identity d172e228

source

result on insert-style

result

p:unwrap d172e238

source

result

match = 'html:style//c:body'

p:catch d172e245

error

p:choose d172e247

$fail-on-error eq 'true'

p:error d172e251

source

 <c:error>Failed to embed HTML resource.</c:error>

result

code = 'html-resource-embed-failed'

p:otherwise

p:identity d172e266

source

current on viewport

result

p:choose d172e274

$debug eq 'yes'

cx:message d172e278

source

result on d172e266

result

message = '[WARNING] failed to embed file: ', $href

p:otherwise

p:identity d172e286

source

result on d172e266

result

p:otherwise

p:identity d172e297

source

current on viewport

result