|
75 | 75 |
|
76 | 76 | return defaultBsPopperConfig; |
77 | 77 | }; |
| 78 | + const getTextHeight = (text, styles) => { |
| 79 | + const tag = doc.createElement('div'); |
| 80 | + |
| 81 | + tag.innerHTML = text; |
| 82 | + |
| 83 | + for (const key in styles) { |
| 84 | + tag.style[key] = styles[key]; |
| 85 | + } |
| 86 | + |
| 87 | + doc.body.appendChild(tag); |
| 88 | + |
| 89 | + const { height: texHeight } = tag.getBoundingClientRect(); |
| 90 | + |
| 91 | + doc.body.removeChild(tag); |
| 92 | + |
| 93 | + return texHeight; |
| 94 | + }; |
| 95 | + const isTitleEllipsized = (node) => { |
| 96 | + const title = node.title || node.dataset.originalTitle; |
| 97 | + const { width: nodeWidth, height: nodeHeight } = node.getBoundingClientRect(); |
| 98 | + const computedNodeStyles = getComputedStyle(node); |
| 99 | + const styles = { |
| 100 | + width: `${nodeWidth}px`, |
| 101 | + padding: computedNodeStyles.getPropertyValue('padding'), |
| 102 | + 'font-size': computedNodeStyles.getPropertyValue('font-size'), |
| 103 | + 'font-family': computedNodeStyles.getPropertyValue('font-family'), |
| 104 | + 'font-weight': computedNodeStyles.getPropertyValue('font-weight'), |
| 105 | + 'font-style': computedNodeStyles.getPropertyValue('font-style'), |
| 106 | + 'font-variant': computedNodeStyles.getPropertyValue('font-variant'), |
| 107 | + 'line-height': computedNodeStyles.getPropertyValue('line-height'), |
| 108 | + }; |
| 109 | + |
| 110 | + const textHeight = getTextHeight(title, styles); |
| 111 | + |
| 112 | + return textHeight > nodeHeight; |
| 113 | + }; |
78 | 114 | const parse = (baseElement = doc) => { |
79 | 115 | if (!baseElement) { |
80 | 116 | return; |
|
93 | 129 | if (hasEllipsisStyle) { |
94 | 130 | resizeEllipsisObserver.observe(tooltipNode); |
95 | 131 |
|
96 | | - const isEllipsized = tooltipNode.scrollWidth > tooltipNode.offsetWidth; |
| 132 | + const isEllipsized = isTitleEllipsized(tooltipNode); |
97 | 133 | const tooltipInstance = bootstrap.Tooltip.getInstance(tooltipNode); |
98 | 134 |
|
99 | 135 | if (tooltipInstance) { |
|
109 | 145 | tooltipNode.title = tooltipNode.dataset.title; |
110 | 146 | } |
111 | 147 | } else { |
| 148 | + if (tooltipNode.title) { |
| 149 | + tooltipNode.dataset.title = tooltipNode.title; |
| 150 | + tooltipNode.title = ''; |
| 151 | + } |
| 152 | + |
112 | 153 | continue; |
113 | 154 | } |
114 | 155 | } |
|
0 commit comments