diff --git a/includes/sanitizers/class-amp-tag-and-attribute-sanitizer.php b/includes/sanitizers/class-amp-tag-and-attribute-sanitizer.php
index cc85fe8f50c..ae5971a5aec 100644
--- a/includes/sanitizers/class-amp-tag-and-attribute-sanitizer.php
+++ b/includes/sanitizers/class-amp-tag-and-attribute-sanitizer.php
@@ -255,7 +255,7 @@ public function sanitize() {
while ( ! empty( $this->stack ) ) {
// Get the next node to process.
- $node = array_pop( $this->stack );
+ $node = array_shift( $this->stack );
/**
* Process this node.
diff --git a/tests/test-class-amp-theme-support.php b/tests/test-class-amp-theme-support.php
index 645e9654af3..5494b341c2a 100644
--- a/tests/test-class-amp-theme-support.php
+++ b/tests/test-class-amp-theme-support.php
@@ -1524,8 +1524,8 @@ public function test_prepare_response() {
'',
// Note these are single-quoted because they are injected after the DOM has been re-serialized, so the type and src attributes come from WP_Scripts::do_item().
- '',
'',
+ '',
'',
'',
diff --git a/tests/test-tag-and-attribute-sanitizer.php b/tests/test-tag-and-attribute-sanitizer.php
index d637cbf2c67..3883434caf4 100644
--- a/tests/test-tag-and-attribute-sanitizer.php
+++ b/tests/test-tag-and-attribute-sanitizer.php
@@ -1060,16 +1060,16 @@ public function test_replace_node_with_children_validation_errors() {
EOB;
$expected_errors[] = array(
- 'node_name' => 'bazfoo',
+ 'node_name' => 'invalid_p',
'parent_name' => 'div',
'code' => 'invalid_element',
- 'node_attributes' => array(),
+ 'node_attributes' => array( 'id' => 'invalid' ),
);
$expected_errors[] = array(
- 'node_name' => 'invalid_p',
+ 'node_name' => 'bazfoo',
'parent_name' => 'div',
'code' => 'invalid_element',
- 'node_attributes' => array( 'id' => 'invalid' ),
+ 'node_attributes' => array(),
);
$content[] = <<