Skip to content

Lightpanda: the headless browser designed for AI and automation

Ignore empty Script.src values

Karl Seguin961da45ecee5
diff --git a/src/browser/tests/element/html/script/dynamic.html b/src/browser/tests/element/html/script/dynamic.html
index d67b1ef2e..91ede4ea1 100644
--- a/src/browser/tests/element/html/script/dynamic.html
+++ b/src/browser/tests/element/html/script/dynamic.html
@@ -43,18 +43,45 @@
});
</script>
-<script id=set_attribute_src>
+<script id=set_attribute_src_before_append>
loadedByAttribute = 0;
-
const attributeBeforeAppend = document.createElement('script');
attributeBeforeAppend.setAttribute('src', 'dynamic_attribute.js');
document.getElementsByTagName('head')[0].appendChild(attributeBeforeAppend);
+ testing.onload(() => {
+ testing.expectEqual(1, loadedByAttribute);
+ });
+</script>
+<script id=set_attribute_src_after_append>
+ loadedByAttribute2 = 0;
const attributeAfterAppend = document.createElement('script');
document.getElementsByTagName('head')[0].appendChild(attributeAfterAppend);
- attributeAfterAppend.setAttribute('src', 'dynamic_attribute.js');
+ attributeAfterAppend.setAttribute('src', 'dynamic_attribute2.js');
+ attributeAfterAppend.setAttribute('src', 'dynamic_attribute2.js');
+ testing.onload(() => {
+ testing.expectEqual(1, loadedByAttribute2);
+ });
+</script>
+
+<script id=remove_attribute_src>
+ loadedByAttribute3 = 0;
+ const attributeRemoved = document.createElement('script');
+ attributeRemoved.setAttribute('src', 'dynamic_attribute3.js');
+ attributeRemoved.removeAttribute('src');
+ document.getElementsByTagName('head')[0].appendChild(attributeRemoved);
+ testing.onload(() => {
+ testing.expectEqual(0, loadedByAttribute3);
+ });
+</script>
+<script id=empty_attribute_src>
+ loadedByAttribute4 = 0;
+ const attributeEmptySrc = document.createElement('script');
+ document.getElementsByTagName('head')[0].appendChild(attributeEmptySrc);
+ attributeEmptySrc.setAttribute('src', '');
+ attributeEmptySrc.setAttribute('src', 'dynamic_attribute4.js');
testing.onload(() => {
- testing.expectEqual(2, loadedByAttribute);
+ testing.expectEqual(1, loadedByAttribute4);
});
</script>
diff --git a/src/browser/tests/element/html/script/dynamic_attribute2.js b/src/browser/tests/element/html/script/dynamic_attribute2.js
new file mode 100644
index 000000000..adc0d8d08
--- /dev/null
+++ b/src/browser/tests/element/html/script/dynamic_attribute2.js
@@ -0,0 +1 @@
+loadedByAttribute2 += 1;
diff --git a/src/browser/tests/element/html/script/dynamic_attribute3.js b/src/browser/tests/element/html/script/dynamic_attribute3.js
new file mode 100644
index 000000000..4f0696e4e
--- /dev/null
+++ b/src/browser/tests/element/html/script/dynamic_attribute3.js
@@ -0,0 +1 @@
+loadedByAttribute3 += 1;
diff --git a/src/browser/tests/element/html/script/dynamic_attribute4.js b/src/browser/tests/element/html/script/dynamic_attribute4.js
new file mode 100644
index 000000000..ac135c362
--- /dev/null
+++ b/src/browser/tests/element/html/script/dynamic_attribute4.js
@@ -0,0 +1 @@
+loadedByAttribute4 += 1;
diff --git a/src/browser/webapi/element/Attribute.zig b/src/browser/webapi/element/Attribute.zig
index c2023a1ef..597e7b418 100644
--- a/src/browser/webapi/element/Attribute.zig
+++ b/src/browser/webapi/element/Attribute.zig
@@ -206,18 +206,20 @@ pub const List = struct {
return gop.value_ptr.*;
}
- pub fn put(self: *List, name: String, value: String, element: *Element, frame: *Frame) !*Entry {
+ pub fn put(self: *List, name: String, value: String, element: *Element, frame: *Frame) ![]const u8 {
const result = try self.getEntryAndNormalizedName(name, frame);
return self._put(result, value, element, frame);
}
- pub fn putSafe(self: *List, name: String, value: String, element: *Element, frame: *Frame) !*Entry {
+ pub fn putSafe(self: *List, name: String, value: String, element: *Element, frame: *Frame) ![]const u8 {
const entry = self.getEntryWithNormalizedName(name);
return self._put(.{ .entry = entry, .normalized = name }, value, element, frame);
}
- // The returned *Entry is only valid until the next mutation of the list.
- fn _put(self: *List, result: NormalizeAndEntry, value: String, element: *Element, frame: *Frame) !*Entry {
+ // Returns the entry's canonical name, not the *Entry: attributeChange can
+ // run script which mutates the list, moving or shifting entries. The
+ // canonical name is interned, so it stays valid.
+ fn _put(self: *List, result: NormalizeAndEntry, value: String, element: *Element, frame: *Frame) ![]const u8 {
const owner = element.ownerFrame(frame);
const is_id = shouldAddToIdMap(result.normalized, element);
@@ -241,15 +243,16 @@ pub const List = struct {
self._len += 1;
}
+ const name = entry.name();
if (is_id) {
const parent = element.asNode()._parent orelse {
- return entry;
+ return name;
};
try owner.addElementId(parent, element, entry.value());
}
owner.domChanged();
owner.attributeChange(element, result.normalized, .wrap(entry.value()), old_value);
- return entry;
+ return name;
}
// Optimized for cloning. We know the names are already normalized and
@@ -284,10 +287,10 @@ pub const List = struct {
ea._element = null;
}
- const entry = try self.put(attribute._name, attribute._value, element, frame);
+ const name = try self.put(attribute._name, attribute._value, element, frame);
attribute._element = element;
const owner = element.ownerFrame(frame);
- try owner._attribute_lookup.put(owner.arena, .{ .list = self, .name = entry._name_ptr }, attribute);
+ try owner._attribute_lookup.put(owner.arena, .{ .list = self, .name = name.ptr }, attribute);
return existing_attribute;
}
diff --git a/src/browser/webapi/element/html/Script.zig b/src/browser/webapi/element/html/Script.zig
index 7c3c9864f..f39c77dec 100644
--- a/src/browser/webapi/element/html/Script.zig
+++ b/src/browser/webapi/element/html/Script.zig
@@ -168,7 +168,7 @@ pub const Build = struct {
const self = element.as(Script);
self._src = element.getAttributeSafe(comptime .wrap("src")) orelse "";
- if (element.asNode().isConnected()) {
+ if (self._src.len > 0 and element.asNode().isConnected()) {
try frame.scriptAddedCallback(false, self);
}
}