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

preactjs / preact / 13604885310

01 Mar 2025 12:42PM UTC coverage: 99.454% (-0.2%) from 99.61%
13604885310

Pull #4549

github

web-flow
Merge be33221f5 into ccd1e71e5
Pull Request #4549: (major) - Tracking PR for v11

1087 of 1113 branches covered (97.66%)

25 of 28 new or added lines in 11 files covered. (89.29%)

1 existing line in 1 file now uncovered.

729 of 733 relevant lines covered (99.45%)

12212.24 hits per line

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

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

4
function setStyle(style, key, value) {
5
        if (key[0] == '-') {
190✔
6
                style.setProperty(key, value == NULL ? '' : value);
13✔
7
        } else if (value == NULL) {
8
                style[key] = '';
177✔
9
        } else {
10
                style[key] = value;
11
        }
12
}
13

14
const CAPTURE_REGEX = /(PointerCapture)$|Capture$/i;
15

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

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

40
        o: if (name == 'style') {
627✔
41
                if (typeof value == 'string') {
42✔
42
                        dom.style.cssText = value;
6✔
43
                } else {
44
                        if (typeof oldValue == 'string') {
38✔
45
                                dom.style.cssText = oldValue = '';
46
                        }
47

48
                        if (oldValue) {
36✔
49
                                for (name in oldValue) {
20✔
50
                                        if (!(value && name in value)) {
21✔
51
                                                setStyle(dom.style, name, '');
52
                                        }
53
                                }
54
                        }
55

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

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

78
                if (!dom._listeners) dom._listeners = {};
291✔
79
                dom._listeners[name + useCapture] = value;
80

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

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

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

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

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

173
const eventProxy = createEventProxy(false);
174
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

© 2026 Coveralls, Inc