Skip to content

Commit

Permalink
chore: update parse5 (#2940)
Browse files Browse the repository at this point in the history
* update parse5

* fix build

* remove unused event
  • Loading branch information
miherlosev authored Aug 2, 2023
1 parent 30e06f9 commit bf25620
Show file tree
Hide file tree
Showing 14 changed files with 137 additions and 111 deletions.
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
"mustache": "^2.1.1",
"nanoid": "^3.1.12",
"os-family": "^1.0.0",
"parse5": "2.2.3",
"parse5": "^7.1.2",
"pinkie": "2.0.4",
"read-file-relative": "^1.2.0",
"semver": "7.5.3",
Expand All @@ -49,7 +49,6 @@
"@types/lru-cache": "5.1.0",
"@types/mustache": "0.8.32",
"@types/node": "^14.0.27",
"@types/parse5": "2.2.34",
"@types/semver": "^7.5.0",
"@types/tough-cookie": "^4.0.1",
"@types/ws": "^7.4.7",
Expand Down
34 changes: 17 additions & 17 deletions src/processing/dom/base-dom-adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// WARNING: this file is used by both the client and the server.
// Do not use any browser or node-specific API!
// -------------------------------------------------------------
import { ASTNode } from 'parse5';
import { Node } from 'parse5/dist/tree-adapters/default';

export default abstract class BaseDomAdapter {
EVENTS: string[] = ['onblur', 'onchange', 'onclick', 'oncontextmenu', 'oncopy', 'oncut',
Expand All @@ -16,24 +16,24 @@ export default abstract class BaseDomAdapter {
'onmspointerout', 'onmspointerenter', 'onmspointerleave', 'onmsgotpointercapture', 'onmslostpointercapture',
];

abstract removeAttr (el: HTMLElement | ASTNode, attr: string): void;
abstract getAttr (el: HTMLElement | ASTNode, attr: string): string | null;
abstract hasAttr (el: HTMLElement | ASTNode, attr: string): boolean;
abstract isSVGElement (el: HTMLElement | ASTNode): boolean;
abstract hasEventHandler (el: HTMLElement | ASTNode): boolean;
abstract getTagName (el: Element | ASTNode): string;
abstract setAttr (el: HTMLElement | ASTNode, attr: string, value: string): void;
abstract setScriptContent (el: HTMLElement | ASTNode, content: string): void;
abstract getScriptContent (el: HTMLElement | ASTNode): string;
abstract getStyleContent (el: HTMLElement | ASTNode): string;
abstract setStyleContent (el: HTMLElement | ASTNode, content: string): void;
abstract needToProcessContent (el: HTMLElement | ASTNode): boolean;
abstract removeAttr (el: HTMLElement | Node, attr: string): void;
abstract getAttr (el: HTMLElement | Node, attr: string): string | null;
abstract hasAttr (el: HTMLElement | Node, attr: string): boolean;
abstract isSVGElement (el: HTMLElement | Node): boolean;
abstract hasEventHandler (el: HTMLElement | Node): boolean;
abstract getTagName (el: Element | Node): string;
abstract setAttr (el: HTMLElement | Node, attr: string, value: string): void;
abstract setScriptContent (el: HTMLElement | Node, content: string): void;
abstract getScriptContent (el: HTMLElement | Node): string;
abstract getStyleContent (el: HTMLElement | Node): string;
abstract setStyleContent (el: HTMLElement | Node, content: string): void;
abstract needToProcessContent (el: HTMLElement | Node): boolean;
abstract needToProcessUrl (tagName: string, target: string): boolean;
abstract hasIframeParent (el: HTMLElement | ASTNode): boolean;
abstract hasIframeParent (el: HTMLElement | Node): boolean;
abstract getProxyUrl (resourceUrl: string, opts: object): string;
abstract isTopParentIframe (el: HTMLElement | ASTNode): boolean;
abstract isTopParentIframe (el: HTMLElement | Node): boolean;
abstract sameOriginCheck (destUrl: string, resourceUrl: string): boolean;
abstract getClassName (el: Element | ASTNode): string;
abstract isExistingTarget (target: string, el?: HTMLElement | ASTNode): boolean;
abstract getClassName (el: Element | Node): string;
abstract isExistingTarget (target: string, el?: HTMLElement | Node): boolean;
abstract processSrcdocAttr (string: string): string;
}
11 changes: 5 additions & 6 deletions src/processing/dom/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import {
} from './attributes';

import BaseDomAdapter from './base-dom-adapter';
import { ASTNode } from 'parse5';
import { Node } from 'parse5/dist/tree-adapters/default';

const CDATA_REG_EX = /^(\s)*\/\/<!\[CDATA\[([\s\S]*)\/\/\]\]>(\s)*$/;
const HTML_COMMENT_POSTFIX_REG_EX = /(\/\/[^\n]*|\n\s*)-->[^\n]*([\n\s]*)?$/;
Expand Down Expand Up @@ -66,7 +66,6 @@ interface ElementProcessingPattern {
type UrlReplacer = (url: string, resourceType?: string, charset?: string, isCrossDomain?: boolean, isUrlsSet?: boolean) => string;

export default class DomProcessor {
readonly HTML_PROCESSING_REQUIRED_EVENT = 'hammerhead|event|html-processing-required';
SVG_XLINK_HREF_TAGS: string[] = SVG_XLINK_HREF_TAGS;
AUTOCOMPLETE_ATTRIBUTE_ABSENCE_MARKER = AUTOCOMPLETE_ATTRIBUTE_ABSENCE_MARKER;
PROCESSED_PRELOAD_LINK_CONTENT_TYPE = PROCESSED_PRELOAD_LINK_CONTENT_TYPE;
Expand Down Expand Up @@ -137,7 +136,7 @@ export default class DomProcessor {
el[ELEMENT_PROCESSED] = processed;
}

_getRelAttribute (el: HTMLElement | ASTNode): string {
_getRelAttribute (el: HTMLElement | Node): string {
return String(this.adapter.getAttr(el, 'rel')).toLowerCase();
}

Expand Down Expand Up @@ -350,7 +349,7 @@ export default class DomProcessor {
return null;
}

getTargetAttr (el: Element | ASTNode): string | null {
getTargetAttr (el: Element | Node): string | null {
const tagName = this.adapter.getTagName(el);

for (const targetAttr of TARGET_ATTRS) {
Expand All @@ -362,9 +361,9 @@ export default class DomProcessor {
return null;
}

_isOpenLinkInIframe (el: HTMLElement | ASTNode): boolean {
_isOpenLinkInIframe (el: HTMLElement | Node): boolean {
const tagName = this.adapter.getTagName(el);
const targetAttr = this.getTargetAttr(el);
const targetAttr = this.getTargetAttr(el as Node);
const target = targetAttr ? this.adapter.getAttr(el, targetAttr) : null;
const rel = this._getRelAttribute(el);

Expand Down
43 changes: 24 additions & 19 deletions src/processing/dom/parse5-dom-adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,12 @@ import * as urlUtils from '../../utils/url';
import * as parse5Utils from '../../utils/parse5';
import { SVG_NAMESPACE } from './namespaces';
import DomProcessor from './index';
import { ASTNode } from 'parse5';
import {
Node,
Element,
ChildNode,
TextNode,
} from 'parse5/dist/tree-adapters/default';
import pageProcessor from '../resources/page';
import Charset from '../encoding/charset';
import RequestPipelineContext from '../../request-pipeline/context';
Expand All @@ -17,27 +22,27 @@ export default class Parse5DomAdapter extends BaseDomAdapter {
super();
}

removeAttr (el: ASTNode, attr: string): void {
removeAttr (el: Element, attr: string): void {
parse5Utils.removeAttr(el, attr);
}

getAttr (el: ASTNode, attr: string): string | null {
getAttr (el: Node, attr: string): string | null {
return parse5Utils.getAttr(el, attr);
}

getClassName (el: ASTNode): string {
getClassName (el: Node): string {
return this.getAttr(el, 'class') || '';
}

hasAttr (el: ASTNode, attr: string): boolean {
hasAttr (el: Node, attr: string): boolean {
return this.getAttr(el, attr) !== null;
}

isSVGElement (el: ASTNode): boolean {
isSVGElement (el: Element): boolean {
return el.namespaceURI === SVG_NAMESPACE;
}

hasEventHandler (el: ASTNode): boolean {
hasEventHandler (el: Element): boolean {
for (let i = 0; i < el.attrs.length; i++) {
if (this.EVENTS.includes(el.attrs[i].name))
return true;
Expand All @@ -46,28 +51,28 @@ export default class Parse5DomAdapter extends BaseDomAdapter {
return false;
}

getTagName (el: ASTNode): string {
getTagName (el: Element): string {
return (el.tagName || '').toLowerCase();
}

setAttr (el: ASTNode, attr: string, value: string) {
setAttr (el: Element, attr: string, value: string) {
return parse5Utils.setAttr(el, attr, value);
}

setScriptContent (script: ASTNode, content: string): void {
script.childNodes = [parse5Utils.createTextNode(content, script)];
setScriptContent (script: Element, content: string): void {
script.childNodes = [parse5Utils.createTextNode(content, script) as ChildNode];
}

getScriptContent (script: ASTNode): string {
return script.childNodes?.[0]?.value || '';
getScriptContent (script: Element): string {
return (script.childNodes?.[0] as TextNode)?.value || '';
}

getStyleContent (style: ASTNode): string {
return style.childNodes?.[0]?.value || '';
getStyleContent (style: Element): string {
return (style.childNodes?.[0] as TextNode)?.value || '';
}

setStyleContent (style: ASTNode, content: string): void {
style.childNodes = [parse5Utils.createTextNode(content, style)];
setStyleContent (style: Element, content: string): void {
style.childNodes = [parse5Utils.createTextNode(content, style) as ChildNode];
}

needToProcessContent (): boolean {
Expand All @@ -94,9 +99,9 @@ export default class Parse5DomAdapter extends BaseDomAdapter {
return urlUtils.sameOriginCheck(location, checkedUrl);
}

isExistingTarget (target: string, el: ASTNode): boolean {
isExistingTarget (target: string, el: Element): boolean {
while (el.parentNode)
el = el.parentNode;
el = el.parentNode as Element;

return !!parse5Utils.findElement(el, e => this.getAttr(e, 'name') === target);
}
Expand Down
Loading

0 comments on commit bf25620

Please sign in to comment.