@marvalt/wparser 0.1.40 → 0.1.42

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.esm.js CHANGED
@@ -2081,13 +2081,45 @@ function extractVideoIframeFromInnerBlocks(block) {
2081
2081
  */
2082
2082
  function extractBackgroundColor(block, context) {
2083
2083
  const attrs = block.attributes || {};
2084
- const wpColorName = attrs['backgroundColor'] || attrs['background'];
2084
+ // Try multiple possible attribute names for background color
2085
+ // WordPress can store it as backgroundColor, background, or in style
2086
+ let wpColorName = attrs['backgroundColor'] ||
2087
+ attrs['background'] ||
2088
+ attrs['backgroundColorSlug'] ||
2089
+ null;
2090
+ // If not found in attributes, check className for WordPress color classes
2091
+ // WordPress uses classes like: has-accent-4-background-color, has-contrast-background-color
2092
+ if (!wpColorName && attrs['className']) {
2093
+ const className = attrs['className'];
2094
+ const colorMatch = className.match(/has-([\w-]+)-background-color/);
2095
+ if (colorMatch && colorMatch[1]) {
2096
+ // Convert kebab-case to slug (e.g., "accent-4" stays "accent-4", "contrast" stays "contrast")
2097
+ wpColorName = colorMatch[1];
2098
+ }
2099
+ }
2100
+ // Debug logging in development
2101
+ if (typeof process !== 'undefined' && process.env?.NODE_ENV === 'development') {
2102
+ if (!wpColorName && context.colorMapper) {
2103
+ console.log('🔍 extractBackgroundColor - No backgroundColor found in block:', {
2104
+ blockName: block.name,
2105
+ hasClassName: !!attrs['className'],
2106
+ className: attrs['className'],
2107
+ hasStyle: !!attrs['style'],
2108
+ availableAttrs: Object.keys(attrs),
2109
+ });
2110
+ }
2111
+ }
2085
2112
  if (!wpColorName || typeof wpColorName !== 'string') {
2086
2113
  return null;
2087
2114
  }
2088
2115
  // Use colorMapper from context if available
2089
2116
  if (context.colorMapper) {
2090
- return context.colorMapper(wpColorName);
2117
+ const result = context.colorMapper(wpColorName);
2118
+ // Debug logging
2119
+ if (typeof process !== 'undefined' && process.env?.NODE_ENV === 'development') {
2120
+ console.log('🎨 extractBackgroundColor - Mapped', wpColorName, '→', result);
2121
+ }
2122
+ return result;
2091
2123
  }
2092
2124
  // Fallback: return null (no background applied)
2093
2125
  return null;
@@ -2102,34 +2134,62 @@ function extractBackgroundColor(block, context) {
2102
2134
  */
2103
2135
  function extractTextColor(block, context) {
2104
2136
  const attrs = block.attributes || {};
2105
- const wpColorName = attrs['textColor'] || attrs['text'];
2137
+ // Try multiple possible attribute names for text color
2138
+ let wpColorName = attrs['textColor'] ||
2139
+ attrs['text'] ||
2140
+ attrs['textColorSlug'] ||
2141
+ null;
2142
+ // If not found in attributes, check className for WordPress color classes
2143
+ // WordPress uses classes like: has-accent-4-color, has-contrast-color
2144
+ // Note: We need to avoid matching has-{color}-background-color, so we check for -color that's not followed by -background
2145
+ if (!wpColorName && attrs['className']) {
2146
+ const className = attrs['className'];
2147
+ // Match: has-{color}-color (text color class, not background-color)
2148
+ // This regex matches "has-accent-4-color" but not "has-accent-4-background-color"
2149
+ const colorMatch = className.match(/has-([\w-]+)-color(?!-background)/);
2150
+ if (colorMatch && colorMatch[1]) {
2151
+ wpColorName = colorMatch[1];
2152
+ }
2153
+ // Debug logging
2154
+ if (typeof process !== 'undefined' && process.env?.NODE_ENV === 'development' && !wpColorName) {
2155
+ // Check if there are any color-related classes
2156
+ const hasColorClass = className.includes('-color');
2157
+ if (hasColorClass && context.colorMapper) {
2158
+ console.log('🔍 extractTextColor - Found color class in className but no match:', {
2159
+ blockName: block.name,
2160
+ className: className,
2161
+ colorClasses: className.split(' ').filter(c => c.includes('-color')),
2162
+ });
2163
+ }
2164
+ }
2165
+ }
2106
2166
  if (!wpColorName || typeof wpColorName !== 'string') {
2107
2167
  return null;
2108
2168
  }
2109
- // Special handling for common WordPress color names when used as text color
2110
- // These mappings take precedence because text color semantics differ from background color
2111
- const textColorMap = {
2112
- 'contrast': 'text-gray-900', // Contrast text is typically dark/black (opposite of contrast background)
2113
- 'base': 'text-white', // Base text on dark backgrounds
2114
- };
2115
- // Check special text color mappings first
2116
- if (textColorMap[wpColorName]) {
2117
- return textColorMap[wpColorName];
2118
- }
2119
2169
  // Use colorMapper from context if available
2120
- // Note: colorMapper might return combined classes like "bg-gray-900 text-white"
2121
- // We'll extract just the text color part
2170
+ // Note: colorMapper returns combined classes like "bg-[#FBFAF3] text-gray-900"
2171
+ // We need to extract just the text color part
2122
2172
  if (context.colorMapper) {
2123
2173
  const mapped = context.colorMapper(wpColorName);
2124
2174
  if (mapped) {
2125
- // If the mapped class includes text color (e.g., "bg-gray-900 text-white"),
2126
- // extract just the text color part
2127
- const textColorMatch = mapped.match(/\btext-\S+/);
2128
- if (textColorMatch) {
2129
- return textColorMatch[0];
2175
+ // Extract text color classes (e.g., "text-white", "text-gray-900")
2176
+ // Match text- classes but not text- in the middle of other classes
2177
+ const textColorMatch = mapped.match(/\btext-[\w-]+/g);
2178
+ if (textColorMatch && textColorMatch.length > 0) {
2179
+ // Return the last text color class (in case there are multiple)
2180
+ const result = textColorMatch[textColorMatch.length - 1];
2181
+ // Debug logging
2182
+ if (typeof process !== 'undefined' && process.env?.NODE_ENV === 'development') {
2183
+ console.log('🎨 extractTextColor - Mapped', wpColorName, '→', result, '(from:', mapped, ')');
2184
+ }
2185
+ return result;
2130
2186
  }
2187
+ // If no text color class found, try to determine from the background color
2188
+ // For theme palette colors, we can infer text color based on brightness
2189
+ // But for now, return null if no text color is in the mapped result
2131
2190
  }
2132
2191
  }
2192
+ // Fallback: return null (no text color applied, will inherit from parent)
2133
2193
  return null;
2134
2194
  }
2135
2195
  /**