Comparing version 2.1.0 to 2.2.0
@@ -15,3 +15,3 @@ const mismatch = require('mismatch'); | ||
* Extract member elements from an XML string. Numbers and booleans will be parsed into their JS types. | ||
* @param {string} tag Which tag to extract, e.g., `div`. | ||
* @param {string|!Array<string>} tag Which tag to extract, e.g., `div`. Can also pass an array of tags, in which case the name of the tag will also be returned. | ||
* @param {string} string The XML string. | ||
@@ -21,8 +21,8 @@ * @example | ||
* const xml = ` | ||
<html> | ||
<div id="1" class="test" contenteditable> | ||
Hello World | ||
</div> | ||
</html> | ||
` | ||
* <html> | ||
* <div id="1" class="test" contenteditable> | ||
* Hello World | ||
* </div> | ||
* </html> | ||
* ` | ||
* const [{ content, props }] = extractTag('div', xml) | ||
@@ -33,11 +33,13 @@ * // content: Hello World | ||
const extractTags = (tag, string) => { | ||
const tags = Array.isArray(tag) ? tag : [tag] | ||
const t = tags.join('|') | ||
const end1 = /\s*\/>/ | ||
const end2 = new RegExp(`>([\\s\\S]+?)?</${tag}>`) | ||
const re = new RegExp(`<${tag}${simple.source}?(?:${end1.source}|${end2.source})`, 'g') | ||
const end2 = />([\s\S]+?)?<\/\1>/ | ||
const re = new RegExp(`<(${t})${simple.source}?(?:${end1.source}|${end2.source})`, 'g') | ||
const matches = mismatch(re, string, ['a', 'v', 'v1', 'v2', 'c']) | ||
const res = matches.map(({ 'a': attributes = '', 'c': content = '' }) => { | ||
const matches = mismatch(re, string, ['t', 'a', 'v', 'v1', 'v2', 'c']) | ||
const res = matches.map(({ 't': tagName, 'a': attributes = '', 'c': content = '' }) => { | ||
const attrs = attributes.replace(/\/$/, '').trim() | ||
const props = extractProps(attrs) | ||
return { content, props } | ||
return { content, props, tag: tagName } | ||
}) | ||
@@ -44,0 +46,0 @@ return res |
@@ -9,3 +9,3 @@ const nameRe = /([^\s>=/]+)/ | ||
*/ | ||
const attributesRe = new RegExp(`\\s*((?:${attributeRe.source}\\s*)*)`) | ||
const attributesRe = new RegExp(`(?:\\s+((?:${attributeRe.source}\\s*)*))`) | ||
@@ -12,0 +12,0 @@ |
@@ -0,1 +1,7 @@ | ||
## 8 August 2019 | ||
### [2.2.0](https://github.com/artdecocode/rexml/compare/v2.1.0...v2.2.0) | ||
- [feature] Extract multiple tags at the same time. | ||
## 3 August 2019 | ||
@@ -2,0 +8,0 @@ |
{ | ||
"name": "rexml", | ||
"version": "2.1.0", | ||
"version": "2.2.0", | ||
"description": "Simple XML parsing with a regular expression.", | ||
@@ -8,7 +8,7 @@ "main": "build/index.js", | ||
"scripts": { | ||
"t": "zoroaster -b -a", | ||
"t": "zoroaster -a", | ||
"test": "yarn t test/spec", | ||
"test-build": "ALAMODE_ENV=test-build yarn test", | ||
"lint": "eslint .", | ||
"doc": "NODE_DEBUG=doc doc -o README.md", | ||
"doc": "NODE_DEBUG=doc doc -o README.md -n _rexml", | ||
"e": "alanode", | ||
@@ -46,8 +46,5 @@ "externs": "typal types/externs.js", | ||
"devDependencies": { | ||
"@babel/cli": "7.5.5", | ||
"@babel/core": "7.5.5", | ||
"@babel/plugin-proposal-unicode-property-regex": "7.4.4", | ||
"@babel/register": "7.5.5", | ||
"alamode": "^2.3.6", | ||
"documentary": "^1.29.0", | ||
"alamode": "^2.4.0", | ||
"documentary": "^1.31.0", | ||
"eslint-config-artdeco": "1.0.1", | ||
@@ -54,0 +51,0 @@ "yarn-s": "1.1.0", |
113
README.md
@@ -19,3 +19,5 @@ # rexml | ||
- [API](#api) | ||
* [`rexml(tag: string, string: string): {content, props}[]`](#rexmltag-stringstring-string-content-props) | ||
* [`extractTags(tag, string): Return`](#extracttagstag-stringarraystringstring-string-return) | ||
* [`Return`](#type-return) | ||
* [Extracting Multiple Tags](#extracting-multiple-tags) | ||
* [`extractProps(string: string, parseValue?: boolean): Object<string,(boolean|string|number)>`](#extractpropsstring-stringparsevalue-boolean-objectstringbooleanstringnumber) | ||
@@ -41,8 +43,15 @@ * [`extractTagsSpec(tag: string, string: string): {content, props}[]`](#extracttagsspectag-stringstring-string-content-props) | ||
### `rexml(`<br/> `tag: string,`<br/> `string: string,`<br/>`): {content, props}[]` | ||
### <code><ins>extractTags</ins>(</code><sub><br/> `tag: string|!Array<string>,`<br/> `string: string,`<br/></sub><code>): <i>Return</i></code> | ||
Extract member elements from an XML string. Numbers and booleans will be parsed into their JS types. | ||
Extract tags from the XML string. The tags are returned as an array with objects containing `content` and `props` properties. The content is the inner content of the tag, and `props` is the attributes specified inside the tag. | ||
- <kbd><strong>tag*</strong></kbd> <em><code>(string \| !Array<string>)</code></em>: Which tag to extract, e.g., `div`. Can also pass an array of tags, in which case the name of the tag will also be returned. | ||
- <kbd><strong>string*</strong></kbd> <em>`string`</em>: The XML string. | ||
```javascript | ||
/* yarn example/ */ | ||
The tags are returned as an array with objects containing `content` and `props` properties. The content is the inner content of the tag, and `props` is the attributes specified inside the tag. | ||
<table> | ||
<tr><th><a href="example/index.js">Source</a></th><th>Output</th></tr> | ||
<tr><td> | ||
```js | ||
import extractTags from 'rexml' | ||
@@ -60,30 +69,76 @@ | ||
const res = extractTags('div', xml) | ||
``` | ||
</td> | ||
<td> | ||
console.log(JSON.stringify(res, null, 2)) | ||
```js | ||
[ { content: '', | ||
props: | ||
{ id: 'd1', | ||
class: 'example', | ||
contenteditable: true }, | ||
tag: 'div' }, | ||
{ content: 'Hello World', | ||
props: { id: 'd2', class: 'example' }, | ||
tag: 'div' } ] | ||
``` | ||
```json | ||
[ | ||
{ | ||
"content": "", | ||
"props": { | ||
"id": "d1", | ||
"class": "example", | ||
"contenteditable": true | ||
} | ||
}, | ||
{ | ||
"content": "Hello World", | ||
"props": { | ||
"id": "d2", | ||
"class": "example" | ||
} | ||
} | ||
] | ||
</td></tr> | ||
</table> | ||
__<a name="type-return">`Return`</a>__: The return type. | ||
| Name | Type | Description | | ||
| ------------ | --------------------------- | ------------------------------------------------------ | | ||
| __content*__ | <em>string</em> | The content of the tag, including possible whitespace. | | ||
| __props*__ | <em>!Object<string, ?></em> | The properties of the element. | | ||
| __tag*__ | <em>string</em> | The name of the extracted element. | | ||
<p align="center"><a href="#table-of-contents"> | ||
<img src="/.documentary/section-breaks/3.svg?sanitize=true" width="15"> | ||
</a></p> | ||
#### Extracting Multiple Tags | ||
It's possible to give an array of tags which should be extracted from the _XML_ string. | ||
<table> | ||
<tr><th><a href="example/array.js">Source</a></th><th>Output</th></tr> | ||
<tr><td> | ||
```js | ||
import extractTags from 'rexml' | ||
const xml = `<html> | ||
<div id="d1"/> | ||
<div id="d2" class="example">Hello World</div> | ||
<footer>Art Deco, 2019</footer> | ||
</html> | ||
` | ||
const res = extractTags(['div', 'footer'], xml) | ||
``` | ||
</td> | ||
<td> | ||
```js | ||
[ { content: '', | ||
props: { id: 'd1' }, | ||
tag: 'div' }, | ||
{ content: 'Hello World', | ||
props: { id: 'd2', class: 'example' }, | ||
tag: 'div' }, | ||
{ content: 'Art Deco, 2019', | ||
props: {}, | ||
tag: 'footer' } ] | ||
``` | ||
</td></tr> | ||
</table> | ||
<p align="center"><a href="#table-of-contents"> | ||
<img src="/.documentary/section-breaks/3.svg?sanitize=true" width="25"> | ||
<img src="/.documentary/section-breaks/4.svg?sanitize=true" width="25"> | ||
</a></p> | ||
### `extractProps(`<br/> `string: string,`<br/> `parseValue?: boolean,`<br/>`): Object<string,(boolean|string|number)>` | ||
### <code><ins>extractProps</ins>(</code><sub><br/> `string: string,`<br/> `parseValue?: boolean,`<br/></sub><code>): <i>Object<string,(boolean|string|number)></i></code> | ||
@@ -153,6 +208,6 @@ Extracts the properties from the attributes part of the tag and returns them as an object. It will parse values if not specified otherwise. | ||
<p align="center"><a href="#table-of-contents"> | ||
<img src="/.documentary/section-breaks/4.svg?sanitize=true" width="25"> | ||
<img src="/.documentary/section-breaks/5.svg?sanitize=true" width="25"> | ||
</a></p> | ||
### `extractTagsSpec(`<br/> `tag: string,`<br/> `string: string,`<br/>`): {content, props}[]` | ||
### <code><ins>extractTagsSpec</ins>(</code><sub><br/> `tag: string,`<br/> `string: string,`<br/></sub><code>): <i>{content, props}[]</i></code> | ||
@@ -188,3 +243,3 @@ Same as the default method, but confirms to the XML specification in defining attributes. | ||
<p align="center"><a href="#table-of-contents"> | ||
<img src="/.documentary/section-breaks/5.svg?sanitize=true"> | ||
<img src="/.documentary/section-breaks/6.svg?sanitize=true"> | ||
</a></p> | ||
@@ -191,0 +246,0 @@ |
@@ -15,3 +15,3 @@ import mismatch from 'mismatch' | ||
* Extract member elements from an XML string. Numbers and booleans will be parsed into their JS types. | ||
* @param {string} tag Which tag to extract, e.g., `div`. | ||
* @param {string|!Array<string>} tag Which tag to extract, e.g., `div`. Can also pass an array of tags, in which case the name of the tag will also be returned. | ||
* @param {string} string The XML string. | ||
@@ -21,8 +21,8 @@ * @example | ||
* const xml = ` | ||
<html> | ||
<div id="1" class="test" contenteditable> | ||
Hello World | ||
</div> | ||
</html> | ||
` | ||
* <html> | ||
* <div id="1" class="test" contenteditable> | ||
* Hello World | ||
* </div> | ||
* </html> | ||
* ` | ||
* const [{ content, props }] = extractTag('div', xml) | ||
@@ -33,11 +33,13 @@ * // content: Hello World | ||
const extractTags = (tag, string) => { | ||
const tags = Array.isArray(tag) ? tag : [tag] | ||
const t = tags.join('|') | ||
const end1 = /\s*\/>/ | ||
const end2 = new RegExp(`>([\\s\\S]+?)?</${tag}>`) | ||
const re = new RegExp(`<${tag}${simple.source}?(?:${end1.source}|${end2.source})`, 'g') | ||
const end2 = />([\s\S]+?)?<\/\1>/ | ||
const re = new RegExp(`<(${t})${simple.source}?(?:${end1.source}|${end2.source})`, 'g') | ||
const matches = mismatch(re, string, ['a', 'v', 'v1', 'v2', 'c']) | ||
const res = matches.map(({ 'a': attributes = '', 'c': content = '' }) => { | ||
const matches = mismatch(re, string, ['t', 'a', 'v', 'v1', 'v2', 'c']) | ||
const res = matches.map(({ 't': tagName, 'a': attributes = '', 'c': content = '' }) => { | ||
const attrs = attributes.replace(/\/$/, '').trim() | ||
const props = extractProps(attrs) | ||
return { content, props } | ||
return { content, props, tag: tagName } | ||
}) | ||
@@ -44,0 +46,0 @@ return res |
@@ -9,2 +9,2 @@ const nameRe = /([^\s>=/]+)/ | ||
*/ | ||
export const attributesRe = new RegExp(`\\s*((?:${attributeRe.source}\\s*)*)`) | ||
export const attributesRe = new RegExp(`(?:\\s+((?:${attributeRe.source}\\s*)*))`) |
@@ -10,4 +10,4 @@ /** | ||
* The return type. | ||
* @typedef {{ content: string, props: !Object<string, ?> }} | ||
* @typedef {{ content: string, props: !Object<string, ?>, tag: string }} | ||
*/ | ||
_rexml.Return |
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
55189
6
431
265