From 9d7392865279b10cf10c6061ca25569bd22e88d3 Mon Sep 17 00:00:00 2001 From: Titus Wormer Date: Mon, 23 Nov 2020 10:38:45 +0100 Subject: [PATCH] Fix `ancestors` not working when in `root` Closes rehypejs/rehype-sanitize#8. --- lib/index.js | 2 +- test.js | 9 +++++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/lib/index.js b/lib/index.js index 1665e11..b9e8cad 100644 --- a/lib/index.js +++ b/lib/index.js @@ -249,7 +249,7 @@ function handleTagName(schema, tagName, node, stack) { // Some nodes can break out of their context if they don’t have a certain // ancestor. if (own.call(schema.ancestors, name)) { - while (++index <= schema.ancestors[name].length) { + while (++index < schema.ancestors[name].length) { if (stack.indexOf(schema.ancestors[name][index]) > -1) { return name } diff --git a/test.js b/test.js index 749fe75..9255452 100644 --- a/test.js +++ b/test.js @@ -647,6 +647,15 @@ test('sanitize()', function (t) { 'should allow known properties' ) + t.deepEqual( + sanitize(u('root', [h('div', h('li', 'text'))]), { + tagNames: ['div', 'ul', 'li'], + ancestors: {li: ['ul']} + }), + u('root', [h('div', 'text')]), + 'should support `ancestors` to enforce certain ancestors in a `root` (rehypejs/rehype-sanitize#8)' + ) + t.end() })