Overriding XML mode for React JSX mode

I am currently working on a CodeMirror mode for React JSX and have run into a problem.

High level, I am jumping between the existing Javascript and XML (HTML) modes as appropriate, pushing mode and state info onto a state stack since each mode can be nested infinitely deep.

My problem presents itself when I try to parse a JSX element attribute value with the XML mode. For a contrived example:

return (<div foo={return 2 > 3;}></div>);

In theory the process is to start in Javascript mode, push to XML mode on the first <, parse with XML mode until just after the =, eat the {, push to a new Javascript mode and state, parse with Javascript mode until the matching }, pop Javascript mode, parse with current XML mode and state, etc.

The problem is that the XML mode has several different states stored in state.state and I can’t figure out how to force the XML mode into one of those states after I finish parsing the Javascript between the {}'s. It should be set to function attrContinuedState() and it isn’t. Nor can I copy the state ahead of time so I can store it on a stack.

Or, to put it slightly less technically, I can’t figure out where to override the XML mode implementation to parse this correctly. Should I rewriting the entire function inTag() tokenizer? I think one of the problems is all of the module-level functions, the tokenizers and the states, that have no public API as far as I can tell.

Sorry, I can’t explain this problem more clearly, but it is rather complex for me and I am just looking for some high level ideas.



Yes, this is a common problem with shuffling modes – the outer mode won’t get a stream that makes sense to it, because wrapped elements replace the syntactic constructs it expects.

For this specific case, I could add a method to the xml mode object that allows you to tell it to skip an attribute value (it seem that the problem only occurs for attribute values here). Would that help?

Thanks for the input, this answers my main question, that a small change to the XML parser code might be necessary to avoid creating duplicate code.

I’m going to research a bit more and see what kind of change might be necessary. I can implement it and submit it, and if it needs to be tweaked, it can be done as part of the PR.

Also, it looks like they are a couple of other PRs looking at this functionality, so I will keep an eye on those as well.

Thanks for your help.