Browse Source

Add `doctype` property

pull/5895/head
Artur Arseniev 2 years ago
parent
commit
002801a260
  1. 2
      src/common/index.ts
  2. 6
      src/dom_components/model/ComponentWrapper.ts
  3. 9
      src/parser/config/config.ts
  4. 2
      test/specs/dom_components/index.ts

2
src/common/index.ts

@ -27,7 +27,7 @@ export type Nullable = undefined | null | false;
export interface OptionAsDocument {
/**
* Treat the HTML string as document (option valid on the root component, eg. will include `<head>`, `<html>`, etc.).
* Treat the HTML string as document (option valid on the root component, eg. will include doctype, html, head, etc.).
*/
asDocument?: boolean;
}

6
src/dom_components/model/ComponentWrapper.ts

@ -55,8 +55,12 @@ export default class ComponentWrapper extends Component {
return this.get('docEl');
}
get doctype(): string {
return this.attributes.doctype || '';
}
toHTML(opts: ToHTMLOptions = {}) {
const { doctype = '' } = this.attributes;
const { doctype } = this;
const asDoc = !isUndefined(opts.asDocument) ? opts.asDocument : !!doctype;
const { head, docEl } = this;
const body = super.toHTML(opts);

9
src/parser/config/config.ts

@ -1,3 +1,4 @@
import { OptionAsDocument } from '../../common';
import { CssRuleJSON } from '../../css_composer/model/CssRule';
import { ComponentDefinitionDefined } from '../../dom_components/model/types';
import Editor from '../../editor';
@ -26,7 +27,7 @@ export interface ParseNodeOptions extends HTMLParserOptions {
skipChildren?: boolean;
}
export interface HTMLParserOptions {
export interface HTMLParserOptions extends OptionAsDocument {
/**
* DOMParser mime type.
* If you use the `text/html` parser, it will fix the invalid syntax automatically.
@ -35,12 +36,6 @@ export interface HTMLParserOptions {
*/
htmlType?: DOMParserSupportedType;
/**
* Parse the string as HTML document. The result will include additional data (eg. doctype, head, etc.)
* @default false
*/
asDocument?: boolean;
/**
* Allow <script> tags.
* @default false

2
test/specs/dom_components/index.ts

@ -375,7 +375,7 @@ describe('DOM Components', () => {
expect(head.get('headp')).toBe(true);
expect(docEl.get('htmlp')).toBe(true);
expect(root.get('bodyp')).toBe(true);
expect(root.get('doctype')).toBe('<!DOCTYPE html>');
expect(root.doctype).toBe('<!DOCTYPE html>');
const outputHtml = `
<!DOCTYPE html>

Loading…
Cancel
Save