-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfixMarkup.js
90 lines (88 loc) · 3.19 KB
/
fixMarkup.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
/*Takes a text presumed to contain a mixture of text properly marked up for use in the editor, and plain xml pasted in. Attempts to make the
*plain xml adhere to the editor's expectations by:
*Finding the plain xml by searching for the first < that is NOT part of a span tag
*DONE-From that point forward, change all < and > to < and >
*DONE-add a <span class=tag"> </span> to wrap around each <xxx></xxx> or <xxx/>
*
*TODO-Change \n to <br>
*
*More to come as need arrises
**/
function fixMarkup(text)
{
var tagBeginnings=text.split( "<" );
var inBadSection=false;
var newText="";
for(i=1;i<tagBeginnings.length;i++)
{
if(!inBadSection)
{
//find the first open bracket that is not connected to a span tag, this is the beginning of incorrectly formatted text
if(tagBeginnings[i].substring(0, 4)!='span' && tagBeginnings[i].substring(0, 5)!='/span')
{
inBadSection=true;
}
else
{
newText+="<"+tagBeginnings[i];
}
}
//If we are in the section with plain xml tags, begin by adding < ad the beggining (the < was lost in the tokenizing) and repacing the first > with >
if(inBadSection)
{
var tmpString=tagBeginnings[i];
var isClosingTag=false;
var isOpeningTag=false;
var slashPosition=tmpString.indexOf('/');
var closeBracketPosition=tmpString.indexOf('>');
//if the slash occurs right before the >, it is a tag like <lb/> which opens and closes, so it needs both treatments
if(slashPosition+1==closeBracketPosition)
{
isClosingTag=true;
isOpeningTag=true;
}
else
{
//if it has a slash, and didnt meet the prvious criteria, it is a closing tag.
if(slashPosition>=0 && slashPosition<closeBracketPosition)
{
isClosingTag=true;
}
else
{
//If it wasnt any of those, its a plain opening tag.
if(slashPosition<closeBracketPosition)
{
isOpeningTag=true;
}
}
if(isOpeningTag)
{
if(isClosingTag)
{
tmpString=tmpString.replace('>','></span>');
}
else
{
tmpString=tmpString.replace('>','>');
}
tmpString='<span class="tag"><'+tmpString;
}
else
{
if(isClosingTag)
{
tmpString=tmpString.replace('>','></span>');
}
else
{
tmpString=tmpString.replace('>','>');
}
tmpString='<'+tmpString;
}
}
newText+=tmpString;
}
}
return newText;
}