This is inspired by Markdown proposals, and by multitude of code highlighters.
Currently there are multiple situations when when you want to apply styling to the text according to a its syntax. Code highlighters use pretty insane markup, often having something like <span class="punctuation">,</span>
, which is really inefficient for huge code documents (e.g. Syntax.xml.Generated.cs which seems to have most of its highlighting disabled because of that). Markdown is more sane, but still requires rewriting to be displayed, obscuring author’s intent.
I think those cases can be solved by an API that would attach a JavaScript formatting rule to an element, which browser would then use to generate formatting on the fly (maybe even in a lazy way, as new parts of the text become visible). This is similar to the current behavior of JS highlighters/formatters, but does not require new elements to be created.
Preliminary potential API (similar to what CodeMirror editor does):
element.textClassifier = function(textStream, state) {
if (textStream.match(/if/))
return 'keyword';
// etc
};
where the returned value is applied as a virtual class to the text that stream moved over. This would behave similar to an element, but might be much more lightweight (e.g. inaccessible through DOM) and wouldn’t require any parsing. Kind of similar to canvas-for-text. This can be called asynchronously and on any part of text (though for the correct state it should go through all preceding text before calling it on new part). Until call is finished the text can just be shown as plain text, which prevents slow formatter from completely blocking the rendering (but may cause relayouts as the text is processed).
The text term above applies to text content within the element (but not its sub-elements).
There are some problems with this API (e.g. links, tables, effects on layout), but before considering improvements it would be interesting to see whether the goal/approach itself makes sense to anyone, or whether its pure madness on my part.
Note that it can also be used for e.g. creating a table from CSV, etc.