license 'use strict' ; import constants from './constants' ; import { predicates } from 'dom5' ; import * as parse5 from 'parse5' ; export interface Matcher { ( node ) : boolean ; } export const nonModuleScript = predicates . AND ( predicates . hasTagName ( 'script' ) , predicates . OR ( predicates . NOT ( predicates . hasAttr ( 'type' ) ) , predicates . hasAttrValue ( 'type' , 'text/javascript' ) , predicates . hasAttrValue ( 'type' , 'application/javascript' ) ) ) ; export const moduleScript = predicates . AND ( predicates . hasTagName ( 'script' ) , predicates . hasAttrValue ( 'type' , 'module' ) ) ; export const externalStyle = predicates . AND ( predicates . hasTagName ( 'link' ) , predicates . hasAttrValue ( 'rel' , 'stylesheet' ) ) ; export const polymerExternalStyle = predicates . AND ( predicates . hasTagName ( 'link' ) , predicates . hasAttrValue ( 'rel' , 'import' ) , predicates . hasAttrValue ( 'type' , 'css' ) ) ; export const styleMatcher = predicates . AND ( predicates . hasTagName ( 'style' ) , predicates . OR ( predicates . NOT ( predicates . hasAttr ( 'type' ) ) , predicates . hasAttrValue ( 'type' , 'text/css' ) ) ) ; export const targetMatcher = predicates . AND ( predicates . OR ( predicates . hasTagName ( 'a' ) , predicates . hasTagName ( 'form' ) ) , predicates . NOT ( predicates . hasAttr ( 'target' ) ) ) ; export const head = predicates . hasTagName ( 'head' ) ; export const body = predicates . hasTagName ( 'body' ) ; export const base = predicates . hasTagName ( 'base' ) ; export const template = predicates . hasTagName ( 'template' ) ; export const domModuleWithoutAssetpath = predicates . AND ( predicates . hasTagName ( 'dom-module' ) , predicates . hasAttr ( 'id' ) , predicates . NOT ( predicates . hasAttr ( 'assetpath' ) ) ) ; export const polymerElement = predicates . hasTagName ( 'polymer-element' ) ; export const externalNonModuleScript = predicates . AND ( predicates . hasAttr ( 'src' ) , nonModuleScript ) ; export const inlineNonModuleScript = predicates . AND ( predicates . NOT ( predicates . hasAttr ( 'src' ) ) , nonModuleScript ) ; export const externalModuleScript = predicates . AND ( predicates . hasAttr ( 'src' ) , moduleScript ) ; export const eagerHtmlImport = predicates . AND ( predicates . hasTagName ( 'link' ) , predicates . hasAttrValue ( 'rel' , 'import' ) , predicates . hasAttr ( 'href' ) , predicates . OR ( predicates . hasAttrValue ( 'type' , 'text/html' ) , predicates . hasAttrValue ( 'type' , 'html' ) , predicates . NOT ( predicates . hasAttr ( 'type' ) ) ) ) ; export const lazyHtmlImport = predicates . AND ( predicates . hasTagName ( 'link' ) , predicates . hasAttrValue ( 'rel' , 'lazy-import' ) , predicates . hasAttr ( 'href' ) , predicates . OR ( predicates . hasAttrValue ( 'type' , 'text/html' ) , predicates . hasAttrValue ( 'type' , 'html' ) , predicates . NOT ( predicates . hasAttr ( 'type' ) ) ) ) ; export const htmlImport = predicates . OR ( eagerHtmlImport , lazyHtmlImport ) ; export const stylesheetImport = predicates . AND ( predicates . hasTagName ( 'link' ) , predicates . hasAttrValue ( 'rel' , 'import' ) , predicates . hasAttr ( 'href' ) , predicates . hasAttrValue ( 'type' , 'css' ) ) ; export const hiddenDiv = predicates . AND ( predicates . hasTagName ( 'div' ) , predicates . hasAttr ( 'hidden' ) , predicates . hasAttr ( 'by-polymer-bundler' ) ) ; export const inHiddenDiv = predicates . parentMatches ( hiddenDiv ) ; export const elementsWithUrlAttrsToRewrite = predicates . AND ( predicates . OR ( ... constants . URL_ATTR . map ( ( attr ) => predicates . hasAttr ( attr ) ) ) , predicates . NOT ( predicates . AND ( predicates . parentMatches ( predicates . hasTagName ( 'dom-module' ) ) , lazyHtmlImport ) ) ) ; /**
 * TODO(usergenic): From garlicnation's PR comment - "This matcher needs to deal
 * with a number of edge cases. Whitespace-only text nodes should be ignored,
 * text nodes with meaningful space should be preserved. Comments should be
 * ignored if --strip-comments is set. License comments should be deduplicated
 * and moved to the start of the document."
 */ const nextToHiddenDiv = ( offset ) => { return ( node ) => { const siblings = node . parentNode ! . childNodes ! ; const hiddenDivIndex = siblings . indexOf ( node ) + offset ; if ( hiddenDivIndex < 0 || hiddenDivIndex >= siblings . length ) { return false ; } return hiddenDiv ( siblings [ hiddenDivIndex ] ) ; } ; } ; export const beforeHiddenDiv = nextToHiddenDiv ( 1 ) ; export const afterHiddenDiv = nextToHiddenDiv ( - 1 ) ; export const orderedImperative = predicates . OR ( eagerHtmlImport , nonModuleScript , styleMatcher , externalStyle , polymerExternalStyle ) ;