Skip to content

Commit b248a25

Browse files
authored
Merge pull request #1071 from lightpanda-io/nikneym/element-dir
Add `element.dir` getter & setter
2 parents 8a2efde + 6826c42 commit b248a25

File tree

2 files changed

+31
-6
lines changed

2 files changed

+31
-6
lines changed

src/browser/dom/element.zig

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,14 @@ pub const Element = struct {
104104
return try parser.nodeName(parser.elementToNode(self));
105105
}
106106

107+
pub fn get_dir(self: *parser.Element) ![]const u8 {
108+
return try parser.elementGetAttribute(self, "dir") orelse "";
109+
}
110+
111+
pub fn set_dir(self: *parser.Element, dir: []const u8) !void {
112+
return parser.elementSetAttribute(self, "dir", dir);
113+
}
114+
107115
pub fn get_id(self: *parser.Element) ![]const u8 {
108116
return try parser.elementGetAttribute(self, "id") orelse "";
109117
}

src/tests/dom/element.html

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<!DOCTYPE html>
22
<script src="../testing.js"></script>
33

4-
<div id="content">
4+
<div id="content" dir="ltr">
55
<a id="link" href="foo" class="ok">OK</a>
66
<p id="para-empty" class="ok empty">
77
<span id="para-empty-child"></span>
@@ -17,6 +17,7 @@
1717
testing.expectEqual('div', content.localName);
1818
testing.expectEqual('DIV', content.tagName);
1919
testing.expectEqual('content', content.id);
20+
testing.expectEqual('ltr', content.dir);
2021

2122
content.id = 'foo';
2223
testing.expectEqual('foo', content.id);
@@ -26,6 +27,7 @@
2627

2728
let p1 = document.getElementById('para-empty');
2829
testing.expectEqual('ok empty', p1.className);
30+
testing.expectEqual('', p1.dir);
2931

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

5355
<script id=attributes>
5456
testing.expectEqual(true, content.hasAttributes());
55-
testing.expectEqual(1, content.attributes.length);
56-
testing.expectEqual(['id'], content.getAttributeNames());
57+
testing.expectEqual(2, content.attributes.length);
58+
testing.expectEqual(['id', 'dir'], content.getAttributeNames());
5759
testing.expectEqual('content', content.getAttribute('id'));
5860
testing.expectEqual('content', content.attributes['id'].value);
5961

6062
let x = '';
6163
for (const attr of content.attributes) {
62-
x += attr.name + '=' + attr.value;
64+
x += attr.name + '=' + attr.value + ',';
6365
}
64-
testing.expectEqual('id=content', x);
66+
testing.expectEqual('id=content,dir=ltr,', x);
6567

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

6971
content.setAttribute('foo', 'bar');
7072
testing.expectEqual(true, content.hasAttribute('foo'));
7173
testing.expectEqual('bar', content.getAttribute('foo'));
72-
testing.expectEqual(['id', 'foo'], content.getAttributeNames());
74+
testing.expectEqual(['id', 'dir', 'foo'], content.getAttributeNames());
7375

7476
testing.expectError('Error: InvalidCharacterError', () => {
7577
content.setAttribute('.foo', 'invalid')
@@ -263,3 +265,18 @@
263265
$('#to-remove').remove();
264266
testing.expectEqual(null, $('#to-remove'));
265267
</script>
268+
269+
<script id=elementDir>
270+
const divElement = document.createElement("div");
271+
// Always initialized with empty string if `dir` attribute not provided.
272+
testing.expectEqual("", divElement.dir);
273+
274+
divElement.dir = "ltr";
275+
testing.expectEqual("ltr", divElement.dir);
276+
277+
divElement.dir = "rtl";
278+
testing.expectEqual("rtl", divElement.dir);
279+
280+
divElement.dir = "auto";
281+
testing.expectEqual("auto", divElement.dir);
282+
</script>

0 commit comments

Comments
 (0)