I just noticed that browser ignores HTML formatting (such as aligning the two attributes in this snippet):
<div id="container">
<div id="contained"
other-prop="some value">
</div>
</div>
If you run
var container = document.getElementById('container');
console.log(container.innerHTML);
You get the output
<div id="contained" other-prop="some value">
</div>
It doesn't matter how it's written in the source or even if you set .innerHTML in JavaScript directly.
Is it possible to query the page source corresponding to an element as the user wrote it in JS? With white-spaces and everything. I can see there being a problem when the user modifies the element using DOM operations, in which case I'll still be happy if
- Original user formatting is kept for everything untouched by modifications, or
- It's possible to get the original source as the user wrote it on page load, without the DOM modifications
A snippet so you can see it in action:
var container = document.getElementById('container');
console.log(container.innerHTML);
container.innerHTML = `
<div id="contained"
other-prop="some value">
</div>
`;
console.log(container.innerHTML);
container.children[0].setAttribute('modification', '');
console.log(container.innerHTML);
<div id="container">
<div id="contained"
other-prop="some value">
</div>
</div>