Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/browser/dom/element.zig
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,14 @@ pub const Element = struct {
return try parser.nodeName(parser.elementToNode(self));
}

pub fn get_dir(self: *parser.Element) ![]const u8 {
return try parser.elementGetAttribute(self, "dir") orelse "";
}

pub fn set_dir(self: *parser.Element, dir: []const u8) !void {
return parser.elementSetAttribute(self, "dir", dir);
}

pub fn get_id(self: *parser.Element) ![]const u8 {
return try parser.elementGetAttribute(self, "id") orelse "";
}
Expand Down
29 changes: 23 additions & 6 deletions src/tests/dom/element.html
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<!DOCTYPE html>
<script src="../testing.js"></script>

<div id="content">
<div id="content" dir="ltr">
<a id="link" href="foo" class="ok">OK</a>
<p id="para-empty" class="ok empty">
<span id="para-empty-child"></span>
Expand All @@ -17,6 +17,7 @@
testing.expectEqual('div', content.localName);
testing.expectEqual('DIV', content.tagName);
testing.expectEqual('content', content.id);
testing.expectEqual('ltr', content.dir);

content.id = 'foo';
testing.expectEqual('foo', content.id);
Expand All @@ -26,6 +27,7 @@

let p1 = document.getElementById('para-empty');
testing.expectEqual('ok empty', p1.className);
testing.expectEqual('', p1.dir);

p1.className = 'foo bar baz';
testing.expectEqual('foo bar baz', p1.className);
Expand All @@ -52,24 +54,24 @@

<script id=attributes>
testing.expectEqual(true, content.hasAttributes());
testing.expectEqual(1, content.attributes.length);
testing.expectEqual(['id'], content.getAttributeNames());
testing.expectEqual(2, content.attributes.length);
testing.expectEqual(['id', 'dir'], content.getAttributeNames());
testing.expectEqual('content', content.getAttribute('id'));
testing.expectEqual('content', content.attributes['id'].value);

let x = '';
for (const attr of content.attributes) {
x += attr.name + '=' + attr.value;
x += attr.name + '=' + attr.value + ',';
}
testing.expectEqual('id=content', x);
testing.expectEqual('id=content,dir=ltr,', x);

testing.expectEqual(false, content.hasAttribute('foo'));
testing.expectEqual(null, content.getAttribute('foo'));

content.setAttribute('foo', 'bar');
testing.expectEqual(true, content.hasAttribute('foo'));
testing.expectEqual('bar', content.getAttribute('foo'));
testing.expectEqual(['id', 'foo'], content.getAttributeNames());
testing.expectEqual(['id', 'dir', 'foo'], content.getAttributeNames());

testing.expectError('Error: InvalidCharacterError', () => {
content.setAttribute('.foo', 'invalid')
Expand Down Expand Up @@ -263,3 +265,18 @@
$('#to-remove').remove();
testing.expectEqual(null, $('#to-remove'));
</script>

<script id=elementDir>
const divElement = document.createElement("div");
// Always initialized with empty string if `dir` attribute not provided.
testing.expectEqual("", divElement.dir);

divElement.dir = "ltr";
testing.expectEqual("ltr", divElement.dir);

divElement.dir = "rtl";
testing.expectEqual("rtl", divElement.dir);

divElement.dir = "auto";
testing.expectEqual("auto", divElement.dir);
</script>