Skip to content

Processing instruction attribute names that setAttribute() accepts don't survive reparsing #1523

Open

Description

@annevk

What is the issue with the DOM Standard?

setAttribute() and toggleAttribute() on ProcessingInstruction accept any valid attribute local name. But update attributes from data uses the xml-stylesheet rules for parsing pseudo-attributes, which require each name to match XML's Name production. As a result, setAttribute() can write data that no longer parses back into the same attributes:

const pi = document.createProcessingInstruction("t", "");
pi.setAttribute("$", "x");
pi.getAttribute("$"); // "x"
pi.data = pi.data;
pi.getAttribute("$"); // null, and the whole map is now empty

$ isn't an XML NameStartChar, but processing-instruction-attributes.html requires setAttribute("$", …) to work. Names containing ", ', <, or & are allowed too, and they produce data that isn't well formed: pi.setAttribute('a"b', "x") gives a"b="x".

Clones and range copies run into the same problem if they reparse their data (see #1504).

cc @foolip @noamr

Activity

  1. annevk commented on Nov 10, 2026

    @annevk
    MemberAuthor

    FWIW, my opinion is that since we decided to go with a strict parser, we should also be strict for the API and keep them 1:1.

  2. noamr commented on Nov 10, 2026

    @noamr
    Contributor

    FWIW, my opinion is that since we decided to go with a strict parser, we should also be strict for the API and keep them 1:1.

    I think I'm OK with this. @foolip ?

  3. foolip commented on Nov 10, 2026

    @foolip
    Member

    Here's the argument I was tempted to make:

    There are lots of ways to use DOM APIs to produce trees that don't survive reparsing, and what we've done here is closer to the existing element.setAttribute(), which is nice for consistency. Those APIs are also for the XML syntax, after all.

    However, that's not a solid argument. The way that PI attributes work isn't quite like anything else, so what should it be consistent with? Moreover, both data and the parsed attributes are always there and in sync, and you can stumble into roundtrip issues with the seemingly innocent pi.data += ' more="attributes"'.

    So yes, I'm OK with this!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions