Skip to content

Streams support (pt. 2) #177

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 14 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"env": {
"browser": true,
"node": true,
"es2021": true
},
"extends": "eslint:recommended",
Expand All @@ -9,5 +10,6 @@
"sourceType": "module"
},
"rules": {
}
},
"ignorePatterns": ["worker.js"]
}
2 changes: 0 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@

<sup>**Social Media Photo by [JJ Ying](https://unsplash.com/@jjying) on [Unsplash](https://unsplash.com/)**</sup>

### This is not a crawler!

LinkeDOM is a [triple-linked list](#data-structure) based DOM-like namespace, for DOM-less environments, with the following goals:

* **avoid** maximum callstack/recursion or **crashes**, even under heaviest conditions.
Expand Down
5 changes: 3 additions & 2 deletions cjs/dom/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,11 @@ class DOMParser {

/** @typedef {{ "text/html": HTMLDocument, "image/svg+xml": SVGDocument, "text/xml": XMLDocument }} MimeToDoc */
/**
* @template {string|NodeJS.ReadableStream} INPUT
* @template {keyof MimeToDoc} MIME
* @param {string} markupLanguage
* @param {INPUT} markupLanguage
* @param {MIME} mimeType
* @returns {MimeToDoc[MIME]}
* @returns {INPUT extends string ? MimeToDoc[MIME] : Promise<MimeToDoc[MIME]>}
*/
parseFromString(markupLanguage, mimeType, globals = null) {
let isHTML = false, document;
Expand Down
135 changes: 135 additions & 0 deletions cjs/dom/stream.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
'use strict';
const {Writable} = require('stream');
const {WritableStream} = require('htmlparser2/lib/WritableStream');

const {
onprocessinginstruction, onopentag, oncomment, ontext, onclosetag
} = require('../shared/parser-handlers')

const {HTMLDocument} = require('../html/document.js');
const {SVGDocument} = require('../svg/document.js');
const {XMLDocument} = require('../xml/document.js');


/**
* @typedef {import('../interface/node').Node} Node
* @typedef {import('../svg/element').SVGElement} SVGElement
* @typedef {{ "text/html": HTMLDocument, "image/svg+xml": SVGDocument, "text/xml": XMLDocument }} MimeToDoc
*/

/**
* @template {keyof MimeToDoc} MIME
* @extends {Writable}
*/
class DOMStream extends Writable {
/**
* @param {MIME} mimeType
* @param {(name: string, attributes: Record<string, string>) => boolean} filter
*/
constructor (mimeType, filter) {
super();
this.mimeType = mimeType;
if (mimeType === 'text/html') this.isHTML = true;
this.filter = filter;
/**
* @type {{
* document: MimeToDoc[MIME]
* node: MimeToDoc[MIME]|Node
* ownerSVGElement: SVGElement|undefined
* rootNode: Node|undefined
* }[]}
*/
this.stack = []; // LIFO
this.init();
}

newDocument () {
let document;
if (this.mimeType === 'text/html') {
document = new HTMLDocument();
} else if (this.mimeType === 'image/svg+xml') {
document = new SVGDocument();
} else {
document = new XMLDocument();
}
if (this.doctype) document.doctype = this.doctype;
this.stack.push({ document, node: document });
}

init () {
this.parserStream = new WritableStream({
// <!DOCTYPE ...>
onprocessinginstruction: (name, data) => {
this.doctype = onprocessinginstruction(name, data);
},
// <tagName>
onopentag: (name, attributes) => {
if (this.filter(name, attributes)) this.newDocument();
for (const item of this.stack) {
onopentag(name, attributes, item, this.isHTML);
}
},
// #text, #comment
oncomment: (data) => {
for (const item of this.stack) {
oncomment(data, item);
}
},
ontext: (text) => {
for (const item of this.stack) {
ontext(text, item);
}
},
// </tagName>
onclosetag: () => {
for (const item of this.stack) {
onclosetag(item, this.isHTML, document => {
this.emit('document', document);
this.stack.length -= 1;
})
}
}
}, {
lowerCaseAttributeNames: false,
decodeEntities: true,
xmlMode: !this.isHTML
})
this.parserStream.on('error', err => this.emit('error', err))
}

/**
* @param {string|Buffer} chunk
* @param {string} encoding
* @param {() => void} callback
*/
_write(chunk, encoding, callback) {
this.parserStream._write(chunk, encoding, callback);
}

/**
* @param {() => void} callback
*/
_final(callback) {
this.parserStream._final(callback);
}

/**
* An alias for `docStream.on('document', doc => {...})`
* @param {(doc: MimeToDoc[MIME]) => void} listener
*/
ondocument (listener) {
this.on('document', listener)
return this
}

/**
* An alias for `docStream.on('error', err => {...})`
* or `docStream.parserStream.on('error', err => {...})`
* @param {(err: Error) => void} listener
*/
onerror (listener) {
this.on('error', listener)
return this
}
}
exports.DOMStream = DOMStream
2 changes: 2 additions & 0 deletions cjs/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
'use strict';
const {DOMParser} = require('./dom/parser.js');
const {DOMStream} = require('./dom/stream.js');
const {Document: _Document} = require('./interface/document.js');

const {illegalConstructor} = require('./shared/facades.js');
Expand All @@ -15,6 +16,7 @@ const {setPrototypeOf} = require('./shared/object.js');
(require('./shared/html-classes.js'));

exports.DOMParser = DOMParser;
exports.DOMStream = DOMStream;

(m => {
exports.CustomEvent = m.CustomEvent;
Expand Down
137 changes: 58 additions & 79 deletions cjs/shared/parse-from-string.js
Original file line number Diff line number Diff line change
@@ -1,114 +1,93 @@
'use strict';
const HTMLParser2 = require('htmlparser2');
const {WritableStream} = require('htmlparser2/lib/WritableStream');

const {ELEMENT_NODE, SVG_NAMESPACE} = require('./constants.js');
const {CUSTOM_ELEMENTS, PREV, END, VALUE} = require('./symbols.js');
const {keys} = require('./object.js');
const {
onprocessinginstruction, onopentag, oncomment, ontext, onclosetag
} = require('../shared/parser-handlers')

const {knownBoundaries, knownSiblings} = require('./utils.js');
const {attributeChangedCallback, connectedCallback} = require('../interface/custom-element-registry.js');

const {Parser} = HTMLParser2;

// import {Mime} from './mime.js';
// const VOID_SOURCE = Mime['text/html'].voidElements.source.slice(4, -2);
// const VOID_ELEMENTS = new RegExp(`<(${VOID_SOURCE})([^>]*?)>`, 'gi');
// const VOID_SANITIZER = (_, $1, $2) => `<${$1}${$2}${/\/$/.test($2) ? '' : ' /'}>`;
// const voidSanitizer = html => html.replace(VOID_ELEMENTS, VOID_SANITIZER);

let notParsing = true;

const append = (self, node, active) => {
const end = self[END];
node.parentNode = self;
knownBoundaries(end[PREV], node, end);
if (active && node.nodeType === ELEMENT_NODE)
connectedCallback(node);
return node;
};

const attribute = (element, end, attribute, value, active) => {
attribute[VALUE] = value;
attribute.ownerElement = element;
knownSiblings(end[PREV], attribute, end);
if (attribute.name === 'class')
element.className = value;
if (active)
attributeChangedCallback(element, attribute.name, null, value);
};
/**
* @typedef {import('../html/document.js').HTMLDocument} HTMLDocument
* @typedef {import('../svg/document.js').SVGDocument} SVGDocument
* @typedef {import('../xml/document.js').XMLDocument} XMLDocument
*/

let notParsing = true;
const isNotParsing = () => notParsing;
exports.isNotParsing = isNotParsing;

/**
* @template {HTMLDocument|SVGDocument|XMLDocument} DOC
* @template {string|NodeJS.ReadableStream} INPUT
* @param {DOC} document
* @param {Boolean} isHTML
* @param {INPUT} markupLanguage
* @returns {INPUT extends string ? DOC : Promise<INPUT>}
*/
const parseFromString = (document, isHTML, markupLanguage) => {
const {active, registry} = document[CUSTOM_ELEMENTS];

let node = document;
let ownerSVGElement = null;

/**
* @type {{
* document: DOC
* node: DOC|Node
* ownerSVGElement: SVGElement|undefined
* rootNode: Node|undefined
* }}
*/
const item = { document, node: document }
notParsing = false;

const content = new Parser({
const content = new WritableStream({
// <!DOCTYPE ...>
onprocessinginstruction(name, data) {
if (name.toLowerCase() === '!doctype')
document.doctype = data.slice(name.length).trim();
document.doctype = onprocessinginstruction(name, data);
},

// <tagName>
onopentag(name, attributes) {
let create = true;
if (isHTML) {
if (ownerSVGElement) {
node = append(node, document.createElementNS(SVG_NAMESPACE, name), active);
node.ownerSVGElement = ownerSVGElement;
create = false;
}
else if (name === 'svg' || name === 'SVG') {
ownerSVGElement = document.createElementNS(SVG_NAMESPACE, name);
node = append(node, ownerSVGElement, active);
create = false;
}
else if (active) {
const ce = name.includes('-') ? name : (attributes.is || '');
if (ce && registry.has(ce)) {
const {Class} = registry.get(ce);
node = append(node, new Class, active);
delete attributes.is;
create = false;
}
}
}

if (create)
node = append(node, document.createElement(name), false);

let end = node[END];
for (const name of keys(attributes))
attribute(node, end, document.createAttribute(name), attributes[name], active);
onopentag(name, attributes, item, isHTML);
},

// #text, #comment
oncomment(data) { append(node, document.createComment(data), active); },
ontext(text) { append(node, document.createTextNode(text), active); },

oncomment(data) {
oncomment(data, item);
},
ontext(text) {
ontext(text, item);
},
// </tagName>
onclosetag() {
if (isHTML && node === ownerSVGElement)
ownerSVGElement = null;
node = node.parentNode;
onclosetag(item, isHTML)
}
}, {
lowerCaseAttributeNames: false,
decodeEntities: true,
xmlMode: !isHTML
});

content.write(markupLanguage);
content.end();

notParsing = true;

return document;
if (typeof markupLanguage === 'string') {
content.write(markupLanguage);
content.end();
notParsing = true;
return item.document;
} else {
return new Promise((resolve, reject) => {
markupLanguage.pipe(content);
markupLanguage.once('end', () => {
notParsing = true;
resolve(item.document);
});
const errorCb = err => {
content.end();
notParsing = true;
reject(err);
}
markupLanguage.once('error', errorCb);
content.once('error', errorCb);
});
}
};
exports.parseFromString = parseFromString;
Loading