From a5f12dda21eed31eeadf9af5a8c0dacd912dfbc6 Mon Sep 17 00:00:00 2001 From: Happy Kumar Mittal Date: Wed, 29 Jan 2025 12:30:19 +0530 Subject: [PATCH] handle attribute with text equal to single quote (i.e. ") (#1726) Attribute method is not able to handle the string with a single quote. We trim the quotes from the string without checking its length, and it's causing an issue if string has only one character. e.g. val=" --- .../src/main/java/org/eclipse/lemminx/dom/DOMNode.java | 6 ++++-- .../test/java/org/eclipse/lemminx/dom/DOMDocumentTest.java | 6 ++++++ 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/org.eclipse.lemminx/src/main/java/org/eclipse/lemminx/dom/DOMNode.java b/org.eclipse.lemminx/src/main/java/org/eclipse/lemminx/dom/DOMNode.java index 4bec436c1..504c18033 100644 --- a/org.eclipse.lemminx/src/main/java/org/eclipse/lemminx/dom/DOMNode.java +++ b/org.eclipse.lemminx/src/main/java/org/eclipse/lemminx/dom/DOMNode.java @@ -392,7 +392,9 @@ public String getAttribute(String name) { // remove quote char c = value.charAt(0); if (c == '"' || c == '\'') { - if (value.charAt(value.length() - 1) == c) { + if (value.length() == 1) { + return value; + } else if (value.charAt(value.length() - 1) == c) { return value.substring(1, value.length() - 1); } return value.substring(1, value.length()); @@ -943,4 +945,4 @@ public Object setUserData(String arg0, Object arg1, UserDataHandler arg2) { return null; } -} \ No newline at end of file +} diff --git a/org.eclipse.lemminx/src/test/java/org/eclipse/lemminx/dom/DOMDocumentTest.java b/org.eclipse.lemminx/src/test/java/org/eclipse/lemminx/dom/DOMDocumentTest.java index d06a4e313..f786c96e7 100644 --- a/org.eclipse.lemminx/src/test/java/org/eclipse/lemminx/dom/DOMDocumentTest.java +++ b/org.eclipse.lemminx/src/test/java/org/eclipse/lemminx/dom/DOMDocumentTest.java @@ -252,4 +252,10 @@ public void noDefaultNamespaceURI() { assertEquals("http://camel.apache.org/schema/spring", camel.getNamespaceURI()); } + + @Test + void findElementWithSingleQuoteValue() { + DOMDocument document = DOMParser.getInstance().parse("", "test", null); + assertEquals("\"", document.getChild(0).getAttribute("attr")); + } }