Skip to content

Commit ec90d0d

Browse files
committed
nitpick
1 parent 23a2bad commit ec90d0d

File tree

8 files changed

+66
-15
lines changed

8 files changed

+66
-15
lines changed

takumi-helpers/src/utils.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,16 @@ import type { Node } from "./types";
44
const defaultTimeout = 5000;
55
const cssUrlPattern = /url\(\s*(['"]?)(.*?)\1\s*\)/g;
66

7+
function isFetchableResourceUrl(value: string): boolean {
8+
return value.startsWith("https://") || value.startsWith("http://");
9+
}
10+
711
function collectCssUrls(value: unknown, urls: Set<string>) {
812
if (typeof value === "string") {
913
for (const match of value.matchAll(cssUrlPattern)) {
10-
if (match[2]) {
11-
urls.add(match[2]);
14+
const url = match[2]?.trim();
15+
if (url && isFetchableResourceUrl(url)) {
16+
urls.add(url);
1217
}
1318
}
1419
} else if (Array.isArray(value)) {
@@ -34,10 +39,7 @@ export function extractResourceUrls(node: Node): string[] {
3439
collectStyleUrls(current.style);
3540
collectStyleUrls(current.preset);
3641

37-
if (
38-
current.type === "image" &&
39-
(current.src.startsWith("https://") || current.src.startsWith("http://"))
40-
) {
42+
if (current.type === "image" && isFetchableResourceUrl(current.src)) {
4143
urls.add(current.src);
4244
return;
4345
}

takumi-helpers/test/utils/extract-resource-urls.test.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,4 +40,15 @@ describe("extractResourceUrls", () => {
4040

4141
expect(extractResourceUrls(node)).toEqual(["https://example.com/good.png"]);
4242
});
43+
44+
test("ignores non-fetchable css url values", () => {
45+
const validUrl = "https://example.com/background.png";
46+
const node = container({
47+
style: {
48+
backgroundImage: `url(background), url("${validUrl}")`,
49+
},
50+
});
51+
52+
expect(extractResourceUrls(node)).toEqual([validUrl]);
53+
});
4354
});

takumi/benches/effects.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ fn run_effect_render(global: &GlobalContext, effect_tw: &str) {
2222
.global(global)
2323
.build();
2424

25-
let _image = render(options).unwrap();
25+
let image = render(options).unwrap();
26+
black_box(image);
2627
}
2728

2829
fn bench_effects(c: &mut Criterion) {

takumi/benches/fixtures.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ fn render_fixture(global: &GlobalContext, node: Node) {
2727
.global(global)
2828
.build();
2929

30-
let _image = render(options).unwrap();
30+
let image = render(options).unwrap();
31+
black_box(image);
3132
}
3233

3334
fn simple_image_blit_fixture() -> Node {

takumi/benches/gradient.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ fn run_gradient_render(global: &GlobalContext, background_image_str: &str) {
3131
.global(global)
3232
.build();
3333

34-
let _image = render(options).unwrap();
34+
let image = render(options).unwrap();
35+
black_box(image);
3536
}
3637

3738
fn bench_gradients(c: &mut Criterion) {

takumi/src/error.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -355,6 +355,15 @@ pub enum TakumiError {
355355
#[error("Invalid viewport: width or height cannot be 0")]
356356
InvalidViewport,
357357

358+
/// RGBA buffer length does not match `width * height * 4`.
359+
#[error("invalid RGBA buffer length: expected {expected} bytes, got {actual}")]
360+
InvalidRgbaBufferLength {
361+
/// Actual RGBA byte length in the buffer.
362+
actual: usize,
363+
/// Expected RGBA byte length from dimensions.
364+
expected: usize,
365+
},
366+
358367
/// Animated encode was requested without any frames.
359368
#[error("{format} animation must contain at least one frame")]
360369
EmptyAnimationFrames {

takumi/src/rendering/components/blur.rs

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
use crate::Result;
21
use crate::rendering::BufferPool;
2+
use crate::{Error, Result};
33

44
/// Specifies the type of blur operation, which affects how the CSS radius is interpreted.
55
#[derive(Debug, Clone, Copy, PartialEq)]
@@ -138,7 +138,10 @@ pub(crate) fn apply_blur_rgba_bytes(
138138
.saturating_mul(height as usize)
139139
.saturating_mul(4);
140140
if data.len() != expected {
141-
return Ok(());
141+
return Err(Error::InvalidRgbaBufferLength {
142+
actual: data.len(),
143+
expected,
144+
});
142145
}
143146

144147
let Some(pass_params) = blur_pass_params(width, height, radius, blur_type, width as usize * 4)
@@ -373,3 +376,26 @@ fn compute_mul_shg(d: u32) -> (u32, i32) {
373376
let mul = ((1u64 << shg) as f64 / d as f64).round() as u32;
374377
(mul, shg)
375378
}
379+
380+
#[cfg(test)]
381+
mod tests {
382+
use super::{BlurType, apply_blur_rgba_bytes};
383+
use crate::{Error, rendering::BufferPool};
384+
385+
#[test]
386+
fn apply_blur_rgba_bytes_returns_error_for_invalid_buffer_length() {
387+
let mut data = vec![0u8; 3];
388+
let mut pool = BufferPool::default();
389+
390+
let err = apply_blur_rgba_bytes(&mut data, 1, 1, 4.0, BlurType::Filter, &mut pool)
391+
.expect_err("expected invalid RGBA buffer length error");
392+
393+
match err {
394+
Error::InvalidRgbaBufferLength { actual, expected } => {
395+
assert_eq!(actual, 3);
396+
assert_eq!(expected, 4);
397+
}
398+
_ => panic!("unexpected error variant: {err}"),
399+
}
400+
}
401+
}

takumi/src/rendering/stacking_context.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use crate::{
1212
},
1313
rendering::{
1414
BorderProperties, Canvas, CanvasSubcanvas, CanvasViewport, NodeMaskAction, Placement, Sizing,
15-
blend_pixel, draw_debug_border, fast_div_255, prepare_node_mask,
15+
blend_pixel, draw_debug_border, prepare_node_mask,
1616
},
1717
};
1818

@@ -81,9 +81,9 @@ fn blend_plus_darker_pixel_raw_4(dst_px: &mut [u8], src_px: &[u8]) {
8181
let green = ((src_px[1] as u16 + dst_px[1] as u16).saturating_sub(255)) as u8;
8282
let blue = ((src_px[2] as u16 + dst_px[2] as u16).saturating_sub(255)) as u8;
8383

84-
dst_px[0] = fast_div_255(red as u32 * result_alpha as u32);
85-
dst_px[1] = fast_div_255(green as u32 * result_alpha as u32);
86-
dst_px[2] = fast_div_255(blue as u32 * result_alpha as u32);
84+
dst_px[0] = red;
85+
dst_px[1] = green;
86+
dst_px[2] = blue;
8787
dst_px[3] = result_alpha;
8888
}
8989

0 commit comments

Comments
 (0)