on master help sign in
Indent properly
6512bf9Evan Czaplicki
libraries/Native/Text.js+124−124
@@ -1,136 +1,136 @@
1
1
2Elm.Native.Text = function(elm) {
2Elm.Native.Text = function(elm) {
3− 'use strict';
3+ 'use strict';
4−
4+
5− elm.Native = elm.Native || {};
5+ elm.Native = elm.Native || {};
6− if (elm.Native.Text) return elm.Native.Text;
6+ if (elm.Native.Text) return elm.Native.Text;
7−
7+
8− var JS = Elm.JavaScript(elm);
8+ var JS = Elm.JavaScript(elm);
9− var htmlHeight = Elm.Native.Utils(elm).htmlHeight;
9+ var htmlHeight = Elm.Native.Utils(elm).htmlHeight;
10− var Color = Elm.Native.Color(elm);
10+ var Color = Elm.Native.Color(elm);
11− var Element = Elm.Graphics.Element(elm);
11+ var Element = Elm.Graphics.Element(elm);
12− var show = Elm.Native.Show(elm).show;
12+ var show = Elm.Native.Show(elm).show;
13−
13+
14− function makeSpaces(s) {
14+ function makeSpaces(s) {
15− if (s.length == 0) { return s; }
15+ if (s.length == 0) { return s; }
16− var arr = s.split('');
16+ var arr = s.split('');
17− if (arr[0] == ' ') { arr[0] = " " }
17+ if (arr[0] == ' ') { arr[0] = " " }
18− for (var i = arr.length; --i; ) {
18+ for (var i = arr.length; --i; ) {
19− if (arr[i][0] == ' ' && arr[i-1] == ' ') {
19+ if (arr[i][0] == ' ' && arr[i-1] == ' ') {
20− arr[i-1] = arr[i-1] + arr[i];
20+ arr[i-1] = arr[i-1] + arr[i];
21+ arr[i] = '';
22+ }
23+ }
24+ for (var i = arr.length; i--; ) {
25+ if (arr[i].length > 1 && arr[i][0] == ' ') {
26+ var spaces = arr[i].split('');
27+ for (var j = spaces.length - 2; j >= 0; j -= 2) {
28+ spaces[j] = ' ';
29+ }
21− arr[i] = '';
30+ arr[i] = spaces.join('');
31+ }
32+ }
33+ arr = arr.join('');
34+ if (arr[arr.length-1] === " ") {
35+ return arr.slice(0,-1) + ' ';
22− }
36+ }
37+ return arr;
23 }
38 }
24− for (var i = arr.length; i--; ) {
39+
40+ function properEscape(str) {
25− if (arr[i].length > 1 && arr[i][0] == ' ') {
41+ if (str.length == 0) return str;
42+ str = str //.replace(/&/g, "&")
43+ .replace(/"/g, /*"*/ '"')
44+ .replace(/'/g, /*'*/ "'")
45+ .replace(/</g, "&#60;")
46+ .replace(/>/g, "&#62;")
47+ .replace(/\n/g, "<br/>");
26− var spaces = arr[i].split('');
48+ var arr = str.split('<br/>');
27− for (var j = spaces.length - 2; j >= 0; j -= 2) {
49+ for (var i = arr.length; i--; ) {
28− spaces[j] = '&nbsp;';
50+ arr[i] = makeSpaces(arr[i]);
29 }
51 }
30− arr[i] = spaces.join('');
52+ return arr.join('<br/>');
31− }
32 }
53 }
33− arr = arr.join('');
54+
34− if (arr[arr.length-1] === " ") {
55+ function toText(str) { return properEscape(JS.fromString(str)); }
35− return arr.slice(0,-1) + '&nbsp;';
56+
57+ function addTag(tag) { return function(text) {
58+ return '<' + tag + ' style="padding:0;margin:0">' + text + '</' + tag + '>';
36 }
59 }
37− return arr;
38− }
60+ }
39−
61+
40− function properEscape(str) {
62+ function addStyle(style, value, text) {
41− if (str.length == 0) return str;
63+ return "<span style='" + style + ":" + value + "'>" + text + "</span>";
42− str = str //.replace(/&/g, "&#38;")
43− .replace(/"/g, /*"*/ '&#34;')
44− .replace(/'/g, /*'*/ "&#39;")
45− .replace(/</g, "&#60;")
46− .replace(/>/g, "&#62;")
47− .replace(/\n/g, "<br/>");
48− var arr = str.split('<br/>');
49− for (var i = arr.length; i--; ) {
50− arr[i] = makeSpaces(arr[i]);
51 }
64 }
52− return arr.join('<br/>');
53− }
54
65
55− function toText(str) { return properEscape(JS.fromString(str)); }
66+ function typeface(name, text) {
67+ return addStyle('font-family', JS.fromString(name), text);
68+ }
69+ function monospace(text) {
70+ return addStyle('font-family', 'monospace', text);
71+ }
72+ function size(px, text) { return addStyle('font-size', px + 'px', text) }
73+ var header = addTag('h1');
74+ function height(h, text) { return addStyle('font-size', h+'px', text) }
75+ function italic(text) { return addStyle('font-style', 'italic', text) }
76+ var bold = addTag('b');
77+
78+ function extract(c) {
79+ if (c._3 === 1) { return 'rgb(' + c._0 + ',' + c._1 + ',' + c._2 + ')'; }
80+ return 'rgba(' + c._0 + ',' + c._1 + ',' + c._2 + ',' + c._3 + ')';
81+ }
82+ function color(c, text) {
83+ return addStyle('color', extract(c), text);
84+ }
85+ function underline(text) { return addStyle('text-decoration', 'underline', text) }
86+ function overline(text) { return addStyle('text-decoration', 'overline', text) }
87+ function strikeThrough(text) {
88+ return addStyle('text-decoration', 'line-through', text);
89+ }
90+ function link(href, text) {
91+ return "<a href='" + toText(href) + "'>" + text + "</a>";
92+ }
93+
94+ function position(pos) { return function(text) {
95+ var e = {ctor:'RawHtml',
96+ _0: '<div style="padding:0;margin:0;text-align:' +
97+ pos + '">' + text + '</div>'
98+ };
99+ var p = A2(htmlHeight, 0, text);
100+ return A3(Element.newElement, p._0, p._1, e);
101+ }
102+ }
103+
104+ function asText(v) {
105+ return position('left')(monospace(toText(show(v))));
106+ }
56
107
57− function addTag(tag) { return function(text) {
108+ function plainText(v) {
58− return '<' + tag + ' style="padding:0;margin:0">' + text + '</' + tag + '>';
109+ return position('left')(toText(v));
59 }
110 }
60− }
111+
61−
62− function addStyle(style, value, text) {
63− return "<span style='" + style + ":" + value + "'>" + text + "</span>";
64− }
65−
66− function typeface(name, text) {
67− return addStyle('font-family', JS.fromString(name), text);
68− }
69− function monospace(text) {
70− return addStyle('font-family', 'monospace', text);
71− }
72− function size(px, text) { return addStyle('font-size', px + 'px', text) }
73− var header = addTag('h1');
74− function height(h, text) { return addStyle('font-size', h+'px', text) }
75− function italic(text) { return addStyle('font-style', 'italic', text) }
76− var bold = addTag('b');
77−
78− function extract(c) {
79− if (c._3 === 1) { return 'rgb(' + c._0 + ',' + c._1 + ',' + c._2 + ')'; }
80− return 'rgba(' + c._0 + ',' + c._1 + ',' + c._2 + ',' + c._3 + ')';
81− }
82− function color(c, text) {
83− return addStyle('color', extract(c), text);
84− }
85− function underline(text) { return addStyle('text-decoration', 'underline', text) }
86− function overline(text) { return addStyle('text-decoration', 'overline', text) }
87− function strikeThrough(text) {
88− return addStyle('text-decoration', 'line-through', text);
89− }
90− function link(href, text) {
91− return "<a href='" + toText(href) + "'>" + text + "</a>";
92− }
93−
94− function position(pos) { return function(text) {
95− var e = {ctor:'RawHtml',
96− _0: '<div style="padding:0;margin:0;text-align:' +
97− pos + '">' + text + '</div>'
98− };
99− var p = A2(htmlHeight, 0, text);
100− return A3(Element.newElement, p._0, p._1, e);
101− }
102− }
103−
104− function asText(v) {
105− return position('left')(monospace(toText(show(v))));
106− }
107−
108− function plainText(v) {
109− return position('left')(toText(v));
110− }
111−
112− return elm.Native.Text = {
112+ return elm.Native.Text = {
113− toText: toText,
113+ toText: toText,
114−
114+
115− header : header,
115+ header : header,
116− height : F2(height),
116+ height : F2(height),
117− italic : italic,
117+ italic : italic,
118− bold : bold,
118+ bold : bold,
119− underline : underline,
119+ underline : underline,
120− overline : overline,
120+ overline : overline,
121− strikeThrough : strikeThrough,
121+ strikeThrough : strikeThrough,
122− monospace : monospace,
122+ monospace : monospace,
123− typeface : F2(typeface),
123+ typeface : F2(typeface),
124− color : F2(color),
124+ color : F2(color),
125− link : F2(link),
125+ link : F2(link),
126−
126+
127− justified : position('justify'),
127+ justified : position('justify'),
128− centered : position('center'),
128+ centered : position('center'),
129− righted : position('right'),
129+ righted : position('right'),
130− text : position('left'),
130+ text : position('left'),
131− plainText : plainText,
131+ plainText : plainText,
132−
132+
133− asText : asText
133+ asText : asText
134− };
134+ };
135
135
136};
136};