I am attempting to serialize Text nodes with custom properties, like an ID, that need to be preserved in the serialized format.
After reviewing this page, it seems that a good approach could be to enclose each Text element within a special tag that includes the custom properties. For example:
<P>some text</P>
may be serialized as:
<P><custom-text id='node27'>some text</custom-text></P>
While I could manually replace each "custom-text" tag with a Text node and its respective properties during deserialization, I have read about the possibility of creating a custom constructor for the tag. This suggests that the conversion could happen automatically during parsing, eliminating the need to rewrite the document later.
However, I am uncertain how to accomplish this. I attempted defining a prototype as the second argument to document.registerElement
, setting it to create a Text node with a getter and setter to manage the custom ID property:
var textId = document.registerElement('custom-text', {
prototype: Object.create(Text.prototype, {
id: {
get: function() { return this.id; },
set: function(value) { this.id = value }
}
})
});
Yet, when the custom-text
tag is parsed, it generates an HTML node instead of a text node. I am using Chrome 33 and the existence of registerElement
within the document
object should suffice for my needs. I considered wrapping the Object.create call in a function to insert an alert or similar debugging step, but I cannot locate documentation on what the prototype value ought to be.