regexper-static/src/js/parser/javascript/literal.js

44 lines
1.2 KiB
JavaScript
Raw Normal View History

2015-04-20 00:55:36 +00:00
// Literal nodes are for plain strings in the regular expression. They are
// rendered as labels with the value of the literal quoted.
import _ from 'lodash';
export default {
type: 'literal',
2015-04-20 00:55:36 +00:00
// Renders the literal into the currently set container.
_render() {
2014-12-21 06:54:12 +00:00
return this.renderLabel(['\u201c', this.literal, '\u201d'])
2015-03-14 21:11:14 +00:00
.then(label => {
2014-12-15 03:17:59 +00:00
var spans = label.selectAll('tspan');
2015-04-20 00:55:36 +00:00
// The quote marks get some styling to lighten their color so they are
// distinct from the actual literal value.
2014-12-15 03:17:59 +00:00
spans[0].addClass('quote');
spans[2].addClass('quote');
2014-12-13 18:42:55 +00:00
label.select('rect').attr({
rx: 3,
ry: 3
});
2015-03-14 21:11:14 +00:00
return label;
2014-12-11 00:23:14 +00:00
});
},
2015-04-20 00:55:36 +00:00
// Merges this literal with another. Literals come back as single characters
// during parsing, and must be post-processed into multi-character literals
// for rendering. This processing is done in [Match](./match.html).
2014-12-17 19:56:02 +00:00
merge(other) {
2014-12-20 02:35:03 +00:00
this.literal += other.literal;
2014-12-17 19:56:02 +00:00
},
setup() {
2015-04-20 00:55:36 +00:00
// Value of the literal.
2014-12-20 02:35:03 +00:00
this.literal = this.properties.literal.textValue;
2015-04-20 00:55:36 +00:00
// Ordinal value of the literal for use in
// [CharsetRange](./charset_range.html).
2014-12-22 20:11:37 +00:00
this.ordinal = this.literal.charCodeAt(0);
}
};