JSFiddle - React, Tailwind, and code Playground
HTML
<test-container>Text inside</test-container>
JavaScript
const getVisibleText = function(elem) {
var lines = [];
appendVisibleTextLinesFromElementInComposedDom_(elem, lines);
return lines;
};
const INLINE_DISPLAY_BOXES_ = [
'inline',
'inline-block',
'inline-table',
'none',
'table-cell',
'table-column',
'table-column-group'
];
const appendVisibleTextLinesFromElementCommon_ = function(
elem, lines, isShownFn, childNodeFn) {
function currLine() {
return /** @type {string|undefined} */ lines[lines.length - 1] || '';
}
// TODO: Add case here for textual form elements.
if (elem.tagName === 'BR') {
lines.push('');
} else {
// TODO: properly handle display:run-in
var isTD = elem.tagName === 'TD';
var display = getComputedStyle(elem).getPropertyValue('display');
// On some browsers, table cells incorrectly show up with block styles.
var isBlock = !isTD &&
!INLINE_DISPLAY_BOXES_.includes(display);
// Add a newline before block elems when there is text on the current line,
// except when the previous sibling has a display: run-in.
// Also, do not run-in the previous sibling if this element is floated.
var previousElementSibling = elem.previousElementSibling;
var prevDisplay = (previousElementSibling) ?
getComputedStyle(previousElementSibling).getPropertyValue('display') : '';
// TODO: getEffectiveStyle should mask this for us
var thisFloat = getComputedStyle(elem).getPropertyValue('float');
var runIntoThis = prevDisplay == 'run-in' && thisFloat == 'none';
if (isBlock && !runIntoThis &&
currLine().trim() !== '') {
lines.push('');
}
// This element may be considered unshown, but have a child that is
// explicitly shown (e.g. this element has "visibility:hidden").
// Nevertheless, any text nodes that are direct descendants of this
// element will not contribute to the visible text.
var shown = isShownFn(elem);
// All text nodes that are children of this element need to know...