From 59c4248efe43e1b2df4a379bd96804efd1740270 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20B=C3=B6hmer?= Date: Sat, 9 Mar 2024 23:09:49 +0100 Subject: [PATCH] Strip HTML tags from more LCSC fields This should hopefully fix issue #553 --- .../Providers/LCSCProvider.php | 30 ++++++++++++++----- 1 file changed, 23 insertions(+), 7 deletions(-) diff --git a/src/Services/InfoProviderSystem/Providers/LCSCProvider.php b/src/Services/InfoProviderSystem/Providers/LCSCProvider.php index 48bc11de..ee88f27d 100755 --- a/src/Services/InfoProviderSystem/Providers/LCSCProvider.php +++ b/src/Services/InfoProviderSystem/Providers/LCSCProvider.php @@ -128,6 +128,21 @@ class LCSCProvider implements InfoProviderInterface return $result; } + /** + * Sanitizes a field by removing any HTML tags and other unwanted characters + * @param string|null $field + * @return string|null + */ + private function sanitizeField(?string $field): ?string + { + if ($field === null) { + return null; + } + + return strip_tags($field); + } + + /** * Takes a deserialized json object of the product and returns a PartDetailDTO * @param array $product @@ -146,8 +161,9 @@ class LCSCProvider implements InfoProviderInterface // LCSC puts HTML in footprints and descriptions sometimes randomly $footprint = $product["encapStandard"] ?? null; - if ($footprint !== null) { - $footprint = strip_tags($footprint); + //If the footprint just consists of a dash, we'll assume it's empty + if ($footprint === '-') { + $footprint = null; } //Build category by concatenating the catalogName and parentCatalogName @@ -163,14 +179,14 @@ class LCSCProvider implements InfoProviderInterface provider_key: $this->getProviderKey(), provider_id: $product['productCode'], name: $product['productModel'], - description: strip_tags($product['productIntroEn']), - category: $category, - manufacturer: $product['brandNameEn'], - mpn: $product['productModel'] ?? null, + description: $this->sanitizeField($product['productIntroEn']), + category: $this->sanitizeField($category ?? null), + manufacturer: $this->sanitizeField($product['brandNameEn'] ?? null), + mpn: $this->sanitizeField($product['productModel'] ?? null), preview_image_url: $product['productImageUrl'], manufacturing_status: null, provider_url: $this->getProductShortURL($product['productCode']), - footprint: $footprint, + footprint: $this->sanitizeField($footprint), datasheets: $this->getProductDatasheets($product['pdfUrl'] ?? null), images: $product_images, parameters: $this->attributesToParameters($product['paramVOList'] ?? []),