Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 21 additions & 6 deletions csscolorparser.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,20 +138,35 @@ function parseCSSColor(css_str) {

// #abc and #abc123 syntax.
if (str[0] === '#') {
var iv = parseInt(str.substr(1), 16); // TODO(deanm): Stricter parsing.
if (str.length === 4) {
var iv = parseInt(str.substr(1), 16); // TODO(deanm): Stricter parsing.
// #rgb
if (!(iv >= 0 && iv <= 0xfff)) return null; // Covers NaN.
return [((iv & 0xf00) >> 4) | ((iv & 0xf00) >> 8),
(iv & 0xf0) | ((iv & 0xf0) >> 4),
(iv & 0xf) | ((iv & 0xf) << 4),
(iv & 0x0f0) | ((iv & 0x0f0) >> 4),
((iv & 0x00f) << 4) | (iv & 0x00f),
1];
} else if (str.length === 7) {
var iv = parseInt(str.substr(1), 16); // TODO(deanm): Stricter parsing.
// #rrggbb
if (!(iv >= 0 && iv <= 0xffffff)) return null; // Covers NaN.
return [(iv & 0xff0000) >> 16,
(iv & 0xff00) >> 8,
iv & 0xff,
(iv & 0x00ff00) >> 8,
iv & 0x0000ff,
1];
} else if (str.length === 5) {
// #rgba
if (!(iv >= 0 && iv <= 0xffff)) return null; // Covers NaN.
return [((iv & 0xf000) >> 8) | ((iv & 0xf000) >> 12),
((iv & 0x0f00) >> 4) | ((iv & 0x0f00) >> 8),
(iv & 0x00f0) | ((iv & 0x00f0) >> 4),
((iv & 0x000f) << 4 | (iv & 0x000f)) / 255];
} else if (str.length === 9) {
// #rrggbbaa
if (!(iv >= 0 && iv <= 0xffffffff)) return null; // Covers NaN.
return [((iv & 0xff000000) >> 24) & 0xff,
(iv & 0x00ff0000) >> 16,
(iv & 0x0000ff00) >> 8,
(iv & 0x000000ff) / 255];
}

return null;
Expand Down