question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Question] How to check that only a part of the text is visible?

See original GitHub issue

Hi team and community. Here is my use case: our component should truncate the header text if it’s too long.

I’m trying to check this and don’t understand how to do it exactly. What I tried: innerText, textContent, text selector, isVisible method.

Could you please suggest how to do this? For example, if I have <h2>the really long text</h2>, how to check that only “the really long” value is visible?

Thanks!

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
rwollcommented, May 20, 2022

I’ll double check with the team to see if I’m missing something or if there is another suggestion.

There’s no convenient/straight-forward built in API in any of the browsers, so I think a screenshot is your best approach for this case at the moment. 😢

If you’re down for some exploration, you might be able to hack something together with the Range API, but I haven’t thought through its limitations (nor complexity dealing with more than a very simple, minimal example):

Screen Shot 2022-05-19 at 10 12 07 PM

(Notice that within a single example, if you compare the <p> width with the selection, it would not quite give a correct answer.)

<style>
  p {
    white-space: nowrap;
    width: 100px;
    overflow: hidden;
    text-overflow: ellipsis;
  }
</style>

<p>this is example text</p>

<div id="debug"></div>

<script>
  // Easily show debug output
  const debug = document.getElementById("debug");
  const addLogLine = (text) => {
    const line = document.createElement('div');
    line.style.fontFamily = 'monospace';
    line.innerText = text;
    debug.appendChild(line);
  };

  const p = document.getElementsByTagName("p")[0];
  addLogLine(`${p.getBoundingClientRect().width}: width of <p>`);

  // Iterate over the p's text to see each range's width
  const range = document.createRange();
  const text = p.firstChild;
  range.setStart(text, 0);
  for (let i = text.textContent.length; i >= 0; i--) {
    range.setEnd(text, i);
    addLogLine(`${range.getBoundingClientRect().width}: width of: "${range.toString()}"`);
  }
</script>

1reaction
rwollcommented, May 19, 2022

I wanted to check everything I can using the functional approach (to save the number of screenshots in Percy).

This is a good instinct! However, in this specific case, I think the screenshot might be best since I don’t believe the browser exposes an API that says which part of the text has/hasn’t been elided or clipped (assuming you’re using CSS to do the overflow handling).

I’ll double check with the team to see if I’m missing something or if there is another suggestion. (cc: @pavelfeldman: if this can’t be done nicely today, without using a screenshot, this could be an non-screenshot assertion capability for layout/VRT scenarios)

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to check if a specific part of a text (string) is visible after ...
The think is: I only want to show the "read more" link if the text is not fully displayed. However, the element that...
Read more >
Display Logic - Qualtrics
Question : Display a question only if the respondent chose a specific answer to a ... logic to make sure the right respondents...
Read more >
Add alternative text to a shape, picture, chart, SmartArt graphic ...
Right-click the object and select Edit Alt Text. Edit Alt Text option in the context menu for a picture · Select the object...
Read more >
visibility - CSS: Cascading Style Sheets - MDN Web Docs
The element box is invisible (not drawn), but still affects layout as normal. Descendants of the element will be visible if they have ......
Read more >
Write good Alt Text to describe images | Digital Accessibility​
Only in extreme circumstances, such as when a logo is used, should you provide an image of text rather than text. If you...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found