Skip to content
This repository was archived by the owner on Aug 9, 2023. It is now read-only.

Commit 8b6e455

Browse files
authored
Add support for accessing node in render
Closes GH-23. Related to rehypejs/rehype-react#19. Reviewed-by: Titus Wormer <tituswormer@gmail.com>
1 parent 6337edb commit 8b6e455

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

index.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,9 @@ function toH(h, node, ctx) {
138138
// Ensure no React warnings are triggered for void elements having children
139139
// passed in.
140140
result =
141-
elements.length === 0 ? h(name, attributes) : h(name, attributes, elements)
141+
elements.length === 0
142+
? h.call(node, name, attributes)
143+
: h.call(node, name, attributes, elements)
142144

143145
// Restore parent schema.
144146
ctx.schema = parentSchema

test.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -707,6 +707,32 @@ test('hast-to-hyperscript', function(t) {
707707
st.end()
708708
})
709709

710+
t.test('should use a node as a rendering context', function(st) {
711+
function mockR() {
712+
return {
713+
node: this
714+
}
715+
}
716+
717+
var node = u(
718+
'element',
719+
{
720+
tagName: 'svg',
721+
properties: {xmlnsXLink: 'http://www.w3.org/1999/xlink'}
722+
},
723+
[
724+
u('element', {
725+
tagName: 'line',
726+
properties: {strokeDashArray: 4}
727+
})
728+
]
729+
)
730+
var actual = toH(mockR, node)
731+
732+
st.equal(actual.node, node, 'equal rendering context')
733+
st.end()
734+
})
735+
710736
t.end()
711737
})
712738

0 commit comments

Comments
 (0)