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

How to avoid node attrs passing to children? #1480

Open
zunsthy opened this issue Aug 8, 2024 · 1 comment
Open

How to avoid node attrs passing to children? #1480

zunsthy opened this issue Aug 8, 2024 · 1 comment

Comments

@zunsthy
Copy link

zunsthy commented Aug 8, 2024

For example, declare a node with background-color and a mark with background-color, the node background-color attribute will pass to the node's children.

const s = new Schema({
  nodes: schema.spec.nodes.append({ colorBlock: {
    content: 'block*', group: 'block',
    attrs: { bc: {} },
    toDOM(node) {
      return ['div', { style: `background-color: ${node.attrs.bc}` }, 0];
    },
    parseDOM: [{ tag: 'div', getAttrs(node) {
      const bc = node.style.backgroundColor;
      return bc ? { bc } : false;
    }}],
  }}),
  marks: schema.spec.marks.append({ colorText: {
    attrs: { bc: {} },
    toDOM(node) {
      return ['span', { style: `background-color: ${node.attrs.bc}`}];
    },
    parseDOM: [{  style: 'background-color',
      getAttrs: (bc) => (bc ? { bc } : false),
    }],
  }}),
});

Input HTML

<div style="background-color: red">abc<span style="background-color: blue">123</span></div>

Expected

<div style="background-color: red"><p>abc<span style="background-color: blue">123</span></p></div>

But get

<div style="background-color: red"><p><span style="background-color: red">abc</span><span style="background-color: blue">123</span></p></div>

The "colorBlock" node matches "red" color attribute, but I can't prevent the "red" color apply to the "abc" text as "colorText" mark.

@zunsthy zunsthy changed the title How to avoid node attrs passing children? How to avoid node attrs passing to children? Aug 8, 2024
@marijnh
Copy link
Member

marijnh commented Aug 8, 2024

Style parse rules will match regardless of whether a node parse rule happened to already have looked at that style (in fact, the library doesn't even know that your getAttrs function looked at the style). I don't really see a way to make this work differently.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants