Skip to content

Commit

Permalink
Merge pull request #523 from mathjax/liteDOM-update
Browse files Browse the repository at this point in the history
Fix LiteDOM comments and add support for DOCTYPE
  • Loading branch information
dpvc authored Jul 23, 2020
2 parents 15f82e1 + dfcac38 commit 6bf0ac0
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 4 deletions.
8 changes: 8 additions & 0 deletions ts/adaptors/HTMLAdaptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ export interface MinDocument<N, T> {
head: N;
body: N;
title: string;
doctype: {name: string};
/* tslint:disable:jsdoc-require */
createElement(kind: string): N;
createElementNS(ns: string, kind: string): N;
Expand Down Expand Up @@ -228,6 +229,13 @@ AbstractDOMAdaptor<N, T, D> implements MinHTMLAdaptor<N, T, D> {
return doc.documentElement;
}

/**
* @override
*/
public doctype(doc: D) {
return `<!DOCTYPE ${doc.doctype.name}>`;
}

/**
* @override
*/
Expand Down
6 changes: 6 additions & 0 deletions ts/adaptors/lite/Document.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ export class LiteDocument {
*/
public body: LiteElement;

/**
* the DOCTYPE comment
*/
public type: string;

/**
* The kind is always #document
*/
Expand All @@ -56,5 +61,6 @@ export class LiteDocument {
this.head = new LiteElement('head'),
this.body = new LiteElement('body')
]);
this.type = '<!DOCTYPE html>';
}
}
14 changes: 11 additions & 3 deletions ts/adaptors/lite/Parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ export namespace PATTERNS {
export const OPTIONALSPACE = '(?:\\s|\\n)*';
export const ATTRIBUTE = ATTNAME + '(?:' + OPTIONALSPACE + '=' + OPTIONALSPACE + VALUE + ')?';
export const ATTRIBUTESPLIT = '(' + ATTNAME + ')(?:' + OPTIONALSPACE + '=' + OPTIONALSPACE + VALUESPLIT + ')?';
export const TAG = '(<(?:' + TAGNAME + '(?:' + SPACE + ATTRIBUTE + ')*' +
OPTIONALSPACE + '/?|/' + TAGNAME + '|!--[^]*?--|![^]*?)(?:>|$))';
export const TAG = '(<(?:' + TAGNAME + '(?:' + SPACE + ATTRIBUTE + ')*'
+ OPTIONALSPACE + '/?|/' + TAGNAME + '|!--[^]*?--|![^]*?)(?:>|$))';
export const tag = new RegExp(TAG, 'i');
export const attr = new RegExp(ATTRIBUTE, 'i');
export const attrsplit = new RegExp(ATTRIBUTESPLIT, 'i');
Expand Down Expand Up @@ -277,6 +277,14 @@ export class LiteParser implements MinDOMParser<LiteDocument> {
protected checkDocument(adaptor: LiteAdaptor, root: LiteDocument) {
let node = this.getOnlyChild(adaptor, adaptor.body(root));
if (!node) return;
for (const child of adaptor.childNodes(adaptor.body(root))) {
if (child === node) {
break;
}
if (child instanceof LiteComment && child.value.match(/^<!DOCTYPE/)) {
root.type = child.value;
}
}
switch (adaptor.kind(node)) {
case 'html':
//
Expand Down Expand Up @@ -365,7 +373,7 @@ export class LiteParser implements MinDOMParser<LiteDocument> {
return adaptor.childNodes(node).map(x => {
const kind = adaptor.kind(x);
return (kind === '#text' ? this.protectHTML(adaptor.value(x)) :
kind === '#comment' ? adaptor.value(x) :
kind === '#comment' ? (x as LiteComment).value :
this.serialize(adaptor, x as LiteElement));
}).join('');
}
Expand Down
10 changes: 9 additions & 1 deletion ts/adaptors/liteAdaptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,13 @@ export class LiteAdaptor extends AbstractDOMAdaptor<LiteElement, LiteText, LiteD
return doc.root;
}

/**
* @override
*/
public doctype(doc: LiteDocument) {
return doc.type;
}

/**
* @override
*/
Expand Down Expand Up @@ -402,7 +409,8 @@ export class LiteAdaptor extends AbstractDOMAdaptor<LiteElement, LiteText, LiteD
* @override
*/
public value(node: LiteNode | LiteText) {
return (node.kind === '#text' ? (node as LiteText).value : '');
return (node.kind === '#text' ? (node as LiteText).value :
node.kind === '#comment' ? (node as LiteComment).value.replace(/^<!(--)?((?:.|\n)*)\1>$/, '$2') : '');
}

/**
Expand Down
11 changes: 11 additions & 0 deletions ts/core/DOMAdaptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,12 @@ export interface DOMAdaptor<N, T, D> {
*/
root(doc: D): N;

/**
* @param {D} doc The document whose doctype is to be obtained
* @return {string} The DOCTYPE comment
*/
doctype(doc: D): string;

/**
* @param {N} node The node to search for tags
* @param {string} name The name of the tag to search for
Expand Down Expand Up @@ -436,6 +442,11 @@ export abstract class AbstractDOMAdaptor<N, T, D> implements DOMAdaptor<N, T, D>
*/
public abstract root(doc: D): N;

/**
* @override
*/
public abstract doctype(doc: D): string;

/**
* @override
*/
Expand Down

0 comments on commit 6bf0ac0

Please sign in to comment.