From unknown Sun Aug 10 16:47:42 2025 Content-Disposition: inline Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 X-Mailer: MIME-tools 5.509 (Entity 5.509) Content-Type: text/plain; charset=utf-8 From: bug#63342 <63342@debbugs.gnu.org> To: bug#63342 <63342@debbugs.gnu.org> Subject: Status: 28.2; dom-by-class does not handle nodes with multiple classes properly Reply-To: bug#63342 <63342@debbugs.gnu.org> Date: Sun, 10 Aug 2025 23:47:42 +0000 retitle 63342 28.2; dom-by-class does not handle nodes with multiple classe= s properly reassign 63342 emacs submitter 63342 Tim Landscheidt severity 63342 normal tag 63342 patch pending thanks From debbugs-submit-bounces@debbugs.gnu.org Sat May 06 23:35:44 2023 Received: (at submit) by debbugs.gnu.org; 7 May 2023 03:35:44 +0000 Received: from localhost ([127.0.0.1]:36116 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1pvVBX-0002Kq-PE for submit@debbugs.gnu.org; Sat, 06 May 2023 23:35:44 -0400 Received: from andalucia.tim-landscheidt.de ([116.203.78.250]:53554) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1pvVBV-0002Ke-CF for submit@debbugs.gnu.org; Sat, 06 May 2023 23:35:42 -0400 Received: from [195.226.160.202] (port=39952 helo=vagabond) by andalucia.tim-landscheidt.de with esmtpsa (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.89) (envelope-from ) id 1pvVBU-0001Mw-2R for submit@debbugs.gnu.org; Sun, 07 May 2023 03:35:40 +0000 From: Tim Landscheidt To: submit@debbugs.gnu.org Subject: 28.2; dom-by-class does not handle nodes with multiple classes properly Organization: https://www.tim-landscheidt.de/ Date: Sun, 07 May 2023 03:35:39 +0000 Message-ID: <875y9495kk.fsf@vagabond.tim-landscheidt.de> User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/28.2 (gnu/linux) MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" X-Spam-Score: 0.0 (/) X-Debbugs-Envelope-To: submit X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: debbugs-submit-bounces@debbugs.gnu.org Sender: "Debbugs-submit" X-Spam-Score: -1.0 (-) --=-=-= Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Package: emacs Version: 28.2 Tags: patch dom-by-class's docstring says: | Return elements in DOM that have a class name that matches regexp MATCH. However, it does not match its argument against the ele- ments' class names, but their class attributes. The class attribute can be composed of multiple, space-separated class names. This means that a node: |

