Debian Codegraph @

src/collapse-whitespace.js

raw symbols SCIP
1 /**
2 * The collapseWhitespace function is adapted from collapse-whitespace
3 * by Luc Thevenard.
4 *
5 * The MIT License (MIT)
6 *
7 * Copyright (c) 2014 Luc Thevenard <[email protected]>
8 *
9 * Permission is hereby granted, free of charge, to any person obtaining a copy
10 * of this software and associated documentation files (the "Software"), to deal
11 * in the Software without restriction, including without limitation the rights
12 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13 * copies of the Software, and to permit persons to whom the Software is
14 * furnished to do so, subject to the following conditions:
15 *
16 * The above copyright notice and this permission notice shall be included in
17 * all copies or substantial portions of the Software.
18 *
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25 * THE SOFTWARE.
26 */
27
28 /**
29 * collapseWhitespace(options) removes extraneous whitespace from an the given element.
30 *
31 * @param {Object} options
32 */
33 function collapseWhitespace (options) {
34 const element = options.element
35 const isBlock = options.isBlock
36 const isVoid = options.isVoid
37 const isPre = options.isPre || function (node) {
38 return node.nodeName === 'PRE'
39 }
40
41 if (!element.firstChild || isPre(element)) return
42
43 let prevText = null
44 let keepLeadingWs = false
45
46 let prev = null
47 let node = next(prev, element, isPre)
48
49 while (node !== element) {
50 if (node.nodeType === 3 || node.nodeType === 4) { // Node.TEXT_NODE or Node.CDATA_SECTION_NODE
51 let text = node.data.replace(/[ \r\n\t]+/g, ' ')
52
53 if ((!prevText || / $/.test(prevText.data)) &&
54 !keepLeadingWs && text[0] === ' ') {
55 text = text.substr(1)
56 }
57
58 // `text` might be empty at this point.
59 if (!text) {
60 node = remove(node)
61 continue
62 }
63
64 node.data = text
65
66 prevText = node
67 } else if (node.nodeType === 1) { // Node.ELEMENT_NODE
68 if (isBlock(node) || node.nodeName === 'BR') {
69 if (prevText) {
70 prevText.data = prevText.data.replace(/ $/, '')
71 }
72
73 prevText = null
74 keepLeadingWs = false
75 } else if (isVoid(node) || isPre(node)) {
76 // Avoid trimming space around non-block, non-BR void elements and inline PRE.
77 prevText = null
78 keepLeadingWs = true
79 } else if (prevText) {
80 // Drop protection if set previously.
81 keepLeadingWs = false
82 }
83 } else {
84 node = remove(node)
85 continue
86 }
87
88 const nextNode = next(prev, node, isPre)
89 prev = node
90 node = nextNode
91 }
92
93 if (prevText) {
94 prevText.data = prevText.data.replace(/ $/, '')
95 if (!prevText.data) {
96 remove(prevText)
97 }
98 }
99 }
100
101 /**
102 * remove(node) removes the given node from the DOM and returns the
103 * next node in the sequence.
104 *
105 * @param {Node} node
106 * @return {Node} node
107 */
108 function remove (node) {
109 const next = node.nextSibling || node.parentNode
110
111 node.parentNode.removeChild(node)
112
113 return next
114 }
115
116 /**
117 * next(prev, current, isPre) returns the next node in the sequence, given the
118 * current and previous nodes.
119 *
120 * @param {Node} prev
121 * @param {Node} current
122 * @param {Function} isPre
123 * @return {Node}
124 */
125 function next (prev, current, isPre) {
126 if ((prev && prev.parentNode === current) || isPre(current)) {
127 return current.nextSibling || current.parentNode
128 }
129
130 return current.firstChild || current.nextSibling || current.parentNode
131 }
132
133 export default collapseWhitespace