• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

preactjs / preact / 13384788306

18 Feb 2025 06:42AM UTC coverage: 98.957% (-0.7%) from 99.609%
13384788306

push

github

web-flow
Merge pull request #4684 from preactjs/10.26.1

10.26.1

1090 of 1153 branches covered (94.54%)

759 of 767 relevant lines covered (98.96%)

707.14 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

92.59
/src/diff/props.js
1
import { IS_NON_DIMENSIONAL, NULL, SVG_NAMESPACE } from '../constants';
2
import options from '../options';
3

4
function setStyle(style, key, value) {
5
        if (key[0] == '-') {
2!
6
                style.setProperty(key, value == NULL ? '' : value);
×
7
        } else if (value == NULL) {
8
                style[key] = '';
2!
9
        } else if (typeof value != 'number' || IS_NON_DIMENSIONAL.test(key)) {
4!
10
                style[key] = value;
11
        } else {
12
                style[key] = value + 'px';
13
        }
14
}
15

16
const CAPTURE_REGEX = /(PointerCapture)$|Capture$/i;
17

18
// A logical clock to solve issues like https://github.com/preactjs/preact/issues/3927.
19
// When the DOM performs an event it leaves micro-ticks in between bubbling up which means that
20
// an event can trigger on a newly reated DOM-node while the event bubbles up.
21
//
22
// Originally inspired by Vue
23
// (https://github.com/vuejs/core/blob/caeb8a68811a1b0f79/packages/runtime-dom/src/modules/events.ts#L90-L101),
24
// but modified to use a logical clock instead of Date.now() in case event handlers get attached
25
// and events get dispatched during the same millisecond.
26
//
27
// The clock is incremented after each new event dispatch. This allows 1 000 000 new events
28
// per second for over 280 years before the value reaches Number.MAX_SAFE_INTEGER (2**53 - 1).
29
let eventClock = 0;
30

31
/**
32
 * Set a property value on a DOM node
33
 * @param {import('../internal').PreactElement} dom The DOM node to modify
34
 * @param {string} name The name of the property to set
35
 * @param {*} value The value to set the property to
36
 * @param {*} oldValue The old value the property had
37
 * @param {string} namespace Whether or not this DOM node is an SVG node or not
38
 */
39
export function setProperty(dom, name, value, oldValue, namespace) {
40
        let useCapture;
41

42
        o: if (name == 'style') {
387✔
43
                if (typeof value == 'string') {
2!
44
                        dom.style.cssText = value;
×
45
                } else {
46
                        if (typeof oldValue == 'string') {
2!
47
                                dom.style.cssText = oldValue = '';
48
                        }
49

50
                        if (oldValue) {
2✔
51
                                for (name in oldValue) {
1✔
52
                                        if (!(value && name in value)) {
1!
53
                                                setStyle(dom.style, name, '');
54
                                        }
55
                                }
56
                        }
57

58
                        if (value) {
2✔
59
                                for (name in value) {
1✔
60
                                        if (!oldValue || value[name] !== oldValue[name]) {
1!
61
                                                setStyle(dom.style, name, value[name]);
62
                                        }
63
                                }
64
                        }
65
                }
66
        }
67
        // Benchmark for comparison: https://esbench.com/bench/574c954bdb965b9a00965ac6
68
        else if (name[0] == 'o' && name[1] == 'n') {
385✔
69
                useCapture = name != (name = name.replace(CAPTURE_REGEX, '$1'));
160✔
70

71
                // Infer correct casing for DOM built-in events:
72
                if (
73
                        name.toLowerCase() in dom ||
371✔
74
                        name == 'onFocusOut' ||
75
                        name == 'onFocusIn'
76
                )
77
                        name = name.toLowerCase().slice(2);
78
                else name = name.slice(2);
79

80
                if (!dom._listeners) dom._listeners = {};
266✔
81
                dom._listeners[name + useCapture] = value;
82

83
                if (value) {
160✔
84
                        if (!oldValue) {
146✔
85
                                value._attached = eventClock;
86
                                dom.addEventListener(
87
                                        name,
88
                                        useCapture ? eventProxyCapture : eventProxy,
115✔
89
                                        useCapture
90
                                );
91
                        } else {
92
                                value._attached = oldValue._attached;
93
                        }
94
                } else {
95
                        dom.removeEventListener(
96
                                name,
97
                                useCapture ? eventProxyCapture : eventProxy,
14!
98
                                useCapture
99
                        );
100
                }
101
        } else {
102
                if (namespace == SVG_NAMESPACE) {
225✔
103
                        // Normalize incorrect prop usage for SVG:
104
                        // - xlink:href / xlinkHref --> href (xlink:href was removed from SVG and isn't needed)
105
                        // - className --> class
106
                        name = name.replace(/xlink(H|:h)/, 'h').replace(/sName$/, 's');
29✔
107
                } else if (
196✔
108
                        name != 'width' &&
2,333✔
109
                        name != 'height' &&
110
                        name != 'href' &&
111
                        name != 'list' &&
112
                        name != 'form' &&
113
                        // Default value in browsers is `-1` and an empty string is
114
                        // cast to `0` instead
115
                        name != 'tabIndex' &&
116
                        name != 'download' &&
117
                        name != 'rowSpan' &&
118
                        name != 'colSpan' &&
119
                        name != 'role' &&
120
                        name != 'popover' &&
121
                        name in dom
122
                ) {
123
                        try {
127✔
124
                                dom[name] = value == NULL ? '' : value;
127!
125
                                // labelled break is 1b smaller here than a return statement (sorry)
126
                                break o;
127✔
127
                        } catch (e) {}
128
                }
129

130
                // aria- and data- attributes have no boolean representation.
131
                // A `false` value is different from the attribute not being
132
                // present, so we can't remove it. For non-boolean aria
133
                // attributes we could treat false as a removal, but the
134
                // amount of exceptions would cost too many bytes. On top of
135
                // that other frameworks generally stringify `false`.
136

137
                if (typeof value == 'function') {
98✔
138
                        // never serialize functions as attribute values
139
                } else if (value != NULL && (value !== false || name[4] == '-')) {
101✔
140
                        dom.setAttribute(name, name == 'popover' && value == true ? '' : value);
194!
141
                } else {
142
                        dom.removeAttribute(name);
98✔
143
                }
144
        }
145
}
146

147
/**
148
 * Create an event proxy function.
149
 * @param {boolean} useCapture Is the event handler for the capture phase.
150
 * @private
151
 */
152
function createEventProxy(useCapture) {
153
        /**
154
         * Proxy an event to hooked event handlers
155
         * @param {import('../internal').PreactEvent} e The event object from the browser
156
         * @private
157
         */
158
        return function (e) {
194✔
159
                if (this._listeners) {
65✔
160
                        const eventHandler = this._listeners[e.type + useCapture];
65✔
161
                        if (e._dispatched == NULL) {
65✔
162
                                e._dispatched = eventClock++;
62✔
163

164
                                // When `e._dispatched` is smaller than the time when the targeted event
165
                                // handler was attached we know we have bubbled up to an element that was added
166
                                // during patching the DOM.
167
                        } else if (e._dispatched < eventHandler._attached) {
3!
168
                                return;
×
169
                        }
170
                        return eventHandler(options.event ? options.event(e) : e);
65✔
171
                }
172
        };
173
}
174

175
const eventProxy = createEventProxy(false);
176
const eventProxyCapture = createEventProxy(true);
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc