From 5d794df9c47145a5c45058e6f03727c92b253c97 Mon Sep 17 00:00:00 2001 From: aproragadozo Date: Tue, 26 Nov 2024 12:36:23 +0100 Subject: [PATCH] handle multi-line values with line breaks and parens --- src/convertToObject.js | 43 ++++++++++++++++-------------------------- 1 file changed, 16 insertions(+), 27 deletions(-) diff --git a/src/convertToObject.js b/src/convertToObject.js index aad0916a..99c5b67e 100644 --- a/src/convertToObject.js +++ b/src/convertToObject.js @@ -9,33 +9,22 @@ function convertToObject(sourceString) { // result object declaration const styleSheet = {}; - // split the string into lines - // filter out empty or whitespace-only lines - const lines = sourceString - .replace(/\n\s*\n/g, '\n') // remove empty lines - .split('\n') - .filter(line => line.trim() && line.trim() !== ';'); - - let currentKey = null; - - // process lines - for (const line of lines) { - // check if this is the continuation of a previous line - if (currentKey && line.trim() && !line.includes(':')) { - // append to existing values - styleSheet[currentKey] += ' ' + line.trim(); - continue; - } - // resume normal key-value parsing - const [key, value] = line.split(':').map(part => part.trim()); - - if (key && value) { - const cleanedValue = value.replace(/;+$/, '').trim(); - // store the current key for potential continuation - currentKey = key; - - // add to stylesheet result - styleSheet[key] = cleanedValue; + const processedString = sourceString + .replace(/\n\s*\n/g, '\n') // Remove completely empty lines + .replace(/;\s*\n\s*\)/g, ' )') // Handle semicolon-newline-closing parenthesis + .replace(/\n\s*/g, ' '); // Replace newlines with spaces + + // Split into declarations + const declarations = processedString + .split(';') + .filter(decl => decl.trim()); + + for (const declaration of declarations) { + const [key, ...valueParts] = declaration.split(':').map(part => part.trim()); + + if (key) { + const value = valueParts.join(':').trim().replace(/;+$/, ''); + styleSheet[key] = value; } } return styleSheet;