1 | import {MS, MOZ, WEBKIT, RULESET, KEYFRAMES, DECLARATION} from './Enum.js'
|
---|
2 | import {match, charat, substr, strlen, sizeof, replace, combine, filter, assign} from './Utility.js'
|
---|
3 | import {copy, lift, tokenize} from './Tokenizer.js'
|
---|
4 | import {serialize} from './Serializer.js'
|
---|
5 | import {prefix} from './Prefixer.js'
|
---|
6 |
|
---|
7 | /**
|
---|
8 | * @param {function[]} collection
|
---|
9 | * @return {function}
|
---|
10 | */
|
---|
11 | export function middleware (collection) {
|
---|
12 | var length = sizeof(collection)
|
---|
13 |
|
---|
14 | return function (element, index, children, callback) {
|
---|
15 | var output = ''
|
---|
16 |
|
---|
17 | for (var i = 0; i < length; i++)
|
---|
18 | output += collection[i](element, index, children, callback) || ''
|
---|
19 |
|
---|
20 | return output
|
---|
21 | }
|
---|
22 | }
|
---|
23 |
|
---|
24 | /**
|
---|
25 | * @param {function} callback
|
---|
26 | * @return {function}
|
---|
27 | */
|
---|
28 | export function rulesheet (callback) {
|
---|
29 | return function (element) {
|
---|
30 | if (!element.root)
|
---|
31 | if (element = element.return)
|
---|
32 | callback(element)
|
---|
33 | }
|
---|
34 | }
|
---|
35 |
|
---|
36 | /**
|
---|
37 | * @param {object} element
|
---|
38 | * @param {number} index
|
---|
39 | * @param {object[]} children
|
---|
40 | * @param {function} callback
|
---|
41 | */
|
---|
42 | export function prefixer (element, index, children, callback) {
|
---|
43 | if (element.length > -1)
|
---|
44 | if (!element.return)
|
---|
45 | switch (element.type) {
|
---|
46 | case DECLARATION: element.return = prefix(element.value, element.length, children)
|
---|
47 | return
|
---|
48 | case KEYFRAMES:
|
---|
49 | return serialize([copy(element, {value: replace(element.value, '@', '@' + WEBKIT)})], callback)
|
---|
50 | case RULESET:
|
---|
51 | if (element.length)
|
---|
52 | return combine(children = element.props, function (value) {
|
---|
53 | switch (match(value, callback = /(::plac\w+|:read-\w+)/)) {
|
---|
54 | // :read-(only|write)
|
---|
55 | case ':read-only': case ':read-write':
|
---|
56 | lift(copy(element, {props: [replace(value, /:(read-\w+)/, ':' + MOZ + '$1')]}))
|
---|
57 | lift(copy(element, {props: [value]}))
|
---|
58 | assign(element, {props: filter(children, callback)})
|
---|
59 | break
|
---|
60 | // :placeholder
|
---|
61 | case '::placeholder':
|
---|
62 | lift(copy(element, {props: [replace(value, /:(plac\w+)/, ':' + WEBKIT + 'input-$1')]}))
|
---|
63 | lift(copy(element, {props: [replace(value, /:(plac\w+)/, ':' + MOZ + '$1')]}))
|
---|
64 | lift(copy(element, {props: [replace(value, /:(plac\w+)/, MS + 'input-$1')]}))
|
---|
65 | lift(copy(element, {props: [value]}))
|
---|
66 | assign(element, {props: filter(children, callback)})
|
---|
67 | break
|
---|
68 | }
|
---|
69 |
|
---|
70 | return ''
|
---|
71 | })
|
---|
72 | }
|
---|
73 | }
|
---|
74 |
|
---|
75 | /**
|
---|
76 | * @param {object} element
|
---|
77 | * @param {number} index
|
---|
78 | * @param {object[]} children
|
---|
79 | */
|
---|
80 | export function namespace (element) {
|
---|
81 | switch (element.type) {
|
---|
82 | case RULESET:
|
---|
83 | element.props = element.props.map(function (value) {
|
---|
84 | return combine(tokenize(value), function (value, index, children) {
|
---|
85 | switch (charat(value, 0)) {
|
---|
86 | // \f
|
---|
87 | case 12:
|
---|
88 | return substr(value, 1, strlen(value))
|
---|
89 | // \0 ( + > ~
|
---|
90 | case 0: case 40: case 43: case 62: case 126:
|
---|
91 | return value
|
---|
92 | // :
|
---|
93 | case 58:
|
---|
94 | if (children[++index] === 'global')
|
---|
95 | children[index] = '', children[++index] = '\f' + substr(children[index], index = 1, -1)
|
---|
96 | // \s
|
---|
97 | case 32:
|
---|
98 | return index === 1 ? '' : value
|
---|
99 | default:
|
---|
100 | switch (index) {
|
---|
101 | case 0: element = value
|
---|
102 | return sizeof(children) > 1 ? '' : value
|
---|
103 | case index = sizeof(children) - 1: case 2:
|
---|
104 | return index === 2 ? value + element + element : value + element
|
---|
105 | default:
|
---|
106 | return value
|
---|
107 | }
|
---|
108 | }
|
---|
109 | })
|
---|
110 | })
|
---|
111 | }
|
---|
112 | }
|
---|