简体   繁体   中英

Representing Document DOM in HTML: Sibling Text Nodes

I read this statement from @Bergi in a very old comment discussing a nextSibling value:

x.nextSibling will likely not be the span, but the line-break text node between the img and the span elements

The example @Bergi was referring to was some script to select the first element in fairly simple HTML, and find the nextSibling :

<img src="something">
<span>

Do browsers insert a "line-break text node" into the DOM between element nodes? What I'm really trying to understand is this comment in an older discussion about serializing a document model to HTML with ProseMirror:

HTML can almost always be used to fully represent the document DOM. There is one exception we have come across: sibling text nodes. Before using ProseMirror we found it was important to have sibling text nodes, so we serialize the HTML to JSON for storing in the database. We still do that now, for compatibility reasons, but given that we don't need sibling text nodes any more, this will likely be gone in the next version of our file format.

The rest of the discussion in that thread was voluminous and not related to this question, so I didn't link it. I think the reason this author is saying that HTML can't mirror the DOM in the case of sibling text nodes is because line-break nodes are automatically inserted into the DOM (and not represented in HTML). Is this accurate?

I think that the concept of "hidden characters" that you mention is not accurate. The thing is that nextSibling returns literally the next sibling on the dom, which doesn't mean it's going to be the next element on the dom. If you wanto to try, remove all linebreak characters and try again, or simply use nextElementSibling instead.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM