@@ -1312,10 +1312,14 @@ public function assert_queried_element_exists( DOMXPath $xpath, $query ) {
1312
1312
*/
1313
1313
public function assert_dev_mode_is_on_queried_element ( DOMXPath $ xpath , $ query ) {
1314
1314
$ element = $ xpath ->query ( $ query )->item ( 0 );
1315
- $ this ->assertInstanceOf ( 'DOMElement ' , $ element , 'Expected element for query: ' . $ query );
1315
+ $ this ->assertInstanceOf (
1316
+ 'DOMElement ' ,
1317
+ $ element ,
1318
+ 'Expected element for query: ' . $ query . "\nDocument: " . $ xpath ->document ->saveHTML ()
1319
+ );
1316
1320
$ this ->assertTrue (
1317
1321
$ element ->hasAttribute ( AMP_Rule_Spec::DEV_MODE_ATTRIBUTE ),
1318
- 'Expected dev mode to be enabled on element for query: ' . $ query . "\nDocument: " . $ element -> ownerDocument ->saveHTML ()
1322
+ 'Expected dev mode to be enabled on element for query: ' . $ query . "\nDocument: " . $ xpath -> document ->saveHTML ()
1319
1323
);
1320
1324
}
1321
1325
@@ -1327,8 +1331,15 @@ public function assert_dev_mode_is_on_queried_element( DOMXPath $xpath, $query )
1327
1331
*/
1328
1332
public function assert_dev_mode_is_not_on_queried_element ( DOMXPath $ xpath , $ query ) {
1329
1333
$ element = $ xpath ->query ( $ query )->item ( 0 );
1330
- $ this ->assertInstanceOf ( 'DOMElement ' , $ element , 'Expected element for query: ' . $ query );
1331
- $ this ->assertFalse ( $ element ->hasAttribute ( AMP_Rule_Spec::DEV_MODE_ATTRIBUTE ), 'Expected dev mode to not be enabled on element for query: ' . $ query );
1334
+ $ this ->assertInstanceOf (
1335
+ 'DOMElement ' ,
1336
+ $ element ,
1337
+ 'Expected element for query: ' . $ query . "\nDocument: " . $ xpath ->document ->saveHTML ()
1338
+ );
1339
+ $ this ->assertFalse (
1340
+ $ element ->hasAttribute ( AMP_Rule_Spec::DEV_MODE_ATTRIBUTE ),
1341
+ 'Expected dev mode to not be enabled on element for query: ' . $ query . "\nDocument: " . $ xpath ->document ->saveHTML ()
1342
+ );
1332
1343
}
1333
1344
1334
1345
/**
@@ -1341,10 +1352,12 @@ public function get_data_to_test_filtering_admin_bar_style_loader_tag_data() {
1341
1352
'admin_bar_exclusively_dependent ' => [
1342
1353
static function () {
1343
1354
wp_enqueue_style ( 'example-admin-bar ' , 'https://example.com/example-admin-bar.css ' , [ 'admin-bar ' ], '0.1 ' );
1355
+ wp_add_inline_style ( 'example-admin-bar ' , '#wpadminbar:after { content: "Hey admin!" } ' );
1344
1356
},
1345
1357
function ( DOMXPath $ xpath ) {
1346
1358
$ this ->assert_dev_mode_is_on_queried_element ( $ xpath , '//link[ @id = "example-admin-bar-css" ] ' );
1347
1359
$ this ->assert_dev_mode_is_on_queried_element ( $ xpath , '//link[ @id = "dashicons-css" ] ' );
1360
+ $ this ->assert_dev_mode_is_on_queried_element ( $ xpath , '//style[ @id = "example-admin-bar-inline-css" ] ' );
1348
1361
},
1349
1362
],
1350
1363
@@ -1398,6 +1411,22 @@ function ( DOMXPath $xpath ) {
1398
1411
$ this ->assert_dev_mode_is_on_queried_element ( $ xpath , '//link[ @id = "dashicons-css" ] ' );
1399
1412
},
1400
1413
],
1414
+
1415
+ 'styles_have_dev_mode_when_flagged ' => [
1416
+ static function () {
1417
+ wp_enqueue_style ( 'custom-style ' , 'https://example.com/custom-style.css ' , [], '1.0 ' , false );
1418
+ wp_add_inline_style ( 'custom-style ' , '/* inline-custom-style */ ' );
1419
+ wp_style_add_data ( 'custom-style ' , 'ampdevmode ' , true );
1420
+
1421
+ wp_enqueue_style ( 'excluded-style ' , 'https://example.com/excluded-style.js ' , [], '1.0 ' , false );
1422
+ },
1423
+ function ( DOMXPath $ xpath ) {
1424
+ $ this ->assert_dev_mode_is_on_queried_element ( $ xpath , '//link[ contains( @href, "/custom-style" ) ] ' );
1425
+ $ this ->assert_dev_mode_is_on_queried_element ( $ xpath , '//style[ @id = "custom-style-inline-css" ] ' );
1426
+ $ this ->assert_dev_mode_is_on_queried_element ( $ xpath , '//style[ contains( text(), "inline-custom-style" ) ] ' );
1427
+ $ this ->assert_dev_mode_is_not_on_queried_element ( $ xpath , '//link[ contains( @href, "/excluded-style" ) ] ' );
1428
+ },
1429
+ ],
1401
1430
];
1402
1431
}
1403
1432
@@ -1423,9 +1452,15 @@ public function test_filter_admin_bar_style_loader_tag( $setup_callback, $assert
1423
1452
echo '</head><body></body></html> ' ;
1424
1453
$ output = ob_get_clean ();
1425
1454
1426
- $ dom = new DOMDocument ();
1455
+ $ dom = new Document ();
1427
1456
$ dom ->loadHTML ( $ output );
1428
1457
1458
+ AMP_Content_Sanitizer::sanitize_document (
1459
+ $ dom ,
1460
+ wp_array_slice_assoc ( amp_get_content_sanitizers (), [ 'AMP_Dev_Mode_Sanitizer ' ] ),
1461
+ [ 'use_document_element ' => true ]
1462
+ );
1463
+
1429
1464
$ assert_callback ( new DOMXPath ( $ dom ) );
1430
1465
}
1431
1466
@@ -1440,13 +1475,15 @@ public function get_data_to_test_filtering_admin_bar_script_loader_tag_data() {
1440
1475
static function () {
1441
1476
wp_enqueue_script ( 'admin-bar ' );
1442
1477
wp_enqueue_script ( 'example-admin-bar ' , 'https://example.com/example-admin-bar.js ' , [ 'admin-bar ' ], '0.1 ' , false );
1443
- wp_add_inline_script ( 'example-admin-bar ' , '/* inline-example-admin-bar */ ' );
1478
+ wp_add_inline_script ( 'example-admin-bar ' , '/* inline-example-admin-bar-before */ ' , 'before ' );
1479
+ wp_add_inline_script ( 'example-admin-bar ' , '/* inline-example-admin-bar-after */ ' , 'after ' );
1444
1480
wp_localize_script ( 'example-admin-bar ' , 'exampleAdminBar ' , [ 'hello ' => 'world ' ] );
1445
1481
},
1446
1482
function ( DOMXPath $ xpath ) {
1447
1483
$ this ->assert_dev_mode_is_on_queried_element ( $ xpath , '//script[ contains( @src, "/example-admin-bar" ) ] ' );
1448
1484
$ this ->assert_dev_mode_is_on_queried_element ( $ xpath , '//script[ contains( @src, "/admin-bar" ) ] ' );
1449
- $ this ->assert_dev_mode_is_on_queried_element ( $ xpath , '//script[ contains( text(), "inline-example-admin-bar" ) ] ' );
1485
+ $ this ->assert_dev_mode_is_on_queried_element ( $ xpath , '//script[ contains( text(), "inline-example-admin-bar-before" ) ] ' );
1486
+ $ this ->assert_dev_mode_is_on_queried_element ( $ xpath , '//script[ contains( text(), "inline-example-admin-bar-after" ) ] ' );
1450
1487
$ this ->assert_dev_mode_is_on_queried_element ( $ xpath , '//script[ contains( text(), "exampleAdminBar" ) ] ' );
1451
1488
if ( wp_script_is ( 'hoverintent-js ' , 'registered ' ) ) {
1452
1489
$ this ->assert_dev_mode_is_on_queried_element ( $ xpath , '//script[ contains( @src, "/hoverintent-js" ) ] ' );
@@ -1502,6 +1539,25 @@ function ( DOMXPath $xpath ) {
1502
1539
$ this ->assert_dev_mode_is_not_on_queried_element ( $ xpath , '//script[ contains( @src, "/hoverintent-js" ) ] ' );
1503
1540
},
1504
1541
],
1542
+
1543
+ 'scripts_have_dev_mode_when_flagged ' => [
1544
+ static function () {
1545
+ wp_enqueue_script ( 'custom-script ' , 'https://example.com/custom-script.js ' , [], '1.0 ' , false );
1546
+ wp_add_inline_script ( 'custom-script ' , '/* inline-custom-script-before */ ' , 'before ' );
1547
+ wp_add_inline_script ( 'custom-script ' , '/* inline-custom-script-after */ ' , 'after ' );
1548
+ wp_localize_script ( 'custom-script ' , 'customScript ' , [ 'hello ' => 'world ' ] );
1549
+ wp_script_add_data ( 'custom-script ' , 'ampdevmode ' , true );
1550
+
1551
+ wp_enqueue_script ( 'excluded-script ' , 'https://example.com/excluded-script.js ' , [], '1.0 ' , false );
1552
+ },
1553
+ function ( DOMXPath $ xpath ) {
1554
+ $ this ->assert_dev_mode_is_on_queried_element ( $ xpath , '//script[ contains( @src, "/custom-script" ) ] ' );
1555
+ $ this ->assert_dev_mode_is_on_queried_element ( $ xpath , '//script[ contains( text(), "inline-custom-script-before" ) ] ' );
1556
+ $ this ->assert_dev_mode_is_on_queried_element ( $ xpath , '//script[ contains( text(), "inline-custom-script-after" ) ] ' );
1557
+ $ this ->assert_dev_mode_is_on_queried_element ( $ xpath , '//script[ contains( text(), "customScript" ) ] ' );
1558
+ $ this ->assert_dev_mode_is_not_on_queried_element ( $ xpath , '//script[ contains( @src, "/excluded-script" ) ] ' );
1559
+ },
1560
+ ],
1505
1561
];
1506
1562
}
1507
1563
0 commit comments