From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Sacha Chua Newsgroups: gmane.emacs.devel Subject: [PATCH] shr.el: correct SVG attribute case Date: Fri, 26 Jan 2024 09:15:15 -0500 Message-ID: <878r4cxjt8.fsf@sachachua.com> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="7821"; mail-complaints-to="usenet@ciao.gmane.io" To: emacs-devel@gnu.org Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane-mx.org@gnu.org Fri Jan 26 15:16:38 2024 Return-path: Envelope-to: ged-emacs-devel@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1rTN0X-0001sg-S3 for ged-emacs-devel@m.gmane-mx.org; Fri, 26 Jan 2024 15:16:37 +0100 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1rTMzm-0001Nx-Rf; Fri, 26 Jan 2024 09:15:51 -0500 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1rTMzZ-0001N4-I7 for emacs-devel@gnu.org; Fri, 26 Jan 2024 09:15:37 -0500 Original-Received: from mail-qt1-x832.google.com ([2607:f8b0:4864:20::832]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1rTMzV-0001wi-N3 for emacs-devel@gnu.org; Fri, 26 Jan 2024 09:15:37 -0500 Original-Received: by mail-qt1-x832.google.com with SMTP id d75a77b69052e-42a683b9733so4526811cf.1 for ; Fri, 26 Jan 2024 06:15:32 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sachachua-com.20230601.gappssmtp.com; s=20230601; t=1706278531; x=1706883331; darn=gnu.org; h=mime-version:message-id:date:subject:to:from:from:to:cc:subject :date:message-id:reply-to; bh=oCvgf+ZPzNNiA0767zHjG0yxMfSJFnFhT2SKRbvQi5M=; b=gn9HGhmx1BVYaEdDuOMRjLy+jzmd86NuD3ojE3Vz6holKoyb4iyW1+CgDSzasdNDUa UbjsOdiJvVtDt9amAhtduI4M5Jv+q4l34ORLSV/7dZYDUft0iP66UWAYE79DMzjE3ocJ 3LLVK9JbAlGKVol99Q4ja1G5mUUBAQoDYmTLkBHLNsx38gcN6H02BOYFxfLHPU7PYE0h FlcAQt3oifZkMMBkVTBiUfZt421J0D/78bFxKNts/IbLP990qpcDwQnRQION8JDHWQw7 h+Qt5kp/RKAcM2iXyPgnUBXPG2OhG4n4wAygdR4fr0L7xMyr1g1VkclmS05UFyCO9ikp +7jQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1706278531; x=1706883331; h=mime-version:message-id:date:subject:to:from:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=oCvgf+ZPzNNiA0767zHjG0yxMfSJFnFhT2SKRbvQi5M=; b=nFkkx8dxijV+NutfmurwZvBdwVeh0ScZFsQRBlmImHCQVxeBVRhap3apuCp0st0Zo5 H+tbAwscpHwWCLZNLARMPlzA9YjQzIAgROZpbF3leF9h9qR4Y0AVhXNrW5+ej0h/keR+ O/M2nJmYkYp20zeAnBtEMF9j/9p6n2l3J90HDbJjrNELDwEsyNUqNvX5tNyk0S4h/qL+ A8xrrjWG/VpQGqI4W5bxtzv4NnQvR1aXDPPgPH/OgFncRdvvzHFHR4r8C79CLyMnkV2w CrxZYEkSSqDpPogoVV+BB5XcIy8/5cQ9d5A5LsvDW6CjU+9z2GkF5uvOXW8+SjjRNnDm eh5A== X-Gm-Message-State: AOJu0YzFO8yXH5Fq0uhJkW74bVhlY8iaLIXsjS7CGU2x9Ukij/9ieo+D xNaL6/hE/FRvtZ1/JLIcIwgGQQJfFX+Pc2R3APuYm2q3r4YivCFsfby8yMylrXJ87O/vWI45NfP k X-Google-Smtp-Source: AGHT+IFvRBpq1FVqFUCgdBV0YRFtbEyeUJZVx01bShUSVJ2S57mOsfRbhaIIUE8HNO1mHYyAL/BKmw== X-Received: by 2002:a05:622a:347:b0:42a:6e24:7ae8 with SMTP id r7-20020a05622a034700b0042a6e247ae8mr2624419qtw.9.1706278531067; Fri, 26 Jan 2024 06:15:31 -0800 (PST) Original-Received: from sacha-x220 ([70.54.59.113]) by smtp.googlemail.com with ESMTPSA id ca21-20020a05622a1f1500b00429cfdac07fsm551944qtb.18.2024.01.26.06.15.29 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 26 Jan 2024 06:15:29 -0800 (PST) Received-SPF: none client-ip=2607:f8b0:4864:20::832; envelope-from=sacha@sachachua.com; helo=mail-qt1-x832.google.com X-Spam_score_int: -18 X-Spam_score: -1.9 X-Spam_bar: - X-Spam_report: (-1.9 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_NONE=0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: emacs-devel@gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: "Emacs development discussions." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-devel-bounces+ged-emacs-devel=m.gmane-mx.org@gnu.org Original-Sender: emacs-devel-bounces+ged-emacs-devel=m.gmane-mx.org@gnu.org Xref: news.gmane.io gmane.emacs.devel:315434 Archived-At: --=-=-= Content-Type: text/plain I've attached a patch for restoring the correct case of SVG attributes parsed with libxml-parse-html-region when displayed with shr, so that SVGs can be displayed properly. HTML attributes are case-insensitive and converted to lowercase, but SVG attributes are case-sensitive, so things like viewBox don't work if they're lowercased. It looks like other implementations (ex: https://github.com/w3c/svgwg/issues/161 ) also use the workaround of fixing attribute cases for SVGs, so I took the list from https://html.spec.whatwg.org/multipage/parsing.html#parsing-main-inforeign and fixed those attributes. Here's a small test to demonstrate the issue: ;; The following should display a full circle, not a quarter circle (with-current-buffer (get-buffer-create "*test*") (erase-buffer) (insert "\n") (shr-insert-document (libxml-parse-html-region (point-min) (point-max))) (display-buffer (current-buffer))) And for shr-correct-dom-case to make sure it's working correctly: (ert-deftest shr-correct-dom-case () (let ((case-fold-search nil)) (should (string-match "viewBox" (shr-dom-to-xml (shr-correct-dom-case (with-temp-buffer (insert "") (libxml-parse-html-region (point-min) (point-max))))))))) There are a few more screenshots at https://sachachua.com/blog/2024/01/patching-elfeed-and-shr-to-handle-svg-images-with-viewbox-attributes/ . I should have copyright assignment papers on file. --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=0001-shr-Correct-SVG-attribute-case.patch >From 0a56473f5a41603ed5afc05db87b6ef35b234f7e Mon Sep 17 00:00:00 2001 From: Sacha Chua Date: Fri, 26 Jan 2024 08:54:03 -0500 Subject: [PATCH] shr: Correct SVG attribute case * lisp/net/shr.el (shr-correct-attribute-case): New constant. (shr-correct-dom-case): New function to correct SVG attribute case. (shr-tag-svg): Correct SVG attribute cases before using them. --- lisp/net/shr.el | 76 +++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 74 insertions(+), 2 deletions(-) diff --git a/lisp/net/shr.el b/lisp/net/shr.el index 17fdffd619d..e23fc6104d2 100644 --- a/lisp/net/shr.el +++ b/lisp/net/shr.el @@ -1437,13 +1437,85 @@ shr-dom-print (shr-dom-print elem))))) (insert (format "" (dom-tag dom)))) +(defconst shr-correct-attribute-case + '((attributename . attributeName) + (attributetype . attributeType) + (basefrequency . baseFrequency) + (baseprofile . baseProfile) + (calcmode . calcMode) + (clippathunits . clipPathUnits) + (diffuseconstant . diffuseConstant) + (edgemode . edgeMode) + (filterunits . filterUnits) + (glyphref . glyphRef) + (gradienttransform . gradientTransform) + (gradientunits . gradientUnits) + (kernelmatrix . kernelMatrix) + (kernelunitlength . kernelUnitLength) + (keypoints . keyPoints) + (keysplines . keySplines) + (keytimes . keyTimes) + (lengthadjust . lengthAdjust) + (limitingconeangle . limitingConeAngle) + (markerheight . markerHeight) + (markerunits . markerUnits) + (markerwidth . markerWidth) + (maskcontentunits . maskContentUnits) + (maskunits . maskUnits) + (numoctaves . numOctaves) + (pathlength . pathLength) + (patterncontentunits . patternContentUnits) + (patterntransform . patternTransform) + (patternunits . patternUnits) + (pointsatx . pointsAtX) + (pointsaty . pointsAtY) + (pointsatz . pointsAtZ) + (preservealpha . preserveAlpha) + (preserveaspectratio . preserveAspectRatio) + (primitiveunits . primitiveUnits) + (refx . refX) + (refy . refY) + (repeatcount . repeatCount) + (repeatdur . repeatDur) + (requiredextensions . requiredExtensions) + (requiredfeatures . requiredFeatures) + (specularconstant . specularConstant) + (specularexponent . specularExponent) + (spreadmethod . spreadMethod) + (startoffset . startOffset) + (stddeviation . stdDeviation) + (stitchtiles . stitchTiles) + (surfacescale . surfaceScale) + (systemlanguage . systemLanguage) + (tablevalues . tableValues) + (targetx . targetX) + (targety . targetY) + (textlength . textLength) + (viewbox . viewBox) + (viewtarget . viewTarget) + (xchannelselector . xChannelSelector) + (ychannelselector . yChannelSelector) + (zoomandpan . zoomAndPan)) + "Attributes for correcting the case in SVG and MathML. +Based on https://html.spec.whatwg.org/multipage/parsing.html#parsing-main-inforeign .") + +(defun shr-correct-dom-case (dom) + "Correct the case for SVG segments." + (dolist (attr (dom-attributes dom)) + (when-let ((rep (assoc-default (car attr) shr-correct-attribute-case))) + (setcar attr rep))) + (dolist (child (dom-children dom)) + (shr-correct-dom-case child)) + dom) + (defun shr-tag-svg (dom) (when (and (image-type-available-p 'svg) (not shr-inhibit-images) (dom-attr dom 'width) (dom-attr dom 'height)) - (funcall shr-put-image-function (list (shr-dom-to-xml dom 'utf-8) - 'image/svg+xml) + (funcall shr-put-image-function + (list (shr-dom-to-xml (shr-correct-dom-case dom) 'utf-8) + 'image/svg+xml) "SVG Image"))) (defun shr-tag-sup (dom) -- 2.34.1 --=-=-= Content-Type: text/plain What do you think? Sacha --=-=-=--