Skip to curriculum
Jump to the editor

23.5. Apply style ranges to text

Medium
Topics
15–30 min

Editors, chat apps and comment boxes rarely store formatting as HTML. They store the plain text and a list of style ranges: "bold from 0 to 5, italic from 3 to 11". Ranges can overlap freely, which tags cannot: <b>Hel<i>lo</b> world</i> is broken HTML. Turning ranges into HTML means cutting the text into pieces where the styles change, and nesting the tags so they never cross.

Write applyStyles(text, ranges) in styles.js.

Signature

JAVASCRIPT
function applyStyles(text, ranges) // returns a string of HTML
  • text: the plain text.
  • ranges: an array of { start, end, style }. start is included and end is not, as in slice(start, end). style is one of "bold", "italic", "underline", "strike" and "code", written as <b>, <i>, <u>, <s> and <code>.

Requirements

  1. Each character is inside exactly the tags of the styles whose ranges cover it, and inside no other tag.
  2. Tags always nest in one fixed order, outermost first: b, i, u, s, code. The order of ranges does not matter.
  3. The HTML is minimal in this sense: a tag that the next piece of text also needs stays open. Neighbouring ranges of one style, and duplicate ranges, give a single tag. A tag is closed and reopened only when the fixed order forces it.
  4. Tags are always balanced and never cross, and every tag is closed at the end.
  5. The text is escaped: & to &amp;, < to &lt;, > to &gt;, " to &quot;, ' to &#39;.
  6. Ranges are clipped to the text. A range that is empty or reversed after clipping is ignored.
  7. A style that is not one of the five throws a TypeError, including names such as "toString".
  8. ranges and its objects are never changed.

Examples

JAVASCRIPT
applyStyles("Hello world", [
  { start: 0, end: 5, style: "bold" },
  { start: 3, end: 11, style: "italic" },
]);
// "<b>Hel<i>lo</i></b><i> world</i>"
JAVASCRIPT
applyStyles("abcdefgh", [
  { start: 2, end: 4, style: "italic" },
  { start: 0, end: 8, style: "bold" },
]);
// "<b>ab<i>cd</i>efgh</b>": bold stays open around the italic part
JAVASCRIPT
applyStyles("abc", [
  { start: 0, end: 3, style: "italic" },
  { start: 1, end: 2, style: "bold" },
]);
// "<i>a</i><b><i>b</i></b><i>c</i>": b must be outside i, so the italic tag closes and reopens

Environment

  • Plain JavaScript in a browser page; the tests call the global applyStyles from styles.js.
  • start and end are integers. Links, paragraphs and line breaks are not part of this problem (see problem 23.6).
  • Any built-in is allowed. The tests also parse your HTML to check every character's tags.

Hints

  1. Where can the set of styles change? Only at a range's start or end. What does that give you to cut the text at?
  2. Between two pieces, the open tags and the next piece's tags often share a beginning. Which tags must close, and which can stay open?

The explanation comes with the course

A walkthrough of every line of a solution, the mistakes the tests catch, other ways to write it, and links to the lessons that teach each idea.

  • Every line explained
  • The mistakes each test catches
  • Links to the lessons behind it
Back to the question
Loading the editor…