[augeas-devel] [PATCH 3/3] Xml: support single _and_ double quoted attribute values

lutter at redhat.com lutter at redhat.com
Tue Oct 23 23:25:29 UTC 2012


From: Francis Giraldeau <francis.giraldeau at gmail.com>

Fixes RHBZ #799885
Fixes ticket #258
---
 lenses/tests/test_xml.aug |   31 ++++++++++++++++++++++++++-----
 lenses/xml.aug            |   22 +++++++++++++++++++---
 2 files changed, 45 insertions(+), 8 deletions(-)

diff --git a/lenses/tests/test_xml.aug b/lenses/tests/test_xml.aug
index 33b41d2..9258ccd 100644
--- a/lenses/tests/test_xml.aug
+++ b/lenses/tests/test_xml.aug
@@ -184,6 +184,21 @@ test Xml.attributes get " refs=\"A1\nA2  A3\"" =
 test Xml.attributes put attr1 after rm "/#attribute[1]";
                                     set "/#attribute/attr2" "foo" = attr2
 
+(* test quoting *)
+(* well formed values *)
+test Xml.attributes get " attr1=\"value1\"" = { "#attribute" { "attr1" = "value1" } }
+test Xml.attributes get " attr1='value1'" = { "#attribute" { "attr1" = "value1" } }
+test Xml.attributes get " attr1='va\"lue1'" = { "#attribute" { "attr1" = "va\"lue1" } }
+test Xml.attributes get " attr1=\"va'lue1\"" = { "#attribute" { "attr1" = "va'lue1" } }
+
+(* illegal as per the XML standard *)
+test Xml.attributes get " attr1=\"va\"lue1\"" = *
+test Xml.attributes get " attr1='va'lue1'" = *
+
+(* malformed values *)
+test Xml.attributes get " attr1=\"value1'" = *
+test Xml.attributes get " attr1='value1\"" = *
+
 (* Group: empty *)
 
 (* Variable: empty1 *)
@@ -622,7 +637,14 @@ test Xml.lns get p10pass1_1 =
     }
   }
 
-test Xml.lns get p10pass1_2 = *
+test Xml.lns get p10pass1_2 =
+  { "doc"
+    { "A" = "#empty"
+      { "#attribute"
+        { "a" = "\"\">'"" }
+      }
+    }
+  }
 
 (* here again, test exclude single quote *)
 let p11pass1 = "<!--Inability to resolve a notation should not be reported as an error-->
@@ -696,9 +718,9 @@ test Xml.lns get p22pass3 =
 
 let p25pass2 = "<?xml version
 
-     
+
 =
-  
+
 
 \"1.0\"?>
 <doc></doc>"
@@ -712,7 +734,7 @@ test Xml.lns get p25pass2 =
   { "doc" }
 
 
-test Xml.lns get "<!DOCTYPE 
+test Xml.lns get "<!DOCTYPE
 
 doc
 
@@ -787,4 +809,3 @@ test Xml.lns get "<!DOCTYPE doc [
 %eldecl;
 ]>
 <doc></doc>" = *
-
diff --git a/lenses/xml.aug b/lenses/xml.aug
index 50b1712..2de1280 100644
--- a/lenses/xml.aug
+++ b/lenses/xml.aug
@@ -24,7 +24,7 @@ let word                = /[a-zA-Z][a-zA-Z0-9._-]*/
 let char                = /.|(\r?\n)/
 (* if we hide the quotes, then we can only accept single or double quotes *)
 (* otherwise a put ambiguity is raised *)
-let sto_dquote          = dels "\"" . store /[^"]*/ . dels "\""
+let sto_dquote          = dels "\"" . store /[^"]*/ . dels "\"" (* " *)
 let sto_squote          = dels "'" . store /[^']*/ . dels "'"
 
 let comment             = [ label "#comment" .
@@ -89,8 +89,24 @@ let decl_outer    = sep_osp . del /\[[ \n\t\r]*/ "[\n" .
 
 let doctype       = decl_def /!DOCTYPE/ (decl_outer|id_def)
 
-let attributes    = [ label "#attribute" .
-                      [ sep_spc . key nmtoken . sep_eq . sto_dquote ]+ ]
+(* General shape of an attribute
+ * q   is the regexp matching the quote character for the value
+ * qd  is the default quote character
+ * brx is what the actual attribute value must match *)
+let attval (q:regexp) (qd:string) (brx:regexp) =
+  let quote = del q qd in
+  let body = store brx in
+  [ sep_spc . key nmtoken . sep_eq . square quote body quote ]
+
+(* We treat attributes according to one of the following three patterns:
+   attval1 : values that must be quoted with single quotes
+   attval2 : values that must be quoted with double quotes
+   attval3 : values that can be quoted with either *)
+let attributes    =
+  let attval1 = attval "'" "'" /[^']*"[^']*/ in (* " *)
+  let attval2 = attval "\"" "\"" /[^"]*'[^"]*/ in
+  let attval3 = attval /['"]/ "\"" /[^'\"]*/ in (* " *)
+  [ label "#attribute" . (attval1|attval2|attval3)+ ]
 
 let prolog        = [ label "#declaration" .
                       dels "<?xml" .
-- 
1.7.7.6




More information about the augeas-devel mailing list