on master help sign in
Change the internal representation of Text

Now generates significantly fewer spans and is a bit faster. I also removed the `header` function.

commit82e9acf16e8bc065f4f742e920bcd8833843010e authorEvan Czaplicki <info@elm-lang.org> dateSun 3 Nov 2013, 15:20 (13 years ago) parent46b9558
4 files changed+78−42
libraries/Native/List.js+17−3 libraries/Native/Text.js+31−33 libraries/Native/Utils.js+29−1 libraries/Text.elm+1−5
libraries/Native/List.js+17−3
@@ -53,8 +53,15 @@ Elm.Native.List.make = function(elm) {
5353 }
5454
5555 function append(xs,ys) {
56− if (xs.isText) return Utils.txt(xs.concat(ys));
57− if (typeof xs === "string") return xs.concat(ys);
56+ // append Text
57+ if (xs.text || ys.text) {
58+ return Utils.txt(Utils.makeText('',xs) + Utils.makeText('',ys));
59+ }
60+
61+ // append Strings
62+ if (typeof xs === "string") return xs + ys;
63+
64+ // append Lists
5865 if (xs.ctor === '[]') { return ys; }
5966 var root = Cons(xs._0, Nil);
6067 var curr = root;
@@ -253,7 +260,14 @@ Elm.Native.List.make = function(elm) {
253260 }
254261
255262 function join(sep, xss) {
256− if (sep.isText) return Utils.txt(toArray(xss).join(sep));
263+ if (sep.text) {
264+ sep = Utils.makeText('',sep);
265+ xss = toArray(xss);
266+ for (var i = xss.length; i--; ) {
267+ xss[i] = Utils.makeText('',xss[i]);
268+ }
269+ return Utils.txt(xss.join(sep));
270+ }
257271 if (typeof sep === 'string') return toArray(xss).join(sep);
258272 if (xss.ctor === '[]') return Nil;
259273 var s = toArray(sep);
libraries/Native/Text.js+31−33
@@ -53,55 +53,53 @@ Elm.Native.Text.make = function(elm) {
5353
5454 function toText(str) { return Utils.txt(properEscape(JS.fromString(str))); }
5555
56− function addTag(tag) {
57− return function(text) {
58− return Utils.txt('<' + tag + '>' + text + '</' + tag + '>');
59− }
60− }
61−
62− function addStyle(style, value, text) {
63− return Utils.txt("<span style='" + style + ":" + value + "'>" + text + "</span>");
56+ function height(px, text) {
57+ return { style: 'font-size:' + px + 'px;', text:text }
6458 }
65−
6659 function typeface(name, text) {
67− return addStyle('font-family', JS.fromString(name), text);
60+ return { style: 'font-family:' + name + ';', text:text }
6861 }
6962 function monospace(text) {
70− return addStyle('font-family', 'monospace', text);
63+ return { style: 'font-family:monospace;', text:text }
7164 }
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 + ')';
65+ function italic(text) {
66+ return { style: 'font-style:italic;', text:text }
8167 }
82− function color(c, text) {
83− return addStyle('color', extract(c), text);
68+ function bold(text) {
69+ return { style: 'font-weight:bold;', text:text }
70+ }
71+ function link(href, text) {
72+ return { href: toText(href), text:text };
73+ }
74+ function underline(text) {
75+ return { line: ' underline', text:text };
76+ }
77+ function overline(text) {
78+ return { line: ' overline', text:text };
8479 }
85− function underline(text) { return addStyle('text-decoration', 'underline', text) }
86− function overline(text) { return addStyle('text-decoration', 'overline', text) }
8780 function strikeThrough(text) {
88− return addStyle('text-decoration', 'line-through', text);
81+ return { line: ' line-through', text:text };
8982 }
90− function link(href, text) {
91− return Utils.txt("<a href='" + toText(href) + "'>" + text + "</a>");
83+
84+ function color(c, text) {
85+ var color = (c._3 === 1)
86+ ? ('rgb(' + c._0 + ', ' + c._1 + ', ' + c._2 + ')')
87+ : ('rgba(' + c._0 + ', ' + c._1 + ', ' + c._2 + ', ' + c._3 + ')');
88+ return { style: 'color:' + color + ';', text:text };
9289 }
9390
9491 function position(align) {
95− return function(text) {
92+ function create(text) {
9693 var raw = {
9794 ctor:'RawHtml',
98− html: '<span style="text-align:' + align + ';">' + text + '</span>',
95+ html: Utils.makeText('text-align:' + align + ';', text),
9996 guid: null,
10097 args: [],
10198 };
10299 var pos = A2(Utils.htmlHeight, 0, raw);
103100 return A3(Element.newElement, pos._0, pos._1, raw);
104101 }
102+ return create;
105103 }
106104
107105 function markdown(text, guid) {
@@ -123,18 +121,18 @@ Elm.Native.Text.make = function(elm) {
123121 return A3(Element.newElement, pos._0, pos._1, raw);
124122 }
125123
124+ var text = position('left');
126125 function asText(v) {
127− return position('left')(monospace(toText(show(v))));
126+ return text(monospace(toText(show(v))));
128127 }
129128
130129 function plainText(v) {
131− return position('left')(toText(v));
130+ return text(toText(v));
132131 }
133132
134133 return elm.Native.Text.values = {
135134 toText: toText,
136135
137− header : header,
138136 height : F2(height),
139137 italic : italic,
140138 bold : bold,
@@ -149,7 +147,7 @@ Elm.Native.Text.make = function(elm) {
149147 justified : position('justify'),
150148 centered : position('center'),
151149 righted : position('right'),
152− text : position('left'),
150+ text : text,
153151 plainText : plainText,
154152 markdown : markdown,
155153
libraries/Native/Utils.js+29−1
@@ -68,10 +68,37 @@ Elm.Native.Utils.make = function(elm) {
6868
6969 function txt(str) {
7070 var t = new String(str);
71− t.isText = true;
71+ t.text = true;
7272 return t;
7373 }
7474
75+ function makeText(style, text) {
76+ var style = style;
77+ var line = '';
78+ var href = '';
79+ while (true) {
80+ if (text.line) {
81+ line += text.line;
82+ text = text.text;
83+ continue;
84+ }
85+ if (text.style) {
86+ style += text.style;
87+ text = text.text;
88+ continue;
89+ }
90+ if (text.href) {
91+ href = text.href;
92+ text = text.text;
93+ continue;
94+ }
95+ if (href) text = '<a href="' + href + '">' + text + '</a>';
96+ if (line) style += 'text-decoration:' + line + ';';
97+ if (style) text = '<span style="' + style + '">' + text + '</span>';
98+ return text;
99+ }
100+ }
101+
75102 var count = 0;
76103 function guid(_) { return count++ }
77104
@@ -186,6 +213,7 @@ Elm.Native.Utils.make = function(elm) {
186213 Tuple2:Tuple2,
187214 chr:chr,
188215 txt:txt,
216+ makeText:makeText,
189217 copy: copy,
190218 remove: remove,
191219 replace: replace,
libraries/Text.elm+1−5
@@ -12,7 +12,7 @@ module Text where
1212@docs color, typeface, height, link
1313
1414# Simple Formatting
15−@docs monospace, header, bold, italic, underline, overline, strikeThrough
15+@docs monospace, bold, italic, underline, overline, strikeThrough
1616
1717-}
1818
@@ -43,10 +43,6 @@ typeface = Native.Text.typeface
4343monospace : Text -> Text
4444monospace = Native.Text.monospace
4545
46−{-| Make text big and noticable. -}
47−header : Text -> Text
48−header = Native.Text.header
49−
5046{-| Create a link. -}
5147link : String -> Text -> Text
5248link = Native.Text.link