|
1 | 1 | import { is_void } from '../../../utils/names';
|
2 |
| -import Attribute from '../../nodes/Attribute'; |
3 | 2 | import Class from '../../nodes/Class';
|
4 | 3 | import { get_attribute_value, get_class_attribute_value } from './shared/get_attribute_value';
|
5 | 4 | import { get_slot_scope } from './shared/get_slot_scope';
|
@@ -80,62 +79,61 @@ export default function(node: Element, renderer: Renderer, options: RenderOption
|
80 | 79 |
|
81 | 80 | let add_class_attribute = class_expression ? true : false;
|
82 | 81 |
|
83 |
| - if (node.attributes.find(attr => attr.is_spread)) { |
| 82 | + if (node.attributes.some(attr => attr.is_spread)) { |
84 | 83 | // TODO dry this out
|
85 | 84 | const args = [];
|
86 | 85 | node.attributes.forEach(attribute => {
|
87 | 86 | if (attribute.is_spread) {
|
88 | 87 | args.push(attribute.expression.node);
|
89 | 88 | } else {
|
90 |
| - if (attribute.name === 'value' && node.name === 'textarea') { |
| 89 | + const name = attribute.name.toLowerCase(); |
| 90 | + if (name === 'value' && node.name.toLowerCase() === 'textarea') { |
91 | 91 | node_contents = get_attribute_value(attribute);
|
92 | 92 | } else if (attribute.is_true) {
|
93 | 93 | args.push(x`{ ${attribute.name}: true }`);
|
94 | 94 | } else if (
|
95 |
| - boolean_attributes.has(attribute.name) && |
| 95 | + boolean_attributes.has(name) && |
96 | 96 | attribute.chunks.length === 1 &&
|
97 | 97 | attribute.chunks[0].type !== 'Text'
|
98 | 98 | ) {
|
99 | 99 | // a boolean attribute with one non-Text chunk
|
100 |
| - args.push(x`{ ${attribute.name}: ${(attribute.chunks[0] as Expression).node} }`); |
101 |
| - } else if (attribute.name === 'class' && class_expression) { |
| 100 | + args.push(x`{ ${attribute.name}: ${(attribute.chunks[0] as Expression).node} || null }`); |
| 101 | + } else if (name === 'class' && class_expression) { |
102 | 102 | // Add class expression
|
103 | 103 | args.push(x`{ ${attribute.name}: [${get_class_attribute_value(attribute)}, ${class_expression}].join(' ').trim() }`);
|
104 | 104 | } else {
|
105 |
| - args.push(x`{ ${attribute.name}: ${attribute.name === 'class' ? get_class_attribute_value(attribute) : get_attribute_value(attribute)} }`); |
| 105 | + args.push(x`{ ${attribute.name}: ${(name === 'class' ? get_class_attribute_value : get_attribute_value)(attribute)} }`); |
106 | 106 | }
|
107 | 107 | }
|
108 | 108 | });
|
109 | 109 |
|
110 | 110 | renderer.add_expression(x`@spread([${args}])`);
|
111 | 111 | } else {
|
112 |
| - node.attributes.forEach((attribute: Attribute) => { |
113 |
| - if (attribute.type !== 'Attribute') return; |
114 |
| - |
115 |
| - if (attribute.name === 'value' && node.name === 'textarea') { |
| 112 | + node.attributes.forEach(attribute => { |
| 113 | + const name = attribute.name.toLowerCase(); |
| 114 | + if (name === 'value' && node.name.toLowerCase() === 'textarea') { |
116 | 115 | node_contents = get_attribute_value(attribute);
|
117 | 116 | } else if (attribute.is_true) {
|
118 | 117 | renderer.add_string(` ${attribute.name}`);
|
119 | 118 | } else if (
|
120 |
| - boolean_attributes.has(attribute.name) && |
| 119 | + boolean_attributes.has(name) && |
121 | 120 | attribute.chunks.length === 1 &&
|
122 | 121 | attribute.chunks[0].type !== 'Text'
|
123 | 122 | ) {
|
124 | 123 | // a boolean attribute with one non-Text chunk
|
125 | 124 | renderer.add_string(` `);
|
126 | 125 | renderer.add_expression(x`${(attribute.chunks[0] as Expression).node} ? "${attribute.name}" : ""`);
|
127 |
| - } else if (attribute.name === 'class' && class_expression) { |
| 126 | + } else if (name === 'class' && class_expression) { |
128 | 127 | add_class_attribute = false;
|
129 |
| - renderer.add_string(` class="`); |
| 128 | + renderer.add_string(` ${attribute.name}="`); |
130 | 129 | renderer.add_expression(x`[${get_class_attribute_value(attribute)}, ${class_expression}].join(' ').trim()`);
|
131 | 130 | renderer.add_string(`"`);
|
132 | 131 | } else if (attribute.chunks.length === 1 && attribute.chunks[0].type !== 'Text') {
|
133 |
| - const { name } = attribute; |
134 | 132 | const snippet = (attribute.chunks[0] as Expression).node;
|
135 |
| - renderer.add_expression(x`@add_attribute("${name}", ${snippet}, ${boolean_attributes.has(name) ? 1 : 0})`); |
| 133 | + renderer.add_expression(x`@add_attribute("${attribute.name}", ${snippet}, ${boolean_attributes.has(name) ? 1 : 0})`); |
136 | 134 | } else {
|
137 | 135 | renderer.add_string(` ${attribute.name}="`);
|
138 |
| - renderer.add_expression(attribute.name === 'class' ? get_class_attribute_value(attribute) : get_attribute_value(attribute)); |
| 136 | + renderer.add_expression((name === 'class' ? get_class_attribute_value : get_attribute_value)(attribute)); |
139 | 137 | renderer.add_string(`"`);
|
140 | 138 | }
|
141 | 139 | });
|
|
0 commit comments