Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Schema ancestors are not respected #8

Closed
emilnordh opened this issue Nov 23, 2020 · 6 comments
Closed

Schema ancestors are not respected #8

emilnordh opened this issue Nov 23, 2020 · 6 comments
Labels
👀 no/external This makes more sense somewhere else

Comments

@emilnordh
Copy link

Subject of the issue

A schemas ancestors property is not respected.

Your environment

  • OS: macOS 10.15.2
  • Packages: hast-util-sanitize@3.0.1, rehype-sanitize@4.0.0
  • Env: Node@12.14.1 npm@6.13.4

Steps to reproduce

Simple schema:

{
  "ancestors": {
    "li": ["ul"]
  },
  "tagNames": [
    "div",
    "ul",
    "li"
  ]
}

HTML:

<div>
  <li>List Item</li>
</div>

Expected behavior

Expecting the resulting tree to exclude the li tag:

root[1]
│ data: {"quirksMode":true}
└─0 element<div>[3]
    │ properties: {}
    ├─0 text "\n  "
    ├─1 text "List Item"
    └─2 text "\n"

Actual behavior

The li tag is still included:

root[1]
│ data: {"quirksMode":true}
└─0 element<div>[3]
    │ properties: {}
    ├─0 text "\n  "
    ├─1 element<li>[1]
    │   │ properties: {}
    │   └─0 text "List Item"
    └─2 text "\n"

I investigated a bit and it seems like this bug has appeared because of this: syntax-tree/hast-util-sanitize@19631bb#diff-92bbac9a308cd5fcf9db165841f2d90ce981baddcb2b1e26cfff170929af3bd1R252. I can create a PR for it if you want.

@emilnordh emilnordh added 🐛 type/bug This is a problem 🙉 open/needs-info This needs some more info labels Nov 23, 2020
@wooorm
Copy link
Member

wooorm commented Nov 23, 2020

I pushed a test that checks that your actual behavior works: syntax-tree/hast-util-sanitize@3068a46.
Maybe it has to do with the rest of your code?

@emilnordh
Copy link
Author

It seems to work if using hast-util-sanitize directly. I think the issue arises when using it with rehype because of the root object?

@wooorm
Copy link
Member

wooorm commented Nov 23, 2020

I doubt that rehype is doing anything weird: https://github.com/rehypejs/rehype-sanitize/blob/a6ddc5eee07b7e2bdbeed1e9d9e9f045989bd9ec/index.js

Can you provide a reproduction?

@wooorm
Copy link
Member

wooorm commented Nov 23, 2020

You’re right, it’s the root node!

@emilnordh
Copy link
Author

Reproduction code:

const rehype = require('rehype');
const sanitize = require('rehype-sanitize');

const schema = {
  "ancestors": {
    "li": ["ul"]
  },
  "tagNames": [
    "div",
    "ul",
    "li"
  ]
};

const html = `<div>
  <li>List Item</li>
</div>`;

(async function run() {
  const vfile = await rehype().use(sanitize, schema).process(html);
})();

@wooorm
Copy link
Member

wooorm commented Nov 23, 2020

thanks, released a fix!

@wooorm wooorm added 👀 no/external This makes more sense somewhere else and removed 🐛 type/bug This is a problem 🙉 open/needs-info This needs some more info labels Nov 23, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
👀 no/external This makes more sense somewhere else
Development

No branches or pull requests

2 participants