Relax types and inheritance
Closed this issue · 3 comments
Right now I'm looking at creating a custom fragment node (containing word and punctuation nodes) to store custom logic and I'd love parsing to still function the same. However, there's a hard limitation on the node types (.allowedChildTypes
) and the allowed types aren't semantic to my use case.
On a similar note, a number of the current nodes look like they could be combined into the same types and instead use another property to denote what they mean. This is analogous to the DOM with <p>
and <span>
both being element nodes but of different types. I propose an overall update on types and child types to better support a plugin architecture. All these types can definitely have different prototype uses and still inherit from each other, but I don't think they should change the actual type for the checks.
TextOM.Node.nodeType = null
TextOM.Element.nodeType = ELEMENT_NODE
TextOM.Text.nodeType = TEXT_NODE
TextOM.RootNode.nodeType = ROOT_NODE
TextOM.ParagraphNode = ELEMENT_NODE
TextOM.SentenceNode = ELEMENT_NODE
TextOM.WordNode = ELEMENT_NODE
TextOM.TextNode = TEXT_NODE
TextOM.SourceNode = TEXT_NODE
TextOM.WhiteSpaceNode = TEXT_NODE
TextOM.PunctuationNode = TEXT_NODE
So you propose a new nodeType
property which better denotes which kind of node a node is? Instead of checking for instanceof
? That sounds good :).
P.S. did you forget to add .nodeType
to the second section of your code example, or do you plan to overwrite the constructors :) ?
Simply adding the nodeType
would be possible by adding the following somewhere:
Node.prototype.nodeType = null;
Parent.prototype.nodeType = PARENT; // RootNode
Child.prototype.nodeType = CHILD; // Node (I think).
Element.prototype.nodeType = ELEMENT; // WordNode, WhiteSpaceNode, PunctuationNode, SentenceNode, ParagraphNode.
Text.prototype.nodeType = TEXT; // SourceNode, TextNode
But how to update allowed-childs? What about white space (Element) and source (Text), which are allowed everywhere, whereas word (Element) and text (Text) are not?
I think you already have a .type
property which is what I was thinking of when I wrote .nodeType
. Other specific actions can probably use instanceof or a .nodeName
(e.g. custom nodes), but you'd probably want to make that standard so however you end up detecting SourceNode
and TextNode
.
Why would WhiteSpaceNode
and PunctuationNode
be elements? They can't accept child and are text representations, so should inherit from Text
right? Then WordNode
, SentenceNode
and ParagraphNode
can have children of type ELEMENT
or TEXT
. Finally ROOT
can also have children of ELEMENT
or TEXT
(perhaps just ELEMENT
but we create an element like source node for unparsed text?).
Well, I saw the above additionally fix multiple TextOMConstructor instances (multiple documents), instead of using instanceof
, nodeType
would be a better mechanism.
So WordNode and PunctuationNode used to, just like WordNode, inherit from Text (TextNode didn’t exit). This was changed to inheriting from Element because WordNode’s actually accept both TextNode and PunctuationNode. The reason for WordNode and PunctuationNode to be the same, is that I’m not sure what other languages (with weird things) exist, white space in punctuation maybe, and I’d rather let them accept multiple child nodes, instead of just a string, for future i18n support. (pushing a type to node.constructor.prototype.allowedChildren
is much easier to do than changing inheritance).
You said that WordNode’s, an element, could accept other elements. What about SentenceNode (an element). Or a paragraph in a word?