mirror of
https://github.com/lightpanda-io/browser.git
synced 2026-03-22 04:34:44 +00:00
markdown: encode resolved URLs in links and images
This commit is contained in:
@@ -298,7 +298,7 @@ fn renderElement(el: *Element, state: *State, writer: *std.Io.Writer, page: *Pag
|
|||||||
}
|
}
|
||||||
try writer.writeAll("](");
|
try writer.writeAll("](");
|
||||||
if (el.getAttributeSafe(comptime .wrap("src"))) |src| {
|
if (el.getAttributeSafe(comptime .wrap("src"))) |src| {
|
||||||
const absolute_src = URL.resolve(page.call_arena, page.base(), src, .{}) catch src;
|
const absolute_src = URL.resolve(page.call_arena, page.base(), src, .{ .encode = true }) catch src;
|
||||||
try writer.writeAll(absolute_src);
|
try writer.writeAll(absolute_src);
|
||||||
}
|
}
|
||||||
try writer.writeAll(")");
|
try writer.writeAll(")");
|
||||||
@@ -313,7 +313,7 @@ fn renderElement(el: *Element, state: *State, writer: *std.Io.Writer, page: *Pag
|
|||||||
if (!has_content and label == null and href_raw == null) return;
|
if (!has_content and label == null and href_raw == null) return;
|
||||||
|
|
||||||
const has_block = hasBlockDescendant(el.asNode());
|
const has_block = hasBlockDescendant(el.asNode());
|
||||||
const href = if (href_raw) |h| URL.resolve(page.call_arena, page.base(), h, .{}) catch h else null;
|
const href = if (href_raw) |h| URL.resolve(page.call_arena, page.base(), h, .{ .encode = true }) catch h else null;
|
||||||
|
|
||||||
if (has_block) {
|
if (has_block) {
|
||||||
try renderChildren(el.asNode(), state, writer, page);
|
try renderChildren(el.asNode(), state, writer, page);
|
||||||
@@ -667,6 +667,7 @@ test "browser.markdown: resolve links" {
|
|||||||
try page.parseHtmlAsChildren(div.asNode(),
|
try page.parseHtmlAsChildren(div.asNode(),
|
||||||
\\<a href="b">Link</a>
|
\\<a href="b">Link</a>
|
||||||
\\<img src="../c.png" alt="Img">
|
\\<img src="../c.png" alt="Img">
|
||||||
|
\\<a href="/my page">Space</a>
|
||||||
);
|
);
|
||||||
|
|
||||||
var aw: std.Io.Writer.Allocating = .init(testing.allocator);
|
var aw: std.Io.Writer.Allocating = .init(testing.allocator);
|
||||||
@@ -676,6 +677,7 @@ test "browser.markdown: resolve links" {
|
|||||||
try testing.expectString(
|
try testing.expectString(
|
||||||
\\[Link](https://example.com/a/b)
|
\\[Link](https://example.com/a/b)
|
||||||
\\
|
\\
|
||||||
|
\\[Space](https://example.com/my%20page)
|
||||||
\\
|
\\
|
||||||
, aw.written());
|
, aw.written());
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user