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 | (function () {
|
34 | 'use strict';
|
35 |
|
36 | var core = require('./core');
|
37 |
|
38 |
|
39 |
|
40 | function getAttributes(attributes) {
|
41 | var t = typeof attributes;
|
42 | if (t === 'string') {
|
43 | return attributes;
|
44 | }
|
45 | if (t === 'object') {
|
46 | return Object.keys(attributes).reduce(function (memo, key) {
|
47 | memo.push(key + '="' + attributes[key] + '"');
|
48 | return memo;
|
49 | }, []).join(' ');
|
50 | }
|
51 | return '';
|
52 | }
|
53 |
|
54 | function getHTMLWrap(tag, attributes, linebreak) {
|
55 | tag = (tag || 'span');
|
56 | attributes = getAttributes(attributes);
|
57 | linebreak = (typeof linebreak !== 'undefined' ? linebreak : '\n');
|
58 |
|
59 | var pre = '<' + tag + (attributes.length > 0 ? ' ' + attributes : '') + '>';
|
60 | var post = '</' + tag + '>' + linebreak;
|
61 |
|
62 | return function (str) {
|
63 | return pre + str + post;
|
64 | };
|
65 | }
|
66 |
|
67 | function getHTMLSpanWrap() {
|
68 | return getHTMLWrap('span', 'style="white-space:pre;font-family:monospace;"', '\n');
|
69 | }
|
70 |
|
71 | function getHTMLDomAppend(tag, attributes) {
|
72 | tag = (tag || 'span');
|
73 | attributes = getAttributes(attributes);
|
74 |
|
75 | var keys = Object.keys(attributes);
|
76 | if (keys.length === 0) {
|
77 | return function (str) {
|
78 | var elem = document.createElement(tag);
|
79 | elem.appendChild(document.createTextNode(str));
|
80 | return elem;
|
81 | };
|
82 | }
|
83 |
|
84 | return function (str) {
|
85 | var elem = document.createElement(tag);
|
86 | elem.appendChild(document.createTextNode(str));
|
87 | for (var i = 0, ii = keys.length; i < ii; i++) {
|
88 | var atr = document.createAttribute(keys[i]);
|
89 | atr.nodeValue = attributes[keys[i]];
|
90 | elem.setAttributeNode(atr);
|
91 | }
|
92 | return elem;
|
93 | };
|
94 | }
|
95 |
|
96 |
|
97 |
|
98 | function htmlString(target, tag, attributes, linebreak) {
|
99 | var wrap = getHTMLWrap((tag || 'span'), (attributes || {style: 'white-space:pre;font-family:monospace;'}), linebreak);
|
100 | var mw = core.base();
|
101 | mw.enabled = true;
|
102 | mw.target = target;
|
103 | mw.writeln = function (line) {
|
104 | if (mw.enabled) {
|
105 | mw.target.writeln(wrap(line));
|
106 | }
|
107 | };
|
108 | mw.toString = function () {
|
109 | return '<miniwrite-html-string>';
|
110 | };
|
111 | return mw;
|
112 | }
|
113 |
|
114 | function htmlAppend(parent, tag, attributes) {
|
115 | var wrap = getHTMLDomAppend((tag || 'span'), (attributes || {style: 'white-space:pre;font-family:monospace;'}));
|
116 | var mw = core.base();
|
117 | mw.enabled = true;
|
118 | mw.parent = parent;
|
119 | mw.writeln = function (line) {
|
120 | if (mw.enabled) {
|
121 | mw.parent.appendChild(wrap(line));
|
122 | }
|
123 | };
|
124 | mw.toString = function () {
|
125 | return '<miniwrite-html-append>';
|
126 | };
|
127 | return mw;
|
128 | }
|
129 |
|
130 |
|
131 |
|
132 |
|
133 | var html = {
|
134 | getHTMLWrap: getHTMLWrap,
|
135 | getHTMLSpanWrap: getHTMLSpanWrap,
|
136 | getHTMLDomAppend: getHTMLDomAppend,
|
137 |
|
138 | htmlString: htmlString,
|
139 | htmlAppend: htmlAppend
|
140 | };
|
141 |
|
142 | module.exports = html;
|
143 |
|
144 | }).call();
|
145 |
|