File: hooks-node-removal-demo.html

package info (click to toggle)
node-dompurify 3.2.5%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 1,944 kB
  • sloc: javascript: 11,172; sh: 2; makefile: 2
file content (30 lines) | stat: -rw-r--r-- 940 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
<!doctype html>
<html>
    <head>
        <script src="../dist/purify.js"></script>
    </head>
    <body>
        <!-- Our DIV to receive content -->
        <div id="sanitized"></div>

        <!-- Now let's sanitize that content -->
        <script>
            'use strict';
            
            // Specify dirty HTML
            const dirty = '<p>Not empty</p><p class="empty"></p>';
            
            // Add a hook to remove empty nodes
            DOMPurify.addHook('beforeSanitizeAttributes', node => {
                // Remove the node in case it is empty
                if (!node.hasChildNodes() && !node.textContent) {
                    node.remove();
                }
            });
            
            // Clean HTML string and write into our DIV
            const clean = DOMPurify.sanitize(dirty);
            document.getElementById('sanitized').innerHTML = clean;
        </script>
    </body>
</html>