-
Notifications
You must be signed in to change notification settings - Fork 2
/
xcss.js
executable file
·1060 lines (966 loc) · 29.5 KB
/
xcss.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*!
* eXpanse CSS Library
* http://github.com/ofk/xcss/
*
* @author ofk
* @license MIT ( http://www.opensource.org/licenses/mit-license.php )
* @version 2011-03-27
*/
(function (window_, document_) {
var IS_MSIE = !!document_.uniqueID, //< IEの判定
IS_MSIE8 = IS_MSIE && !!document_.querySelector, //< IE8の判定
MEMENTO = 'xcss-memento', //< IEでのstyle要素のテキストの格納先
PAGE_URL = location.href.replace(/#[^#]*$/, ''); //< 現在ページのURL
// cssRulesやrulesにアクセスするとstyle要素のテキストが壊れるため、事前に収集しておく。
if (IS_MSIE) {
(function () {
var nodes = document_.getElementsByTagName('style');
for (var i = 0, iz = nodes.length; i < iz; ++i) {
var node = nodes[i];
node[MEMENTO] = node.innerHTML;
}
}());
}
// xcssの定義
var xcss = window_.xcss = {
// CSSのパース結果など
css: [],
// プロパティー変換(string: string, function)
properties: {},
// 値変換(string: string, function)
values: {},
// 関数変換(string: string, function)
functions: {},
// ベンダープリフィックス
vendorPrefix: IS_MSIE ? (IS_MSIE8 ? '-ms-' : null) :
window_.opera ? '-o-' :
window_.Components ? '-moz-' :
!navigator.taintEnabled ? '-webkit-' : null,
// void xcss.run(force:boolean = false)
run: xcssRun,
// boolean xcss.isRun()
isRun: xcssIsRun,
// void xcss.parser(force:boolean = false)
parser: xcssParser,
// void xcss.collect_(csss:object[], sheets:CSSStyleSheet[], base_url:string = PAGE_URL, safe_urls:object = null)
collect_: xcssCollect_,
// void xcss.parser_(css:object)
parser_: xcssParser_,
// void xcss.attach(force:boolean = false)
attach: xcssAttach,
// void xcss.attach_(force:boolean, csss:object[])
attach_: xcssAttach_,
// void xcss.removeRules_(css:object)
removeRules_: xcssRemoveRules_,
// void xcss.addRules_(css:object)
addRules_: xcssAddRules_,
// void xcss.hookRule_(rule:object)
hookRule_: xcssHookRule_,
rules: {
// string xcss.rules.camelize(str:string)
camelize: xcssRulesCamelize,
// string xcss.rules.decamelize(str:string)
decamelize: xcssRulesDecamelize,
// int xcss.rules.add(selector:string, property:string, sheet:CSSStyleSheet, index:number = null)
add: xcssRulesAdd,
// int xcss.rules.remove(sheet:CSSStyleSheet, index:number)
remove: xcssRulesRemove
},
absoluteUrl: {
// void xcss.absoluteUrl.open()
open_: xcssAbsoluteUrlOpen_,
// void xcss.absoluteUrl.close()
close_: xcssAbsoluteUrlClose_,
// string xcss.absoluteUrl.get(url:string, base_url:string)
get: xcssAbsoluteUrlGet
}
};
var REG_URL = /^\w+:\/\//,
REG_NL = /\s*[\r\n]+/g,
REG_TRIM = /^\s+|\s+$/g;
//= 実行
// void xcss.run(force:boolean = false)
function xcssRun(force) {
if (this.isRun()) {
this.parser(force);
this.attach(force);
}
}
//= 実行する必要性があるかどうか
// boolean xcss.isRun()
function xcssIsRun() {
for (var i in this.properties) return true;
for (var i in this.values) return true;
for (var i in this.functions) return true;
return false;
}
// URLが訪問済みかどうかの判定
var collect_visited = {};
//= パーサー
// void xcss.parser(force:boolean = false)
function xcssParser(force) {
if (force) {
collect_visited = {};
this.css = [];
}
this.collect_(this.css, document_.styleSheets);
this.absoluteUrl.close_();
}
//= CSSの収集
// void xcss.collect_(csss:object[], sheets:CSSStyleSheet[], base_url:string = PAGE_URL, safe_urls:object = null)
function xcssCollect_(csss, sheets, base_url, safe_urls) {
base_url = base_url || PAGE_URL;
var node_key = IS_MSIE ? 'owningElement' : 'ownerNode', //< 親ノード
text_key = IS_MSIE ? MEMENTO : 'textContent', //< style要素のテキスト
safe_urls_index = 0;
for (var i = 0, iz = sheets.length; i < iz; ++i) {
var sheet = sheets[i];
// 通常のルールなので離脱
if (sheet.type === 1) {
break;
}
// cssRules[]の@import読み込みを追跡する
if (sheet.type === 3) {
sheet = sheet.styleSheet;
}
// style要素なら必ず文字列
// Cr, Sf, Fx: 常に'text/css'
// Op: 常に''
// IE: ノードのtypeに依存する文字列
if (typeof sheet.type !== 'string') {
break;
}
// CSS外枠の生成
var css = csss[i];
if (!css) {
css = {
sheet: sheet, //< CSSStyleSheet
attached: null, //< CSSStyleSheetに付与したCSS行数
href: null, //< URL
text: null, //< CSSテキスト
imports: null, //< @import
rules: null //< パース結果
};
csss.push(css);
}
// CSSStyleSheetが無効ならスキップ
if (sheet.disabled) {
continue;
}
// データ取得が無かったら取得
if (css.text === null) {
// CSSテキストの収集
var url = sheet.href || null, text;
// link要素もしくは@import
if (url && url !== PAGE_URL) {
// IE8のURL変換
if (safe_urls) {
var filename = url.match(/[^\/]*$/)[0];
for (; safe_urls_index < safe_urls.length; ++safe_urls_index) {
if (filename === safe_urls[safe_urls_index].name) {
url = safe_urls[safe_urls_index].path;
break;
}
}
}
// URLを絶対URLに
url = REG_URL.test(url) ? url : this.absoluteUrl.get(url, base_url);
// 訪問していないURLのみ受信
if (!collect_visited[url]) {
collect_visited[url] = true;
text = loadSync(url) || '';
}
}
// style要素
else {
url = null;
text = sheet[node_key] && sheet[node_key][text_key] || '';
}
// 格納
css.url = url;
css.text = text.replace(REG_NL, '\n');
css.rules = null;
}
// パース
if (!css.rules) {
css.rules = [];
css.text && this.parser_(css);
}
// @import取得
var next_sheets = sheet.imports || sheet.cssRules;
if (next_sheets.length) {
// IE8でURLが壊れる対策
var safe_urls = null;
if (IS_MSIE8) {
safe_urls = [];
for (var j = 0, jz = css.rules.length; j < jz; ++j) {
var rule = css.rules[j];
if (rule.name === '@import') {
var v = rule.values[0];
if (v) {
safe_urls.push({
path: v.value,
name: v.value.match(/[^\/]*$/)[0]
});
}
}
else if (!rule.rule) {
break;
}
}
}
css.imports = css.imports || [];
this.collect_(css.imports, next_sheets, url, safe_urls);
}
}
}
// パース用正規表現
var PARSER_REG = /<!--|-->|\/\*[^*]*\*+(?:[^\/][^\*]*\*+)*\/|"[^"]*"|'[^']*'|url\(\s*(?:"[^"]*"|'[^']*'|(?:[!#-~\u00c0-\uFFFF]|\\.)*)\s*\)|U\+[0-9A-F?]{1,6}(-[0-9A-F]{1,6})?|[\+\-]?(?:[0-9]*\.)?[0-9]+(?:%|c[hm]|deg|e[mx]|g(?:rad|d)|in|m[ms]|p[ctx]|r(?:ad|em)|s|turn|v[hmw])?|\s+|!important|[!-\/:-@\[-`{-~]=|[@#]?(?:[a-zA-Z0-9\u00c0-\uFFFF_-]|\\.)+|./g,
PARSER_RULE = { '@charset': 2, '@import': 3, '@media': 4, '@font-face': 5, '@page': 6 },
PARSER_RULE_INLINE = { '@font-face': true };
// void xcss.parser_(css:object)
function xcssParser_(css) {
var words = css.text.match(PARSER_REG),
targets = [ css ], index = 0,
vtargets = [], vindex = -1,
current,
is_inline = css.inline;
var tmp = [];
for (var i = 0, iz = words.length; i < iz; ++i) {
var word = words[i];
// 階層構造の移動準備
switch (word) {
case '{':
// 次の格納先が存在すれば移動
if (targets[index + 1]) {
++index;
}
// 現在の格納先の情報
var now_target = targets[index];
is_inline = now_target.inline;
if (now_target.name) {
now_target.name = now_target.name.replace(REG_TRIM, '');
}
break;
case '}':
// 前の格納先が存在すれば移動
if (index > 0) {
--index;
}
// 現在の格納先の情報
is_inline = targets[index].inline;
break;
}
// 強制的に次に行く文字列
switch (word) {
// 構造用符号
case '{':
case '}':
case ';':
if (current && current.value) {
current.value = current.value.replace(REG_TRIM, '');
}
current = null;
// FALLTHROUGH
// コメント
case '<!--':
case '-->':
continue;
}
// コメント
if (!word.indexOf('/*')) {
continue;
}
// 空白の処理
var is_blank = !/\S/.test(word);
if (is_blank) {
word = ' ';
}
var obj = null;
// inline
// prop: value;
if (is_inline) {
// インラインの開始
if (!current) {
if (is_blank) continue; //< 空白は無視
current = {
name: word,
value: '',
values: []
};
targets[index].rules.push(current);
vindex = -1;
}
// インラインの経過
else {
// プロパティー名の入力
if (vindex < 0) {
if (word === ':') {
vtargets[++vindex] = current;
}
else {
current.name += word;
}
continue;
}
var vtarget = vtargets[vindex];
// 関数の終わり以外を格納
if (word !== ')') {
vtarget.value += word;
}
if (is_blank) continue; //< 空白は無視
switch (word) {
// 関数の開始
case '(':
if (vtargets[vindex + 1]) {
++vindex;
}
// 直前のタイプを関数に書き換え
vtarget = vtargets[vindex];
vtarget.type = 3;
vtarget.name = vtarget.value;
vtarget.value = '';
vtarget.values = [];
continue;
// 関数の離脱
case ')':
var tmp = vtarget.value = vtarget.value.replace(REG_TRIM, '');
vtarget = vtargets[--vindex];
if (vtarget) {
vtarget.value += tmp + word;
}
continue;
}
// TODO: !important の拾い上げ
obj = parserValue(word);
vtargets[vindex + 1] = obj;
vtarget.values.push(obj);
}
}
// block
// @rule, selector {}
else {
// @rule
if (word.charAt(0) === '@') {
obj = {
name: word,
values: [],
rules: [],
inline: PARSER_RULE_INLINE[word],
rule: PARSER_RULE[word] || 0
};
}
// ブロックの開始
else if (!current) {
if (is_blank) continue; //< 空白は無視
obj = {
name: word,
rules: [],
inline: true
};
}
// ブロックの経過
else {
// @rule
if (current.values) {
if (is_blank) continue; //< 空白は無視
current.values.push(parserValue(word));
}
else {
current.name += word;
}
}
// ターゲットに追加
if (obj) {
targets[index].rules.push(obj);
targets[index + 1] = obj;
current = obj;
}
}
}
}
// object parserValue(value:string)
var PARSER_VALUE_REG = /^(?:(\s+|[!-\/:-@\[-`{-~])|"([^"]*)"|'([^']*)'|url\(\s*(?:"([^"]*)"|'([^']*)'|((?:[!#-~\u00c0-\uFFFF]|\\.)*))\s*\)|([\+\-]?)([0-9]*\.)?([0-9]+)(%|c[hm]|deg|e[mx]|g(?:rad|d)|in|m[ms]|p[ctx]|r(?:ad|em)|s|turn|v[hmw])?)$/,
PARSER_VALUE_SIGN_REG = /^$/,
PARSER_VALUE_STRING_REG = /\\0*([0-9a-f]*)/ig,
PARSER_VALUE_STRING_FUNC = function ($0, $1) { return String.fromCharCode(parseInt($1 || 0, 16)); };
function parserValue(value) {
var m = PARSER_VALUE_REG.exec(value);
// キーワード
if (m && !m[1]) {
// "...", '...'
if (m[2] || m[3]) {
return {
type: 2,
quote: m[2] ? '"' : "'",
value: (m[2] || m[3]).replace(PARSER_VALUE_STRING_REG, PARSER_VALUE_STRING_FUNC),
original: value
};
}
// url("..."), url('...')
if (m[4] || m[5]) {
value = (m[4] || m[5]).replace(PARSER_VALUE_STRING_REG, PARSER_VALUE_STRING_FUNC);
return {
type: 3,
name: 'url',
quote: m[4] ? '"' : "'",
value: value,
values: [ parserValue(value) ],
original: value
};
}
// url(...)
if (m[6]) {
value = m[6];
return {
type: 3,
name: 'url',
value: value,
values: [ parserValue(value) ],
original: value
};
}
// 数値
if (m[9]) {
return {
type: 1,
unit: m[10] || '',
value: parseFloat((m[7] || '') + (m[8] === '.' ? '0.' : m[8] || '') + m[9]),
original: value
};
}
}
return {
type: m || value === '!important' ? 0 : 1,
value: value,
original: value
};
}
// void xcss.attach(force:boolean = false)
function xcssAttach(force) {
this.attach_(force, this.css);
}
// void xcss.attach_(force:boolean, csss:object[])
function xcssAttach_(force, csss) { //< forceは現在利用していない
for (var i = 0, iz = csss.length; i < iz; ++i) {
var css = csss[i];
// @importの展開
if (css.imports && css.imports.length) {
this.attach_(force, css.imports);
}
// ルールの削除
if (css.attached) {
this.removeRules_(css);
}
// ルールの展開
if (css.rules && css.rules.length) {
this.addRules_(css);
}
}
}
// void xcss.removeRules_(css:object)
function xcssRemoveRules_(css) {
while (css.attached) {
this.rules.remove(css.sheet, 0);
--css.attached;
}
}
// void xcss.addRules_(css:object)
function xcssAddRules_(css) {
// 後ろのルールから順に処理する
var start = IS_MSIE ? 0 : css.imports.length;
for (var i = css.rules.length - 1; i >= 0; --i) {
var ruleset = css.rules[i];
// @rule
// TODO: 後で対応する
if (ruleset.name.charAt(0) === '@') {
continue;
}
// 変更するプロパティーを検査する
var rules = ruleset.rules, props = '';
for (var j = 0, jz = rules.length; j < jz; ++j) {
var prop = this.hookRule_(rules[j]);
if (prop) {
props += prop;
}
}
// 変更がある場合は先頭に追加
if (props) {
this.rules.add(ruleset.name, props, css.sheet, start);
}
}
}
// void xcss.hookRule_(rule:object)
function xcssHookRule_(rule) {
// 'property' => 'new-property'
// 'property' => function (value, ...) { return null; };
// 'property' => function (value, ...) { return 'new-property'; };
// 'property' => function (value, ...) { return { 'new-property': 'new-value', ... }; };
// 'value' => 'new-value'
// 'value' => function (property) { return null; }
// 'value' => function (property) { return 'new-value'; }
// 'value' => function (property) { return { 'new-property': 'new-value', ... }; }
// 'function' => 'new-function'
// 'function' => function (arg, ...) { return null; };
// 'function' => function (arg, ...) { return 'new-function(new-args, ...)'; };
// 'function' => function (arg, ...) { return { 'new-property': 'new-value', ... }; };
var props = {}, str = '',
rprop = hookRun(this.properties[rule.name], [ rule.value ], rule, props),
rvals = hookRunRecursive(rule, props);
if (rprop || rvals.updated) {
props[rprop || rule.name] = rvals.results.join(' ');
}
for (var i in props) {
str += i + ':' + props[i] + ';';
}
return str;
}
// string hookRun(result:mixed, args:array, me:object, props:object)
function hookRun(result, args, me, props) {
// 空
if (!result) {
return null;
}
// 関数なら実行
if (typeof result === 'function') {
result = result.apply(me, args);
if (!result) {
return null;
}
if (typeof result === 'object') {
for (var i in result) {
props[xcss.rules.decamelize(i)] = result[i];
}
return null;
}
}
return result;
}
// { results: array, updated: boolean } hookRunRecursive(rule:object, props:object)
function hookRunRecursive(rule, props) {
var results = [], updated = false;
for (var i = 0, iz = rule.values.length; i < iz; ++i) {
var valueset = rule.values[i];
switch (valueset.type) {
// 区切り記号
case 0:
// 文字列
case 2:
results.push(valueset.original);
break;
// 値
case 1:
var rval = hookRun(xcss.values[valueset.value], [ rule.name, rule.value ], rule, props);
if (rval) {
updated = true;
}
results.push(rval || valueset.original);
break;
// 関数
case 3:
var rval = hookRunRecursive(valueset, props), args = [];
if (rval.updated) {
updated = true;
}
// 引数の生成
for (var j = 0, jz = rval.results.length, c = 0; j < jz; ++j) {
var arg = rval.results[j];
// 区切り符号
if (arg === ',') {
++c;
continue;
}
if (args[c]) {
args[c] += ' ' + arg;
}
else {
args[c] = arg;
}
}
var name = valueset.name,
func = xcss.functions[name],
fres = typeof func === 'function' ? hookRun(func, args, rule, props) : null;
// 関数による文字列置き換え
if (fres) {
updated = true;
results.push(fres);
}
else {
// 関数名置き換え
if (func) {
updated = true;
name = func;
}
results.push(name + '(' + (rval.updated ? args.join(',') : valueset.value) + ')');
}
break;
}
}
return { results: results, updated: updated };
}
//= キャメルケース変換
// string xcss.rules.camelize(str:string)
var CAMELIZE_REG = /-([a-z])/g,
CAMELIZE_FUNC = function ($0, $1) { return $1.toUpperCase(); }
function xcssRulesCamelize(str) {
return str.replace(CAMELIZE_REG, CAMELIZE_FUNC);
}
//= キャメルケース逆変換
// string xcss.rules.decamelize(str:string)
var DECAMELIZE_REG = /([A-Z])/g;
function xcssRulesDecamelize(str) {
return str.replace(DECAMELIZE_REG, '-$1').toLowerCase();
}
//= CSSルールの追加
// int xcss.rules.add(selector:string, property:string, sheet:CSSStyleSheet, index:number = null)
function xcssRulesAdd(selector, property, sheet, index) {
// W3C
if (sheet.insertRule) {
return sheet.insertRule(selector + '{' + property + '}', index == null ? sheet.cssRules.length : index);
}
// IE
if (sheet.addRule) {
return sheet.addRule(selector, property, index == null ? sheet.rules.length : index);
}
return null;
}
//= CSSルールの削除
// int xcss.rules.remove(sheet:CSSStyleSheet, index:number)
function xcssRulesRemove(sheet, index) {
sheet.deleteRule(index);
}
//= 絶対URL用のノード生成
// void xcss.absoluteUrl.open()
function xcssAbsoluteUrlOpen_() {
// 隠しiframeの生成
var iframe = this.iframe_ = document_.createElement('iframe');
iframe.style.display = 'none';
document_.documentElement.appendChild(iframe);
// iframeのHTML生成
var iframe_document = iframe.contentWindow.document;
iframe_document.open();
iframe_document.write('<!DOCTYPE html><html><head><base /></head><body></body></html>');
iframe_document.close();
// baseタグ,bodyタグの取得
this.base_ = iframe_document.getElementsByTagName('base')[0];
this.body_ = iframe_document.body;
}
//= 絶対URL用のノード破棄
// void xcss.absoluteUrl.close()
function xcssAbsoluteUrlClose_() {
if (this.iframe_) {
this.body_ = null;
this.base_ = null;
this.iframe_.parentNode.removeChild(this.iframe_);
this.iframe_ = null;
}
}
//= 絶対URL取得
// string xcss.absoluteUrl.get(url:string, base_url:string)
function xcssAbsoluteUrlGet(url, base_url) {
// ノード生成
if (!this.iframe_) {
this.open_();
}
this.base_.href = base_url;
var body = this.body_;
body.innerHTML = '<a href="' + url + '" />';
// TODO: 理由を思い出す
return body.firstChild ? body.firstChild.href
: /href\="([^"]+)"/.exec(body.innerHTML)[1];
}
//= 同期通信
// string loadSync(url:string)
function loadSync(url) {
try {
var xhr = window_.XMLHttpRequest ? new XMLHttpRequest()
: new ActiveXObject('Microsoft.XMLHTTP');
xhr.open('GET', url, false); //< sync
xhr.send(null);
if (xhr.status === 200 || !xhr.status) {
return xhr.responseText;
}
} catch (e) {}
return '';
}
}(this, document));
/*
* eXpanse CSS Library Run
* http://github.com/ofk/xcss/
*
* @author ofk
* @license MIT ( http://www.opensource.org/licenses/mit-license.php )
* @version 2011-03-27
*/
(function (xcss, document_) {
// 変換先ペア
// 'property' => 'new-property'
// 'property' => function (value, ...) { return null; };
// 'property' => function (value, ...) { return 'new-property'; };
// 'property' => function (value, ...) { return { 'new-property': 'new-value', ... }; };
// 'value' => 'new-value'
// 'value' => function (property) { return null; }
// 'value' => function (property) { return 'new-value'; }
// 'value' => function (property) { return { 'new-property': 'new-value', ... }; }
// 'function' => 'new-function'
// 'function' => function (arg, ...) { return null; };
// 'function' => function (arg, ...) { return 'new-function(new-args, ...)'; };
// 'function' => function (arg, ...) { return { 'new-property': 'new-value', ... }; };
var properties = xcss.properties,
values = xcss.values,
funcs = xcss.functions,
root = document_.documentElement,
view = document_.defaultView,
isGetComputedStyle = view && view.getComputedStyle,
div = root.insertBefore(document_.createElement('div'), root.firstChild),
divStyle = div.style,
divComputedStyle = isGetComputedStyle ? view.getComputedStyle(div, null) : div.currentStyle,
vendorPrefix = xcss.vendorPrefix;
// string getCSS(name:string)
var getCSS = isGetComputedStyle ? function (name) {
return '' + divComputedStyle.getPropertyValue(xcss.rules.decamelize(name));
} : function (name) {
return '' + divComputedStyle[xcss.rules.camelize(name)];
};
// number range(num:number, min:number, max:number)
function range(num, min, max) {
return Math.min(Math.max(min, num), max);
}
// boolean vendorProperty(name:string, flag:boolean = false)
function vendorProperty(name, flag) {
// 実装済み
if (divStyle[xcss.rules.camelize(name)] !== void 0) {
return true;
}
// ベンダープリフィックス付き実装済み
if (!flag) {
if (vendorPrefix && divStyle[xcss.rules.camelize(vendorPrefix + name)] !== void 0) {
properties[name] = vendorPrefix + name;
return true;
}
}
// 実装無し
return false;
}
// boolean vendorValueBase(target:object, name:string, value:string, args:string, flag:boolean = false)
function vendorValueBase(target, name, value, args, flag) {
var camelize_name = xcss.rules.camelize(name);
// 値の設定
try {
divStyle[camelize_name] = value + args;
if (getCSS(name).indexOf(value) !== -1) {
return true;
}
} catch (e) {}
// ベンダープリフィックス付きもテスト
if (!flag && vendorPrefix) {
var vp_value = vendorPrefix + value;
try {
divStyle[camelize_name] = vp_value + args;
if (getCSS(name).indexOf(vp_value) !== -1) {
target[value] = vp_value;
return true;
}
} catch (e) {}
}
// 実装無し
return false;
}
// boolean vendorValue(name:string, value:string, flag:boolean = false)
function vendorValue(name, value, flag) {
return vendorValueBase(values, name, value, '', flag);
}
// boolean vendorFunction(name:string, func:string, args:string, flag:boolean = false)
function vendorFunction(name, func, args, flag) {
return vendorValueBase(funcs, name, func, '(' + args + ')', flag);
}
//= multi-column
vendorProperty('column-count');
vendorProperty('column-gap');
vendorProperty('column-rule');
vendorProperty('column-rule-color');
vendorProperty('column-rule-style');
vendorProperty('column-rule-width');
vendorProperty('column-span');
vendorProperty('column-width');
vendorProperty('columns');
vendorProperty('float-offset');
//= animation
// @keyframesは未対応
vendorProperty('animation');
vendorProperty('animation-delay');
vendorProperty('animation-direction');
vendorProperty('animation-duration');
vendorProperty('animation-iteration-count');
vendorProperty('animation-name');
vendorProperty('animation-play-state');
vendorProperty('animation-timing-function');
//= grid
vendorProperty('grid-columns');
vendorProperty('grid-rows');
//= text
vendorProperty('hanging-punctuation');
vendorProperty('line-break');
vendorProperty('punctuation-trim');
vendorProperty('text-align-last');
vendorProperty('text-autospace');
vendorProperty('text-decoration');
vendorProperty('text-decoration-color');
vendorProperty('text-decoration-line');
vendorProperty('text-decoration-skip');
vendorProperty('text-decoration-style');
vendorProperty('text-emphasis');
vendorProperty('text-emphasis-color');
vendorProperty('text-emphasis-position');
vendorProperty('text-emphasis-style');
vendorProperty('text-justify');
vendorProperty('text-outline');
vendorProperty('text-shadow');
vendorProperty('text-underline-position');
vendorProperty('white-space-collapsing');
vendorProperty('word-break');
vendorProperty('word-wrap');
//= hyperlink
vendorProperty('target');
vendorProperty('target-name');
vendorProperty('target-new');
vendorProperty('target-position');
//= border
vendorProperty('border-image');
vendorProperty('border-image-outset');
vendorProperty('border-image-repeat');
vendorProperty('border-image-slice');
vendorProperty('border-image-source');
vendorProperty('border-image-width');
vendorProperty('border-radius');
if (!vendorProperty('border-top-left-radius') && vendorProperty(vendorPrefix + 'border-radius-topleft', true)) {
properties['border-top-left-radius'] = vendorPrefix + 'border-radius-topleft'
}
if (!vendorProperty('border-top-right-radius') && vendorProperty(vendorPrefix + 'border-radius-topright', true)) {
properties['border-top-right-radius'] = vendorPrefix + 'border-radius-topright'
}
if (!vendorProperty('border-bottom-left-radius') && vendorProperty(vendorPrefix + 'border-radius-bottomleft', true)) {
properties['border-bottom-left-radius'] = vendorPrefix + 'border-radius-bottomleft'
}
if (!vendorProperty('border-bottom-right-radius') && vendorProperty(vendorPrefix + 'border-radius-bottomright', true)) {
properties['border-bottom-right-radius'] = vendorPrefix + 'border-radius-bottomright'
}
vendorProperty('box-align');
vendorProperty('box-decoration-break');
vendorProperty('box-direction');
vendorProperty('box-flex');
vendorProperty('box-lines');
vendorProperty('box-ordinal-group');
vendorProperty('box-orient');
vendorProperty('box-pack');
vendorProperty('box-shadow');
vendorProperty('rotation');
vendorProperty('rotation-point');
//= marquee
// vendorProperty('marquee-direction');
// vendorProperty('marquee-loop');
// vendorProperty('marquee-play-count');
// vendorProperty('marquee-speed');
// vendorProperty('marquee-style');
// vendorProperty('overflow-style');
// -webkit-marquee-repetition
//= ui
vendorProperty('box-sizing');
vendorProperty('nav-index');
vendorProperty('nav-x');
vendorProperty('outline');
vendorProperty('outline-color');
vendorProperty('outline-offset');
vendorProperty('outline-style');
vendorProperty('outline-width');
vendorProperty('resize');
vendorProperty('text-overflow');
//= transform
vendorProperty('transform');
vendorProperty('transform-origin');
vendorProperty('transform-style');
//= background
vendorProperty('background-clip');
vendorProperty('background-origin');
vendorProperty('background-size');
if (!vendorProperty('opacity')) {
if (vendorProperty('filter', true)) {
properties.opacity = function () {
if (this.values[0]) {
return {
zoom: 1,
filter: 'alpha(opacity=' + range((this.values[0].value * 100) | 0, 0, 100) + ')'
};
}
};
}
}
//= transition
vendorProperty('transition');
vendorProperty('transition-delay');
vendorProperty('transition-duration');
vendorProperty('transition-property');
vendorProperty('transition-timing-function');
//= linear-gradient
if (!vendorFunction('background-image', 'linear-gradient', '#000 0%, #000 100%')) {
if (vendorFunction('background-image', '-webkit-gradient', 'linear,left top,left bottom,color-stop(0,#000),color-stop(1,#000)', true)) {
xcss.functions['linear-gradient'] = function () {
var args = [ 'linear', 'left top', 'left bottom' ];
for (var i = 0, iz = arguments.length; i < iz; ++i) {