Skip to content

Commit

Permalink
Improve custom CSS & JS implementation and tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
xwmx committed Nov 7, 2023
1 parent 55b4638 commit 4ff8fde
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 10 deletions.
22 changes: 12 additions & 10 deletions nb
Original file line number Diff line number Diff line change
Expand Up @@ -10597,6 +10597,10 @@ _render() {

# Usage: _render_print_head [--title <title>]
_render_print_head() {
local _custom_css="${NB_CUSTOM_CSS:-}"
local _custom_css_element=
local _custom_javascript="${NB_CUSTOM_JAVASCRIPT:-${NB_CUSTOM_JS:-}}"
local _custom_javascript_element=
local _html_title="${_ME}"

if [[ "${1:-}" == "--title" ]] && [[ -n "${2:-}" ]]
Expand Down Expand Up @@ -10659,16 +10663,14 @@ _render() {
esac
fi

# Set custom JS
local _custom_js=""
if [[ "${NB_CUSTOM_JS-}" ]]; then
_custom_js="<script src='${NB_CUSTOM_JS}'></script>"
if [[ -n "${_custom_javascript:-}" ]]
then
_custom_javascript_element="<script src='${_custom_javascript:-}'></script>"
fi

# Set custom CSS
local _custom_css=""
if [[ "${NB_CUSTOM_CSS-}" ]]; then
_custom_css="<link rel='stylesheet' href='${NB_CUSTOM_CSS}'/>"
if [[ -n "${_custom_css:-}" ]]
then
_custom_css_element="<link rel='stylesheet' href='${_custom_css:-}'/>"
fi

cat <<HEREDOC
Expand Down Expand Up @@ -11350,8 +11352,8 @@ document.addEventListener('keydown', function(event) {
}
});
</script>
${_custom_css}
${_custom_js}
${_custom_css_element:-}
${_custom_javascript_element:-}
<meta name="referrer" content="no-referrer" />
</head>
<body>
Expand Down
63 changes: 63 additions & 0 deletions test/browse.bats
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,69 @@

load test_helper

# vustom css and javascript ####################################################

@test "'browse' omits custom css or javascript tags by default." {
{
"${_NB}" init

sleep 1
}

run "${_NB}" browse --print

printf "\${status}: '%s'\\n" "${status}"
printf "\${output}: '%s'\\n" "${output}"

[[ "${status}" -eq 0 ]]

[[ ! "${output}" =~ \<link\ rel=\'stylesheet\' ]]
[[ ! "${output}" =~ \<script\ src= ]]
}

@test "'browse' includes custom css and javascript tags when assigned." {
{
"${_NB}" init

sleep 1

declare _custom_css=".example { color: blue; }"
declare _custom_js=
_custom_js="$(cat <<HEREDOC
function example() { console.log("Example."); }
HEREDOC
)"
}

NB_CUSTOM_CSS="${_custom_css:-}" \
NB_CUSTOM_JS="${_custom_js:-}" \
run "${_NB}" browse --print

printf "\${status}: '%s'\\n" "${status}"
printf "\${output}: '%s'\\n" "${output}"

[[ "${status}" -eq 0 ]]

[[ "${output}" =~ \
\<link\ rel=\'stylesheet\'\ href=\'.example\ {\ color:\ blue\;\ }\'/\> ]]
[[ "${output}" =~ \
\<script\ src=\'function\ example\(\)\ {\ console.log\(\"Example.\"\)\;\ }\'\>\</script\> ]]

NB_CUSTOM_CSS="${_custom_css:-}" \
NB_CUSTOM_JAVASCRIPT="${_custom_js:-}" \
run "${_NB}" browse --print

printf "\${status}: '%s'\\n" "${status}"
printf "\${output}: '%s'\\n" "${output}"

[[ "${status}" -eq 0 ]]

[[ "${output}" =~ \
\<link\ rel=\'stylesheet\'\ href=\'.example\ {\ color:\ blue\;\ }\'/\> ]]
[[ "${output}" =~ \
\<script\ src=\'function\ example\(\)\ {\ console.log\(\"Example.\"\)\;\ }\'\>\</script\> ]]
}

# configuration ###############################################################

@test "'browse' sets the server hostname to localhost by default." {
Expand Down

0 comments on commit 4ff8fde

Please sign in to comment.