X-Loop: help-debbugs@HIDDEN Subject: bug#63342: 28.2; dom-by-class does not handle nodes with multiple classes properly Resent-From: Tim Landscheidt <tim@HIDDEN> Original-Sender: "Debbugs-submit" <debbugs-submit-bounces <at> debbugs.gnu.org> Resent-CC: bug-gnu-emacs@HIDDEN Resent-Date: Sun, 07 May 2023 03:36:02 +0000 Resent-Message-ID: <handler.63342.B.16834305448984 <at> debbugs.gnu.org> Resent-Sender: help-debbugs@HIDDEN X-GNU-PR-Message: report 63342 X-GNU-PR-Package: emacs X-GNU-PR-Keywords: patch To: 63342 <at> debbugs.gnu.org X-Debbugs-Original-To: submit <at> debbugs.gnu.org Received: via spool by submit <at> debbugs.gnu.org id=B.16834305448984 (code B ref -1); Sun, 07 May 2023 03:36:02 +0000 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 <debbugs-submit-bounces <at> debbugs.gnu.org>) id 1pvVBX-0002Kq-PE for submit <at> 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 <tim@HIDDEN>) id 1pvVBV-0002Ke-CF for submit <at> 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 <tim@HIDDEN>) id 1pvVBU-0001Mw-2R for submit <at> debbugs.gnu.org; Sun, 07 May 2023 03:35:40 +0000 From: Tim Landscheidt <tim@HIDDEN> Organization: https://www.tim-landscheidt.de/ Date: Sun, 07 May 2023 03:35:39 +0000 Message-ID: <875y9495kk.fsf@HIDDEN> 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-BeenThere: debbugs-submit <at> debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list List-Id: <debbugs-submit.debbugs.gnu.org> List-Unsubscribe: <https://debbugs.gnu.org/cgi-bin/mailman/options/debbugs-submit>, <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=unsubscribe> List-Archive: <https://debbugs.gnu.org/cgi-bin/mailman/private/debbugs-submit/> List-Post: <mailto:debbugs-submit <at> debbugs.gnu.org> List-Help: <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=help> List-Subscribe: <https://debbugs.gnu.org/cgi-bin/mailman/listinfo/debbugs-submit>, <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=subscribe> Errors-To: debbugs-submit-bounces <at> debbugs.gnu.org Sender: "Debbugs-submit" <debbugs-submit-bounces <at> debbugs.gnu.org> 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: | <p class=3D"class1 class12">=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))) --=-=-=--
Content-Disposition: inline Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 X-Mailer: MIME-tools 5.505 (Entity 5.505) Content-Type: text/plain; charset=utf-8 X-Loop: help-debbugs@HIDDEN From: help-debbugs@HIDDEN (GNU bug Tracking System) To: Tim Landscheidt <tim@HIDDEN> Subject: bug#63342: Acknowledgement (28.2; dom-by-class does not handle nodes with multiple classes properly) Message-ID: <handler.63342.B.16834305448984.ack <at> debbugs.gnu.org> References: <875y9495kk.fsf@HIDDEN> X-Gnu-PR-Message: ack 63342 X-Gnu-PR-Package: emacs X-Gnu-PR-Keywords: patch Reply-To: 63342 <at> debbugs.gnu.org Date: Sun, 07 May 2023 03:36:02 +0000 Thank you for filing a new bug report with debbugs.gnu.org. This is an automatically generated reply to let you know your message has been received. Your message is being forwarded to the package maintainers and other interested parties for their attention; they will reply in due course. Your message has been sent to the package maintainer(s): bug-gnu-emacs@HIDDEN If you wish to submit further information on this problem, please send it to 63342 <at> debbugs.gnu.org. Please do not send mail to help-debbugs@HIDDEN unless you wish to report a problem with the Bug-tracking system. --=20 63342: https://debbugs.gnu.org/cgi/bugreport.cgi?bug=3D63342 GNU Bug Tracking System Contact help-debbugs@HIDDEN with problems
Received: (at control) by debbugs.gnu.org; 6 Sep 2023 20:34:39 +0000 From debbugs-submit-bounces <at> debbugs.gnu.org Wed Sep 06 16:34:39 2023 Received: from localhost ([127.0.0.1]:37969 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from <debbugs-submit-bounces <at> debbugs.gnu.org>) id 1qdzEV-00078E-Dm for submit <at> 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 <stefankangas@HIDDEN>) id 1qdzEU-00077z-8Q for control <at> 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 <control <at> debbugs.gnu.org>; 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 <stefankangas@HIDDEN> MIME-Version: 1.0 Date: Wed, 6 Sep 2023 13:34:30 -0700 Message-ID: <CADwFkmmw10qWNbxzdwLT52TwbhUO=40HFKjmib_ZMTtz6jMjnA@HIDDEN> Subject: control message for bug #63342 To: control <at> debbugs.gnu.org Content-Type: text/plain; charset="UTF-8" X-Spam-Score: 0.0 (/) X-Debbugs-Envelope-To: control X-BeenThere: debbugs-submit <at> debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list List-Id: <debbugs-submit.debbugs.gnu.org> List-Unsubscribe: <https://debbugs.gnu.org/cgi-bin/mailman/options/debbugs-submit>, <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=unsubscribe> List-Archive: <https://debbugs.gnu.org/cgi-bin/mailman/private/debbugs-submit/> List-Post: <mailto:debbugs-submit <at> debbugs.gnu.org> List-Help: <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=help> List-Subscribe: <https://debbugs.gnu.org/cgi-bin/mailman/listinfo/debbugs-submit>, <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=subscribe> Errors-To: debbugs-submit-bounces <at> debbugs.gnu.org Sender: "Debbugs-submit" <debbugs-submit-bounces <at> debbugs.gnu.org> X-Spam-Score: -1.0 (-) tags 63342 + pending quit
GNU bug tracking system
Copyright (C) 1999 Darren O. Benham,
1997 nCipher Corporation Ltd,
1994-97 Ian Jackson.