PrismJS / PrismJS/prism

[Discussion] textContent vs. innerText

Open
#1,773 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

core
Dominant language
JavaScript
Stars
13k
Forks
1.4k
Avg merge
15h 36m
Merged PRs (30d)
3

Description

In #1764 we began a discussion as to whether Prism.highlightElement should use textContent or innerText to get the text of the element to highlight.

Summary:

  • If the element to be highlighted doesn't contain any tags, innerText and textContent will return the same text.
  • innerText only returns the human-readable text of the element which makes it more intuitive. textContent ignores the meaning of all tags. This causes linebreaks that are implemented via <br> or a wrapper with display: block to be ignored.
  • The result of innerText depends on whether the element is visible. (If it's not visible it will return the same as textContent.)
  • textContent is generally faster.
    innerText takes the current style of the element (plus children) into account (which might require the layout to be recomputed).

Additional concerns:

Not concerns:

  • Both innerText and textContent have to good cross-browser compatibility with some older version of Firefox not supporting innerText and some older versions of IE not supporting textContent.
  • We don't have to change the implementation of Unescaped markup when using innerText.

/cc @SetTrend @mAAdhaTTah @Golmote @LeaVerou

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the Prism.highlightElement entry point and review the Keep markup, Line numbers, and Normalize whitespace plugins named in the discussion. Compare the current behavior of textContent and innerText, including visibility and browser differences. Done requires an agreed choice and coordinated behavior across the affected implementation and plugins.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript
Domain
frontend
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.