Use _inNewlineContext method instead of bare onNewline

This makes sure that we escape start-of-line syntax when needed, since
onNewline is often not yet set.

Discussion / background:
[19:18] <subbu> this will fix it, but, i think this is asking for another
minor refactoring of these flags ... because this is a subtle fix which means
it might be possible to make it clearer.  onNewline is one true in on
direction, i.e. if true, we are in a new line state, but if we are in a
newline context, onNewline is not true, which is why this new method is
needed.
[19:19] <subbu> i dont know if it is possible, but it seems like it shoudl be
possible.  but, something for later.
[19:20] <subbu> badly phraed.  "onNewline" ==> in new line context, but if in
new line context, onNewline may be false.
[19:20] <gwicke> we should perhaps update it as early as possible instead
[19:21] <subbu> i cannot today, but possible monday.  i am heading out in
about 15-30 mins.
[19:22] <gwicke> will need to check all conditions depending on it in
_serializeToken
[19:22] <subbu> oh, i misunderstood you :)
[19:22] <gwicke> and if there are cases where the onNewline / onStartOfLine
state could be reverted later
[19:23] <subbu> you were referring to the flag, i thought you meant we should
fix this sooner than later.
[19:23] <gwicke> yes, I wasn't terribly clear
[19:23] <gwicke> you wrote something about following productions swallowing
newlines, but I think we don't actually do that any more
[19:24] <gwicke> I'm quite optimistic that updating those flags much earlier
would work
[19:25] <subbu> yes, it could fix it.
[19:26] <subbu> you might be right reg. swallowing.  it was happening earlier.
but, not right now, after single-line mode and other fixes.

Change-Id: Ic1d8141c04eb54a59977d0ba87bcf06bafd421e0
This commit is contained in:
Gabriel Wicke 2012-06-23 18:53:28 +02:00
parent f731125804
commit 08b5ed1a43

View file

@ -68,7 +68,8 @@ WSP.escapeWikiText = function ( state, text ) {
// this is synchronous for now, will still need sync version later, or
// alternatively make text processing in the serializer async
var prefixedText = text;
if ( ! state.onNewline ) {
var inNewlineContext = WSP._inNewLineContext( state );
if ( ! inNewlineContext ) {
// Prefix '_' so that no start-of-line wiki syntax matches. Strip it from
// the result.
prefixedText = '_' + text;
@ -82,7 +83,7 @@ WSP.escapeWikiText = function ( state, text ) {
p.process( prefixedText );
if ( ! state.onNewline ) {
if ( ! inNewlineContext ) {
// now strip the leading underscore.
if ( tokens[0] === '_' ) {
tokens.shift();
@ -112,7 +113,7 @@ WSP.escapeWikiText = function ( state, text ) {
rangeStart = cursor;
} else {
rangeStart = startRange[0];
if ( ! state.onNewline ) {
if ( ! inNewlineContext ) {
// compensate for underscore.
rangeStart--;
}
@ -130,7 +131,7 @@ WSP.escapeWikiText = function ( state, text ) {
rangeEnd = text.length;
} else {
rangeEnd = endRange[1];
if ( ! state.onNewline ) {
if ( ! inNewlineContext ) {
// compensate for underscore.
rangeEnd--;
}
@ -194,6 +195,18 @@ var id = function(v) {
};
};
WSP._inStartOfLineContext = function(state) {
return state.onStartOfLine ||
state.emitNewlineOnNextToken ||
(state.availableNewlineCount > 0);
};
WSP._inNewLineContext = function(state) {
return state.onNewline ||
state.emitNewlineOnNextToken ||
(state.availableNewlineCount > 0);
};
WSP._listHandler = function( handler, bullet, state, token ) {
function isListItem(token) {
if (token.constructor !== TagTk) return false;
@ -242,11 +255,6 @@ WSP._listEndHandler = function( state, token ) {
};
WSP._listItemHandler = function ( handler, bullet, state, token ) {
function inStartOfLineContext(state) {
return state.onStartOfLine ||
state.emitNewlineOnNextToken ||
(state.availableNewlineCount > 0);
}
function isRepeatToken(state, token) {
return state.prevToken.constructor === EndTagTk &&
@ -284,7 +292,7 @@ WSP._listItemHandler = function ( handler, bullet, state, token ) {
//
var res;
if (curList.itemCount > 1 &&
( inStartOfLineContext(state) ||
( WSP._inStartOfLineContext(state) ||
isRepeatToken(state, token) ||
isMultiLineDtDdPair(state, token)
)