@@ -7459,183 +7459,196 @@ describe("parseHtml — insertion modes", () => {
74597459describe ( "SourceProcessor — attribute rewrites as options" , ( ) => {
74607460 const { SourceProcessor } = require ( "../lib/html/syntax" ) ;
74617461
7462+ const OPEN = "<!doctype html><body>" ;
7463+ const CLOSE = "</body>" ;
7464+
74627465 /**
74637466 * @param {string } body the document body
74647467 * @param {object= } options extra print options
7465- * @returns {string } the minified serialization
7468+ * @returns {string } the body's whole minified serialization
74667469 */
74677470 const minify = ( body , options ) =>
7468- new SourceProcessor ( ) . process ( `<!DOCTYPE html><body>${ body } ` , {
7469- mode : "minify" ,
7470- ...options
7471- } ) . code ;
7471+ new SourceProcessor ( )
7472+ . process ( `<!DOCTYPE html><body>${ body } ` , { mode : "minify" , ...options } )
7473+ . code . slice ( OPEN . length , - CLOSE . length ) ;
74727474
74737475 it ( "collapses a boolean attribute the spec canonicalizes, by default" , ( ) => {
7474- expect ( minify ( '<input checked="checked" disabled="">' ) ) . toContain (
7476+ expect ( minify ( '<input checked="checked" disabled="">' ) ) . toBe (
74757477 "<input checked disabled>"
74767478 ) ;
74777479 expect (
74787480 minify ( '<input checked="checked">' , { collapseBooleanAttributes : false } )
7479- ) . toContain ( "<input checked=checked>" ) ;
7481+ ) . toBe ( "<input checked=checked>" ) ;
74807482 } ) ;
74817483
74827484 it ( "collapses any value at `all`, and the empty one at every tier" , ( ) => {
74837485 expect (
74847486 minify ( '<input checked="false">' , { collapseBooleanAttributes : "all" } )
7485- ) . toContain ( "<input checked>" ) ;
7486- expect ( minify ( '<input checked="false">' ) ) . toContain (
7487- "<input checked=false>"
7488- ) ;
7487+ ) . toBe ( "<input checked>" ) ;
7488+ expect ( minify ( '<input checked="false">' ) ) . toBe ( "<input checked=false>" ) ;
74897489 // The tokenizer reads `x` and `x=""` as the same attribute, so the bare
74907490 // name is not the option's to keep.
74917491 expect (
74927492 minify ( '<input checked="">' , { collapseBooleanAttributes : false } )
7493- ) . toContain ( "<input checked>" ) ;
7493+ ) . toBe ( "<input checked>" ) ;
74947494 } ) ;
74957495
74967496 it ( "collapses across the elements each boolean attribute belongs to" , ( ) => {
7497- for ( const [ element , name ] of [
7498- [ "input" , "checked" ] ,
7499- [ "input" , "disabled" ] ,
7500- [ "script" , "async" ] ,
7501- [ "script" , "defer" ] ,
7502- [ "option" , "selected" ] ,
7503- [ "select" , "multiple" ] ,
7504- [ "details" , "open" ] ,
7505- [ "ol" , "reversed" ] ,
7506- [ "iframe" , "allowfullscreen" ] ,
7507- [ "video" , "muted" ] ,
7508- [ "form" , "novalidate" ]
7497+ for ( const [ element , name , expected ] of [
7498+ [ "input" , "checked" , "<input checked>" ] ,
7499+ [ "input" , "disabled" , "<input disabled>" ] ,
7500+ [ "script" , "async" , "<script async></script>" ] ,
7501+ [ "script" , "defer" , "<script defer></script>" ] ,
7502+ [ "option" , "selected" , "<option selected>" ] ,
7503+ [ "select" , "multiple" , "<select multiple></select>" ] ,
7504+ [ "details" , "open" , "<details open></details>" ] ,
7505+ [ "ol" , "reversed" , "<ol reversed></ol>" ] ,
7506+ [ "iframe" , "allowfullscreen" , "<iframe allowfullscreen></iframe>" ] ,
7507+ [ "video" , "muted" , "<video muted></video>" ] ,
7508+ [ "form" , "novalidate" , "<form novalidate></form>" ]
75097509 ] ) {
7510- expect ( minify ( `<${ element } ${ name } ="${ name } "></${ element } >` ) ) . toContain (
7511- `< ${ element } ${ name } >`
7510+ expect ( minify ( `<${ element } ${ name } ="${ name } "></${ element } >` ) ) . toBe (
7511+ expected
75127512 ) ;
75137513 }
75147514 } ) ;
75157515
75167516 it ( "leaves an attribute that is not boolean on that element" , ( ) => {
75177517 // `hidden` is enumerated in the living standard (`until-found`), so it is
75187518 // not one of these either.
7519- for ( const [ element , name ] of [
7520- [ "p" , "checked" ] ,
7521- [ "div" , "open" ] ,
7522- [ "span" , "selected" ] ,
7523- [ "p" , "hidden" ]
7519+ for ( const [ element , name , expected ] of [
7520+ [ "p" , "checked" , "<p checked=checked>x" ] ,
7521+ [ "div" , "open" , "<div open=open>x</div>" ] ,
7522+ [ "span" , "selected" , "<span selected=selected>x</span>" ] ,
7523+ [ "p" , "hidden" , "<p hidden=hidden>x" ]
75247524 ] ) {
7525- expect ( minify ( `<${ element } ${ name } ="${ name } ">x</${ element } >` ) ) . toContain (
7526- ` ${ name } = ${ name } `
7525+ expect ( minify ( `<${ element } ${ name } ="${ name } ">x</${ element } >` ) ) . toBe (
7526+ expected
75277527 ) ;
75287528 }
75297529 } ) ;
75307530
75317531 it ( "keeps the quotes a value cannot go without" , ( ) => {
75327532 for ( const value of [ "a b" , "a=b" , "a<b" , "a>b" , "a`b" , "a'b" ] ) {
7533- expect ( minify ( `<p id="${ value } ">x</p>` ) ) . toContain ( ` id="${ value } "`) ;
7533+ expect ( minify ( `<p id="${ value } ">x</p>` ) ) . toBe ( `<p id="${ value } ">x `) ;
75347534 }
7535- expect ( minify ( '<p id="a/b">x</p>' ) ) . toContain ( " id=a/b") ;
7536- expect ( minify ( '<p id="a"b">x</p>' ) ) . toContain ( " id='a\"b'") ;
7535+ expect ( minify ( '<p id="a/b">x</p>' ) ) . toBe ( "<p id=a/b>x ") ;
7536+ expect ( minify ( '<p id="a"b">x</p>' ) ) . toBe ( "<p id='a\"b'>x ") ;
75377537 } ) ;
75387538
75397539 it ( "collapses only where the attribute is a boolean one" , ( ) => {
75407540 expect (
75417541 minify ( '<p contenteditable="contenteditable">x</p>' , {
75427542 collapseBooleanAttributes : "all"
75437543 } )
7544- ) . toContain ( "<p contenteditable=contenteditable>" ) ;
7544+ ) . toBe ( "<p contenteditable=contenteditable>x " ) ;
75457545 } ) ;
75467546
75477547 it ( "normalizes a value into its own grammar's shortest spelling" , ( ) => {
75487548 const body =
75497549 '<p class=" b a " dir="RTL">x</p><img srcset="a.png 1x" alt=a>' ;
7550- expect ( minify ( body ) ) . toContain ( 'class="b a" dir=rtl' ) ;
7551- expect ( minify ( body ) ) . toContain ( 'srcset="a.png 1x"' ) ;
7552- const off = minify ( body , { normalizeAttributeValues : false } ) ;
7553- expect ( off ) . toContain ( 'class=" b a " dir=RTL' ) ;
7554- expect ( off ) . toContain ( 'srcset="a.png 1x"' ) ;
7550+ expect ( minify ( body ) ) . toBe (
7551+ '<p class="b a" dir=rtl>x</p><img srcset="a.png 1x" alt=a>'
7552+ ) ;
7553+ expect ( minify ( body , { normalizeAttributeValues : false } ) ) . toBe (
7554+ '<p class=" b a " dir=RTL>x</p><img srcset="a.png 1x" alt=a>'
7555+ ) ;
75557556 } ) ;
75567557
75577558 it ( "reaches every family the rewrite covers, not only the obvious ones" , ( ) => {
75587559 /** @type {[string, string, string][] } */
75597560 const families = [
7560- [ '<p dir="RTL">x</p>' , "dir=rtl" , "dir=RTL" ] ,
7561+ [ '<p dir="RTL">x</p>' , "<p dir=rtl>x " , "<p dir=RTL>x " ] ,
75617562 [
75627563 '<img srcset="a.png 1x" alt=a>' ,
7563- 'srcset="a.png 1x"' ,
7564- 'srcset="a.png 1x"'
7564+ '<img srcset="a.png 1x" alt=a>' ,
7565+ '<img srcset="a.png 1x" alt=a>'
7566+ ] ,
7567+ [
7568+ '<p class=" b a ">x</p>' ,
7569+ '<p class="b a">x' ,
7570+ '<p class=" b a ">x'
75657571 ] ,
7566- [ '<p class=" b a ">x</p>' , 'class="b a"' , 'class=" b a "' ] ,
75677572 [
75687573 '<meta name=viewport content="width=device-width, initial-scale=1">' ,
7569- 'content="width=device-width,initial-scale=1"' ,
7570- 'content="width=device-width, initial-scale=1"'
7574+ '<meta name=viewport content="width=device-width,initial-scale=1"> ' ,
7575+ '<meta name=viewport content="width=device-width, initial-scale=1"> '
75717576 ] ,
75727577 [
75737578 '<a ping=" a.php , b.php ">x</a>' ,
7574- 'ping="a.php , b.php"' ,
7575- 'ping=" a.php , b.php "'
7579+ '<a ping="a.php , b.php">x</a> ' ,
7580+ '<a ping=" a.php , b.php ">x</a> '
75767581 ] ,
7577- [ '<a href=" /a ">x</a>' , "href=/a" , 'href=" /a "' ] ,
7578- [ '<ol start=" 007 "><li>x</ol>' , "start=7" , 'start=" 007 "' ] ,
7579- [ '<img width=" 100 " src=a alt=a>' , "width=100" , 'width=" 100 "' ]
7582+ [ '<a href=" /a ">x</a>' , "<a href=/a>x</a>" , '<a href=" /a ">x</a>' ] ,
7583+ [
7584+ '<ol start=" 007 "><li>x</ol>' ,
7585+ "<ol start=7><li>x</ol>" ,
7586+ '<ol start=" 007 "><li>x</ol>'
7587+ ] ,
7588+ [
7589+ '<img width=" 100 " src=a alt=a>' ,
7590+ "<img width=100 src=a alt=a>" ,
7591+ '<img width=" 100 " src=a alt=a>'
7592+ ]
75807593 ] ;
75817594 for ( const [ body , on , off ] of families ) {
7582- expect ( minify ( body ) ) . toContain ( on ) ;
7583- expect ( minify ( body , { normalizeAttributeValues : false } ) ) . toContain ( off ) ;
7595+ expect ( minify ( body ) ) . toBe ( on ) ;
7596+ expect ( minify ( body , { normalizeAttributeValues : false } ) ) . toBe ( off ) ;
75847597 }
75857598 } ) ;
75867599
75877600 it ( "leaves the style attribute to its own option" , ( ) => {
75887601 const body = '<p class=" b a " style="color: #ff0000 ;">x</p>' ;
7589- expect ( minify ( body , { normalizeAttributeValues : false } ) ) . toContain (
7590- 'class=" b a " style=color:red'
7602+ expect ( minify ( body , { normalizeAttributeValues : false } ) ) . toBe (
7603+ '<p class=" b a " style=color:red>x '
75917604 ) ;
7592- expect ( minify ( body , { minifyStyleAttribute : false } ) ) . toContain (
7593- 'class="b a" style="color: #ff0000 ;"'
7605+ expect ( minify ( body , { minifyStyleAttribute : false } ) ) . toBe (
7606+ '<p class="b a" style="color: #ff0000 ;">x '
75947607 ) ;
75957608 } ) ;
75967609
75977610 it ( "keeps the quotes the tokenizer does not need, when asked" , ( ) => {
7598- expect ( minify ( '<p id="q" class="a">x</p>' ) ) . toContain ( "<p id=q class=a>" ) ;
7611+ expect ( minify ( '<p id="q" class="a">x</p>' ) ) . toBe ( "<p id=q class=a>x " ) ;
75997612 expect (
76007613 minify ( '<p id="q" class="a">x</p>' , { removeAttributeQuotes : false } )
7601- ) . toContain ( '<p id="q" class="a">' ) ;
7614+ ) . toBe ( '<p id="q" class="a">x ' ) ;
76027615 } ) ;
76037616
76047617 it ( "keeps them through the respelling a reference would otherwise go bare in" , ( ) => {
7605- expect ( minify ( '<p title="red">x</p>' ) ) . toContain ( "<p title=red>" ) ;
7618+ expect ( minify ( '<p title="red">x</p>' ) ) . toBe ( "<p title=red>x " ) ;
76067619 expect (
76077620 minify ( '<p title="red">x</p>' , { removeAttributeQuotes : false } )
7608- ) . toContain ( '<p title="red">' ) ;
7621+ ) . toBe ( '<p title="red">x ' ) ;
76097622 } ) ;
76107623
76117624 it ( "sorts a token list asked for on its own, normalization off" , ( ) => {
76127625 const body = '<p class="b a c" id="q">x</p>' ;
76137626 expect (
76147627 minify ( body , { sortTokenLists : true , normalizeAttributeValues : false } )
7615- ) . toContain ( ' class="a b c"') ;
7616- expect ( minify ( body , { normalizeAttributeValues : false } ) ) . toContain (
7617- 'class="b a c"'
7628+ ) . toBe ( '<p class="a b c" id=q>x ') ;
7629+ expect ( minify ( body , { normalizeAttributeValues : false } ) ) . toBe (
7630+ '<p class="b a c" id=q>x '
76187631 ) ;
76197632 // `ping` is the order its requests go out in, so it is no set to sort.
76207633 expect (
76217634 minify ( '<a ping=" b a ">x</a>' , {
76227635 sortTokenLists : true ,
76237636 normalizeAttributeValues : false
76247637 } )
7625- ) . toContain ( ' ping=" b a "') ;
7638+ ) . toBe ( '<a ping=" b a ">x</a> ') ;
76267639 } ) ;
76277640
76287641 it ( "keeps a value the source left unquoted unquoted" , ( ) => {
7629- expect ( minify ( "<p id=q>x</p>" , { removeAttributeQuotes : false } ) ) . toContain (
7630- "<p id=q>"
7642+ expect ( minify ( "<p id=q>x</p>" , { removeAttributeQuotes : false } ) ) . toBe (
7643+ "<p id=q>x "
76317644 ) ;
76327645 } ) ;
76337646
76347647 it ( "rewrites nothing in foreign content, whatever the options say" , ( ) => {
76357648 const svg =
76367649 '<svg xmlns="http://www.w3.org/2000/svg"><rect class=" a b "/></svg>' ;
7637- expect ( minify ( svg , { collapseBooleanAttributes : "all" } ) ) . toContain (
7638- 'class=" a b "'
7650+ expect ( minify ( svg , { collapseBooleanAttributes : "all" } ) ) . toBe (
7651+ '<svg xmlns=http://www.w3.org/2000/svg><rect class=" a b "/></svg> '
76397652 ) ;
76407653 } ) ;
76417654} ) ;
0 commit comments