=E2=80=A6 will not get matched by (dom-by-class dom "^class1$"). This can be worked around by using matches =C3=A0 la: | "\\(?:^\\| \\)class1\\(?:$\\| \\)" The attached patch fixes this by testing the match for each class name individually. --=-=-= Content-Type: text/x-patch Content-Disposition: inline; filename=fix-dom-by-class.patch diff --git a/lisp/dom.el b/lisp/dom.el index 3066673954a..314a38f194f 100644 --- a/lisp/dom.el +++ b/lisp/dom.el @@ -131,7 +131,12 @@ dom-strings (defun dom-by-class (dom match) "Return elements in DOM that have a class name that matches regexp MATCH." - (dom-elements dom 'class match)) + (dom-search dom + (lambda (d) + (if-let ((class-attr (dom-attr d 'class))) + (seq-find + (apply-partially #'string-match match) + (split-string class-attr " ")))))) (defun dom-by-style (dom match) "Return elements in DOM that have a style that matches regexp MATCH." diff --git a/test/lisp/dom-tests.el b/test/lisp/dom-tests.el index abb586435a7..213c367b3d8 100644 --- a/test/lisp/dom-tests.el +++ b/test/lisp/dom-tests.el @@ -43,7 +43,16 @@ dom-tests--tree (dom-node "div" '((class . "foo") (style . "color: red;")) (dom-node "p" '((id . "bar")) - "foo")) + "foo") + (dom-node "p" '((id . "test-case-1") + (class . "class1")) + "text1") + (dom-node "p" '((id . "test-case-2") + (class . "class12")) + "text2") + (dom-node "p" '((id . "test-case-3") + (class . "class1 class2")) + "text3")) (dom-node "div" '((title . "2nd div")) "bar")))) @@ -105,8 +114,9 @@ dom-tests--tree (ert-deftest dom-tests-texts () (let ((dom (dom-tests--tree))) - (should (equal (dom-texts dom) "Test foo bar")) - (should (equal (dom-texts dom ", ") "Test, foo, bar")))) + (should (equal (dom-texts dom) "Test foo text1 text2 text3 bar")) + (should (equal (dom-texts dom ", ") + "Test, foo, text1, text2, text3, bar")))) (ert-deftest dom-tests-child-by-tag () (let ((dom (dom-tests--tree))) @@ -121,13 +131,29 @@ dom-tests--tree (ert-deftest dom-tests-strings () (let ((dom (dom-tests--tree))) - (should (equal (dom-strings dom) '("Test" "foo" "bar"))) + (should (equal (dom-strings dom) + '("Test" "foo" "text1" "text2" "text3" "bar"))) (should (equal (dom-strings (dom-children dom)) '("Test"))))) (ert-deftest dom-tests-by-class () (let ((dom (dom-tests--tree))) (should (equal (dom-tag (dom-by-class dom "foo")) "div")) - (should-not (dom-by-class dom "bar")))) + (should-not (dom-by-class dom "bar")) + (should (equal (mapcar (lambda (d) (dom-attr d 'id)) + (dom-by-class dom "class1")) + '("test-case-1" "test-case-2" "test-case-3"))) + (should (equal (mapcar (lambda (d) (dom-attr d 'id)) + (dom-by-class dom "class1$")) + '("test-case-1" "test-case-3"))) + (should (equal (mapcar (lambda (d) (dom-attr d 'id)) + (dom-by-class dom "^class2")) + '("test-case-3"))) + ;; Test that workaround still works. + (should (equal (mapcar (lambda (d) (dom-attr d 'id)) + (dom-by-class + dom + "\\(?:^\\| \\)class1\\(?:$\\| \\)")) + '("test-case-1" "test-case-3"))))) (ert-deftest dom-tests-by-style () (let ((dom (dom-tests--tree))) --=-=-=-- From debbugs-submit-bounces@debbugs.gnu.org Wed Sep 06 16:34:39 2023 Received: (at control) by debbugs.gnu.org; 6 Sep 2023 20:34:39 +0000 Received: from localhost ([127.0.0.1]:37969 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1qdzEV-00078E-Dm for submit@debbugs.gnu.org; Wed, 06 Sep 2023 16:34:39 -0400 Received: from mail-lf1-x131.google.com ([2a00:1450:4864:20::131]:52394) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1qdzEU-00077z-8Q for control@debbugs.gnu.org; Wed, 06 Sep 2023 16:34:38 -0400 Received: by mail-lf1-x131.google.com with SMTP id 2adb3069b0e04-501cef42bc9so325788e87.0 for ; Wed, 06 Sep 2023 13:34:36 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1694032471; x=1694637271; darn=debbugs.gnu.org; h=to:subject:message-id:date:mime-version:from:from:to:cc:subject :date:message-id:reply-to; bh=KOPvPlnRJcjHR05tx1DOg82e6/8O70bfFQKEsdvh+Wk=; b=RubkdgqPp4//AJQbhE5+2LyilxiRIgCSwsEnjfRDdyyFJH1R5YPQVEdJUCnPZ5q5yb 13pkZr42Nr/m/ldhAEtqfpacFQ7VAINKAJy/wAhYLWc4DZ8epgn1zJn7DyUhDx+1uJ4a I0SrRklL6/wgKnc6YYtYOQWX7Eta/H8g8cWqOjtXuOR183QtjTCoCAYPTaYOKdIqPWD0 Vq1OW+Okt4EkCn0b1/UygGB1G+ei7zKXQ8n4ResIBoWdCfwcjoiiXwz3kj4fAO1wsv95 Hi0sYImL81I5kgtjvkLrcCgZJXFZsStEGR4cW+1XKUwszy6yxLfLAaoGoI8C/+UIYgeN FLew== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1694032471; x=1694637271; h=to:subject:message-id:date:mime-version:from:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=KOPvPlnRJcjHR05tx1DOg82e6/8O70bfFQKEsdvh+Wk=; b=FnzTVSUa6XG7o4ky7Ibsx15BMroVP7vZpYvz8ZbC4HgwZ7RYtCJcW5mkfBC5WRei1W Pv1MxKk2I4sP7t7Ms9KgN5zDxKtIoVEdsbbCGXkAjwChRHWTZRrfq9O3kxfzfrtjpjQ2 peLHNj4xszCplSeOQ9omZC68C6OBzhtQTXqRg9ZiU9EQh6EFNUMOFbaJ4I2GBc00lSm9 mmnVAuXqEvkAcGmixWCnxPK6xi6rYYpvMT1l4wRrx4GsLAEgtoh5RGIKVxaTFVSk6PdS hd8GK34n2iS1eOaRGj077BpD5L/g3vH/PhtyF41m6w1P89OAtcNK/pPalUK5B9igk4DK lE2w== X-Gm-Message-State: AOJu0Yy30NGL6EBaxsI39eXqas2B+nnv9J1mbU7E3DabCeNcVvRUSsz9 K/l5EDWTwqgZaPh5tdW1f/ET8gvkt1vi9NvCqN/nYJpuXXw= X-Google-Smtp-Source: AGHT+IGYNVXhlfGw0exi2nVj/vglVRqjl1H4jScUgFFLAWb5l5WcmbrpYm/aDlSHosgQxzUyWpkq5WBHsYLaqIVEnp4= X-Received: by 2002:a19:ae13:0:b0:500:a60d:c677 with SMTP id f19-20020a19ae13000000b00500a60dc677mr3061365lfc.59.1694032470816; Wed, 06 Sep 2023 13:34:30 -0700 (PDT) Received: from 753933720722 named unknown by gmailapi.google.com with HTTPREST; Wed, 6 Sep 2023 13:34:30 -0700 From: Stefan Kangas MIME-Version: 1.0 Date: Wed, 6 Sep 2023 13:34:30 -0700 Message-ID: Subject: control message for bug #63342 To: control@debbugs.gnu.org Content-Type: text/plain; charset="UTF-8" X-Spam-Score: 0.0 (/) X-Debbugs-Envelope-To: control X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: debbugs-submit-bounces@debbugs.gnu.org Sender: "Debbugs-submit" X-Spam-Score: -1.0 (-) tags 63342 + pending quit