Answers for "JSDOM - getting source location of a node with `nodeLocation(node)` // `parse5` lib helps to serialize and/or parse"

0

JSDOM - getting source location of a node with `nodeLocation(node)` // `parse5` lib helps to serialize and/or parse

const dom = new JSDOM(
  `<p>Hello
    <img src="foo.jpg">
  </p>`,
  { includeNodeLocations: true }
);

const document = dom.window.document;
const bodyEl = document.body; // implicitly created
const pEl = document.querySelector("p");
const textNode = pEl.firstChild;
const imgEl = document.querySelector("img");

console.log(dom.nodeLocation(bodyEl));   // null; it's not in the source
console.log(dom.nodeLocation(pEl));      // { startOffset: 0, endOffset: 39, startTag: ..., endTag: ... }
console.log(dom.nodeLocation(textNode)); // { startOffset: 3, endOffset: 13 }
console.log(dom.nodeLocation(imgEl));    // { startOffset: 13, endOffset: 32 }
Posted by: Guest on May-14-2021

Code answers related to "Javascript"

Browse Popular Code Answers by Language