This repository has been archived by the owner on Jun 21, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
160 lines (136 loc) · 3.3 KB
/
index.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
153
154
155
156
157
158
159
160
/**
* @author Titus Wormer
* @copyright 2014-2015 Titus Wormer
* @license MIT
* @module retext:dom
* @fileoverview Compile NLCST to a DOM tree.
*/
'use strict';
/*
* Dependencies.
*/
var nlcstToString = require('nlcst-to-string');
var extend = require('extend.js');
/*
* Throw when not running in the browser (or a
* simulated browser environment).
*/
/* istanbul ignore if */
if (typeof document !== 'object') {
throw new Error('missing document object');
}
/*
* Default map of NLCST node types mapping to DOM
* tag names.
*/
var defaults = {
'WhiteSpaceNode': 'span',
'PunctuationNode': 'span',
'SymbolNode': 'span',
'ParagraphNode': 'p',
'RootNode': 'div'
}
/**
* Convert `node` (NLCST) into a DOM node.
*
* @param {NLCSTNode} node - Node to convert.
* @param {Object.<string, string?>} names - Map of NLCST
* node types mapping to DOM tag names.
* @return {HTMLElement} - Converted `node`.
*/
function toDOMNode(node, names) {
var $node;
var name = names[node.type];
var children = node.children;
var length = children && children.length;
var index = -1;
var attributes = node.attributes;
var key;
var value;
/*
* Ensure text-nodes are only created when with value
* and without name.
*/
if (!name && 'value' in node) {
$node = document.createTextNode('');
} else {
$node = document.createElement(name || 'span');
}
/*
* Ignore attributes when operating on a `#text`.
*/
if (attributes && 'setAttribute' in $node) {
for (key in attributes) {
value = attributes[key];
if (value !== null && value !== undefined) {
$node.setAttribute(key, value);
}
}
}
/*
* Set `textContent` when with `value`, otherwise,
* append each child.
*/
if ('value' in node) {
$node.textContent = nlcstToString(node);
} else if ('children' in node) {
while (++index < length) {
$node.appendChild(toDOMNode(children[index], names));
}
}
return $node;
}
/**
* Attach a DOM-compiler to retext.
*
* @param {Retext} retext - Instance.
* @param {Object} [options] - Option configuration.
*/
function attacher(retext, options) {
var names = extend({}, defaults, (options || {}).tags);
/**
* Construct a new compiler.
*
* @example
* var domCompiler = new DOMCompiler(new File('Foo'));
*
* @constructor
* @class {DOMCompiler}
* @param {File} file - Virtual file.
*/
function DOMCompiler(file) {
this.file = file;
}
/**
* Stringify the bound file.
*
* @example
* var file = new VFile('Foo');
*
* file.namespace('retext').cst = {
* type: 'WordNode',
* children: [{
* type: 'TextNode',
* value: 'Foo'
* }]
* });
*
* new DOMCompiler(file).compile();
* // '<span>Foo</span>'
*
* @this {DOMCompiler}
* @return {HTMLElement} - DOM element.
*/
function compile() {
return toDOMNode(this.file.namespace('retext').cst, names);
}
/*
* Attach.
*/
DOMCompiler.prototype.compile = compile;
retext.Compiler = DOMCompiler;
}
/*
* Expose.
*/
module.exports = attacher;