MCPcopy Create free account
hub / github.com/ampproject/amphtml / markElementForDiffing

Function markElementForDiffing

src/purifier/sanitation.js:34–56  ·  view source on GitHub ↗
(element, generateKey)

Source from the content-addressed store, hash-verified

32 * @param {function(): string} generateKey
33 */
34export function markElementForDiffing(element, generateKey) {
35 const isAmpElement = element.tagName.startsWith('AMP-');
36 // Don't DOM diff nodes with bindings because amp-bind scans newly rendered
37 // elements and discards _all_ old elements _before_ diffing, so preserving
38 // old elements would cause loss of functionality.
39 //
40 // Alternatively, we could do diffing _before_ bindings are updated and
41 // more precisely add/remove bindings from nodes that set-dom diffs, but...
42 // 1. Would result in content flashing unless we clone the old DOM tree.
43 // 2. Need to change set-dom e.g. handle changed bind attribute values.
44 const hasBinding = element.hasAttribute('i-amphtml-binding');
45
46 if (!hasBinding && DIFFABLE_AMP_ELEMENTS[element.tagName]) {
47 // Nodes marked with "ignore" will not be touched (old element stays).
48 // We want this to allow manual diffing afterwards.
49 element.setAttribute(DIFF_IGNORE, '');
50 } else if (hasBinding || isAmpElement) {
51 // Diff'ed node pairs with unique "key" will always be replaced.
52 if (!element.hasAttribute(DIFF_KEY)) {
53 element.setAttribute(DIFF_KEY, generateKey());
54 }
55 }
56}
57
58/**
59 * @const {!{[key: string]: boolean}}

Callers 2

Calls 1

setAttributeMethod · 0.45

Tested by

no test coverage detected