-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathparser.js
153 lines (128 loc) · 3.53 KB
/
parser.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
// https://github.com/vishnevskiy/bbcodejs/blob/master/src/coffee/parser.coffee
import React from 'react';
import { SPACE_RE, TOKEN_RE, START_NEWLINE_RE } from './constants';
import defaultTags from './tags';
import Tag from './tag';
import Renderer from './renderer';
export default class Parser {
constructor(allowedTags = null) {
this.tags = {};
if (!allowedTags) {
this.tags = defaultTags;
} else {
allowedTags.forEach(name => {
if (defaultTags[name]) {
this.tags[name] = defaultTags[name];
}
});
}
this.renderer = new Renderer();
}
registerTag(name, tag) {
this.tags[name] = tag;
}
parseParams(token) {
const params = [];
function addParam(name, value) {
if (name) {
const n = name.trim();
// ignore on* events attribute
if (n.length && n.toLowerCase().indexOf('on') !== 0) {
params.push([n, value]);
}
}
}
if (token) {
let key = [];
let target = key;
let value = [];
let terminate = ' ';
let skipNext = false;
Array.from(token).forEach(c => {
if (skipNext) {
skipNext = false;
} else if (target === key && c === '=') {
target = value;
} else if (target === key && c === ':') {
target = value;
} else if (!value.length && c === '"') {
terminate = c;
} else if (c !== terminate) {
target.push(c);
} else {
addParam(key.join(''), value.join(''));
if (!SPACE_RE.test(terminate)) {
skipNext = true;
}
target = key = [];
value = [];
terminate = ' ';
}
});
addParam(key.join(''), value.join(''));
}
return params;
}
createTextNode(parent, text) {
const ref = parent.children.slice(-1)[0];
//console.log('ref', ref, text)
if (ref != null && ref.STRIP_OUTER) {
text = text.replace(START_NEWLINE_RE, '');
}
return new Tag(this.renderer, { text, parent });
}
parse(input) {
const root = new Tag(this.renderer);
const tokens = input.split(TOKEN_RE);
let current = root;
let token = null;
while (tokens.length) {
token = tokens.shift();
if (!token.length) {
continue;
}
if (token.match(TOKEN_RE)) {
let params = this.parseParams(token.slice(1, -1));
let tagName = params[0][0].toLowerCase();
if (current.CLOSED_BY.indexOf(tagName) > -1) {
tokens.unshift(token);
tagName = `/${current.name}`;
params = [];
}
if (tagName[0] === '/') {
tagName = tagName.slice(1);
if (!this.tags[tagName]) {
this.createTextNode(current, token);
continue;
}
if (current.name === tagName) {
current = current.parent;
}
} else {
const cls = this.tags[tagName];
if (!cls) {
this.createTextNode(current, token);
continue;
}
const tag = new cls(this.renderer, {
name: tagName,
parent: current,
params,
});
if (!tag.SELF_CLOSE && (tag.CLOSED_BY.indexOf(tagName) < 0 || current.name !== tagName)) {
current = tag;
}
}
} else {
this.createTextNode(current, token);
}
}
return root;
}
toHTML(input) {
return this.parse(input).toHTML();
}
toReact(input) {
return this.parse(input).toReact();
}
}