Comparing version 1.0.0 to 1.1.0
@@ -8,2 +8,6 @@ "use strict"; | ||
var _lib = require("./lib"); | ||
var _res = require("./lib/res"); | ||
const execRes = (re, s) => { | ||
@@ -15,37 +19,2 @@ const res = re.exec(s); | ||
}; | ||
const extractProps = s => { | ||
const propsRe = /(\w+)="(.*?)"/g; | ||
let t; | ||
const r = []; | ||
while ((t = propsRe.exec(s)) !== null) { | ||
const [, key, value] = t; | ||
r.push({ | ||
key, | ||
value: getPropValue(value) | ||
}); | ||
} | ||
const tt = s.replace(propsRe, '').trim().split(' ').filter(a => a).reduce((a, k) => ({ ...a, | ||
[k]: true | ||
}), {}); | ||
return r.reduce((acc, { | ||
key, | ||
value | ||
}) => ({ ...acc, | ||
[key]: value | ||
}), tt); | ||
}; | ||
const getPropValue = val => { | ||
if (val == 'true') return true; | ||
if (val == 'false') return false; | ||
if (/^\d+$/.test(val)) { | ||
return parseInt(val, 10); | ||
} | ||
return val; | ||
}; | ||
/** | ||
@@ -71,3 +40,5 @@ * Extract member elements from an XML string. Numbers and booleans will be parsed into their JS types. | ||
const extractTags = (tag, string) => { | ||
const re = new RegExp(`<${tag}( .[^>]+)?(?: /)?>(?:([\\s\\S]+?)</${tag}>)?`, 'g'); | ||
const end1 = /\s*\/>/; | ||
const end2 = new RegExp(`>([\\s\\S]+?)?</${tag}>`); | ||
const re = new RegExp(`<${tag}${_res.attributesRe.source}?(?:${end1.source}|${end2.source})`, 'gu'); | ||
const r = []; | ||
@@ -82,3 +53,3 @@ let t; | ||
const props = extractProps(p); | ||
const props = (0, _lib.extractProps)(p); | ||
const item = { | ||
@@ -85,0 +56,0 @@ props, |
@@ -0,1 +1,10 @@ | ||
## 19 July 2018 | ||
### 1.1.0 | ||
- [fix] Parse inverted quotes (e.g., `attr="'hello' world"`). | ||
- [fix] Extract attributes with unicode regular expression. | ||
- [fix] Handle tags without attributes or content. | ||
- [fix] Process multiple self-closing tags. | ||
## 18 July 2018 | ||
@@ -2,0 +11,0 @@ |
{ | ||
"name": "rexml", | ||
"version": "1.0.0", | ||
"version": "1.1.0", | ||
"description": "Simple XML parsing with a regular expression.", | ||
@@ -37,2 +37,3 @@ "main": "build", | ||
"devDependencies": { | ||
"@babel/plugin-proposal-unicode-property-regex": "7.0.0-beta.54", | ||
"documentary": "1.8.2", | ||
@@ -43,2 +44,2 @@ "eslint-config-artdeco": "1.0.1", | ||
} | ||
} | ||
} |
@@ -35,25 +35,32 @@ # rexml | ||
<html> | ||
<div id="1" class="test" contenteditable> | ||
Hello World | ||
</div> | ||
<div id="d1" | ||
class="example" | ||
contenteditable /> | ||
<div id="d2" class="example">Hello World</div> | ||
</html> | ||
` | ||
const [{ content, props }] = extractTags('div', xml) | ||
const res = extractTags('div', xml) | ||
console.log(JSON.stringify({ | ||
content, | ||
props, | ||
}, null, 2)) | ||
console.log(JSON.stringify(res, null, 2)) | ||
``` | ||
```json | ||
{ | ||
"content": "\n Hello World\n ", | ||
"props": { | ||
"contenteditable": true, | ||
"id": 1, | ||
"class": "test" | ||
[ | ||
{ | ||
"props": { | ||
"id": "d1", | ||
"class": "example", | ||
"contenteditable": true | ||
}, | ||
"content": "" | ||
}, | ||
{ | ||
"props": { | ||
"id": "d2", | ||
"class": "example" | ||
}, | ||
"content": "Hello World" | ||
} | ||
} | ||
] | ||
``` | ||
@@ -60,0 +67,0 @@ |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
28114
10
159
72
5