Skip to the code
cf.
+10 XP

#626 Split a Style Attribute

easy

An HTML sanitizer reads inline style attributes like color: red; margin: 0 4px and checks each declaration.

Write splitStyle(style) that returns a list of [property, value] pairs in order. Declarations are separated by ;. Split each one at its first :. Trim spaces around the property and the value, and lowercase the property.

Skip a declaration if it has no :, or if its property or value is empty after trimming. Values never contain ;, but they may contain : (as in a URL).

Example 1

Input: style = "color: red; margin: 0 4px"
Output: [["color","red"],["margin","0 4px"]]

Two declarations; spaces around each part are trimmed.

Example 2

Input: style = "Background: url(https://x.dev/a.png);;"
Output: [["background","url(https://x.dev/a.png)"]]

Only the first : splits; the empty declarations are skipped.

Constraints

  • 0 <= style.length <= 10^4
Saved in this browser

Run tries your code on the examples. Submit runs every test, including hidden edge cases, and solves the problem when they all pass.

Ctrl + Enter runs, Ctrl + Shift + Enter submits (⌘ on a Mac).