You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

41 lines
1.6 KiB

  1. # w3c-xmlserializer
  2. An XML serializer that follows the [W3C specification](https://w3c.github.io/DOM-Parsing/).
  3. This package can be used in Node.js, as long as you feed it a DOM node, e.g. one produced by [jsdom](https://github.com/jsdom/jsdom).
  4. ## Basic usage
  5. Assume you have a DOM tree rooted at a node `node`. In Node.js, you could create this using [jsdom](https://github.com/jsdom/jsdom) as follows:
  6. ```js
  7. const { JSDOM } = require("jsdom");
  8. const { document } = new JSDOM().window;
  9. const node = document.createElement("akomaNtoso");
  10. ```
  11. Then, you use this package as follows:
  12. ```js
  13. const serialize = require("w3c-xmlserializer");
  14. console.log(serialize(node));
  15. // => '<akomantoso xmlns="http://www.w3.org/1999/xhtml"></akomantoso>'
  16. ```
  17. ## `requireWellFormed` option
  18. By default the input DOM tree is not required to be "well-formed"; any given input will serialize to some output string. You can instead require well-formedness via
  19. ```js
  20. serialize(node, { requireWellFormed: true });
  21. ```
  22. which will cause `Error`s to be thrown when non-well-formed constructs are encountered. [Per the spec](https://w3c.github.io/DOM-Parsing/#dfn-require-well-formed), this largely is about imposing constraints on the names of elements, attributes, etc.
  23. As a point of reference, on the web platform:
  24. * The [`innerHTML` getter](https://w3c.github.io/DOM-Parsing/#dom-innerhtml-innerhtml) uses the require-well-formed mode, i.e. trying to get the `innerHTML` of non-well-formed subtrees will throw.
  25. * The [`xhr.send()` method](https://xhr.spec.whatwg.org/#the-send()-method) does not require well-formedness, i.e. sending non-well-formed `Document`s will serialize and send them anyway.