I am developing a web page capturer and find that some stylesheet rules of a web page captured from Reddit.com are lost.
After a further investigation I found that the source HTML code of a Reddit.com page has a style element like this:
<style type="text/css" data-styled-components="..." data-styled-components-is-local="true">...</style>
When JavaScript has been on, the style element is processed by the script and be emptied:
<style type="text/css" data-styled-components="" data-styled-components-is-local="true"></style>
And that's why my capturer failed to get the stylesheets.
When the content of a style element is changed by script, the document stylesheet of the page would normally change accordingly and the page would be re-rendered to reflect the change.
However, for the Reddit.com page, after the style element is emptied, its stylesheet rules can still be accessed via document.styleSheets[1].cssRules
, while document.styleSheets[1].ownerNode.textContent
is ""
.
Additionally, if I modify the style element by running a script like document.styleSheets[1].ownerNode.textContent = "/*null*/"
, document.styleSheets[1].cssRules
becomes empty and the web page is re-rendered, just like what my capturer has got.
I am confused by the bizarre behavior. I'd like to know why and how the Reddit.com page keep the styles after emptying the style element. Any information is appreciated.