trim attribute values

This commit is contained in:
rhysd 2020-01-10 14:27:15 +09:00
parent 6f158dc6db
commit b51f41fe34
1 changed files with 13 additions and 12 deletions

View File

@ -104,7 +104,7 @@ pub fn walk_and_embed_assets(
let mut link_type = LinkType::Unknown; let mut link_type = LinkType::Unknown;
for attr in attrs_mut.iter_mut() { for attr in attrs_mut.iter_mut() {
if &attr.name.local == "rel" { if &attr.name.local == "rel" {
let value = attr.value.as_ref(); let value = attr.value.trim();
if is_icon(value) { if is_icon(value) {
link_type = LinkType::Icon; link_type = LinkType::Icon;
break; break;
@ -233,7 +233,7 @@ pub fn walk_and_embed_assets(
} else if let Some((dataurl, _)) = (&found_datasrc) } else if let Some((dataurl, _)) = (&found_datasrc)
.into_iter() .into_iter()
.chain(&found_src) // Give dataurl priority .chain(&found_src) // Give dataurl priority
.map(|attr| &attr.value) .map(|attr| attr.value.trim())
.filter(|src| !src.is_empty()) // Ignore empty srcs .filter(|src| !src.is_empty()) // Ignore empty srcs
.next() .next()
.and_then(|src| resolve_url(&url, src).ok()) // Make absolute .and_then(|src| resolve_url(&url, src).ok()) // Make absolute
@ -259,7 +259,7 @@ pub fn walk_and_embed_assets(
let attr_name: &str = &attr.name.local; let attr_name: &str = &attr.name.local;
if attr_name == "src" { if attr_name == "src" {
let src_full_url = resolve_url(&url, attr.value.as_ref()) let src_full_url = resolve_url(&url, attr.value.trim())
.unwrap_or_else(|_| attr.value.to_string()); .unwrap_or_else(|_| attr.value.to_string());
attr.value.clear(); attr.value.clear();
attr.value.push_slice(src_full_url.as_str()); attr.value.push_slice(src_full_url.as_str());
@ -270,7 +270,7 @@ pub fn walk_and_embed_assets(
attr.value.push_slice(TRANSPARENT_PIXEL); attr.value.push_slice(TRANSPARENT_PIXEL);
} else { } else {
let srcset_full_url = let srcset_full_url =
resolve_url(&url, attr.value.as_ref()).unwrap_or_default(); resolve_url(&url, attr.value.trim()).unwrap_or_default();
let (source_dataurl, _) = retrieve_asset( let (source_dataurl, _) = retrieve_asset(
cache, cache,
client, client,
@ -290,13 +290,13 @@ pub fn walk_and_embed_assets(
"a" => { "a" => {
for attr in attrs_mut.iter_mut() { for attr in attrs_mut.iter_mut() {
if &attr.name.local == "href" { if &attr.name.local == "href" {
let attr_value = attr.value.trim();
// Don't touch email links or hrefs which begin with a hash sign // Don't touch email links or hrefs which begin with a hash sign
if attr.value.starts_with('#') || url_has_protocol(&attr.value) { if attr_value.starts_with('#') || url_has_protocol(attr_value) {
continue; continue;
} }
let href_full_url = let href_full_url = resolve_url(&url, attr_value).unwrap_or_default();
resolve_url(&url, attr.value.as_ref()).unwrap_or_default();
attr.value.clear(); attr.value.clear();
attr.value.push_slice(href_full_url.as_str()); attr.value.push_slice(href_full_url.as_str());
} }
@ -326,7 +326,7 @@ pub fn walk_and_embed_assets(
for attr in attrs_mut.iter_mut() { for attr in attrs_mut.iter_mut() {
if &attr.name.local == "src" { if &attr.name.local == "src" {
let src_full_url = let src_full_url =
resolve_url(&url, attr.value.as_ref()).unwrap_or_default(); resolve_url(&url, attr.value.trim()).unwrap_or_default();
let (js_dataurl, _) = retrieve_asset( let (js_dataurl, _) = retrieve_asset(
cache, cache,
client, client,
@ -368,10 +368,11 @@ pub fn walk_and_embed_assets(
"form" => { "form" => {
for attr in attrs_mut.iter_mut() { for attr in attrs_mut.iter_mut() {
if &attr.name.local == "action" { if &attr.name.local == "action" {
let attr_value = attr.value.trim();
// Modify action to be a full URL // Modify action to be a full URL
if !is_valid_url(&attr.value) { if !is_valid_url(attr_value) {
let href_full_url = let href_full_url =
resolve_url(&url, attr.value.as_ref()).unwrap_or_default(); resolve_url(&url, attr_value).unwrap_or_default();
attr.value.clear(); attr.value.clear();
attr.value.push_slice(href_full_url.as_str()); attr.value.push_slice(href_full_url.as_str());
} }
@ -387,7 +388,7 @@ pub fn walk_and_embed_assets(
continue; continue;
} }
let iframe_src = attr.value.as_ref(); let iframe_src = attr.value.trim();
// Ignore iframes with empty source (they cause infinite loops) // Ignore iframes with empty source (they cause infinite loops)
if iframe_src.is_empty() { if iframe_src.is_empty() {
@ -427,7 +428,7 @@ pub fn walk_and_embed_assets(
"video" => { "video" => {
for attr in attrs_mut.iter_mut() { for attr in attrs_mut.iter_mut() {
if &attr.name.local == "poster" { if &attr.name.local == "poster" {
let video_poster = attr.value.as_ref(); let video_poster = attr.value.trim();
// Skip posters with empty source // Skip posters with empty source
if video_poster.is_empty() { if video_poster.is_empty() {